Graph Theory Chapter 13 Graph Algorithms 2011 10

  • Slides: 21
Download presentation
Graph Theory Chapter 13 Graph Algorithms 大葉大學 資訊 程系 黃鈴玲 2011. 10

Graph Theory Chapter 13 Graph Algorithms 大葉大學 資訊 程系 黃鈴玲 2011. 10

Contents Ø Ø 13. 4 13. 5 13. 6 13. 7 Breadth-First Search Depth-First

Contents Ø Ø 13. 4 13. 5 13. 6 13. 7 Breadth-First Search Depth-First Search Connected Components Dijkstra’s Shortest Path Algorithm 2

13. 4 Breadth-First Search 先進先出 Three operations: (1) Enqueue(Q, u): adds u to the

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. 3

Example 13. 5 T 1 T 2 T 3 T 4 T 5 s

Example 13. 5 T 1 T 2 T 3 T 4 T 5 s y=6 Q: 1 6 8 5 9 x=1 5

source 2 7 4 3 6

source 2 7 4 3 6

7

7

Ex: Find a BFS forest for the following graph G with source v 1.

Ex: Find a BFS forest for the following graph G with source v 1. (下標小的優先選) G v 12 v 5 v 13 v 8 v 2 v 7 v 6 v 15 v 4 v 10 v 14 v 9 v 3 v 11 8

13. 5 Depth-First Search 9

13. 5 Depth-First Search 9

10

10

Ex: Find a DFS forest for the following graph G with source v 1.

Ex: Find a DFS forest for the following graph G with source v 1. (下標小的優先選) G v 12 v 5 v 13 v 8 v 2 v 7 v 6 v 15 v 4 v 10 v 14 v 9 v 3 v 11 11

13. 6 Connected Components Component number 1 2 3 4 1 1 3 2

13. 6 Connected Components Component number 1 2 3 4 1 1 3 2 2 2 4 用DFS拜訪所有點 1 4 4 2 1 12

13

13

13. 7 Dijkstra’s Shortest Path algorithm 加Father[. ] of size n: 記錄shortest path的上一點 Father[i]

13. 7 Dijkstra’s Shortest Path algorithm 加Father[. ] of size n: 記錄shortest path的上一點 Father[i] =1 if W 1 i Father[u] =v if pathcost[u] = pathcost[v] +Wuv 14

第 1個for迴圈 Father[3] =1 source Father[4] =1 Umin={3 } 15

第 1個for迴圈 Father[3] =1 source Father[4] =1 Umin={3 } 15

第 2個for迴圈 Father[3] =1 i=1 Father[2] =3 source Father[5] =3 Father[6] =3 Father[4] =3

第 2個for迴圈 Father[3] =1 i=1 Father[2] =3 source Father[5] =3 Father[6] =3 Father[4] =3 Umin={4 } 16

Father[3] =1 i=2 Father[2] =3 source Father[5] =4 Father[6] =3 Father[4] =3 Umin={5 }

Father[3] =1 i=2 Father[2] =3 source Father[5] =4 Father[6] =3 Father[4] =3 Umin={5 } 17

Father[3] =1 i=3 Father[2] =3 source Father[5] =4 Father[6] =5 Father[4] =3 Umin={2 }

Father[3] =1 i=3 Father[2] =3 source Father[5] =4 Father[6] =5 Father[4] =3 Umin={2 } 18

Father[3] =1 i=4 Father[2] =3 source Father[5] =4 Father[4] =3 Father[6] =2 end 19

Father[3] =1 i=4 Father[2] =3 source Father[5] =4 Father[4] =3 Father[6] =2 end 19

u 0 Source Ex 16 10 v 1 13 v 6 pathcost v 7

u 0 Source Ex 16 10 v 1 13 v 6 pathcost v 7 17 6 14 5 7 v 4 minimum v 3 father v 2 v 5 9 11 v 2 v 1 8 v 3 v 4 v 5 v 6 v 7 U {v 1, …, v 7 ( , -) (13, u 0) ( , -) (16, u 0) (8, u 0) ( , -) {v}1, …, v 7}-v 5 ( , -) ( , -) (18, v 5) (25, v 5) (15, v 5) ( , -) {v 1, v 3, v 4, v 6, v 7} {v 1, v 3, v 6, v 7} ( , -) (18, v 5) (20, v 4) ( , -) (18, v 5) (13, u 0) (25, v 5) (15, v 5) ( , -) Shortest u 0 -v 3 path: u 0, v 5, v 4 {v 3, v 6, v 7} {v 6, v 7} Ch 4 -20

Ex: Find the distances from 1 to x for each x=2, 3, …, 8,

Ex: Find the distances from 1 to x for each x=2, 3, …, 8, and determine their shortest paths. 5 2 4 1 2 2 3 1 5 2 4 5 3 5 21