Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Similar documents
Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms for Minimum Spanning Trees

2 A Template for Minimum Spanning Tree Algorithms

Algorithms and Theory of Computation. Lecture 7: Priority Queue

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

CS 310 Advanced Data Structures and Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Chapter 23. Minimum Spanning Trees

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Minimum spanning trees

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

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

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

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Greedy Algorithms Part Three

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

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

Minimum Spanning Trees

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

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

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Minimum Spanning Trees. Lecture II: Minimium Spanning Tree Algorithms. An Idea. Some Terminology. Dr Kieran T. Herley

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

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

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

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

Minimum Spanning Trees

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

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

What is a minimal spanning tree (MST) and how to find one

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

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

Kruskal s MST Algorithm

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Chapter 5. Greedy algorithms

Design and Analysis of Algorithms

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees. COMPSCI 355 Fall 2016

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

23.2 Minimum Spanning Trees

Minimum Spanning Trees

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

Minimum Spanning Trees My T. UF

CS 561, Lecture 9. Jared Saia University of New Mexico

Introduction to Algorithms

CS 6783 (Applied Algorithms) Lecture 5

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Example of why greedy step is correct

6.1 Minimum Spanning Trees

Announcements Problem Set 5 is out (today)!

COMP 355 Advanced Algorithms

Lecture Summary CSC 263H. August 5, 2016

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

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

Lecture Notes for Chapter 23: Minimum Spanning Trees

CSE 521: Design and Analysis of Algorithms I

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

Minimum Spanning Trees and Prim s Algorithm

Lecture 6 Basic Graph Algorithms

Introduction to Algorithms

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

Minimum Spanning Trees Ch 23 Traversing graphs

Introduction to Algorithms

Week 11: Minimum Spanning trees

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Lecture 13: Minimum Spanning Trees Steven Skiena

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

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

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

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

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

Minimum-Cost Spanning Tree. Example

Minimum Spanning Trees

Minimum Spanning Trees

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

Minimum Spanning Trees

For this graph, give its (a) adjacency matrix, (b) upper triangular adjacency matrix input format, (c) adjacency list, and (d) adjacency list input

Randomized Graph Algorithms

looking ahead to see the optimum

Minimum Spanning Trees

Minimum Spanning Trees

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

tree follows. Game Trees

CHAPTER 13 GRAPH ALGORITHMS

Minimum Trees. The problem. connected graph G = (V,E) each edge uv has a positive weight w(uv)

1 Minimum Cut Problem

Partha Sarathi Manal

Graphs Weighted Graphs. Reading: 12.5, 12.6, 12.7

CSE 100: GRAPH ALGORITHMS

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

CS521 \ Notes for the Final Exam

Minimum Spanning Tree (5A) Young Won Lim 5/11/18

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

Transcription:

Algorithms and Theory of Computation Lecture 5: Minimum Spanning Tree Xiaohui Bei MAS 714 August 31, 2017 Nanyang Technological University MAS 714 August 31, 2017 1 / 30

Minimum Spanning Trees (MST) A weighted graph is a graph in which each edge e is associated with a numerical weight c e. Spanning Tree A spanning tree of an undirected connected graph G = (V, E) is a set of edges T E such that T forms a tree, and (V, T) is connected. In a weighted graph G, a minimum spanning tree is a spanning tree with the smallest sum of edge weights MST Find a minimum spanning tree of a weighted graph G. Nanyang Technological University MAS 714 August 31, 2017 2 / 30

Example a 7 5 b 8 c 7 d 9 15 e 5 6 f 8 11 9 g Nanyang Technological University MAS 714 August 31, 2017 3 / 30

Applications A basic and fundamental problem in graph theory, often used to measure costs to establish connections between vertices. 1 Network Design: designing networks with minimum cost while guaranting connectivity 2 Approximation Algorithm: can be used to approximate other hard problems such as Traveling Salesman Problem, Steiner Trees, etc. Nanyang Technological University MAS 714 August 31, 2017 4 / 30

Greedy Template Algorithm: SomeGreedyMSTAlgorithm(G): Initialize T = ; // T will store edges of a MST while T is not a spanning tree of G do choose e E that satisfies condition; add e to T; return T Which edges, and in what order, should be processed and added to the spanning tree? Nanyang Technological University MAS 714 August 31, 2017 5 / 30

Kruskal s Algorithm Process edges in the order of their costs (in increasing order), and add edges to T as long as they don t form a cycle. a 7 5 b 8 c 7 d 9 15 e 5 6 f 8 11 9 g Nanyang Technological University MAS 714 August 31, 2017 6 / 30

