Shortest path problems - PowerPoint PPT Presentation

1 / 77
About This Presentation
Title:

Shortest path problems

Description:

Title: PowerPoint Presentation Author: Juris Last modified by: katia Created Date: 10/16/2000 4:49:17 PM Document presentation format: Apresenta o na tela – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 78
Provided by: Jur130
Category:
Tags: ford | path | problems | shortest

less

Transcript and Presenter's Notes

Title: Shortest path problems


1
Shortest path problems
Adapted from K.Wayne
2
Shortest path problems
Adapted from K.Wayne
3
Shortest path problems
Adapted from K.Wayne
4
Single-Source Shortest Paths
Given graph (directed or undirected) G (V,E)
with weight function w E ? R and a vertex s?V,
find for all vertices v?V the minimum possible
weight for path from s to v.
  • We will discuss two general case algorithms
  • Dijkstra's (positive edge weights only)
  • Bellman-Ford (positive end negative edge weights)

If all edge weights are equal (let's say 1), the
problem is solved by BFS in ?(VE) time.
5
Dijkstras Algorithm - Relax
Relax(vertex u, vertex v, weight w) if dv gt
du w(u,v) then dv ? du w(u,v) pv
? u
Adapted from K.Wayne
6
Dijkstras Algorithm - Idea
Adapted from K.Wayne
7
Dijkstras Algorithm - SSSP-Dijkstra
SSSP-Dijkstra(graph (G,w), vertex
s) InitializeSingleSource(G, s) S ? ? Q ?
VG while Q ? 0 do u ? ExtractMin(Q) S ? S
? u for v ? Adju do Relax(u,v,w)
InitializeSingleSource(graph G, vertex s) for v
? VG do dv ? ? pv ? 0 ds ? 0
8
Dijkstras Algorithm - Example
1
10
9
2
3
4
6
7
5
2
9
Dijkstras Algorithm - Example
1
?
?
10
9
2
3
0
4
6
7
5
?
?
2
10
Dijkstras Algorithm - Example
1
10
?
10
9
2
3
0
4
6
7
5
5
?
2
11
Dijkstras Algorithm - Example
1
10
?
10
9
2
3
0
4
6
7
5
5
?
2
12
Dijkstras Algorithm - Example
1
8
14
10
9
2
3
0
4
6
7
5
5
7
2
13
Dijkstras Algorithm - Example
1
8
14
10
9
2
3
0
4
6
7
5
5
7
2
14
Dijkstras Algorithm - Example
1
8
13
10
9
2
3
0
4
6
7
5
5
7
2
15
Dijkstras Algorithm - Example
1
8
13
10
9
2
3
0
4
6
7
5
5
7
2
16
Dijkstras Algorithm - Example
1
8
9
10
9
2
3
0
4
6
7
5
5
7
2
17
Dijkstras Algorithm - Example
1
8
9
10
9
2
3
0
4
6
7
5
5
7
2
18
Dijkstras Algorithm - Complexity
SSSP-Dijkstra(graph (G,w), vertex
s) InitializeSingleSource(G, s) S ? ? Q ?
VG while Q ? 0 do u ? ExtractMin(Q) S ? S
? u for u ? Adju do Relax(u,v,w)
executed ?(V) times
?(E) times in total
InitializeSingleSource(graph G, vertex s) for v
? VG do dv ? ? pv ? 0 ds ? 0
?(V)
Relax(vertex u, vertex v, weight w) if dv gt
du w(u,v) then dv ? du w(u,v) pv
? u
?(1) ?
19
Dijkstras Algorithm - Complexity
InitializeSingleSource TI(V,E) ?(V) Relax
TR(V,E) ?(1)?
SSSP-Dijkstra T(V,E) TI(V,E) ?(V) V
?(log V) E TR(V,E) ?(V) ?(V) V ?(log
V) E ?(1) ?(E V log V)
20
Dijkstras Algorithm - Complexity
Adapted from K.Wayne
21
Dijkstras Algorithm - Correctness
Adapted from K.Wayne
22
Dijkstras Algorithm - negative weights?
Adapted from K.Wayne
23
Bellman-Ford Algorithm - negative cycles?
Adapted from K.Wayne
24
Bellman-Ford Algorithm - Idea
Adapted from X.Wang
25
Bellman-Ford Algorithm - SSSP-BellmanFord
SSSP-BellmanFord(graph (G,w), vertex
s) InitializeSingleSource(G, s) for i ? 1 to
VG ? 1 do for (u,v) ? EG
do Relax(u,v,w) for (u,v) ? EG do if
dv gt du w(u,v) then return false return
true
26
Bellman-Ford Algorithm - Example
-2
5
6
-3
8
7
-4
2
7
9
27
Bellman-Ford Algorithm - Example
-2
?
?
5
6
-3
8
0
7
-4
2
7
?
?
9
28
Bellman-Ford Algorithm - Example
-2
6
?
5
6
-3
8
0
7
-4
2
7
7
?
9
29
Bellman-Ford Algorithm - Example
-2
6
4
5
6
-3
8
0
7
-4
2
7
7
2
9
30
Bellman-Ford Algorithm - Example
-2
2
4
5
6
-3
8
0
7
-4
2
7
7
2
9
31
Bellman-Ford Algorithm - Example
-2
2
4
5
6
-3
8
0
7
-4
2
7
7
-2
9
32
Bellman-Ford Algorithm - Complexity
SSSP-BellmanFord(graph (G,w), vertex
s) InitializeSingleSource(G, s) for i ? 1 to
VG ? 1 do for (u,v) ? EG
do Relax(u,v,w) for (u,v) ? EG do if
dv gt du w(u,v) then return false return
true
executed ?(V) times
?(E)
?(1)
?(E)
33
Bellman-Ford Algorithm - Complexity
InitializeSingleSource TI(V,E) ?(V) Relax
TR(V,E) ?(1)?
SSSP-BellmanFord T(V,E) TI(V,E) V E
TR(V,E) E ?(V) V E ?(1) E ?(V E)
34
Bellman-Ford Algorithm - Correctness
Adapted from T.Cormen, C.Leiserson, R. Rivest
35
Bellman-Ford Algorithm - Correctness
Adapted from T.Cormen, C.Leiserson, R. Rivest
36
Bellman-Ford Algorithm - Correctness
Adapted from T.Cormen, C.Leiserson, R. Rivest
37
Bellman-Ford Algorithm - Correctness
Adapted from T.Cormen, C.Leiserson, R. Rivest
38
Shortest Paths in DAGs - SSSP-DAG
SSSP-DAG(graph (G,w), vertex s) topologically
sort vertices of G InitializeSingleSource(G,
s) for each vertex u taken in topologically
sorted order do for each vertex v ? Adju
do Relax(u,v,w)
39
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
4
3
2
40
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
?
?
?
?
0
?
4
3
2
41
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
?
?
?
?
0
?
4
3
2
42
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
?
?
?
?
0
?
4
3
2
43
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
6
?
?
?
0
2
4
3
2
44
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
6
6
4
?
0
2
4
3
2
45
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
6
6
4
?
0
2
4
3
2
46
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
6
5
4
?
0
2
4
3
2
47
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
6
5
4
?
0
2
4
3
2
48
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
6
5
3
?
0
2
4
3
2
49
Shortest Paths in DAGs - Example
6
1
5
2
7
-1
-2
6
5
3
?
0
2
4
3
2
50
Shortest Paths in DAGs - Complexity
SSSP-DAG(graph (G,w), vertex s) topologically
sort vertices of G InitializeSingleSource(G,
s) for each vertex u taken in topologically
sorted order do for each vertex v ? Adju
do Relax(u,v,w)
T(V,E) ?(V E) ?(V) ?(V) E ?(1) ?(V
E)
51
Application of SSSP - currency conversion
Adapted from K.Wayne
52
Application of SSSP - currency conversion
Adapted from K.Wayne
53
Application of SSSP - currency conversion
Adapted from K.Wayne
54
Application of SSSP - constraint satisfaction
Adapted from T.Cormen, C.Leiserson, R. Rivest
55
Application of SSSP - constraint satisfaction
Adapted from T.Cormen, C.Leiserson, R. Rivest
56
Application of SSSP - constraint satisfaction
Adapted from T.Cormen, C.Leiserson, R. Rivest
57
Application of SSSP - constraint satisfaction
Adapted from T.Cormen, C.Leiserson, R. Rivest
58
Application of SSSP - constraint satisfaction
Adapted from T.Cormen, C.Leiserson, R. Rivest
59
Application of SSSP - constraint satisfaction
Adapted from T.Cormen, C.Leiserson, R. Rivest
60
All-Pairs Shortest Paths
Given graph (directed or undirected) G (V,E)
with weight function w E ? R find for all pairs
of vertices u,v ? V the minimum possible weight
for path from u to v.
Adapted from M.Jacome
61
Floyd-Warshall Algorithm - Idea
Adapted from M.Jacome
62
Floyd-Warshall Algorithm - Idea
Adapted from M.Jacome
63
Floyd-Warshall Algorithm - Idea
ds,t(i) the shortest path from s to t
containing only vertices v1, ..., vi
ds,t(0) w(s,t)
w(s,t) if k 0 minds,t(k-1), ds,k(k-1)
dk,t(k-1) if k gt 0
ds,t(k)
64
Floyd-Warshall Algorithm - Algorithm
FloydWarshall(matrix W, integer n) for k ? 1 to
n do for i ? 1 to n do for j ? 1 to n
do dij(k) ? min(dij(k-1), dik(k-1)
dkj(k-1)) return D(n)
65
Floyd-Warshall Algorithm - Example
W
2
4
3
7
0 3 8 ? -4
? 0 ? 1 7
? 4 0 ? ?
2 ? -5 0 ?
? ? ? 6 0
1
1
3
8
2
-4
-5
4
5
6
66
Floyd-Warshall Algorithm - Example
D(0) ?(0)
0 3 8 ? -4
? 0 ? 1 7
? 4 0 ? ?
2 ? -5 0 ?
? ? ? 6 0
0 0 0 0
0 0 0
0 0
0 0 0
0 0
67
Floyd-Warshall Algorithm - Example
D(1) ?(1)
0 3 8 ? -4
? 0 ? 1 7
? 4 0 ? ?
2 5 -5 0 -2
? ? ? 6 0
0 0 0 0
0 0 0
0 0
0 1 0 0 1
0 0
68
Floyd-Warshall Algorithm - Example
D(2) ?(2)
0 3 8 4 -4
? 0 ? 1 7
? 4 0 5 11
2 5 -5 0 -2
? ? ? 6 0
0 0 0 2 0
0 0 0
0 0 2 2
0 1 0 0 1
0 0
69
Floyd-Warshall Algorithm - Example
D(3) ?(3)
0 3 8 4 -4
? 0 ? 1 7
? 4 0 5 11
2 -1 -5 0 -2
? ? ? 6 0
0 0 0 2 0
0 0 0
0 0 2 2
0 3 0 0 1
0 0
70
Floyd-Warshall Algorithm - Example
D(4) ?(4)
0 3 -1 4 -4
3 0 -4 1 -1
7 4 0 5 3
2 -1 -5 0 -2
8 5 1 6 0
0 0 4 2 0
4 0 4 0 1
4 0 0 2 1
0 3 0 0 1
4 3 4 0 0
71
Floyd-Warshall Algorithm - Example
D(5) ?(5)
0 3 -1 2 -4
3 0 -4 1 -1
7 4 0 5 3
2 -1 -5 0 -2
8 5 1 6 0
0 0 4 5 0
4 0 4 0 1
4 0 0 2 1
0 3 0 0 1
4 3 4 0 0
72
Floyd-Warshall Algorithm - Extracting the
shortest paths
Adapted from S.Cheng
73
Floyd-Warshall Algorithm - Complexity
FloydWarshall(matrix W, integer n) for k ? 1 to
n do for i ? 1 to n do for j ? 1 to n
do dij(k) ? min(dij(k-1), dik(k-1)
dkj(k-1)) return D(n)
3 for cycles, each executed exactly n times
T(V,E) ?(n3) ?(V3)
74
All-Pairs Shortest Paths -Johnson's algorithm
Adapted from M.Jacome
75
All-Pairs Shortest Paths - Reweighting
Adapted from M.Jacome
76
All-Pairs Shortest Paths - Reweighting
Adapted from M.Jacome
77
All-Pairs Shortest Paths - Reweighting
Adapted from M.Jacome
Write a Comment
User Comments (0)
About PowerShow.com