Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
Ch17: Single Source Shortest
PathPath
305233, 305234
Algorithm Analysis and Design
Jiraporn PooksookNaresuan University
Shortest Path Problem
• A motorist wishes to find the shortest possible
route from Chicago to Boston.
• Given a road map of the US on which the
distance between each pair of adjacent distance between each pair of adjacent
intersections is marked.
• How can we determine the shortest route?
Shortest Path Problem
• Given a weighted directed graph G =(V,E) with weight function
w : E -> R
• Mapping edges to read valued weights.
• Let path p = (v0 , v1 ,…, vk ) and (vi , vi+1 ) ϵ E for 0 ≤ i < k• Let path p = (v0 , v1 ,…, vk ) and (vi , vi+1 ) ϵ E for 0 ≤ i < k
• The weight of path p = (v0 , v1 ,…, vk ) is the sum of the weights of its constituent edges:
• We define the shortest-path weight from u to v by
If there is a path from u to v , otherwise
∑=
−=
k
iii
vvwpw1
1),()(
}:)(min{),( vupwvup
→=δ
∞=),( vuδ
Shortest Path Problem
0
s
a c
e1
5 3
2
0
b d
f2
1
1
31
1
42
d(u)=current weight
Shortest Path Problem
0
s
1
a c
e1
5 3
2
0
2
b d
f2
1
1
31
1
42
d(u)=current weight
Shortest Path Problem
0
s
1
a
6
c
3 e1
5 3
2
0
2
b d
f2
1
1
31
1
42
d(u)=current weight
Can we find a shorter path??6),( =csδ
Shortest Path Problem
0
s
1
a
5
c
3 e1
5 3
2
0
2
b d
f2
1
1
31
1
42
d(u)=current weight
5),( =csδ
Representing Shortest Path
• Given a graph G=(V,E)
• For each vertex v ϵ V, a predecessor ¶[v] that
is either another vertex or NIL.
• We denote d(v) as a value inside a • We denote d(v) as a value inside a
circle(graph) to be a current weight.
• We denote ¶[v], for any vertex v, as a
predecessor on the current best path to v.
• ¶[s]=NIL
Representing Short Path
0
s
1
a
6
c
3 e1
5 3
2
0
2
b d
f2
1
1
31
1
42
¶[e] = a
¶[a] = s
Negative-weight Edges
• There may be edges whose weights are
negative.
• If there is a negative-weight cycle reachable
from s, shortest-path weights are not well from s, shortest-path weights are not well
defined.
• If there is a negative-weight cycle on some
path from s to v, we define −∞=),( vsδ
Negative-weight Edges
3
a
-1
b
c dgs
3
-4
6
5 8
4
5
c
11
d
-ꚙ
e
-ꚙ
f
-ꚙ0
-3
3
-6
5
2
8
7
Negative-weight Edges
ꚙ
h
ꚙ
i
s
2
-8 3
ꚙ
j
0 -8 3
General Structure of Shortest Path
• Initialize single source
– For u ϵ V, we set d[v] = ꚙ, ¶[u] = NIL and d[s] = 0
• Relaxation
– Repeatedly select edge(u,v) and relax(u,v) by – Repeatedly select edge(u,v) and relax(u,v) by checking the condition:
if d[v] > d[u] + w(u,v)
then d[v] = d[u] + w(u,v)
¶[v] = u
Initialize-Single-Source(G,s)
for each vertex v in V[G]
do d[v] = ꚙ
¶[v] = NIL
d[s] = 0
Initialize-Single-Source(G,s)
0
s
ꚙ
a
ꚙ
c
ꚙ e1
5 3
2
0
ꚙ
b
ꚙ
d
ꚙf
2
1
1
31
1
42
d(u)=current weight
Relaxation(u,v,w)
if d[v] > d[u] + w(u,v)
then d[v] = d[u] + w(u,v)
¶[v] = u
Relaxation(d, c, w(d,c) )
0
s
1
a
6
c
3 e1
5 3
2
0
2
b
3
d
f2
1
1
31
1
42
d[c] > d[d] + w(d,c)
6 > 3 + 2
Hence, d[c] = 5 and ¶[c] = d
Shortest path in Directed Acyclic
Graphs
• We can compute shortest paths from a single
source in O(V+E) time using relaxation on
edges of a weighted directed acyclic
graph(dag).graph(dag).
DAG-SHORTEST-PATHS(G,w,s)
topologically sort the vertices of G
INITIALIZE-SINGLE-SOURCE(G,s)
for each vertex u, taken in topologically sorted order
do for each vertex v ϵ Adj[u]
do RELAX(u, v, w)
do for each vertex v ϵ Adj[u]
do RELAX(u, v, w)
Example: DAG
ꚙ
r
0
s
ꚙ
t
ꚙ
x
ꚙ
y
ꚙ
z5
6
2 7 -1 -2ꚙ 0 ꚙ ꚙ ꚙ ꚙ
5 2 7 -1 -2
3 4
2
Example: DAG
ꚙ
r
0
s
ꚙ
t
ꚙ
x
ꚙ
y
ꚙ
z5
6
2 7 -1 -2ꚙ 0 ꚙ ꚙ ꚙ ꚙ
5 2 7 -1 -2
3 4
2
Example: DAG
ꚙ
r
0
s
2
t
6
x
ꚙ
y
ꚙ
z5
6
2 7 -1 -2ꚙ 0 2 6 ꚙ ꚙ
5 2 7 -1 -2
3 4
2
Example: DAG
ꚙ
r
0
s
2
t
6
x
6
y
4
z5
6
2 7 -1 -2ꚙ 0 2 6 6 4
5 2 7 -1 -2
3 4
2
Example: DAG
ꚙ
r
0
s
2
t
6
x
5
y
4
z5
6
2 7 -1 -2ꚙ 0 2 6 5 4
5 2 7 -1 -2
3 4
2
Example: DAG
ꚙ
r
0
s
2
t
6
x
5
y
3
z5
6
2 7 -1 -2ꚙ 0 2 6 5 3
5 2 7 -1 -2
3 4
2
Example: DAG
ꚙ
r
0
s
2
t
6
x
5
y
3
z5
6
2 7 -1 -2ꚙ 0 2 6 5 3
5 2 7 -1 -2
3 4
2
Dijkstra Algorithm
• Solves the single-source shortest-paths
problem on a weighted directed graph G =
(V,E) for the case in which all edge weights are
nonnegative.nonnegative.
• We assume that w(u,v) ≥ 0 for each edge (u,v)
ϵ E.
Dijkstra(G,w,s)
INITIALIZE-SINGLE-SOURCE(G,s)
S =
Q = V[G]
while Q !=
do u = EXTRACT-MIN(Q)
∅
∅
do u = EXTRACT-MIN(Q)
S = S ꓴ {u}
for each vertex v ϵ Adj[u]
do RELAX(u,v,w)
∅
Analyze Dijkstra
• The running time of Dijkstra depends on how
to implement the min-priority queue.
• If we implement the min-priority queue with a
binary min-heap which has running time binary min-heap which has running time
O(lg V) if all vertices are reachable from the
source. Hence total time is O((V+E)lg V)
= O( E lg V)
Example: Dijkstra
0s
ꚙ
t
ꚙ
x
10
1
6
923
0s
ꚙ
y
ꚙ
z
4 6
2
7
23
5
S ={ }
Q = {0, ꚙ, ꚙ, ꚙ, ꚙ}
Example: Dijkstra
0s
10
t
ꚙ
x
10
1
6
923
0s
5
y
ꚙ
z
4 6
2
7
23
5
S ={ s }
Q = {0, 10, 5, ꚙ, ꚙ}
Example: Dijkstra
0s
8
t
14
x
10
1
6
923
0s
5
y
7
z
4 6
2
7
23
5
S ={ s , y }
Q = {0, 8, 5, 14, 7}
Example: Dijkstra
0s
8
t
13
x
10
1
6
923
0s
5
y
7
z
4 6
2
7
23
5
S ={ s , y, z }
Q = {0, 8, 5, 13, 7}
Example: Dijkstra
0s
8
t
9
x
10
1
6
923
0s
5
y
7
z
4 6
2
7
23
5
S ={ s ,t, y, z }
Q = {0, 8, 5, 9, 7}
Example: Dijkstra
0s
8
t
9
x
10
1
6
923
0s
5
y
7
z
4 6
2
7
23
5
S ={ s ,t,x, y, z }
Q = {0, 8, 5, 9, 7}
Bellman-Ford Algorithm
• Solves the single-source shortest-paths problem in general case in which edge weights may be
negative.
• The Bellman-Ford algorithm returns a booleanvalue indicating whether or not there is a value indicating whether or not there is a negative-weight cycle that is reachable from the source. If there is such a cycle, the algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortespaths and their weights.
Bellman-Ford(G,w,s)
INITIALIZE-SINGLE-SOURCE(G,s)
for i =1 to |V[G]| -1
do for each edge (u,v) ϵ E[G]
do RELAX(u, v , w)
for each edge (u,v) ϵ E[G]for each edge (u,v) ϵ E[G]
do if d[v] > d[u] + w(u,v)
then return false
return true
Analyze Bellman-Ford
• The running time is O(VE) since the
initialization take O(V) and each of |V|-1
passes over edges in lines 2-4 takes O(E), and
for loop in lines 5-7 takes O(E) time.for loop in lines 5-7 takes O(E) time.
Example: Bellman-Ford
0s ꚙ
x
ꚙ
z2 2
0s ꚙ ꚙ
ꚙ
y
2 2
Example: Bellman-Ford
0s 2
x
4
z2 2
0s 2 4
4
y
2 2
Example: Bellman-Ford
0s ꚙ
x
ꚙ
y-2 -2
0s ꚙ ꚙ
ꚙ
z
-2 -2
Example: Bellman-Ford
0s -2
x
-4
y-2 -2
-4 > -8 + -2
Then
d[y] = -10
0s -2 -4
-6
z
-2 -2
RETURN false !!!
-2 > -6 + -2
Then
d[x] = -8
Example: Bellman-Ford
0s
ꚙ
t
ꚙ
x
6
-2
-3
8
5
0s
ꚙ
y
ꚙ
z
7-4
9
2
8
7
Example: Bellman-Ford
0s
6
t
ꚙ
x
6
-2
-3
8
5
0s
7
y
ꚙ
z
7-4
9
2
8
7
Example: Bellman-Ford
0s
6
t
4
x
6
-2
-3
8
5
0s
7
y
2
z
7-4
9
2
8
7
Example: Bellman-Ford
0s
2
t
4
x
6
-2
-3
8
5
0s
7
y
2
z
7-4
9
2
8
7
Example: Bellman-Ford
0s
2
t
4
x
6
-2
-3
8
5
0s
7
y
-2
z
7-4
9
2
8
7
RETURN true
With shortest path 0, 2, 4, 7, -2
Negative weighted cycle
0
s3 2 3 3 2
-6
v
03 2
The shortest simple path to reach v from s = 13
If we have negative edge cycle in a path , it takes
exponential running time to solve.