Title: Single-Source Shortest Paths (25/24)
1Single-Source Shortest Paths (25/24)
- HW 25-2 and 25-3 p. 546/24-2 and 24-3 p.615
- Given a graph G(V,E) and w E ? ?
- weight of ltv1,...,vkgt is w(p) ?
w(vi,vi1) - Single-source shortest-paths
- find a shortest path from a source s to every
vertex v ?V - Single pair shortest path problem asks for a u-v
shortest path for some pair of vertices - All pairs shortest-paths problem will be next time
2 Shortest Paths (25/24)
- Predecessor subgraph for restoring shortest paths
- Shortest-paths tree rooted at source s
- Th Subpath of a shortest path is a shortest path
- Triangle Inequality
- ?(u,v) ? ?(u,x) ?(x,v)
- Well defined some paths may not exist if there
is a negative-weight cycle in graph
u
v
x
lt0
3 Bellman-Ford (25.3/24.1)
- Most basic (BFS based) algorithm, shortest paths
(tree) easy to reconstruct. - for each v ?V do dv ? ? ds ? 0
- Relaxation
- for i 1,...,V-1 do
- for each edge (u,v) ? E do
- dv ? mindv, duw(u,v)
- Negative cycle checking
- for each v ?V do if dvgt du w(u,v) then no
solution - Finally dv ?(s,v)
4 Bellman-Ford Analysis (25.3/24.1)
- Runtime O(VE)
- Correctness
- Lemma dv ? ?(s,v)
- Initially true
- Let dv du w(u,v)
- by triangle inequality for first violation
dv lt ?(s,v) ? ?(s,u)w(u,v) ?
d(u)w(u,v) - After V-1 passes all d values are ?s if there
are no negative cycles - s ? v1 ? v2 ? ... ? v (some shortest path)
- After i-th iteration ds,vi is correct and
final
5 Dag Shortest Paths (25.4/24.2)
- DAG shortest paths
- Bellman-Ford O(VE)
- Topological sort O(VE) (DFS)
- Will never relaxed edges out of vertex v until
have done all edges in v - Runtime O(VE)
- Application PERT (program evaluation and review
technique) - Critical path is the longest path through the dag
- Negating the edge weights and running dag
shortest paths algorithm
6 Dijkstras Shortest Paths (25.2/24.3)
- Better than BF since non-negative weights.
- Like BFS but uses priority queue.
- for each v ?V do dv ? ? ds ? 0
- S ? ? Q ? V
- While Q ? ? do
- u ? Extract-Min(Q)
- S ? S u
- for v adjacent to u do
- dv ? mindv, duw(u,v)
- (relaxation Decrease-Key)
7d
?
3
17
b
c
e
?
?
?
13
9
12
6
22
11
7
a
12
?
14
f
0
8
10
8
?
4
?
20
?
i
h
10
g
8
1
For each v ? V do dv lt- ? ds lt- 0S lt-
?, Q lt- V
?
j
8d
?
3
17
b
c
e
12
?
?
13
9
12
6
22
11
7
a
12
?
14
f
0
8
10
8
?
4
?
20
8
i
h
10
g
8
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
1
?
j
9d
?
3
17
b
c
e
12
?
?
13
9
12
6
22
11
7
a
12
?
14
f
0
8
10
8
?
4
28
20
8
i
h
10
g
8
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
1
?
j
10d
?
3
17
b
c
e
12
25
?
13
9
12
6
22
11
7
a
12
?
14
f
0
8
10
8
?
4
19
20
i
8
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
?
j
11d
?
3
17
b
c
e
12
25
?
13
9
12
6
22
11
7
a
12
27
14
f
0
8
10
8
23
4
19
20
8
i
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
29
j
12d
?
3
17
b
c
e
12
25
?
13
9
12
6
22
11
7
a
12
27
14
f
0
8
10
8
23
4
19
20
8
i
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
29
j
13d
28
3
17
b
c
e
12
25
34
13
9
12
6
22
11
7
a
12
27
14
f
0
8
10
8
23
4
19
20
8
i
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
29
j
14d
28
3
17
b
c
e
12
25
34
13
9
12
6
22
11
7
a
12
27
14
f
0
8
10
8
23
4
19
20
8
i
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
29
j
15d
28
3
17
b
c
e
12
25
34
13
9
12
6
22
11
7
a
12
27
14
f
0
8
10
8
23
4
19
20
8
i
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
29
j
16d
28
3
17
b
c
e
12
25
34
13
9
12
6
22
11
7
a
12
27
14
f
0
8
10
8
23
4
19
20
8
i
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
29
j
17d
28
3
17
b
c
e
12
25
34
13
9
12
6
22
11
7
a
12
27
14
f
0
8
10
8
23
4
19
20
8
i
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
29
j
18d
28
3
17
b
c
e
12
25
34
13
9
12
6
22
11
7
a
12
27
14
f
0
8
10
8
23
4
19
20
8
i
h
10
g
8
1
While Q ? ? do U Extract-Min(Q) S
S u for v adjacent to u do dv
mindv, duw(u,v)
29
j
19 Dijkstras Runtime (25.2/24.3)
- Extract-Min executed V times
- Decrease-Key executed E times
- Time V?T(Extract-Min) (finddelete O(log
V)) - E ?T(Decrease-Key) (deleteadd
O(log V)) - Binary Heap E ? log V (30
years ago) - Fibonacci Heap E V ? log V (10 years
ago) - Optimal time algorithm found 1/2 year ago. It
runs in time O(E) (Mikel Thorup)
20 Dijkstras Correctness (25.2/24.3)
- The same Lemma as for BF dv ? ?(s,v)
- Th Whenever u is added to S, du ?(s,u)
- Let u be first s.t. du gt ?(s,u)
- Let y be first ?V-S on actual
- shortest s-u path ? dy ?(s,y)
- For ys predecessor x, dx ?(s,x)
- when put x in S dy gets ?(s,y)
- du gt ?(s,u) ?(s,y) ?(y,u) dy ?(y,u) ?
dy
S
u
s
y
Q
x