Академический Документы
Профессиональный Документы
Культура Документы
Chapter 6 Graph
• 村村通
6.1 Basic Concepts
G1=(V1,VR1)
V1={A,B,C,D,E}
VR1={<A,B>, <A,E>, <B,C>, <C,D>, <D,B>, <D,A>, <E,C>}
• G2=(V2,VR2)
• V2={A,B,C,D,E,F}
• VR2={(A,B), (A,E), (B,E), (C,D), (D,F), (B,F), (C,F)}
• weight/cost
• degree
• for digraph
➢OD
➢ID
➢TD
• path
• length
3 6
1 3 6
graph and sub graph
例 2 4 5
1 5 7
1 3 6
3 2 4 6 G1
G2 vertex 2’s in degree :1 out degree:3
vertex 5’s degree:3 vertex 4’s in degree :1 out degree :0
vertex 2’s degree :4
path:1,2,3,5,6,3
例 length:5
2 4 5 simple path:1,2,3,5
loop:1,2,3,5,6,3,1
1 3 6 simple loop:3,5,6,3
G1
例
1 5 7 path :1,2,5,7,6,5,2,3
length :7
simple path :1,2,5,7,6
3 2 4 6 loop :1,2,5,7,6,5,2,1
G2 simple loop :1,2,3,1
例
2 4 5
connected
1 3 6
例 5
strongly connected
3 6
例
2 4 5
none connected
1 3 6
7.2 Storage Structure of Graph
– Linked Lists
V1
1 2
Two next V4 ^ V2 ^ ^
3 4
G1 V3 ^
Disadvantage?
^ V1 V2
1 2
3 Three next V3
4 5
G2
V4 ^ V5 ^
• disadvantage of linked lists
• how many ‘next’ of each node depend on the maximum degree
of the vertex
• waste space
➢adjacency matrix
➢adjacency list
– adjacency matrix
例 1 2
例 1 2 3
4 5
3 4 G2
G1
1 0 1 0 1 0
1 0 1 1 0 2
2 1 0 1 0 1
0 0 0 0 V = 3 = 0 1
V = A= A
1 0 1
3 0 1
0 0
4 1 0 1 0 0
4
1 0 0 0 5
0 1 1 0 0
– undirected graph,symmetric matrix;
– degree of each vertex,sum of the row/column
– digraph
» OD=sum of the row
» ID=sum of the column
– adjacency matrix of the weighted graph:
例 1 5 2
3
7 8 4
5
1
6
3 4 1 5 7 3
2
2
5 4 8
V = 3 A=
7 2 1
4 4 2 6
5
3 8 1 6
➢adjacency matrix
if the graph is not dense, (sparse)
save a lot of zero
waste space
➢adjacency list
adjacency list
node
data firstarc adjvex nextarc
例 a b 1 a 3 2 ^
adjvex nextarc info
2 b ^
c d 3 c 4 ^
header G1 4 d 1 ^
data firstarc
#define MAX_VERTEX_NUM 20
typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc; adjvex nextarc info
InfoType *info;
}ArcNode;
Typedef struct{
AdiList vertices;
int vexnum, arcnum;
int kind;
}ALGraph; //邻接表-图
adjacency list
data firstarc adjvex nextarc
disadvantage? 例 a b 1 a 3 2 ^
easy to calculate out degree 2 b ^
not the in degree c d 3 c 4 ^
G1 4 d 1 ^
c d 3 c 1 ^
G1 4 d 3 ^
Multi / Orthogonal lists十字链表
1 2 tailvex headvex hlink tlink
例 a b
1 a 1 2 1 3 ^
2 b ^
3 c 3 1 3 4 ^ ^
4 d 4 1 ^ 4 2 ^ 4 3 ^ ^
6.3 Graph Traversal
• Depth First Search
• Breadth First Search
• DFS
• BFS
Graph Traversal
– depth-first search(DFS)
• Starting at some vertex,v, we process v and then recursively traverse
all vertices adjacent to v
V1
V2 V3
V8
例 V1
V2 V3
V4 V5 V6 V7
V8
DFS:V1 V2 V4 V8 V5 V6 V3 V7
例 V1
V2 V5 V6 V3
V4 V8 V7
V4 V5 V6 V7
V8
DFS:V1 V2 V4 V8 V3 V6 V7 V5
例 V1
V2 V3
✓:visited
V4 V5 V6 V7
例 V1
V2 V3 BFS:V1 V2 V3 V4 V5 V6 V7 V8
V4 V5 V6 V7
V8
例 V1
V2 V3
V4 V5 V6 V7
V8
BFS :V1 V2 V3 V4 V5 V6 V7 V8
例 V1
V2 V5 V6 V3
V4 V8 V7
V4 V5 V6 V7
V8
BFS :V1 V2 V3 V4 V6 V7 V8 V5
vexdata firstarc adjvex next
例 2
1 1 1 4 3 ^
2 2 5 1 ^
2 3 4
3 3 5 1 ^
5 4 4 5 1 ^
5 5 4 3 2 ^
Why do we need DFS & BFS both?
DFS
Why do we need DFS & BFS both?
Why do we need DFS & BFS both?
6.4 Minimum Spanning Tree
• Greedy Algorithm
• Prim
• Kruskal
1 1 1 1
Prim 5
6
2 1 4 1 1
5 5
3 3 3
3 2 4
6 4
5 6 6 6
1 1 1
1 4 2 1 4 2 1 4
5 5
3 3 3
2 2 3 2
4 4 4
6 6 5 6
1
例 6 5
2 1 4
5 5
3
3 2
6 4
5 6 6
2 1 4
5
3
3 4 2
5 6
Kruskal
1 1
例 6 5
2 1 4 1
5 5 2 5 4
3 3
3 2 2
6 4 3
4
5 6 6 5 6
• Prim
grow the tree
• Kruskal
forest-tree
6.5 Topological Sort
• ordering of vertices in a directed acyclic graph
• Mega projects
• Prerequisite courses
Course NO. Course Prerequisite
C1 Programming None
C2 Discrete mathematics C1
C3 Data Structure C1,C2
C4 汇编语言 C1
C5 语言的设计和分析 C3,C4
C6 计算机原理 C11
C7 编译原理 C3.C5
C4 C5 C8 计算机网络 C3,C6
C2 C9 Math 无
C10 Linear Algebra C9
C1 C3 C7 C11 Physics C9
C12 C12 数值分析 C1,C9,C10
C8
C9 C10 C6
C11
C4 C5
C2
C1 C3 C7
C12
C8
C9 C10 C6
C11
topological ordering:C1--C2--C3--C4--C5--C7--C9--C10--C11--C6--C12--C8
or :C9--C10--C11--C6--C1--C12--C4--C2--C3--C5--C7--C8
C2
C1 C3 C7
C12
C8
C9 C10 C6
C11
C4 C5 C4 C5
C2
C3 C7 C3 C7
C12 C12
C8
C8
C9 C10 C6
C9 C10 C6
C11
C11
ordering :C1--C2
ordering:C1
(2)
(1)
C4 C5 C5
C7 C7
C12 C12
C8 C8
C9 C10 C6 C9 C10 C6
C11 C11
C12
C12 C8
C8
C6
C10 C6
C11
C11 ordering :C1--C2--C3--C4--C5--C7--C9
ordering :C1--C2--C3--C4--C5--C7--C9 --C10
(8)
C12 C12
C8 C8
C6
(9) (10)
C8
4
3
top 2
top 6 1
top 1 0
in link vex next
1 0 4 3 2 ^
2 2 ^
3 1 5 2 ^
4 2 5 ^
5 3 ^
6 0 5 4 ^
例
4
1 2
3
top 2
top 6 4 3
1
1 0
6 5
ordering:6
in link vex next
1 0 4 3 2 ^
2 2 ^
3 1 5 2 ^
4 2 5 ^
5 3 ^
6 0 5 4 ^
p
例
4
1 2
3
2
top 4 3
1
1 0
6 5
ordering :6
in link vex next
1 0 4 3 2 ^
2 2 ^
3 1 5 2 ^
4 2 5 ^
5 2 ^
6 0 5 4 ^
p
例
4
1 2
3
2
top 4 3
1
1 0
6 5
ordering :6
in link vex next
1 0 4 3 2 ^
2 2 ^
3 1 5 2 ^
4 2 5 ^
5 2 ^
6 0 5 4 ^
p
例
4
1 2
3
2
top 4 3
1
1 0
6 5
ordering :6
in link vex next
1 0 4 3 2 ^
2 2 ^
3 1 5 2 ^
4 1 5 ^
5 2 ^
6 0 5 4 ^
p
例
4
1 2
3
2
top 4 3
1
1 0
6 5
ordering :6
in link vex next
1 0 4 3 2 ^
2 2 ^
3 1 5 2 ^
4 1 5 ^
5 2 ^
6 0 5 4 ^
p=NULL
例
4
1 2
3
2
top 4 3
1
1 0
6 5
ordering :6
in link vex next
1 0 4 3 2 ^
2 2 ^
3 1 5 2 ^
4 1 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
2
top 4 3
1
top 1 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 2 ^ p
3 1 5 2 ^
4 1 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
2
1 4 3
top 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 2 ^ p
3 1 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
2
1 4 3
top 4 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 2 ^ p
3 1 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
2
top 4 3
1
4 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 2 ^ p
3 1 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
2
top 4 3
1
4 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 2 ^ p
3 0 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
2
top 4 3
3 1
4 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 2 ^ p
3 0 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
top 2
3 1 4 3
4 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 2 ^ p
3 0 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
top 2
3 1 4 3
4 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 1 ^ p
3 0 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
top 2
3 1 4 3
4 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 1 p=NULL
^
3 0 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
top 2
3 1 4 3
4 0
6 5
ordering :6 1
in link vex next
1 0 4 3 2 ^
2 1 ^
3 0 5 2 ^
4 0 5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
2
top 1 4 3
3
4 0
6 5
输出序列:6 1 3
in link vex next
1 0 4 3 2 ^
2 1 ^
3 0 5 2 ^
4 0 p
5 ^
5 2 ^
6 0 5 4 ^
例
4
1 2
3
2
top 1 4 3
4 0
6 5
ordering :6 1 3
in link vex next
1 0 4 3 2 ^
2 1 ^
3 0 5 2 ^
4 0 p
5 ^
5 1 ^
6 0 5 4 ^
例
4
1 2
3
2
top 1 4 3
4 0
6 5
ordering :6 1 3
in link vex next
1 0 4 3 2 ^
2 1 ^
3 0 5 2 ^
4 0 5 ^ p
5 1 ^
6 0 5 4 ^
例
4
1 2
3
2
top 1 4 3
4 0
6 5
ordering :6 1 3
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 5 ^ p
5 1 ^
6 0 5 4 ^
例
4
1 2
3
2
top 1 4 3
2
4 0
6 5
ordering :6 1 3
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 5 ^ p
5 1 ^
6 0 5 4 ^
例
4
1 2
3
top 2
2 1 4 3
4 0
6 5
ordering :6 1 3
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 p=NULL
5 ^
5 1 ^
6 0 5 4 ^
例
4
1 2
3
top 2
2 1 4 3
4 0
6 5
ordering :6 1 3
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 p=NULL
5 ^
5 1 ^
6 0 5 4 ^
例
4
1 2
3
2
top 4 3
2 1
4 0
6 5
ordering :6 1 3 2
in link vex next
1 0 4 3 2 ^
2 0 ^ p=NULL
3 0 5 2 ^
4 0 5 ^
5 1 ^
6 0 5 4 ^
例
4
1 2
3
2
top 4 3
1
4 0
6 5
ordering :6 1 3 2
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 5 ^
5 1 ^
6 0 5 4 ^
例
4
1 2
3
2
1 4 3
top
4 0
6 5
ordering :6 1 3 2 4
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 5 ^
5 1 ^ p
6 0 5 4 ^
例
4
1 2
3
2
1 4 3
top
0
6 5
ordering :6 1 3 2 4
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 5 ^
5 0 ^ p
6 0 5 4 ^
例
4
1 2
3
2
1 4 3
top
5 0
6 5
ordering :6 1 3 2 4
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 5 ^
p=NULL
5 0 ^
6 0 5 4 ^
例
4
1 2
3
2
top 1 4 3
5 0
6 5
ordering :6 1 3 2 4
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 5 ^
5 0 ^
6 0 5 4 ^
例
4
1 2
3
2
1 4 3
top
5 0
6 5
ordering :6 1 3 2 4 5
in link vex next
1 0 4 3 2 ^
2 0 ^
3 0 5 2 ^
4 0 5 ^
5 0 ^ p=NULL
6 0 5 4 ^
例
4
1 2
3
2
1 4 3
top
0
6 5
ordering :6 1 3 2 4 5
6.6 The Shortest Path
V0
0 32
8 shortest path weight
13 <V0,V1> 13
2 1 V1
9 7 V2 <V0,V2> 8
30 5 6 V3 <V0,V2,V3> 13
3 19
5 17 V4 <V0,V2,V3,V4>
6 2 V5 <V0,V2,V3,V4,V5>21
4 V6 <V0,V1,V6> 20
0 32
8 13 1
2
30 5 9 7 6
the shortest path from V0 to other vertex 3
6 2 5 17
13 13 4
V1
<V0,V1> <V0,V1> ------- ------- --------
V2 8
<V0,V2> ------- ------- ------- --------
V3 13 13
<V0,V2,V3> <V0,V2,V3> ------- --------
30 30 30 19
V4
<V0,V4> <V0,V4> <V0,V4> <V0,V2,V3,V4> --------
V5 22 22 21
<V0,V1,V5> <V0,V1,V5> <V0,V2,V3,V4,V5>
32 32 20 20 20
V6
<V0,V6> <V0,V6> <V0,V1,V6> <V0,V1,V6> <V0,V1,V6>
V2:8 V1:13 V3:13 V4:19 V6:20
Vj
<V0,V2> <V0,V1> <V0,V2,V3><V0,V2,V3,V4> <V0, V1,V6>
<V0,V1, <V0,V1, <V0,V1,
S V2,V3,V4>
<V0,V2> <V0,V1,V2> V2,V3> V2,V3,V4,V6>
• Dijkstra Algorithm
• step:
① S={V0},T={other vertex};
② choose the shortest from ,add to S;
③ modify the path length if it become shorter;
④ repeat until S=V.
• shortest path between any pair of vertex
➢method2:Floyd algorithm
idea:test the path by adding new vertex
A B C AB AC
6 A 0 4 11 path: BA BC
A 4 B B 6 0 2
11 C 3 0 CA
3 2
C
A B C AB AC
add A: A 0 4 11 path: BA BC
B 6 0 2
C 3 7 0 CA CAB
A B C AB ABC
add B: A 0 4 6 path: BA BC
B 6 0 2
C 3 7 0 CA CAB
A B C AB ABC
add C: A 0 4 6 path: BCA BC
B 5 0 2
C 3 7 0 CA CAB
• Experiment Part
• Final project-graph
• test data
• write a program to