Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

Similar documents
Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Introduction: (Edge-)Weighted Graph

Introduction to Algorithms

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

22 Elementary Graph Algorithms. There are two standard ways to represent a

Minimum Spanning Trees

Week 11: Minimum Spanning trees

22 Elementary Graph Algorithms. There are two standard ways to represent a

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Analysis of Algorithms, I

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Introduction to Algorithms

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Minimum Spanning Trees My T. UF

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Partha Sarathi Manal

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Shortest path problems

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Single Source Shortest Path

6.1 Minimum Spanning Trees

Introduction to Algorithms

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Depth-first Search (DFS)

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

CHAPTER 13 GRAPH ALGORITHMS

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

Outline. Computer Science 331. Analysis of Prim's Algorithm

Week 12: Minimum Spanning trees and Shortest Paths

Lecture 11: Analysis of Algorithms (CS ) 1

Shortest Path Problem

2 A Template for Minimum Spanning Tree Algorithms

Minimum Spanning Trees

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

23.2 Minimum Spanning Trees

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Minimum Spanning Trees

Lecture 5: Graph algorithms 1

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS2 Algorithms and Data Structures Note 6

Lecture Notes for Chapter 23: Minimum Spanning Trees

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

Minimum Spanning Trees and Prim s Algorithm

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Solutions to relevant spring 2000 exam problems

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Minimum Spanning Trees

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

CS 310 Advanced Data Structures and Algorithms

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Algorithm Design and Analysis

Greedy Algorithms. Previous Examples: Huffman coding, Minimum Spanning Tree Algorithms

Reference Sheet for CO142.2 Discrete Mathematics II

Minimum Spanning Trees

1 Dijkstra s Algorithm

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

Chapter 23. Minimum Spanning Trees

CS2 Algorithms and Data Structures Note 6

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Problem Set 2 Solutions

Overview. Original. The context of the problem Nearest related work Our contributions The intuition behind the algorithm. 3.

Lecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

CHAPTER 23. Minimum Spanning Trees

Unit 2: Algorithmic Graph Theory

tree follows. Game Trees

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Chapter 9 Graph Algorithms

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

CS261: Problem Set #1

Lecture 8: The Traveling Salesman Problem

In this lecture, we ll look at applications of duality to three problems:

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

TIE Graph algorithms

Trees Algorhyme by Radia Perlman

Algorithms for Minimum Spanning Trees

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Lecture 4: Graph Algorithms

Exam 3 Practice Problems

MST & Shortest Path -Prim s -Djikstra s

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Graph Algorithms shortest paths, minimum spanning trees, etc.

Announcements Problem Set 5 is out (today)!

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

CS521 \ Notes for the Final Exam

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Distributed minimum spanning tree problem

Depth First Search A B C D E F G A B C 5 D E F 3 2 G 2 3

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Transcription:

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm ADS: lects 14 & 15 slide 1

Weighted Graphs Definition 1 A weighted (directed or undirected graph) is a pair (G, W ) consisting of a graph G = (V, E) and a weight function W : E R. In this lecture, we always assume that weights are non-negative, i.e., that W (e) 0 for all e E. Example A 2.0 B F 9.0 5.0 6.0 G 2.0 5.0 4.0 I H 5.0 4.0 C 6.0 3.0 2.0 E D ADS: lects 14 & 15 slide 2

ADS: lects 14 & 15 slide 3 Representations of Weighted Graphs (as Matrices) A 2.0 B F 9.0 5.0 6.0 G 2.0 5.0 4.0 I H 5.0 4.0 C 6.0 3.0 2.0 E D Adjacency Matrix 0 2.0 0 0 0 9.0 5.0 0 0 2.0 0 4.0 0 0 0 6.0 0 0 0 4.0 0 2.0 0 0 0 5.0 0 0 0 2.0 0 0 0 0 0 0 0 0 6.0 0 0 3.0 9.0 0 0 0 6.0 0 0 0 5.0 6.0 0 0 0 0 0 5.0 2.0 0 0 5.0 0 0 5.0 0 4.0 0 0 0 0 3.0 2.0 4.0 0

Representations of Weighted Graphs (Adjacency List) A 2.0 B F 9.0 5.0 6.0 G 2.0 5.0 4.0 I H 5.0 4.0 C 6.0 3.0 2.0 E D Adjacency Lists A B 2.0 G 5.0 F 9.0 B C D E F C 4.0 G 6.0 A D 2.0 H 5.0 B C 2.0 H E D I 3.0 F A 9.0 I E 2.0 4.0 6.0 6.0 G H I A 5.0 B 6.0 H 5.0 I G 5.0 C 5.0 D I F G 2.0 H 4.0 E 2.0 4.0 3.0 ADS: lects 14 & 15 slide 4

