210 likes | 243 Views
Graph Theory. Chapter 13 Graph Algorithms. 大葉大學 資訊工程系 黃鈴玲 2011.10. Contents. 13.4 Breadth-First Search 13.5 Depth-First Search 13.6 Connected Components 13.7 Dijkstra’s Shortest Path Algorithm. 13.4 Breadth-First Search. 先進先出.
E N D
Graph Theory Chapter 13 Graph Algorithms 大葉大學 資訊工程系 黃鈴玲 2011.10
Contents • 13.4 Breadth-First Search • 13.5 Depth-First Search • 13.6 Connected Components • 13.7 Dijkstra’s Shortest Path Algorithm
13.4 Breadth-First Search 先進先出 Three operations: (1) Enqueue(Q, u): adds u to the tail of Q.(2) Dequeue(Q): removes one item from the head of the nonempty queue Q and returns that element.(3) Empty(Q): returns True if Q is empty, False otherwise.
// 每次mark一點就把 i + 1 若G不是connected,要執行到所有點都mark為止。
Example 13.5 T1 T2 T3 T4 T5 s y=6 1 6 Q: 8 5 9 x=1
source 2 7 4 3
G v5 v13 v8 v2 v12 v7 v6 v1 v10 v15 v14 v4 v9 v3 v11 Ex: Find a BFS forest for the following graph G with source v1. (下標小的優先選)
G v5 v13 v8 v2 v12 v7 v6 v1 v10 v15 v14 v4 v9 v3 v11 Ex: Find a DFS forest for the following graph G with source v1. (下標小的優先選)
13.6 Connected Components Component number 1 2 用DFS拜訪所有點 3 4 4 1 3 2 2 4 1 1 4 2 1 2 1
13.7 Dijkstra’s Shortest Path algorithm 加Father[.] of size n: 記錄shortest path的上一點 Father[i] =1 if W1i Father[u] =v if pathcost[u] = pathcost[v] +Wuv
Father[3] =1 第1個for迴圈 source Father[4] =1 Umin={3}
第2個for迴圈 Father[3] =1 Father[2] =3 i=1 source Father[5] =3 Father[6] =3 Father[4] =3 Umin={4}
Father[3] =1 Father[2] =3 i=2 source Father[5] =4 Father[6] =3 Father[4] =3 Umin={5}
Father[3] =1 Father[2] =3 i=3 source Father[5] =4 Father[6] =5 Father[4] =3 Umin={2}
Father[3] =1 Father[2] =3 i=4 source Father[5] =4 Father[6] =2 Father[4] =3 end
Sourceu0 minimum pathcost father v2 v3 v4 v5 v6 v7 U v1 (, -) (, -) (, -) (, -) (, -) (, -) (, -) (, -) (, -) (, -) (, -) (, -) (, -) (18, v5) (, -) (, -) (, -) (, -) (, -) (, -) (, -) (, -) Ex 8 v1 16 v5 10 13 v7 v6 9 11 7 17 v2 6 v4 14 5 v3 {v1,…,v7} (13, u0) (16, u0) (8, u0) {v1,…,v7}-v5 {v1,v3,v4,v6,v7} (18, v5) (13, u0) (25, v5) (15, v5) {v1,v3,v6,v7} (25, v5) (15, v5) {v3,v6,v7} (18, v5) (20, v4) {v6,v7} (20, v4) v5, v4 u0, Shortest u0-v3path:
Ex: Find the distances from 1 to x for each x=2, 3,…,8, and determine their shortest paths. 2 5 4 1 2 2 3 1 3 1 5 2 4 5 3 5