Shortest Paths Review Single Source Shortest Path Dijkstras

  • Slides: 44
Download presentation
Shortest Paths (Review)

Shortest Paths (Review)

Single Source Shortest Path • Dijkstra’s Algorithm – Label Path length to each vertex

Single Source Shortest Path • Dijkstra’s Algorithm – Label Path length to each vertex as (or 0 for the start vertex) – Loop while there is a vertex • Remove up the vertex with minimum path length • Check and if required update the path length of its adjacent neighbors Update rule: Let ‘a’ be the vertex removed and ‘b’ be its adjacent vertex Let ‘e’ be the edge connecting a to b. if (a. path. Length + e. weight < b. path. Length) b. path. Length a. Path. Length + e. weight b. parent a – end loop 6 C 6 B E 5 6 A B E 2 2 D 5 1 6 8 7 2 6 C 10 1 6 8 D 6 7 5 A

Single Source Shortest Path • DAG based Algorithm May have negative weight – Label

Single Source Shortest Path • DAG based Algorithm May have negative weight – Label Path length to each vertex as (or 0 for the start vertex) – Compute Topological Ordering – loop for i 1 to n-1 • Check and if required update the path length of adjacent neighbors of vi – end loop -2 C 6 B D 3 -5 8 Path Length from A i=1 To E 5 9 2 A B C D E i=2 i=3 i=4

