Download presentation
Presentation is loading. Please wait.
1
Chapter 13 Graph Algorithms
Graph Theory Chapter 13 Graph Algorithms 大葉大學 資訊工程系 黃鈴玲
2
Contents 13.4 Breadth-First Search 13.5 Depth-First Search
13.6 Connected Components 13.7 Dijkstra’s Shortest Path Algorithm
3
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.
4
// 每次mark一點就把 i + 1 若G不是connected,要執行到 所有點都mark為止。
5
Example 13.5 T1 T2 T3 T4 T5 s y=6 Q: 1 6 8 5 9 x=1
6
source 2 7 4 3
8
Ex: Find a BFS forest for the following graph G with source v1
9
13.5 Depth-First Search
11
Ex: Find a DFS forest for the following graph G with source v1
12
13.6 Connected Components 用DFS拜訪所有點 Component number 1 2 3 4 1 1 3 2 2
14
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
15
Father[3] =1 第1個for迴圈 source Father[4] =1 Umin={3}
16
第2個for迴圈 Father[3] =1 Father[2] =3 i=1 Father[5] =3 Father[4] =3
source Father[5] =3 Father[4] =3 Father[6] =3 Umin={4}
17
Father[3] =1 Father[2] =3 i=2 Father[5] =4 Father[4] =3 Father[6] =3
source Father[5] =4 Father[4] =3 Father[6] =3 Umin={5}
18
Father[3] =1 Father[2] =3 i=3 Father[5] =4 Father[6] =5 Father[4] =3
source Father[5] =4 Father[4] =3 Father[6] =5 Umin={2}
19
Father[3] =1 Father[2] =3 i=4 Father[5] =4 Father[4] =3 Father[6] =2
source Father[5] =4 Father[4] =3 Father[6] =2 end
20
Ex v1 v2 v3 v4 v5 v6 v7 U Shortest u0-v3 path: u0, v5, v4 v5 v7 v6 v2
Source u0 Ex 8 v1 16 v5 10 13 v7 v6 9 11 7 17 v2 6 v4 pathcost father minimum 14 5 v3 v1 v2 v3 v4 v5 v6 v7 U (, -) {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} (18, v5) (, -) (25, v5) (15, v5) {v3,v6,v7} (18, v5) (20, v4) (, -) {v6,v7} (20, v4) (, -) (, -) Shortest u0-v3 path: u0, v5, v4
21
Ex: Find the distances from 1 to x for each x=2, 3,…,8, and determine their shortest paths.
5 4 1 2 2 3 1 3 1 5 2 4 5 3 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.