Prim’s Algorithm and an MST Speed-Up

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Single Source Shortest Paths
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Design and Analysis of Algorithms Minimum Spanning trees
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Minimum Spanning Trees
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
CS112A1 Spring 2008 Practice Final. ASYMPTOTIC NOTATION: a)Show that log(n) and ln(n) are the same in terms of Big-Theta notation b)Show that log(n+1)
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
Runtime O(VE), for +/- edges, Detects existence of neg. loops
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
F d a b c e g Prim’s Algorithm – an Example edge candidates choosen.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Minimum- Spanning Trees
Graphs Upon completion you will be able to:
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
Prims Algorithm for finding a minimum spanning tree
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Minimum Spanning Tree Graph Theory Basics - Anil Kishore.
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Introduction to Algorithms
Greedy function greedy { S <- S0 //Initialization
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Data Structures & Algorithms Graphs
Minimum-Cost Spanning Tree
Minimum Spanning Trees
CS200: Algorithm Analysis
Minimum Spanning Tree Neil Tang 3/25/2010
Connected Components Minimum Spanning Tree
Minimum Spanning Tree.
Minimum-Cost Spanning Tree
Greedy Algorithm (17.4/16.4) Greedy Algorithm (GA)
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
MA/CSSE 473 Day 33 Student Questions Change to HW 13
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Tree Neil Tang 4/3/2008
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Introduction to Algorithms: Greedy Algorithms (and Graphs)
Implementation of Dijkstra’s Algorithm
Minimum Spanning Trees (MSTs)
Prim’s Minimum Spanning Tree Algorithm Neil Tang 4/1/2008
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
Single-Source Shortest Path & Minimum Spanning Trees
Prim’s algorithm IDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least-weight edge connecting it to a vertex in.
Minimum-Cost Spanning Tree
Presentation transcript:

Prim’s Algorithm and an MST Speed-Up Berj Chilingirian

Today’s Plan (1) Prim’s Algorithm (2) Implementation (3) Runtime Analysis (4) Speed-Up

Improved MST Algorithm Key Idea Improved MST Algorithm 𝑂(𝑚 log log 𝑛 ) Boruvka’s Algorithm 𝑂(𝑚 log 𝑛 ) Prim’s Algorithm 𝑂(𝑚+𝑛 log 𝑛 )

Minimum Spanning Tree Given: undirected, connected, weighted graph Return: tree with all vertices and of minimum weight n vertices m edges

Prim’s Algorithm: The Idea

Algorithm 𝐼𝑁𝐼𝑇𝐼𝐴𝐿𝐼𝑍𝐸 𝐴=∅ 𝐼𝑁𝐼𝑇𝐼𝐴𝐿𝐼𝑍𝐸 𝐴=∅ 𝐼𝑁𝐼𝑇𝐼𝐴𝐿𝐼𝑍𝐸 𝑆= 𝑠 // 𝑠 𝑖𝑠 𝑠𝑜𝑚𝑒 𝑎𝑟𝑏𝑖𝑡𝑟𝑎𝑟𝑦 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖𝑛 𝐺 𝑊𝐻𝐼𝐿𝐸 S≠𝑉 𝑓=𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑤𝑒𝑖𝑔ℎ𝑡 𝑒𝑑𝑔𝑒 𝑢,𝑣 𝑤𝑖𝑡ℎ 𝑒𝑥𝑎𝑐𝑡𝑙𝑦 𝑜𝑛𝑒 𝑒𝑛𝑑𝑝𝑜𝑖𝑛𝑡, 𝑢, 𝑖𝑛 𝑆 𝑎𝑑𝑑 𝑜𝑡ℎ𝑒𝑟 𝑒𝑛𝑑𝑝𝑜𝑖𝑛𝑡 𝑣 𝑡𝑜 𝑆 𝑎𝑑𝑑 𝑒𝑑𝑔𝑒 𝑓 𝑡𝑜 𝐴

Correctness Cut Lemma: At every step, all edges in 𝐴 are in the edges of the MST.

Simple Implementation 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉 𝑘𝑒𝑦 𝑣 ←∞ 𝑝𝑟𝑒𝑑 𝑣 ←𝑛𝑢𝑙𝑙 𝑘𝑒𝑦 𝑠 ←0 𝑆←{𝑠} 𝑤ℎ𝑖𝑙𝑒 |𝑆|≠|𝑉| 𝑣←𝑣𝑒𝑟𝑡𝑒𝑥 𝑤𝑖𝑡ℎ 𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑘𝑒𝑦 𝑣 𝑎𝑑𝑑 𝑣 𝑡𝑜 𝑆 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣,𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢∉𝑆 𝑖𝑓 𝑘𝑒𝑦 𝑢 >𝑐 𝑣,𝑢 𝑘𝑒𝑦 𝑢 ←𝑐(𝑣,𝑢) 𝑝𝑟𝑒𝑑[𝑢]←𝑣

Better Implementation 𝑄←𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉 𝑘𝑒𝑦 𝑣 ←∞ 𝑝𝑟𝑒𝑑 𝑣 ←𝑛𝑢𝑙𝑙 𝑸.𝒊𝒏𝒔𝒆𝒓𝒕(𝒗, 𝒌𝒆𝒚 𝒗 ) 𝑸.𝒅𝒆𝒄𝒓𝑲𝒆𝒚(𝒔,𝟎) 𝑤ℎ𝑖𝑙𝑒 ! 𝑸.𝒊𝒔𝑬𝒎𝒑𝒕𝒚 𝑣←𝑸.𝒆𝒙𝒕𝒓𝒂𝒄𝒕𝑴𝒊𝒏 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣,𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢∈𝑄 𝑖𝑓 𝑘𝑒𝑦 𝑢 >𝑐 𝑣,𝑢 𝑘𝑒𝑦 𝑢 ←𝑐(𝑣,𝑢) 𝑸.𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖,𝒌𝒆𝒚[𝒖] 𝑝𝑟𝑒𝑑[𝑢]←𝑣

Runtime Analysis Procedure Number of Calls Time per Operation Array Binary Heap Fibonacci Heap Insert 𝑛 𝑂(𝑛) 𝑂 log 𝑛 𝑂(1) Extract Min Decrease Key 𝑚 Prim’s - 𝑂( 𝑛 2 ) 𝑂(𝑚log⁡𝑛) 𝑂(𝑚+𝑛 log 𝑛 )

Achieving 𝑂(𝑚 log log 𝑛 ) Time Yao, 1975: use linear median finding algorithm to approx. sorting of edges Boruvka’s + Prim’s: run Boruvka’s for 𝑘 iterations to produce 𝐺 ∗ run Prim’s on 𝐺 ∗

After 𝒌 iterations of Boruvka’s Algorithm Boruvka’s + Prim’s After 𝒌 iterations of Boruvka’s Algorithm # of Edges ≤𝑚 # of Vertices ≤ 𝑛 2 𝑘

# of vertices is ≤ 𝑛 2 log log 𝑛 = 𝒏 𝐥𝐨𝐠 𝒏 Boruvka’s + Prim’s Suppose 𝑘= log log 𝑛 , then # of vertices is ≤ 𝑛 2 log log 𝑛 = 𝒏 𝐥𝐨𝐠 𝒏

For 𝑘= log log 𝑛 iterations Boruvka’s + Prim’s Boruvka’s Algorithm For 𝑘= log log 𝑛 iterations 𝑂(𝑚 log log 𝑛) Prim’s Algorithm 𝑛 ∗ ≤ 𝑛 log 𝑛 𝐺 ∗ 𝑂 𝑚+ 𝑛 ∗ log 𝑛 =𝑂 𝑚+ 𝑛 log 𝑛 log 𝑛 =𝑂(𝑚+𝑛)

Boruvka’s + Prim’s 𝑂(𝑚 log log 𝑛 ) + 𝑂 𝑚+ 𝑛 log 𝑛 log 𝑛 =𝑂(𝑚 log log 𝑛 ) Boruvka’s Algorithm Prim’s Algorithm

Questions?

Similarity to Dijkstra’s Dijkstra’s Algorithm Prim’s Algorithm 𝑄←𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉 𝑘𝑒𝑦 𝑣 ←∞ 𝑝𝑟𝑒𝑑 𝑣 ←𝑛𝑢𝑙𝑙 𝑄.𝑖𝑛𝑠𝑒𝑟𝑡(𝑣, 𝑘𝑒𝑦 𝑣 ) 𝑄.𝑑𝑒𝑐𝑟𝐾𝑒𝑦(𝑠,0) 𝑤ℎ𝑖𝑙𝑒 ! 𝑄.𝑖𝑠𝐸𝑚𝑝𝑡𝑦 𝑣←𝑄.𝑒𝑥𝑡𝑟𝑎𝑐𝑡𝑀𝑖𝑛 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣,𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢∈𝑄 𝒊𝒇 𝒌𝒆𝒚 𝒖 >𝒄 𝒗,𝒖 +𝒌𝒆𝒚 𝒗 𝒌𝒆𝒚 𝒖 ←𝒄 𝒗,𝒖 +𝒌𝒆𝒚[𝒗] 𝑸.𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖,𝒌𝒆𝒚[𝒖] 𝑝𝑟𝑒𝑑 𝑢 ←𝑣 𝑄←𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉 𝑘𝑒𝑦 𝑣 ←∞ 𝑝𝑟𝑒𝑑 𝑣 ←𝑛𝑢𝑙𝑙 𝑄.𝑖𝑛𝑠𝑒𝑟𝑡(𝑣, 𝑘𝑒𝑦 𝑣 ) 𝑄.𝑑𝑒𝑐𝑟𝐾𝑒𝑦(𝑠,0) 𝑤ℎ𝑖𝑙𝑒 ! 𝑄.𝑖𝑠𝐸𝑚𝑝𝑡𝑦 𝑣←𝑄.𝑒𝑥𝑡𝑟𝑎𝑐𝑡𝑀𝑖𝑛 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣,𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢∈𝑄 𝒊𝒇 𝒌𝒆𝒚 𝒖 >𝒄 𝒗,𝒖 𝒌𝒆𝒚 𝒖 ←𝒄(𝒗,𝒖) 𝑸.𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖,𝒌𝒆𝒚[𝒖] 𝑝𝑟𝑒𝑑[𝑢]←𝑣