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
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)
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.
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
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.
1.6 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm Find shortest path from s to t. s 3 t
1.7 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 0 distance label S = { } Q = { s, 2, 3, 4, 5, 6, 7, t }
1.8 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 0 distance label S = { } Q = { s, 2, 3, 4, 5, 6, 7, t } ExtractMin()
1.9 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 distance label S = { s } Q = { 2, 3, 4, 5, 6, 7, t } decrease key X X X
1.10 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 distance label S = { s } Q = { 2, 3, 4, 5, 6, 7, t } X X X ExtractMin()
1.11 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X X X
1.12 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X X X decrease key X 32
1.13 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } X X X X 32 ExtractMin()
1.14 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 6 } Q = { 3, 4, 5, 7, t } X X X X X
1.15 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 6 } Q = { 3, 4, 5, 7, t } X X X X X ExtractMin()
1.16 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } X X X X X 35 X 59 X
1.17 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } X X X X X 35 X 59 X ExtractMin
1.18 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 3, 6, 7 } Q = { 4, 5, t } X X X X X 35 X 59 XX 51 X 34
1.19 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 3, 6, 7 } Q = { 4, 5, t } X X X X X 35 X 59 XX 51 X 34 ExtractMin
1.20 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 3, 5, 6, 7 } Q = { 4, t } X X X X X 35 X 59 XX 51 X 34 X 50 X 45
1.21 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 3, 5, 6, 7 } Q = { 4, t } X X X X X 35 X 59 XX 51 X 34 X 50 X 45 ExtractMin
1.22 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 3, 4, 5, 6, 7 } Q = { t } X X X X X 35 X 59 XX 51 X 34 X 50 X 45
1.23 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 3, 4, 5, 6, 7 } Q = { t } X X X X X 35 X 59 XX 51 X 34 X 50 X 45 ExtractMin
1.24 Data Structure and Algorithm Dijkstra's Shortest Path Algorithm s 3 t 14 0 S = { s, 2, 3, 4, 5, 6, 7, t } Q = { } X X X X X 35 X 59 XX 51 X 34 X 50 X 45 ExtractMin
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)
1.26 Data Structure and Algorithm Example:Bellman-Ford z u x v y 0
1.27 Data Structure and Algorithm Example:Bellman-Ford z u x v y 0 6
1.28 Data Structure and Algorithm Example:Bellman-Ford z u x v y
1.29 Data Structure and Algorithm Example:Bellman-Ford z u x v y
1.30 Data Structure and Algorithm Example:Bellman-Ford z u x v y