Minimum Spanning Tree Today’s announcements: ◮ PA3 due 5 April 11:59p ◮ Final Exam, 12 April 7:00p, SRC A & B 5 Today’s Plan: 6 ◮ Prim’s algorithm 2 ◮ Dijkstra’s shortest path algorithm 7 2 Partition Property A cut is a set of edges whose removal disconnects G . Any edge that is a smallest weight edge in a cut is in some minimum spanning tree. 1 / 8
Prim’s Minimum Spanning Tree Algorithm (i.e., Breadth-first search with a priority queue) Suppose I have an MST for a connected subgraph of G . What edge (and vertex) should I add next? 15 c b 16 10 5 5 2 d 13 11 a e 17 12 12 2 16 8 f h 4 g 9 2 / 8
Prim’s Minimum Spanning Tree Algorithm (i.e., Breadth-first search with a priority queue) Prim(G) 8 c b for each vertex v 9 5 4 unmark v; d[v]=infinity 6 2 d 8 PQ.insert(v,d[v]) a e 3 6 while PQ is not empty 7 v = PQ.removeMin() f mark v // add v to MST v ’s priority is edge weight for each edge (v,w) to closest tree vertex from v if (w is unmarked && weight(v,w) < d[w]) PQ Adj. Matrix Adj. List d[w]=weight(v,w) Θ( n 2 ) Θ( n 2 ) array parent[w] = v Θ( n 2 + m log n ) heap Θ( m log n ) decreasePrior(w,d[w]) Which is best? 3 / 8
Dijkstra’s Single-Source Shortest Path (another breadth-first search with a priority queue) 3 10 A B C Assume edge weights are non-negative. -5 1 2 D E Dijkstra’s algorithm is a greedy algorithm (it makes the current best choice without considering future consequences). Intuition: Find shortest paths from source in order of length. ◮ Start at the source vertex (shortest path length = 0) ◮ The next shortest path extends some already discovered shortest path by one edge. Why? ◮ Find it (by considering all one-edge extensions) and repeat. 4 / 8
Dijkstra’s Algorithm Pseudocode 1. Unmark all vertices 2. Initialize the dist to each vertex to ∞ 3. Initialize the dist to the source to 0 4. While there are unmarked vertices left in the graph ◮ Select the unmarked vertex v with the lowest dist ◮ Mark v with SPlength dist ◮ For each edge ( v , w ) ◮ dist( w ) = min { dist( w ), dist( v ) + weight of ( v , w ) } 2 2 C B F 1 1 3 9 4 A 10 2 H 2 8 1 1 D E G 7 4 vertex A B C D E F G H dist SPlength 5 / 8
Correctness: The Cloud Proof u Q s P y cloud ◮ Assume Dijkstra’s algorithm finds the correct shortest path to the first k vertices it visits (the cloud ). ◮ But it fails on the ( k + 1)st vertex u . ◮ So there is some shorter path, P , from s to u . ◮ Path P must contain a first vertex y not in the cloud. ◮ But since the path, Q , to u is the shortest path out of the cloud, the path on P upto y must be at least as long as Q . ◮ Thus the whole path P is at least as long as Q . Contradiction (What did I use in that last step?) 6 / 8
Data Structures for Dijkstra’s Algorithm n times: Select the unmarked vertex with the lowest dist. removeMin m times: dist( w ) = min { dist( w ), dist( v ) + weight of ( v , w ) } decreasePrior Runtime: (adjacency matrix or adjacency list?) 7 / 8
Fibonacci Heaps ◮ Another implementation of a Priority Queue ◮ Amortized O (1) time for decreasePrior. ◮ O (log n ) time for removeMin Dijkstra’s uses n removeMins and m decreasePriors Runtime with Fibonacci heaps: 8 / 8
Recommend
More recommend