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

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

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

Minimum Spanning Trees My T. UF

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Lecture Notes for Chapter 23: Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

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

Minimum Spanning Trees

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Minimum Spanning Trees

Minimum spanning trees

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

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

Announcements Problem Set 5 is out (today)!

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

Week 11: Minimum Spanning trees

Introduction to Algorithms

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

2 A Template for Minimum Spanning Tree Algorithms

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

Complexity of Prim s Algorithm

23.2 Minimum Spanning Trees

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

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

CHAPTER 23. Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees

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

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

Introduction to Algorithms

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Algorithm Design and Analysis

Introduction to Algorithms

Kruskal s MST Algorithm

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

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Outline: MST

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

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

Lecture 10: Analysis of Algorithms (CS ) 1

Partha Sarathi Manal

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

Introduction to Algorithms. Minimum Spanning Tree. Chapter 23: Minimum Spanning Trees

Greedy Approach: Intro

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

Minimum Spanning Trees

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

Disjoint-set data structure: Union-Find. Lecture 20

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

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

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

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

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Week 12: Minimum Spanning trees and Shortest Paths

Minimum Spanning Trees and Prim s Algorithm

6.1 Minimum Spanning Trees

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

Greedy Algorithms Part Three

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

CS 6783 (Applied Algorithms) Lecture 5

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

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

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Depth-first Search (DFS)

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

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.

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

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

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

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

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

Chapter 5. Greedy algorithms

Unit 2: Algorithmic Graph Theory

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Minimum Spanning Tree

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Greedy Algorithms CSE 6331

Disjoint Sets. Definition Linked List Representation Disjoint-Set Forests. CS 5633 Analysis of Algorithms Chapter 21: Slide 1

Minimum Spanning Trees

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

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

Design and Analysis of Algorithms

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

Analysis of Algorithms, I

The minimum spanning tree problem

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

CSE 100: GRAPH ALGORITHMS

Sorting and Searching

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

Three Graph Algorithms

Three Graph Algorithms

CSE 100 Minimum Spanning Trees Prim s and Kruskal

1 Minimum Spanning Trees (MST) b 2 3 a. 10 e h. j m

Transcription:

2pt 0em Computer Science & Engineering 423/823 Design and of s Lecture 04 Minimum-Weight Spanning Trees (Chapter 23) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 18

Given a connected, undirected graph G = (V, E), a spanning tree is an acyclic subset T E that connects all vertices in V T acyclic a tree T connects all vertices spans G If G is weighted, then T s weight is w(t ) = (u,v) T w(u, v) A minimum weight spanning tree (or minimum spanning tree, or MST) is a spanning tree of minimum weight Not necessarily unique Applications: anything where one needs to connect all nodes with minimum cost, e.g. wires on a circuit board or fiber cable in a network 2 / 18

MST 3 / 18

The Disjoint-Set Data Structure Greedy algorithm: Make the locally best choice at each step Starts by declaring each vertex to be its own tree (so all nodes together make a forest) Iteratively identify the minimum-weight edge (u, v) that connects two distinct trees, and add it to the MST T, merging u s tree with v s tree 4 / 18

MST-Kruskal(G, w) The Disjoint-Set Data Structure A = ; 1 for each vertex v V do 2 Make-Set(v) ; 3 end 4 sort edges in E into nondecreasing order by weight w ; 5 for each edge (u, v) E, taken in nondecreasing order do 6 if Find-Set(u) Find-Set(v) then 7 A = A {(u, v)} ; 8 Union(u, v) ; 9 10 end 11 return A 5 / 18

MST-Kruskal(G, w), Part 2 The Disjoint-Set Data Structure Find-Set(u) returns a representative element from the set (tree) that contains u Union(u, v) combines u s tree to v s tree These functions are based on the disjoint-set data structure More on this later 6 / 18

(1) The Disjoint-Set Data Structure 7 / 18

(2) The Disjoint-Set Data Structure 8 / 18

(3) The Disjoint-Set Data Structure 9 / 18

Disjoint-Set Data Structure The Disjoint-Set Data Structure Given a universe U = {x 1,..., x n } of elements (e.g. the vertices in a graph G), a DSDS maintains a collection S = {S 1,..., S k } of disjoint sets of elements such that Each element x i is in exactly one set S j No set S j is empty Membership in sets is dynamic (changes as program progresses) Each set S S has a representative element x S Chapter 21 10 / 18

Disjoint-Set Data Structure (2) The Disjoint-Set Data Structure 11 / 18 DSDS implementations support the following functions: Make-Set(x) takes element x and creates new set {x}; returns pointer to x as set s representative Union(x, y) takes x s set (S x ) and y s set (S y, assumed disjoint from S x ), merges them, destroys S x and S y, and returns representative for new set from S x S y Find-Set(x) returns a pointer to the representative of the unique set that contains x Section 21.3: can perform d D-S operations on e elements in time O(d α(e)), where α(e) = o(lg e) = o(log e) is very slowly growing: α(e) = 0 if 0 e 2 1 if e = 3 2 if 4 e 7 3 if 8 e 2047 4 if 2048 e 16 512

of The Disjoint-Set Data Structure Sorting edges takes time O( E log E ) Number of disjoint-set operations is O( V + E ) on O( V ) elements, which can be done in time O(( V + E ) α( V )) = O( E α( V )) since E V 1 Since α( V ) = o(log V ) = O(log E ), we get total time of O( E log E ) = O( E log V ) since log E = O(log V ) 12 / 18

The Greedy algorithm, like In contrast to, algorithm maintains a single tree rather than a forest Starts with an arbitrary tree root r Repeatedly finds a minimum-weight edge that is incident to a node not yet in tree 13 / 18

MST-Prim(G, w, r) The A = ; 1 for each vertex v V do 2 key[v] = ; 3 π[v] = nil ; 4 end 5 key[r] = 0 ; 6 Q = V ; 7 while Q do 8 u = Extract-Min(Q) ; 9 for each v Adj[u] do 10 if v Q and w(u, v) < key[v] then 11 π[v] = u ; 12 key[v] = w(u, v) ; 13 14 end 15 end 14 / 18

MST-Prim(G, w, r), Part 2 The key[v] is the weight of the minimum weight edge from v to any node already in MST Extract-Min uses a minimum heap (minimum priority queue) data structure Binary tree where the key at each node is keys of its children Thus minimum value always at top Any subtree is also a heap Height of tree is lg n Can build heap on n elements in O(n) time After returning the minimum, can filter new minimum to top in time O(log n) Based on Chapter 6 15 / 18

(1) The 16 / 18

(2) The 17 / 18

of The Invariant: Prior to each iteration of the while loop: 1 Nodes already in MST are exactly those in V \ Q 2 For all vertices v Q, if π[v] nil, then key[v] < and key[v] is the weight of the lightest edge that connects v to a node already in the tree Time complexity: Building heap takes time O( V ) Make V calls to Extract-Min, each taking time O(log V ) For loop iterates O( E ) times In for loop, need constant time to check for queue membership and O(log V ) time for decreasing v s key and updating heap Yields total time of O( V log V + E log V ) = O( E log V ) Can decrease total time to O( E + V log V ) using Fibonacci heaps 18 / 18