Prim s Algorithm T maintained by the algorithm will be a tree, starting from a single vertex. In each iteration, pick edges with least attachedment cost to T. a 7 5 b 8 c 7 d 9 15 e 5 6 f 8 11 9 g Nanyang Technological University MAS 714 August 31, 2017 7 / 30

Correctness of MST algorithms Many different MST algorithms. All of them rely on some basic properties of MSTs. Assumption For simplicity, we assume that edge costs are distinct, that is no two edge costs are equal. Nanyang Technological University MAS 714 August 31, 2017 8 / 30

Cut Cut Given a graph G = (V, E), a cut is a partition of the vertices into two disjoint subsets. Edges that have one endpoint in each subset of the partition are the edges of the cut, and these edges are said to cross the cut. S V \ S Nanyang Technological University MAS 714 August 31, 2017 9 / 30

Safe Edges Safe Edges An edge e is a safe edge if there exists a partition of V into S and V\S, and e is the unique minimum cost edge crossing this partition. S 13 13 7 7 3 3 5 5 V \ S 11 11 Safe edge in the cut (S, V \ S) Nanyang Technological University MAS 714 August 31, 2017 10 / 30

Cut Property Theorem Given an undirected connected graph G with distinct edge costs, the set of safe edges in G form the unique MST of G. Proof. Prove the lemma in two steps: 1 If e is a safe edge, then every MST contains e. 2 The set of safe edges form a connected graph that covers every vertex. Nanyang Technological University MAS 714 August 31, 2017 11 / 30

Cut Property Lemma 1 If e is a safe edge, then every MST contains e. Proof. Assume by contradiction that e is not in some MST T. 1 e = {u, v} is safe = there is an S V such that e is the unique minimum cost edge crossing S. 2 Adding e to T creates a cycle C in T {e}. 3 There must exist another edge e in T cross this cut. 4 T = (T\{e }) {e} is a spanning tree of lower cost. Nanyang Technological University MAS 714 August 31, 2017 12 / 30

Cut Property Lemma 2 The set of safe edges from a connected graph that covers every vertex. Proof. 1 Assume not. Let S be a connected component in the graph induced by safe edges. 2 Let e be the smallest cost edge crossing S = e is a safe edge. Nanyang Technological University MAS 714 August 31, 2017 13 / 30

Kruskal s Algorithm Process edges in the order of their costs (in increasing order), and add edges to T as long as they don t form a cycle. Algorithm: Kruskal(G): Initialize T = ; // T will store edges of a MST while T is not a spanning tree of G do choose e E of minimum cost; if T {e} does not contain cycles then add e to T; return T Presort edges by costs. Choosing minimum then takes O(1) time. Do DFS on T {e}. Takes O(n) time. Total time O(m log m) + O(mn) = O(mn). Nanyang Technological University MAS 714 August 31, 2017 14 / 30

Correctness Process edges in the order of their costs (in increasing order), and add edges to T as long as they don t form a cycle. Only need to show that all edges added are safe. Proof of Correctness. When e = (u, v) is added to the tree, let S and S be the connected components containing u and v respectively. e has minimum cost under all edges forming no cycle, hence e has minimum cost among all edges crossing the cut S (and also S ) e is a safe edge. Nanyang Technological University MAS 714 August 31, 2017 15 / 30

More Efficient Implementation Algorithm: SmarterKruskal(G): Initialize T = ; // T will store edges of a MST Put each vertex u V into a set by itself; foreach e = {u, v} E in the order of increasing costs do if u and v belong to different sets then add e to T; merge the two sets containing u and v; return T Need a data structure to: check if two elements belong to same set merge two sets Nanyang Technological University MAS 714 August 31, 2017 16 / 30

Data Structure: Union-Find Union-Find Store a set of disjoint sets with the following operations: 1 Make-Set(V): generate a set {v} for each vertex v V. Name of set {v} is v. 2 Find(u): find the name of the set containing vertex u. 3 Union(u, v): merge the sets named u and v. Name of the new set is either u or v. The running time of Kruskal algorithm will depend on the implementation of the data structure. Nanyang Technological University MAS 714 August 31, 2017 17 / 30

Union-Find: Implementation Sets are represented as trees, by pointers towards the roots. All elements in one tree belong to a set with root s name. Find(u): Traverse from u to the root Union(u, v): Make root of u (smaller set) point to root of v. Takes O(1) time. Each vertex u has a pointer parent(u) to its ancestor. s v w s v w u u Figure Figure: Union(Find(v), Find(u)) Nanyang Technological University MAS 714 August 31, 2017 18 / 30

New Implementation Algorithm: Make-Set(G): foreach u V do parent(u) = u; Algorithm: Find(u): while parent(u) u do u = parent(u); return u Algorithm: Union(u, v): ( parent(u) = u & parent(v) = v ) if component(u) component(v) then parent(u) = v else parent(v) = u set new componen size to component(u) + component(v). Nanyang Technological University MAS 714 August 31, 2017 19 / 30

