Download presentation
Presentation is loading. Please wait.
1
Graph Algorithms Shortest path problems [Adapted from K.Wayne]
2
Graph Algorithms Shortest path problems [Adapted from K.Wayne]
3
Graph Algorithms Shortest path problems [Adapted from K.Wayne]
4
Graph Algorithms 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 (V+E) time.
5
Graph Algorithms Dijkstra’s Algorithm - Relax Relax(vertex u, vertex v, weight w) if d[v] > d[u] + w(u,v) then d[v] d[u] + w(u,v) p[v] u [Adapted from K.Wayne]
6
Graph Algorithms Dijkstra’s Algorithm - Idea [Adapted from K.Wayne]
7
Graph Algorithms Dijkstra’s Algorithm - SSSP-Dijkstra SSSP-Dijkstra(graph (G,w), vertex s) InitializeSingleSource(G, s) S Q V[G] while Q 0 do u ExtractMin(Q) S S {u} for v Adj[u] do Relax(u,v,w) InitializeSingleSource(graph G, vertex s) for v V[G] do d[v] p[v] 0 d[s] 0
8
Graph Algorithms Dijkstra’s Algorithm - Example 10 1 5 2 6 4 9 7 2 3
9
Graph Algorithms Dijkstra’s Algorithm - Example 0 10 1 5 2 6 4 9 7 2 3
10
Graph Algorithms Dijkstra’s Algorithm - Example 0 5 10 1 5 2 6 4 9 7 2 3
11
Graph Algorithms Dijkstra’s Algorithm - Example 0 5 10 1 5 2 6 4 9 7 2 3
12
Graph Algorithms Dijkstra’s Algorithm - Example 0 5 8 7 14 10 1 5 2 6 4 9 7 2 3
13
Graph Algorithms Dijkstra’s Algorithm - Example 0 5 8 7 14 10 1 5 2 6 4 9 7 2 3
14
Graph Algorithms Dijkstra’s Algorithm - Example 0 5 8 7 13 10 1 5 2 6 4 9 7 2 3
15
Graph Algorithms Dijkstra’s Algorithm - Example 0 5 8 7 13 10 1 5 2 6 4 9 7 2 3
16
Graph Algorithms Dijkstra’s Algorithm - Example 0 5 8 7 9 10 1 5 2 6 4 9 7 2 3
17
Graph Algorithms Dijkstra’s Algorithm - Example 0 5 8 7 9 10 1 5 2 6 4 9 7 2 3
18
Graph Algorithms Dijkstra’s Algorithm - Complexity SSSP-Dijkstra(graph (G,w), vertex s) InitializeSingleSource(G, s) S Q V[G] while Q 0 do u ExtractMin(Q) S S {u} for u Adj[u] do Relax(u,v,w) InitializeSingleSource(graph G, vertex s) for v V[G] do d[v] p[v] 0 d[s] 0 Relax(vertex u, vertex v, weight w) if d[v] > d[u] + w(u,v) then d[v] d[u] + w(u,v) p[v] u (V) (1) ? (E) times in total executed (V) times
19
Graph Algorithms Dijkstra’s Algorithm - Complexity InitializeSingleSource T I (V,E) = (V) Relax T R (V,E) = (1)? SSSP-Dijkstra T(V,E) = T I (V,E) + (V) + V (log V) + E T R (V,E) = = (V) + (V) + V (log V) + E (1) = (E + V log V)
20
Graph Algorithms Dijkstra’s Algorithm - Complexity [Adapted from K.Wayne]
21
Graph Algorithms Dijkstra’s Algorithm - Correctness [Adapted from K.Wayne]
22
Graph Algorithms Dijkstra’s Algorithm - negative weights? [Adapted from K.Wayne]
23
Graph Algorithms Bellman-Ford Algorithm - negative cycles? [Adapted from K.Wayne]
24
Graph Algorithms Bellman-Ford Algorithm - Idea [Adapted from X.Wang]
25
Graph Algorithms Bellman-Ford Algorithm - SSSP-BellmanFord SSSP-BellmanFord(graph (G,w), vertex s) InitializeSingleSource(G, s) for i 1 to |V[G] 1| do for (u,v) E[G] do Relax(u,v,w) for (u,v) E[G] do if d[v] > d[u] + w(u,v) then return false return true
26
Graph Algorithms Bellman-Ford Algorithm - Example 6 7 7 -3 2 8 -4 9 5 -2
27
Graph Algorithms Bellman-Ford Algorithm - Example 0 6 7 7 -3 2 8 -4 9 5 -2
28
Graph Algorithms Bellman-Ford Algorithm - Example 0 7 6 6 7 7 -3 2 8 -4 9 5 -2
29
Graph Algorithms Bellman-Ford Algorithm - Example 0 7 6 2 4 6 7 7 -3 2 8 -4 9 5 -2
30
Graph Algorithms Bellman-Ford Algorithm - Example 0 7 2 2 4 6 5 7 7 -3 2 8 -2 -4 9
31
Graph Algorithms Bellman-Ford Algorithm - Example 0 7 2 -2 4 6 5 7 7 -3 2 8 -2 -4 9
32
Graph Algorithms Bellman-Ford Algorithm - Complexity SSSP-BellmanFord(graph (G,w), vertex s) InitializeSingleSource(G, s) for i 1 to |V[G] 1| do for (u,v) E[G] do Relax(u,v,w) for (u,v) E[G] do if d[v] > d[u] + w(u,v) then return false return true executed (V) times (E) (1)
33
Graph Algorithms Bellman-Ford Algorithm - Complexity InitializeSingleSource T I (V,E) = (V) Relax T R (V,E) = (1)? SSSP-BellmanFord T(V,E) = T I (V,E) + V E T R (V,E) + E = = (V) + V E (1) + E = = (V E)
34
Graph Algorithms Bellman-Ford Algorithm - Correctness [Adapted from T.Cormen, C.Leiserson, R. Rivest]
35
Graph Algorithms Bellman-Ford Algorithm - Correctness [Adapted from T.Cormen, C.Leiserson, R. Rivest]
36
Graph Algorithms Bellman-Ford Algorithm - Correctness [Adapted from T.Cormen, C.Leiserson, R. Rivest]
37
Graph Algorithms Bellman-Ford Algorithm - Correctness [Adapted from T.Cormen, C.Leiserson, R. Rivest]
38
Graph Algorithms 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 Adj[u] do Relax(u,v,w)
39
Graph Algorithms Shortest Paths in DAGs - Example 527-2 61 3 4 2
40
Graph Algorithms Shortest Paths in DAGs - Example 0 527-2 61 3 4 2
41
Graph Algorithms Shortest Paths in DAGs - Example 0 527-2 61 3 4 2
42
Graph Algorithms Shortest Paths in DAGs - Example 0 527-2 61 3 4 2
43
Graph Algorithms Shortest Paths in DAGs - Example 6 02 527-2 61 3 4 2
44
Graph Algorithms Shortest Paths in DAGs - Example 664 02 527-2 61 3 4 2
45
Graph Algorithms Shortest Paths in DAGs - Example 664 02 527-2 61 3 4 2
46
Graph Algorithms Shortest Paths in DAGs - Example 654 02 527-2 61 3 4 2
47
Graph Algorithms Shortest Paths in DAGs - Example 654 02 527-2 61 3 4 2
48
Graph Algorithms Shortest Paths in DAGs - Example 653 02 527-2 61 3 4 2
49
Graph Algorithms Shortest Paths in DAGs - Example 653 02 527-2 61 3 4 2
50
Graph Algorithms Shortest Paths in DAGs - Complexity T(V,E) = (V + E) + (V) + (V) + E (1) = (V + E) 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 Adj[u] do Relax(u,v,w)
51
Graph Algorithms Application of SSSP - currency conversion [Adapted from K.Wayne]
52
Graph Algorithms Application of SSSP - currency conversion [Adapted from K.Wayne]
53
Graph Algorithms Application of SSSP - currency conversion [Adapted from K.Wayne]
54
Graph Algorithms Application of SSSP - constraint satisfaction [Adapted from T.Cormen, C.Leiserson, R. Rivest]
55
Graph Algorithms Application of SSSP - constraint satisfaction [Adapted from T.Cormen, C.Leiserson, R. Rivest]
56
Graph Algorithms Application of SSSP - constraint satisfaction [Adapted from T.Cormen, C.Leiserson, R. Rivest]
57
Graph Algorithms Application of SSSP - constraint satisfaction [Adapted from T.Cormen, C.Leiserson, R. Rivest]
58
Graph Algorithms Application of SSSP - constraint satisfaction [Adapted from T.Cormen, C.Leiserson, R. Rivest]
59
Graph Algorithms Application of SSSP - constraint satisfaction [Adapted from T.Cormen, C.Leiserson, R. Rivest]
60
Graph Algorithms 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
Graph Algorithms Floyd-Warshall Algorithm - Idea [Adapted from M.Jacome]
62
Graph Algorithms Floyd-Warshall Algorithm - Idea [Adapted from M.Jacome]
63
Graph Algorithms Floyd-Warshall Algorithm - Idea d s,t (i) – the shortest path from s to t containing only vertices v 1,..., v i d s,t (0) = w(s,t) d s,t (k) = w(s,t)if k = 0 min{d s,t (k-1), d s,k (k-1) + d k,t (k-1) }if k > 0
64
Graph Algorithms 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 d ij (k) min(d ij (k-1), d ik (k-1) + d kj (k-1) ) return D (n)
65
Graph Algorithms Floyd-Warshall Algorithm - Example 2 45 13 3 4 -4 -5 6 7 1 8 2 038 -4 0 17 40 2 -50 60 W
66
Graph Algorithms Floyd-Warshall Algorithm - Example 038 -4 0 17 40 2 -50 60 0000 000 00 000 00 D (0) (0)
67
Graph Algorithms Floyd-Warshall Algorithm - Example 038 -4 0 17 40 25-50-2 60 0000 000 00 01001 00 D (1) (1)
68
Graph Algorithms Floyd-Warshall Algorithm - Example 0384-4 0 17 40511 25-50-2 60 00020 000 0022 01001 00 D (2) (2)
69
Graph Algorithms Floyd-Warshall Algorithm - Example 0384-4 0 17 40511 2-50-2 60 00020 000 0022 03001 00 D (3) (3)
70
Graph Algorithms Floyd-Warshall Algorithm - Example 034-4 30 1 74053 2 -50-2 85160 00420 40401 40021 03001 43400 D (4) (4)
71
Graph Algorithms Floyd-Warshall Algorithm - Example 032-4 30 1 74053 2 -50-2 85160 00450 40401 40021 03001 43400 D (5) (5)
72
Graph Algorithms Floyd-Warshall Algorithm - Extracting the shortest paths [Adapted from S.Cheng]
73
Graph Algorithms Floyd-Warshall Algorithm - Complexity T(V,E) = (n 3 ) = (V 3 ) FloydWarshall(matrix W, integer n) for k 1 to n do for i 1 to n do for j 1 to n do d ij (k) min(d ij (k-1), d ik (k-1) + d kj (k-1) ) return D (n) 3 for cycles, each executed exactly n times
74
Graph Algorithms All-Pairs Shortest Paths -Johnson's algorithm [Adapted from M.Jacome]
75
Graph Algorithms All-Pairs Shortest Paths - Reweighting [Adapted from M.Jacome]
76
Graph Algorithms All-Pairs Shortest Paths - Reweighting [Adapted from M.Jacome]
77
Graph Algorithms All-Pairs Shortest Paths - Reweighting [Adapted from M.Jacome]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.