Download presentation
Presentation is loading. Please wait.
1
1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source Shortest Paths
2
1.2 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm Dijkstra’s algorithm solves the single- source shortest paths problem on a weighted, directed graph G =(V,E) fro the case in which all edge weights are nonnegative. We assume that w(u,v) >=0 for each edge (u,v)
3
1.3 Data Structure and Algorithm Algorithm Dijkstra(G,w,s) Initialize(G,s) S = 0 Q = V[G] While Q != 0 do U = ExtractMin(Q) S = S union {u} For each vertex v of adj[u] do Relax(u,v,w) Complexty: Using priority queue:O (V 2 +E) Using heap as priority queue: O( (V+E) lg 2 V) cost of building heap is O(V) cost of ExtracMin is O(lgV) and there are |V| such operations cost of relax is O(lgV) and there are |E| such operations.
4
1.4 Data Structure and Algorithm Algorithm( Cont.) Initialize(G,s) for each vertex of V[G] do d[v] = infinity Л[v] = NIL d[s] = 0 relax(u,v,w) if d[v] >d[u] +w(u,v) then d[v] = d[u] + w(u,v) Л[v] = u
5
1.5 Data Structure and Algorithm Complexity Using priority queue:O (V2 +E) Using heap as priority queue: O( (V+E) lg2V) cost of building heap is O(V) cost of ExtracMin is O(lgV) and there are |V| such operations cost of relax is O(lgV) and there are |E| such operations.
6
1.6 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm Find shortest path from s to t. s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6
7
1.7 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 0 distance label S = { } Q = { s, 2, 3, 4, 5, 6, 7, t }
8
1.8 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 0 distance label S = { } Q = { s, 2, 3, 4, 5, 6, 7, t } ExtractMin()
9
1.9 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 distance label S = { s } Q = { 2, 3, 4, 5, 6, 7, t } decrease key X X X
10
1.10 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 distance label S = { s } Q = { 2, 3, 4, 5, 6, 7, t } X X X ExtractMin()
11
1.11 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X X X
12
1.12 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X X X decrease key X 32
13
1.13 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X X X X 32 ExtractMin()
14
1.14 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 6 } Q = { 3, 4, 5, 7, t } X X X X 32 44 X
15
1.15 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 6 } Q = { 3, 4, 5, 7, t } X X X X 32 44 X ExtractMin()
16
1.16 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } X X X X 32 44 X 35 X 59 X
17
1.17 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } X X X X 32 44 X 35 X 59 X ExtractMin
18
1.18 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 3, 6, 7 } Q = { 4, 5, t } X X X X 32 44 X 35 X 59 XX 51 X 34
19
1.19 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 3, 6, 7 } Q = { 4, 5, t } X X X X 32 44 X 35 X 59 XX 51 X 34 ExtractMin
20
1.20 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 3, 5, 6, 7 } Q = { 4, t } X X X X 32 44 X 35 X 59 XX 51 X 34 X 50 X 45
21
1.21 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 3, 5, 6, 7 } Q = { 4, t } X X X X 32 44 X 35 X 59 XX 51 X 34 X 50 X 45 ExtractMin
22
1.22 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 3, 4, 5, 6, 7 } Q = { t } X X X X 32 44 X 35 X 59 XX 51 X 34 X 50 X 45
23
1.23 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 3, 4, 5, 6, 7 } Q = { t } X X X X 32 44 X 35 X 59 XX 51 X 34 X 50 X 45 ExtractMin
24
1.24 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 15 9 14 0 S = { s, 2, 3, 4, 5, 6, 7, t } Q = { } X X X X 32 44 X 35 X 59 XX 51 X 34 X 50 X 45 ExtractMin
25
1.25 Data Structure and Algorithm The Bellman-Form Algorithm BELLMAN-FORD(G,w,s) Initialize() for i = 1 to |V[G]| -1 do for each edge (u,v) of E[G] do relax(u,v,w) for each edge (u,v) of E[G] do if d[v] > d[u] + w(u,v) then return FALSE return TRUE Complexity O(VE)
26
1.26 Data Structure and Algorithm Example:Bellman-Ford z u x v y 0 7 6 8 7 9 5 -2 -3 -4 2
27
1.27 Data Structure and Algorithm Example:Bellman-Ford z u x v y 0 6 7 7 6 8 7 9 5 -2 -3 -4 2
28
1.28 Data Structure and Algorithm Example:Bellman-Ford z u x v y 0 6 4 2 7 7 6 8 7 9 5 -2 -3 -4 2
29
1.29 Data Structure and Algorithm Example:Bellman-Ford z u x v y 0 2 4 2 7 7 6 8 7 9 5 -2 -3 -4 2
30
1.30 Data Structure and Algorithm Example:Bellman-Ford z u x v y 0 2 4 -2 7 7 6 8 7 9 5 -3 -4 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.