1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference:...
-
date post
20-Dec-2015 -
Category
Documents
-
view
216 -
download
0
Transcript of 1.1 Data Structure and Algorithm Lecture 11 Application of BFS Shortest Path Topics Reference:...
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
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)
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.
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
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.
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
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 }
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()
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
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()
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
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
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()
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
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()
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
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
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
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
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
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
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
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
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
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)
1.26Data Structure and Algorithm
Example:Bellman-Ford
z
u
x
v
y
0
7
6
8 7
9
5
-2
-3
-4
2
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
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
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
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