Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source...
Transcript of Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source...
![Page 1: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/1.jpg)
Single-Source Shortest Paths
Hosung Jo
Division of Computer and Engineering
Hanyang University
![Page 2: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/2.jpg)
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
![Page 3: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/3.jpg)
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)
![Page 4: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/4.jpg)
Definition
Shortest-path problems
� Single-source & single-destination
� Single-source
4
� Single-destination
� All pairs
![Page 5: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/5.jpg)
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
![Page 6: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/6.jpg)
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
![Page 7: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/7.jpg)
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
![Page 8: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/8.jpg)
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
![Page 9: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/9.jpg)
Cycles
Cycles
� A shortest path does not include cycles.
� A shortest-path length is at most |V|-1.
9
![Page 10: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/10.jpg)
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
![Page 11: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/11.jpg)
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
![Page 12: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/12.jpg)
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
![Page 13: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/13.jpg)
Dijkstra's algorithm
Dijkstra's algorithm
� It works properly when all edge weights are nonnegative.
13
![Page 14: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/14.jpg)
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
![Page 15: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/15.jpg)
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
![Page 16: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/16.jpg)
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}
![Page 17: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/17.jpg)
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
![Page 18: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/18.jpg)
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
![Page 19: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/19.jpg)
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
![Page 20: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/20.jpg)
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
![Page 21: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/21.jpg)
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
![Page 22: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/22.jpg)
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
![Page 23: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/23.jpg)
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.
![Page 24: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/24.jpg)
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
![Page 25: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/25.jpg)
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
∈
∈
![Page 26: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/26.jpg)
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
![Page 27: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/27.jpg)
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
![Page 28: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/28.jpg)
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
![Page 29: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/29.jpg)
The Bellman-Ford algorithm
The Bellman-Ford algorithm
� Running time : O(VE)
29
![Page 30: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/30.jpg)
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
![Page 31: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/31.jpg)
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)
∈
![Page 32: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/32.jpg)
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
![Page 33: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/33.jpg)
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
![Page 34: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/34.jpg)
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
![Page 35: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/35.jpg)
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
![Page 36: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/36.jpg)
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.
![Page 37: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/37.jpg)
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.
![Page 38: Single-Source Shortest Pathskowon.dongseo.ac.kr/~lbg/web_lecture/algorithm/... · Single-source shortest paths can be defined if there are not any negative-weight cyclesreachable](https://reader033.fdocuments.in/reader033/viewer/2022060518/604b12137ea78a6cc36971fc/html5/thumbnails/38.jpg)
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 “<”.