Problem Our Graph Model Connecting Sites Given a collection of sites and costs of connecting them, find a minimum cost way of connecting all sites. Sites are vertices of a weighted graph, and (non-negative) weights of the edges represent the cost of connecting their endpoints. It is reasonable to assume that the graph is undirected and connected. The cost of a subgraph is the sum of the costs of its edges. The problem is to find a subgraph of minimum cost that connects all vertices. ADS: lects 14 & 15 slide 5

ADS: lects 14 & 15 slide 6 Spanning Trees G = (V, E) undirected connected graph and W weight function. H = (V H, E H ) with V H V and E H E subgraph of G. The weight of H is the number W (H) = e E H W (e). H is a spanning subgraph of G if V H = V. Observation 2 A connected spanning subgraph of minimum weight is a tree.

ADS: lects 14 & 15 slide 7 Minimum Spanning Trees (G, W ) undirected connected weighted graph Definition 3 A minimum spanning tree (MST) of G is a connected spanning subgraph T of G of minimum weight. The minimum spanning tree problem: Given: Undirected connected weighted graph (G, W ) Output: An MST of G

Idea Prim s Algorithm Grow an MST out of a single vertex by always adding fringe (neighbouring) edges of minimum weight. A fringe edge for a subtree T of a graph is an edge with exactly one endpoint in T (so e = (u, v) with u T and v T). Algorithm Prim(G, W ) 1. T one vertex tree with arbitrary vertex of G 2. while there is a fringe edge do 3. add fringe edge of minimum weight to T 4. return T Note that this is another use of the greedy strategy. ADS: lects 14 & 15 slide 8

ADS: lects 14 & 15 slide 9 Example A 2.0 B 9.0 5.0 G 6.0 4.0 2.0 5.0 F I 4.0 H 5.0 C 6.0 3.0 2.0 E D

Correctness of Prim s algorithm 1. Throughout the execution of Prim, T remains a tree. Proof: To show this we need to show that throughout the execution of the algorithm, T is (i) always connected and (ii) never contains a cycle. (i) Only edges with an endpoint in T are added to T, so T remains connected. (ii) We never add any edge which has both endpoints in T (we only allow a single endpoint), so the algorithm will never construct a cycle. ADS: lects 14 & 15 slide 10

ADS: lects 14 & 15 slide 11 Correctness of Prim s algorithm (cont d) 2. All vertices will eventually be added to T. Proof: by contradiction... (depends on our assumption that the graph G was connected.) Suppose w is a vertex that never gets added to T (as usual, in proof by contradiction, we suppose the opposite of what we want). Let v = v0 e 1 v 1 e 2...v n = w be a path from some vertex v inside T to w (we know such a path must exist, because G is connected). Let v i be the first vertex on this path that never got added to T. After v i 1 was added to T, e i = (v i 1, v i ) would have become a fringe edge. Also, it would have remained as a fringe edge unless v i was added to T. So eventually vi must have been added, because Prims algorithm only stops if there are no fringe edges. So our assumption was wrong. So we must have w in T for every vertex w.

ADS: lects 14 & 15 slide 12 Correctness of Prim s algorithm (cont d) 3. Throughout the execution of Prim, T is contained in some MST of G. Proof: (by Induction) Suppose that T is contained in an MST T and that fringe edge e = (x, y) is then added to T by Prim. We shall prove that T + e is contained in some MST T (not necessarily T ). case (i): If e is contained in T, our proof is easy, we simply let T = T. case (ii): Otherwise, if e T, consider the unique path P from x to y in T (P is the pink path in the example overleaf). Then P contains exactly one fringe edge e = (x, y ) (same names in example).

ADS: lects 14 & 15 slide 13 Correctness of Prim s algorithm (cont d) x T (partial MST) x e y y x e y T (overall blue tree) Define T to be T + (x,y) (x,y ) ("drop (x,y ) and add (x,y)")

ADS: lects 14 & 15 slide 14 Correctness of Prim s algorithm (cont d) 3. case (ii) cont d Then W (e) W (e ). (otherwise e would definitely have been added before e) Let T = T + e e. T is a tree. Why? Well, we drop e = (x, y ), which splits the global MST T into two components: T x and the other subtree T y = T \ T x. We know x and y are now in different components after this split, because we have broken the unique path P between x and y in T. Hence we can add e = (x, y) to re-join T x and T y without making a cycle. T has the same vertices as T, thus it is a spanning tree. Moreover, W (T ) = W (T ) + W (e) W (e ), and because we know W (e) W (e ), this gives W (T ) W (T ), thus T is also a MST.

Towards an Implementation Improvement Instead of fringe edges, we think about adding fringe vertices to the tree A fringe vertex is a vertex y not in T that is an endpoint of a fringe edge. The weight of a fringe vertex y is min{w (e) e = (x, y) a fringe edge} (ie, the best weight that could bring y into the MST ) To be able to recover the tree, every time we bring a fringe vertex y into the tree, we store its parent in the tree. We will store the fringe vertices in a priority queue. ADS: lects 14 & 15 slide 15

