300 likes | 417 Views
Lecture 11. Application of BFS Shortest Path. Topics. Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source Shortest Paths. Dijkstra's Shortest Path Algorithm.
E N D
Lecture 11 • Application of BFS • Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source Shortest Paths 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) 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 (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. 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 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. Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm • Find shortest path from s to t. 2 23 3 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { } Q = { s, 2, 3, 4, 5, 6, 7, t } 2 23 3 0 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { } Q = { s, 2, 3, 4, 5, 6, 7, t } ExtractMin() 2 23 3 0 9 s 18 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s } Q = { 2, 3, 4, 5, 6, 7, t } decrease key 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s } Q = { 2, 3, 4, 5, 6, 7, t } ExtractMin() 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 distance label 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } decrease key 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2 } Q = { 3, 4, 5, 6, 7, t } 32 X 9 X 2 23 3 0 9 ExtractMin() s 18 14 X 14 6 2 6 4 19 30 11 5 15 5 6 16 20 t 7 44 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 6 } Q = { 3, 4, 5, 7, t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 44 4 X 19 30 11 5 15 5 6 16 20 t 7 44 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 6 } Q = { 3, 4, 5, 7, t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 44 4 X 19 30 11 5 15 5 6 16 20 t 7 44 15 ExtractMin() X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 35 44 X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 59 X 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 6, 7 } Q = { 3, 4, 5, t } ExtractMin 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 35 44 X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 59 X 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 6, 7 } Q = { 4, 5, t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 35 34 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 51 59 X X 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 6, 7 } Q = { 4, 5, t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 35 34 44 X X 4 X 19 30 11 5 15 5 6 16 ExtractMin 20 t 7 44 51 59 X X 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 5, 6, 7 } Q = { 4, t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 5, 6, 7 } Q = { 4, t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 ExtractMin 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7 } Q = { t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 50 51 59 X X X 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7 } Q = { t } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 ExtractMin 50 51 59 X X X 15 X Data Structure and Algorithm
Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } Q = { } 32 X 9 X 2 23 3 0 9 s 18 14 X 14 6 2 6 45 35 34 X 44 X X 4 X 19 30 11 5 15 5 6 16 20 t 7 44 ExtractMin 50 51 59 X X X 15 X 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) Data Structure and Algorithm
Example:Bellman-Ford 5 v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 Data Structure and Algorithm
Example:Bellman-Ford 5 6 v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 7 Data Structure and Algorithm
Example:Bellman-Ford 5 6 4 v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 7 2 Data Structure and Algorithm
Example:Bellman-Ford 5 2 4 v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 7 2 Data Structure and Algorithm
Example:Bellman-Ford 5 2 4 v u -2 6 -3 z 8 7 2 0 -4 7 x y 9 7 -2 Data Structure and Algorithm