1.1 data structure and algorithm lecture 11 application of bfs shortest path topics reference:...
Post on 20-Dec-2015
216 views
TRANSCRIPT
![Page 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](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/1.jpg)
1.1Data Structure and Algorithm
Lecture 11
Application of BFS Shortest Path
Topics
Reference: Introduction to Algorithm by Cormen
Chapter 25: Single-Source Shortest Paths
![Page 2: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/2.jpg)
1.2Data 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)
![Page 3: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/3.jpg)
1.3Data 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.
![Page 4: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/4.jpg)
1.4Data 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
![Page 5: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/5.jpg)
1.5Data 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.
![Page 6: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/6.jpg)
1.6Data 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
![Page 7: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/7.jpg)
1.7Data 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 }
![Page 8: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/8.jpg)
1.8Data 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()
![Page 9: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/9.jpg)
1.9Data 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
![Page 10: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/10.jpg)
1.10Data 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()
![Page 11: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/11.jpg)
1.11Data 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
![Page 12: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/12.jpg)
1.12Data 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
![Page 13: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/13.jpg)
1.13Data 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()
![Page 14: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/14.jpg)
1.14Data 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
44X
![Page 15: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/15.jpg)
1.15Data 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
44X
ExtractMin()
![Page 16: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/16.jpg)
1.16Data 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
44X
35X
59 X
![Page 17: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/17.jpg)
1.17Data 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
44X
35X
59 X
ExtractMin
![Page 18: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/18.jpg)
1.18Data 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
44X
35X
59 XX 51
X 34
![Page 19: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/19.jpg)
1.19Data 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
44X
35X
59 XX 51
X 34
ExtractMin
![Page 20: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/20.jpg)
1.20Data 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
44X
35X
59 XX 51
X 34
X 50
X 45
![Page 21: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/21.jpg)
1.21Data 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
44X
35X
59 XX 51
X 34
X 50
X 45
ExtractMin
![Page 22: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/22.jpg)
1.22Data 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
44X
35X
59 XX 51
X 34
X 50
X 45
![Page 23: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/23.jpg)
1.23Data 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
44X
35X
59 XX 51
X 34
X 50
X 45
ExtractMin
![Page 24: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/24.jpg)
1.24Data 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
44X
35X
59 XX 51
X 34
X 50
X 45
ExtractMin
![Page 25: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/25.jpg)
1.25Data 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)
![Page 26: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/26.jpg)
1.26Data Structure and Algorithm
Example:Bellman-Ford
z
u
x
v
y
0
7
6
8 7
9
5
-2
-3
-4
2
![Page 27: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/27.jpg)
1.27Data Structure and Algorithm
Example:Bellman-Ford
z
u
x
v
y
0
6
7
7
6
8 7
9
5
-2
-3
-4
2
![Page 28: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/28.jpg)
1.28Data 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
![Page 29: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/29.jpg)
1.29Data 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
![Page 30: 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source](https://reader031.vdocument.in/reader031/viewer/2022032800/56649d435503460f94a1ff8c/html5/thumbnails/30.jpg)
1.30Data 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