All-Pairs Shortest Paths i Uses only vertices numbered 1, …, k (compute weight of

All-Pairs Shortest Paths i Uses only vertices numbered 1, …, k (compute weight of this edge) Uses only vertices numbered 1, …, k-1 j k Uses only vertices numbered 1, …, k-1

All-Pairs Shortest Paths Dynamic Programming approach • • Initialize a n n table with

All-Pairs Shortest Paths Dynamic Programming approach • • Initialize a n n table with 0 or edge length. Check and Update the table bottom up in nested loops for k, for i, for j -2 C 6 B 3 -5 8 D E 5 9 2 A d j v 1 v 2 v 3 v 4 v 5 i v 1 v 2 v 3 v 4 v 5

Minimum Spanning Trees 2704 BOS 867 849 PVD ORD 740 621 1846 LAX 1391

Minimum Spanning Trees 2704 BOS 867 849 PVD ORD 740 621 1846 LAX 1391 1464 1235 144 JFK 1258 184 802 SFO 337 187 BWI 1090 DFW 946 1121 MCO 2342

Minimum Spanning Tree Spanning subgraph ORD – Subgraph of a graph G containing all

Minimum Spanning Tree Spanning subgraph ORD – Subgraph of a graph G containing all the vertices of G 1 Spanning tree – Spanning subgraph that is itself a (free) tree DEN Minimum spanning tree (MST) – Spanning tree of a weighted graph with minimum total edge weight 10 PIT 9 6 STL 4 8 7 3 DCA 5 2 • Applications – Communications networks – Transportation networks DFW ATL

Cycle Property f Cycle Property: – Let T be a minimum spanning tree of

Cycle Property f Cycle Property: – Let T be a minimum spanning tree of a weighted graph G – Let e be an edge of G that is not in T – Let C let be the cycle formed by e with T – For every edge f of C, weight(f) weight(e) Proof: – By contradiction – If weight(f) > weight(e) we can get a spanning tree of smaller weight by replacing e with f 2 8 4 C 6 9 3 e 8 7 7 Replacing f with e yields a better spanning tree f 2 6 8 4 C 9 3 8 7 e 7

Partition Property U f Partition Property: – Consider a partition of the vertices of

Partition Property U f Partition Property: – Consider a partition of the vertices of G into subsets U and V – Let e be an edge of minimum weight across the partition – There is a minimum spanning tree of G containing edge e Proof: – Let T be an MST of G – If T does not contain e, consider the cycle C formed by e with T and let f be an edge of C across the partition – By the cycle property, weight(f) weight(e) – Thus, weight(f) = weight(e) – We obtain another MST by replacing f with e V 7 4 9 5 2 8 8 3 e 7 Replacing f with e yields another MST U 2 f V 7 4 9 5 8 8 e 7 3

Minimum Spanning Tree • Prim’s Algorithm (Prim-Jarnik Algorithm) – Label cost of each vertex

Minimum Spanning Tree • Prim’s Algorithm (Prim-Jarnik Algorithm) – Label cost of each vertex as (or 0 for the start vertex) – Loop while there is a vertex • Remove a vertex that will extend the tree with minimum additional cost • Check and if required update the path length of its adjacent neighbors (Update rule different from Dijkstra’s algorithm) C – end loop 6 6 B 1 6 8 D E 2 7 2 A Update rule: Let ‘a’ be the vertex removed and ‘b’ be its adjacent vertex Let ‘e’ be the edge connecting a to b. if (e. weight < b. cost) b. cost e. weight b. parent a

MST: Prim-Jarnik’s Algorithm 6 C 6 B 1 6 8 D E 2 2

MST: Prim-Jarnik’s Algorithm 6 C 6 B 1 6 8 D E 2 2 7 A 0

MST: Prim-Jarnik’s Algorithm 6 C 8 6 B 2, A 1 6 D 2,

MST: Prim-Jarnik’s Algorithm 6 C 8 6 B 2, A 1 6 D 2, A E 7, A 7 2 2 A 0

MST: Prim-Jarnik’s Algorithm 6 C 8 6 B 2, A 1 6 D 2,

MST: Prim-Jarnik’s Algorithm 6 C 8 6 B 2, A 1 6 D 2, A E 7, A 7 2 2 A 0

MST: Prim-Jarnik’s Algorithm 8, B 6 C 8 6 B 2, A 1 6

MST: Prim-Jarnik’s Algorithm 8, B 6 C 8 6 B 2, A 1 6 D 2, A E 6, B 7 2 2 A 0

MST: Prim-Jarnik’s Algorithm 8, B 6 C 8 6 B 2, A 1 6

MST: Prim-Jarnik’s Algorithm 8, B 6 C 8 6 B 2, A 1 6 D 2, A E 6, B 7 2 2 A 0

MST: Prim-Jarnik’s Algorithm 6, D 6 C 8 6 B 2, A 1 6

MST: Prim-Jarnik’s Algorithm 6, D 6 C 8 6 B 2, A 1 6 D 2, A E 1, D 7 2 2 A 0

MST: Prim-Jarnik’s Algorithm 6, D 6 C 8 6 B 2, A 1 6

MST: Prim-Jarnik’s Algorithm 6, D 6 C 8 6 B 2, A 1 6 D 2, A E 1, D 7 2 2 A 0

MST: Prim-Jarnik’s Algorithm 6, D 6 C 8 6 B 2, A 1 6

MST: Prim-Jarnik’s Algorithm 6, D 6 C 8 6 B 2, A 1 6 D 2, A E 1, D 7 2 2 A 0

MST: Prim-Jarnik’s Algorithm 6, D 6 C 8 6 B 2, A 1 6

MST: Prim-Jarnik’s Algorithm 6, D 6 C 8 6 B 2, A 1 6 D 2, A E 1, D 7 2 2 A 0

Prim-Jarnik Algorithm MST (G) Q new priority queue Let s be any vertex for

Prim-Jarnik Algorithm MST (G) Q new priority queue Let s be any vertex for all v G. vertices() if v = s v. cost 0 else v. cost v. parent null Q. en. Queue(v. cost, v) while Q. is. Empty() v Q. remove. Min() v. path. Known true for all e G. incident. Edges(v) w opposite(v, e) if w. path. Known if weight(e) < w. cost weight(e) w. parent v update key of w in Q O(n) O(n log n) O((n+m) log n)

Example 2 7 B 0 2 B 5 C 0 2 0 A 4

Example 2 7 B 0 2 B 5 C 0 2 0 A 4 9 5 C 5 F 8 8 7 E 7 7 2 4 F 8 7 B 7 D 7 3 9 8 A D 7 5 F E 7 2 2 4 8 8 A 9 8 C 5 2 D E 2 3 7 7 B 0 3 7 7 4 9 5 C 5 F 8 8 A D 7 E 3 7 4

Example (contd. ) 2 2 B 0 4 9 5 C 5 F 8

Example (contd. ) 2 2 B 0 4 9 5 C 5 F 8 8 A D 7 7 7 E 4 3 3 2 2 B 0 4 9 5 C 5 F 8 8 A D 7 7 7 E 3 3 4

Minimum Spanning Tree • Kruskal’s Algorithm – Create a forest of n trees –

Minimum Spanning Tree • Kruskal’s Algorithm – Create a forest of n trees – Loop while (there is > 1 tree in the forest) • Remove an edge with minimum weight • Accept the edge only if it connects 2 trees from the forest in to one. – end loop 6 C 6 B 1 6 8 D E 2 7 2 E A D C B A

MST: Kruskal’s Algorithm 6 C 6 B 1 6 8 D E 2 7

MST: Kruskal’s Algorithm 6 C 6 B 1 6 8 D E 2 7 2 A E D C B A

MST: Kruskal’s Algorithm 6 C 6 B 1 6 8 D E 2 7

MST: Kruskal’s Algorithm 6 C 6 B 1 6 8 D E 2 7 2 A E D C B A

MST: Kruskal’s Algorithm 6 C 6 B 1 6 8 D E 2 7

MST: Kruskal’s Algorithm 6 C 6 B 1 6 8 D E 2 7 2 A E D C B A

MST: Kruskal’s Algorithm 6 C 6 B 1 6 8 D E 2 7

MST: Kruskal’s Algorithm 6 C 6 B 1 6 8 D E 2 7 2 A E D C B A

Kruskal’s Algorithm Kruskal. MST(G) let Q be a priority queue. Insert all edges into

Kruskal’s Algorithm Kruskal. MST(G) let Q be a priority queue. Insert all edges into Q using their weights as the key Create a forest of n trees where each vertex is a tree number. Of. Trees n while number. Of. Trees > 1 do edge e Q. remove. Min() Let u, v be the endpoints of e if Tree(v) Tree(u) then Combine Tree(v) and Tree(u) using edge e decrement number. Of. Trees return T O(m log m)

Kruskal Example 2704 BOS 867 849 ORD 740 621 1846 337 LAX 1391 1464

Kruskal Example 2704 BOS 867 849 ORD 740 621 1846 337 LAX 1391 1464 1235 187 144 JFK 1258 184 802 SFO PVD BWI 1090 DFW 946 1121 MIA 2342

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example

Example 2704 BOS 867 849 ORD 740 621 1846 337 LAX 1391 1464 1235

Example 2704 BOS 867 849 ORD 740 621 1846 337 LAX 1391 1464 1235 187 144 JFK 1258 184 802 SFO PVD BWI 1090 946 DFW 1121 MIA 2342

Minimum Spanning Tree • Baruvka’s Algorithm – Create a forest of n trees –

Minimum Spanning Tree • Baruvka’s Algorithm – Create a forest of n trees – Loop while (there is > 1 tree in the forest) • For each tree Ti in the forest – Find the smallest edge e = (u, v), in the edge list with u in Ti and v in Tj Ti – connects 2 trees from the forest in to one. – end loop 6 C 6 B 1 6 8 D E 5 7 2 E A D C B A

Baruvka’s Algorithm • Like Kruskal’s Algorithm, Baruvka’s algorithm grows many “clouds” at once. Algorithm

Baruvka’s Algorithm • Like Kruskal’s Algorithm, Baruvka’s algorithm grows many “clouds” at once. Algorithm Baruvka. MST(G) T V {just the vertices of G} while T has fewer than n-1 edges do for each connected component C in T do Let edge e be the smallest-weight edge from C to another component in T. if e is not already in T then Add edge e to T return T • Each iteration of the while-loop halves the number of connected compontents in T. – The running time is O(m log n).