Analysis Make-Set: O(n) time. Union: O(1) time. Find: O(depth of the tree) time. Proposition The maximum depth of trees in union-find is O(log n). Proof. Depth of tree(u) increases by at most 1 only when the set containing u changes its name. If depth of tree u increases then the size of the set containing u (at least) doubles. Maximum set size is n; so the depth of any tree is at most O(log n). Nanyang Technological University MAS 714 August 31, 2017 20 / 30

Speed up! When calling Find(u), we traverse the path from u to the root. Consecutive calls of find(u) traverse the same path. Idea: Path Compression Make all vertices on the path in Find(u) point to root directly. Nanyang Technological University MAS 714 August 31, 2017 21 / 30

Path Compression: Example Algorithm: Find(u): if parent(u) u then parent(u) = Find(parent(u)); return parent(u) r r v v w w u u Figure Figure: After Find(u) Nanyang Technological University MAS 714 August 31, 2017 22 / 30

Path Compression Question Does Path Compression help? Yes! Theorem With Path Compression, the amortized running time of Find operations is O(α(n)), where α(n) is the inverse of the Ackermann function A(n, n). Nanyang Technological University MAS 714 August 31, 2017 23 / 30

Ackermann and Inverse Ackermann Functions Ackermann function A(m, n) defined for m, n 0: n + 1 if m = 0 A(m, n) = A(m 1, 1) if m > 0 and n = 0 A(m 1, A(m, n 1)) if m > 0 and n > 0 A(3, n) = 2 n+3 3 A(4, 3) = 2 265536 3 α(n) is the inverse of A(n, n) For all practical purposes, α(n) 5. Nanyang Technological University MAS 714 August 31, 2017 24 / 30

Running time of Kruskal s Algorithm Using Union-Find data structure, Kruskal s Algorithm takes O(m) Find operations (two for each edge) O(n) Union operations (one for each edge added to T) 1 sorting operation Total time = O(mα(n) + n + m log m) = O(m log m) Nanyang Technological University MAS 714 August 31, 2017 25 / 30

Prim s Algorithm T maintained by the algorithm will be a tree, starting from a single vertex. In each iteration, pick edges with least attachedment cost to T. Algorithm: Prim(u): Initialize T = ; // T will store edges of a MST Initialize S = {1}; while T is not a spanning tree of G do choose e = (u, v) E of minimum cost such that u S and v V S; T = T {e}; S = S {v}; return T O(n) iterations O(m) time to pick edge e in each iteration Total running time = O(mn) Nanyang Technological University MAS 714 August 31, 2017 26 / 30

Correctness T maintained by the algorithm will be a tree, starting from a single vertex. In each iteration, pick edges with least attachedment cost to T. Proof of correctness. 1 If e is added to the tree, then e is safe Let S be the vertices connected by edges in T when e is added. e is the minimum cost edge crossing cut (S, V\S). 2 S is connected in each iteration and eventually S = V. Nanyang Technological University MAS 714 August 31, 2017 27 / 30

More Efficient Implementation Algorithm: SmarterPrim(u): Initialize T = ; // T will store edges of a MST Initialize S = {1}; for u / S, a(u) = arg min e=(u,v),v S c e ; while T is not a spanning tree of G do pick minimum a(u) = (u, v) ; T = T {a(u)}; S = S {u}; update array a; return T Maintain vertices in V\S in a priority queue. Nanyang Technological University MAS 714 August 31, 2017 28 / 30

Priority Queue Priority Queues Store a set S of n elements, where each element v S has an associated real/integer key k(v), with the following operations: 1 Make-Queue: create an empty queue 2 Find-Min: find the minimum key in S 3 Extract-Min: remove v S with the smallest key and return it 4 Decrease-Key(v, k (v)): decrease key of v from k(v) to k (v) 5 Add(v, k(v)): add new element v with key k(v) to S Very useful data structure, will discuss in detail in later lectures. Prim requires O(n) Extract-Min and O(m) Decrease-Key operations. Using standard Heaps, total time = O((m + n) log n). Using Fibonacci Heaps, total time = O(n log n + m). Nanyang Technological University MAS 714 August 31, 2017 29 / 30

More about MST There is an algorithm that runs in O(n + mα(n)) time. There is a randomized algorithm that runs in O(m + n) expected time. There is an algorithm using bit operations in RAM model that runs in O(m + n) time. Still open: Is there an O(n + m) time deterministic algorithm in the comparison model? Nanyang Technological University MAS 714 August 31, 2017 30 / 30