single-source shortest pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · single-source...
TRANSCRIPT
Single-Source Shortest Paths
Hosung Jo
Division of Computer and Engineering
Hanyang University
Contents
Definition
The Bellman-Ford algorithm
Single-source shortest paths in directed acyclic graphs
2
Single-source shortest paths in directed acyclic graphs
Dijkstra's algorithm
Definition
� Edge weight
� Path weight
� The sum of all edge weights in the path.
3
� A Shortest path from u to v.
� A path from u to v whose weight is the smallest.
� Vertex u is the source and v is the destination.
� The Shortest-path weight from u and v.
� The weight of a shortest-path from u and v
� δ(u,v)
Definition
Shortest-path problems
� Single-source & single-destination
� Single-source
4
� Single-destination
� All pairs
Negative-weight edges
What is a shortest path from s to g?
-4a b
5
3 -1
5 11
-∞ -∞
0 -∞
-4
6
-3
3
-6
4
8
72
5
3
a b
gdcs
e f
∞ ∞
∞
h i
j
2
3-8
Negative-weight edges
Do all negative-weight edges make problems?
Do all negative-weight cycles make problems?
a b
6
3 -1
5 11
-∞ -∞
0 -∞
-4
6
-3
3
-6
4
8
72
5
3
a b
gdcs
e f
∞ ∞
∞
h i
j
2
3-8
Negative-weight edges
Do all negative-weight cycles reachable from the
source make problems?
a b
7
3 -1
5 11
-∞ -∞
0 -∞
-4
6
-3
3
-6
4
8
72
5
3
a b
gdcs
e f
∞ ∞
∞
h i
j
2
3-8
Negative-weight edges
Single-source shortest paths can be defined if there
are not any negative-weight cycles reachable from
the source.
8
3 -1
5 11
-∞ -∞
0 -∞
-4
6
-3
3
-6
4
8
72
5
3
a b
gdcs
e f
∞ ∞
∞
h i
j
2
3-8
Cycles
Cycles
� A shortest path does not include cycles.
� A shortest-path length is at most |V|-1.
9
Predecessor subgraph
Predecessor subgraph
� Shortest-path tree
� Optimal substructure
10
0s
3
t
5
y
9
x
11
z
3
2 12 7
6
4
3
6
5
Predecessor subgraph
3
t
9
x
3
6
4
3
t
9
x
3
6
11
0s
5
y
11
z
2 12 7
4
3
6
5
0s
5
y
11
z
3
2 12 7
4
3
6
5
Relaxation
Relaxation
u v u v
12
952
752
Relax(u,v,w)
652
652
Relax(u,v,w)
u v
u v u v
u v
Dijkstra's algorithm
Dijkstra's algorithm
� It works properly when all edge weights are nonnegative.
13
Dijkstra's algorithm
DIJKSTRA(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 S ← Ø
3 Q ← V[G]
14
3 Q ← V[G]
4 while Q ≠ Ø
5 do u ← EXTRACT-MIN(Q)
6 S ← S {u}
7 for each vertex v Adj[u]
8 do RELAX(u, v, w)
∈
U
Dijkstra’s Algorithm
s t y x z
0 ∞ ∞ ∞ ∞ 0s
∞
t
∞
x
Q10
2 3
1
94 6
15
0 ∞ ∞ ∞ ∞ 0
∞
y
∞
z
S
5 7
2
Dijkstra’s Algorithm
s t y x z
0 ∞ ∞ ∞ ∞ 0s
10
t
∞
x
Q10
2 3
1
94 6
16
0 ∞ ∞ ∞ ∞
10 5 - -
0
5
y
∞
z
S
5 7
2
={s}
Dijkstra’s Algorithm
s t y x z
0 ∞ ∞ ∞ ∞ 0s
10
t
∞
x
Q10
2 3
1
94 6
={s, y}17
0 ∞ ∞ ∞ ∞
10 5 - -
0
5
y
∞
z
S
5 7
2
Dijkstra’s Algorithm
s t y x z
0 ∞ ∞ ∞ ∞ 0s
8
t
14
x
Q10
2 3
1
94 6
={s, y}18
0 ∞ ∞ ∞ ∞
10 5 - -
8 14 7
0
5
y
7
z
S
5 7
2
Dijkstra’s Algorithm
s t y x z
0 ∞ ∞ ∞ ∞ 0s
8
t
13
x
Q10
2 3
1
94 6
={s, y, z, t}19
0 ∞ ∞ ∞ ∞
10 5 - -
8 14 7
8 13
0
5
y
7
z
S
5 7
2
Dijkstra’s Algorithm
s t y x z
0 ∞ ∞ ∞ ∞ 0s
8
t
9
x
Q10
2 3
1
94 6
={s, y, z, t}20
0 ∞ ∞ ∞ ∞
10 5 - -
8 14 7
8 13
9
0
5
y
7
z
S
5 7
2
Dijkstra’s Algorithm
s t y x z
0 ∞ ∞ ∞ ∞ 0s
8
t
9
x
Q10
2 3
1
94 6
={s, y, z, t, x}21
0 ∞ ∞ ∞ ∞
10 5 - -
8 14 7
8 13
9
0
5
y
7
z
S
5 7
2
Dijkstra's algorithm
DIJKSTRA(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 S ← Ø
3 Q ← V[G]
22
3 Q ← V[G]
4 while Q ≠ Ø
5 do u ← EXTRACT-MIN(Q)
6 S ← S {u}
7 for each vertex v Adj[u]
8 do RELAX(u, v, w)
∈
U
Dijkstra's algorithm
Running time
� O(V2) if we use an array
� O(VlgV + ElgV) if we use a heap
23
� O(VlgV + E) if we use a Fibonacci heap.
The Bellman-Ford algorithm
The Bellman-Ford algorithm
� it solves the single source shortest-paths problem in the
general case in which edge weights may be negative.
24
The Bellman-Ford algorithm
BELLMAN-FORD(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 For i←1 to |V[G]|-1
3 do for each edge(u, v) E[G]
25
3 do for each edge(u, v) E[G]
4 do RELAX(u, v, w)
5 for each edge(u, v) E[G]
6 do if d[v] > d[u] + w(u, v)
7 then return FALSE
8 return TRUE
∈
∈
The Bellman-Ford algorithm
5 xt
� Relaxation order
�(t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)
xt 5
26
5
∞ ∞
∞ ∞
0s
zy
6
7
8-3
72
9
-2xt
-4
6 ∞
7 ∞
0s
zy
6
7
8-3
72
9
-2xt
-4
5
The Bellman-Ford algorithm
� Relaxation order
�(t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)
5
27
6 4
7 2
0s
zy
6
7
8-3
72
9
-2xt
-4
5
2 4
7 2
0s
zy
6
7
8-3
72
9
-2xt
-4
5
The Bellman-Ford algorithm
� Relaxation order
�(t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)
28
2 4
7 −2
0s
zy
6
7
8-3
72
9
-2xt
-4
5
The Bellman-Ford algorithm
The Bellman-Ford algorithm
� Running time : O(VE)
29
The Bellman-Ford algorithm
( )∑∑=
−−
=
+≤k
i
iii
k
i
i vvwvdvd1
11
1
),(][][
∑ ∑= =
−− +=k
i
k
i
iii vvwvd1 1
11 ),(][
30
= =i i1 1
∑∑=
−
=
=k
i
i
k
i
i vdvd1
1
1
][][
∑=
−≤k
i
ii vvw1
1 ),(0
Single-source shortest paths
in directed acyclic graphs
DAG-SHORTEST-PATHS(G, w, s)
1 topologically sort the vertices of G
2 INITIALIZE-SINGLE-SOURCE(G, s)
3 for each vertex u, taken in topologically sorted order
31
3 for each vertex u, taken in topologically sorted order
4 do for each vertex v Adj[u]
5 do RELAX(u, v, w)
∈
Single-source shortest paths
in directed acyclic graphs
0∞ ∞∞ ∞∞
r s t x y z5
3
2
6
7 -1 -2
1
42
32
0∞ ∞∞ ∞∞
r s t x y z5
3
2
6
7 -1 -2
1
42
Single-source shortest paths
in directed acyclic graphs
0∞ 62 ∞∞
r s t x y z5
3
2
6
7 -1 -2
1
42
33
0∞ 62 46
r s t x y z5
3
2
6
7 -1 -2
1
42
Single-source shortest paths
in directed acyclic graphs
0∞ 62 45
r s t x y z5
3
2
6
7 -1 -2
1
42
34
0∞ 62 35
r s t x y z5
3
2
6
7 -1 -2
1
42
Single-source shortest paths
in directed acyclic graphs
0∞ 62 35
r s t x y z5
3
2
6
7 -1 -2
1
42
35
Running time: O(V+E) time
PERT chart
PERT
� Program evaluation and review technique
� Edges represent jobs to be performed.
36
� Edge weights represent the times required to perform
particular jobs.
PERT chart
PERT
� If edge (u,v) enters vertex v and edge (v,x) leaves v,
then job (u,v) must be performed prior to job (v,x).
A path through this dag represents a sequence of jobs
37
� A path through this dag represents a sequence of jobs
that must be performed in a particular order.
� A critical path is a longest path through the dag.
PERT chart
Finding a critical path in a dag
� Negate the edge weights and run DAG-SHORTEST-
38
Negate the edge weights and run DAG-SHORTEST-
PATHS or
� Run DAG-SHORTEST PATHS, with the modification
that we replace “∞” by “-∞” and “>” by “<”.