Download presentation
Presentation is loading. Please wait.
1
Lecture 11 Topics Application of BFS Shortest Path
Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source Shortest Paths Data Structure and Algorithm
2
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
3
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
4
Algorithm( Cont.) Initialize(G,s) relax(u,v,w)
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
5
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
6
Dijkstra's Shortest Path Algorithm
Find shortest path from s to t. 2 23 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 Data Structure and Algorithm
7
Dijkstra's Shortest Path Algorithm
Q = { s, 2, 3, 4, 5, 6, 7, t } 2 23 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label Data Structure and Algorithm
8
Dijkstra's Shortest Path Algorithm
Q = { s, 2, 3, 4, 5, 6, 7, t } ExtractMin() 2 23 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label Data Structure and Algorithm
9
Dijkstra's Shortest Path Algorithm
Q = { 2, 3, 4, 5, 6, 7, t } decrease key X 9 2 23 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label 15 X Data Structure and Algorithm
10
Dijkstra's Shortest Path Algorithm
Q = { 2, 3, 4, 5, 6, 7, t } ExtractMin() X 9 2 23 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label 15 X Data Structure and Algorithm
11
Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 6, 7, t } X 9 2 23 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X Data Structure and Algorithm
12
Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 6, 7, t } decrease key X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X Data Structure and Algorithm
13
Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 6, 7, t } X 32 X 9 2 23 3 9 ExtractMin() s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X Data Structure and Algorithm
14
Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 7, t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 30 X 4 19 11 15 5 5 6 20 16 t 7 44 15 X Data Structure and Algorithm
15
Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, 7, t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 4 30 X 19 11 15 5 5 6 20 16 t 7 44 15 X ExtractMin() Data Structure and Algorithm
16
Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 X 4 30 19 11 15 5 5 6 20 16 t 7 44 59 15 X X Data Structure and Algorithm
17
Dijkstra's Shortest Path Algorithm
Q = { 3, 4, 5, t } ExtractMin X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 4 30 X 19 11 15 5 5 6 20 16 t 7 44 59 15 X X Data Structure and Algorithm
18
Dijkstra's Shortest Path Algorithm
Q = { 4, 5, t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 30 X 4 19 11 15 5 5 6 20 16 t 7 44 51 59 15 X X X Data Structure and Algorithm
19
Dijkstra's Shortest Path Algorithm
Q = { 4, 5, t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 30 X 4 19 11 15 5 5 6 20 ExtractMin 16 t 7 44 51 59 15 X X X Data Structure and Algorithm
20
Dijkstra's Shortest Path Algorithm
Q = { 4, t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 45 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 59 15 X X X X Data Structure and Algorithm
21
Dijkstra's Shortest Path Algorithm
Q = { 4, t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 34 45 X X 4 30 X 19 11 15 5 ExtractMin 5 6 20 16 t 7 44 50 51 59 15 X X X X Data Structure and Algorithm
22
Dijkstra's Shortest Path Algorithm
Q = { t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 45 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 59 15 X X X X Data Structure and Algorithm
23
Dijkstra's Shortest Path Algorithm
Q = { t } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 34 45 X X 4 30 X 19 11 15 5 5 6 20 16 t 7 44 ExtractMin 50 51 59 15 X X X X Data Structure and Algorithm
24
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t } Q = { } X 32 X 9 2 23 3 9 s 18 14 X 14 2 6 6 44 X 35 34 45 X X 4 30 X 19 11 15 5 5 6 20 16 t 7 44 ExtractMin 50 51 59 15 X X X X Data Structure and Algorithm
25
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) if d[v] > d[u] + w(u,v) then return FALSE return TRUE Complexity O(VE) Data Structure and Algorithm
26
Example:Bellman-Ford
z u x v y 7 6 8 9 5 -2 -3 -4 2 Data Structure and Algorithm
27
Example:Bellman-Ford
z u x v y 6 7 8 9 5 -2 -3 -4 2 Data Structure and Algorithm
28
Example:Bellman-Ford
z u x v y 6 4 2 7 8 9 5 -2 -3 -4 Data Structure and Algorithm
29
Example:Bellman-Ford
z u x v y 2 4 7 6 8 9 5 -2 -3 -4 Data Structure and Algorithm
30
Example:Bellman-Ford
z u x v y 2 4 -2 7 6 8 9 5 -3 -4 Data Structure and Algorithm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.