Priority Queues with Decreasing Key A Priority Queue is an ADT for storing a collection of elements with an associated key. The following methods are supported: Insert(e, k): Insert element e with key k. Get-Min(): Return an element with minimum key; an error occurs if the priority queue is empty. Extract-Min(): Return and remove an element with minimum key; an error if the priority queue is empty. Is-Empty(): Return true if the priority queue is empty and false otherwise. To update the keys during the execution of Prim, we need priority queues supporting the following additional method: Decrease-Key(e, k): Set the key of e to k and update the priority queue. It is assumed that k is smaller than of equal to the old key of e. ADS: lects 14 & 15 slide 16

ADS: lects 14 & 15 slide 17 Implementation of Prim s Algorithm Algorithm Prim(G, W ) 1. Initialise parent array π: π[v] nil for all vertices v 2. Initialise weight array: weight[v] for all v 3. Initialise inmst array: inmst[v] false for all v 4. Initialise priority queue Q 5. v arbitrary vertex of G 6. Q.Insert(v, 0) 7. weight[v] = 0; 8. while not(q.is-empty()) do 9. y Q.Extract-Min() 10. inmst[y] true 11. for all z adjacent to y do 12. Relax(y,z) 13. return π Algorithm Relax(y, z) 1. w W (y, z) 2. if weight[z] = then 3. weight[z] w 4. π[z] y 5. Q.Insert(z, w) 6. else if (w < weight[z] and 7. not (inmst[z])) then 8. weight[z] w 9. π[z] y 10. Q.Decrease Key(z, w)

Analysis of Prim s algorithm Let n be the number of vertices and m the number of edges of the input graph. Lines 1-7, 13 of Prim require Θ(n) time altogether. Q will extract each of the n vertices of G once. Thus the loop at lines 8-12 is iterated n times. Thus, disregarding (for now) the time to execute the inner loop (lines 11-12) the execution of the loop requires time Θ ( n T Extract-Min (n) ) The inner loop is executed at most once for each edge (and at least once for each edge). So its execution requires time Θ ( m T Relax (n, m) ). ADS: lects 14 & 15 slide 18

Analysis of Prim s algorithm (Relax) Decreasing the time needed to execute Insert and Decrease-Key, the execution of Relax requires time Θ(1). Insert is executed once for every vertex, which requires time Θ ( n T Insert (n) ) Decrease-Key is executed at most once for every edge. This can require time of size Θ ( m T Decrease-key (n) ) Overall, we get T Prim (n, m) = Θ ( n ( T Extract-Min (n) + T Insert (n) ) + mt Decrease-Key (n) ) ADS: lects 14 & 15 slide 19

Priority Queue Implementations Array: Elements simply stored in an array. Heap: Elements are stored in a binary heap (see Inf2B (ADS note 7), [CLRS] Section 6.5) Fibonacci Heap: Sophisticated variant of the simple binary heap (see [CLRS] Chapters 19 and 20) method running time Array Heap Fibonacci Heap Insert Θ(1) Θ(lg n) Θ(1) Extract-Min Θ(n) Θ(lg n) Θ(lg n) Decrease-Key Θ(1) Θ(lg n) Θ(1) (amortised) ADS: lects 14 & 15 slide 20

Running-time of Prim T Prim (n, m) = Θ ( n ( T Extract-Min (n) + T Insert (n) ) + mt Decrease-Key (n) ) Which Priority Queue implementation? With array implementation of priority queue: T Prim (n, m) = Θ(n 2 ). With heap implementation of priority queue: T Prim (n, m) = Θ((n + m) lg(n)). With Fibonacci heap implementation of priority queue: T Prim (n, m) = Θ(n lg(n) + m). (n being the number of vertices and m the number of edges) ADS: lects 14 & 15 slide 21

ADS: lects 14 & 15 slide 22 Remarks The Fibonacci heap implementation is mainly of theoretical interest. It is not much used in practice because it is very complicated and the constants hidden in the Θ-notation are large. For dense graphs with m = Θ(n 2 ), the array implementation is probably the best, because it is so simple. For sparser graphs with m O( n2 lg n ), the heap implementation is a good alternative, since it is still quite simple, but more efficient for smaller m. Instead of using binary heaps, the use of d-ary heaps for some d 1 can speed up the algorithm (see [Sedgewick] for a discussion of practical implementations of Prims algorithm).

Reading Assignment [CLRS] Chapter 23. Problems 1. Exercises 23.1-1, 23.1-2, 23.1-4 of [CLRS] 2. In line 3 of Prim s algorithm, there may be more than one fringe edge of minimum weight. Suppose we add all these minimum edges in one step. Does the algorithm still compute a MST? 3. Prove that our implementation of Prim s algorithm on slide 6 is correct - ie, that it computes an MST. What is the difference between this and the suggested algorithm of Problem 4? ADS: lects 14 & 15 slide 23