Minimum Spanning Trees

Similar documents
Minimum Spanning Trees Outline: MST

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

Partha Sarathi Manal

Minimum Spanning Trees

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

Lecture Notes for Chapter 23: Minimum Spanning Trees

Minimum Spanning Trees

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

Minimum Spanning Trees My T. UF

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

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

Minimum spanning trees

Minimum Spanning Trees Ch 23 Traversing graphs

Week 11: Minimum Spanning trees

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

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

2 A Template for Minimum Spanning Tree Algorithms

Minimum Spanning Trees and Prim s Algorithm

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees

Announcements Problem Set 5 is out (today)!

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

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

Minimum Spanning Trees

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

Introduction to Algorithms

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Introduction to Algorithms

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

Minimum Spanning Trees

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

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CHAPTER 23. Minimum Spanning Trees

Introduction to Algorithms

Complexity of Prim s Algorithm

Minimum Spanning Tree

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

Algorithm Design and Analysis

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

Partha Sarathi Mandal

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

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

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

Week 12: Minimum Spanning trees and Shortest Paths

Tree. number of vertices. Connected Graph. CSE 680 Prof. Roger Crawfis

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Example of why greedy step is correct

Solutions to relevant spring 2000 exam problems

Unit 2: Algorithmic Graph Theory

Minimum Spanning Trees

Kruskal s MST Algorithm

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

15 211: RECITATION7, SECTION M. Hui Han Chin,

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

Minimum-Cost Spanning Tree. Example

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

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

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

Lecture 10: Analysis of Algorithms (CS ) 1

6.1 Minimum Spanning Trees

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

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

Algorithms for Minimum Spanning Trees

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

23.2 Minimum Spanning Trees

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

Introduction: (Edge-)Weighted Graph

Depth-first Search (DFS)

CS 6783 (Applied Algorithms) Lecture 5

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

CSE 100 Minimum Spanning Trees Prim s and Kruskal

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

looking ahead to see the optimum

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

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

Minimum Spanning Tree (undirected graph)

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

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

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

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Design and Analysis of Algorithms

Greedy Algorithms Part Three

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

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

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

CHAPTER 13 GRAPH ALGORITHMS

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

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

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

CS : Data Structures

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

CSE 100: GRAPH ALGORITHMS

tree follows. Game Trees

Lecture 4: Graph Algorithms

Transcription:

Minimum Spanning Trees 1

Minimum- Spanning Trees 1. Concrete example: computer connection. Definition of a Minimum- Spanning Tree

Concrete example Imagine: You wish to connect all the computers in an office building using the least amount of cable - Each vertex in a graph G represents a computer - Each edge represents the amount of cable needed to connect all computers

Spanning Tree A spanning tree of G is a subgraph which is tree (acyclic) and connect all the vertices in V. Spanning tree has only V - 1 edges.

Problem: Laying Telephone Wire Central office

Wiring: Naive Approach Central office Expensiv e!

Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

Spanning Trees A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees. Graph A Some Spanning Trees from Graph A o r o r o r

Complete Graph All 16 of its Spanning Trees

Total Number of Spanning Trees A complete graph with n vertices has n (n-) spanning trees.(cayley's formula) is 1 6 is 64 10 is 100 million 100 is 1016 Compare: there are 1.76*106 seconds in a year. A nanosecond is one billionth (10-) of a second. (An electrical signal can travel about 0cm in a nanosecond.) There are 1.76*101 nanoseconds in a year. We are not going to be able to find all spanning trees for large graphs even on the fastest computers, at least not in our lifetimes. We have to get smart about trees. 10

Minimum Spanning Tree Input: Undirected connected graph G = (V, E) and weight function w : E R, Output: A Minimum spanning tree T : tree that connects all the vertices and minimizes Greedy Algorithms Generic MST algorithm Kruskal s algorithm Prim s algorithm w( T) w( u, v) ( u, v) T 11

Hallmark for greedy algorithms Greedy-choice property A locally optimal choice is globally optimal. Theorem. Let T be the MST of G = (V, E), and let A V. Suppose that (u, v) E is the least-weight edge connecting A to V A. Then, (u, v) T. 1 1

Growing a Minimum Spanning Tree (MST) Generic algorithm Grow MST one edge at a time Manage a set of edges A, maintaining the following loop invariant: Prior to each iteration, A is a subset of some MST At each iteration, we determine an edge (u, v) that can be added to A without violate this invariant A {(u, v)} is also a subset of a MST (u, v) is called a safe edge for A 1 1

GENERIC-MST Loop in lines -4 is executed V - 1 times Any MST tree contains V - 1 edges The execution time depends on how to find a safe edge

How to Find A Safe Edge? Theorem.1. Let A be a subset of E that is included in some MST, let (S, V-S) be any cut of G that respects A, and let (u, v) be a light edge crossing (S, V-S). Then edge (u, v) is safe for A Cut (S, V-S): a partition of V Crossing edge: one endpoint in S and the other in V-S A cut respects a set of A of edges if no edges in A crosses the cut A light edge crossing a cut if its weight is the minimum of any edge crossing the cut

16 16

Illustration of Theorem.1 A={(a,b}, (c, i}, (h, g}, {g, f}} S={a, b, c, i, e}; V-S = {h, g, f, d} many kinds of cuts satisfying the requirements of Theorem.1 (c, f) is the light edges crossing S and V-S and will be a safe edge 17

Example: MST 1

Example: MST 1

Kruskal's Algorithm Edge based algorithm Greedy strategy: From the remaining edges, select a least-cost edge that does not result in a cycle when added to the set of already selected edges Repeat V -1 times 0

Kruskal's Algorithm INPUT: edge-weighted graph G = (V, E), with V = n OUTPUT: a spanning tree A of G touches all vertices, has n-1 edges of minimum cost ( = total edge weight) Algorithm: Start with A empty, Add the edges one at a time, in increasing weight order An edge is accepted it if connects vertices of distinct trees (if the edge does not form a cycle in A) until A contains n-1 edges 1

Kruskal's Algorithm MST-Kruskal(G,w) 1 A for each vertex v V[G] do Make-Set(v) //creates set containing v (for initialization) 4 sort the edges of E for each (u,v)e do 6 if Find-Set(u) Find-Set(v) then // different component 7 A A {(u,v)} Union(Set(u),Set(v)) // merge return A

Data Structures For Kruskal s Algorithm Does the addition of an edge (u, v) to T result in a cycle? Each component of T is a tree. When u and v are in the same component, the addition of the edge (u, v) creates a cycle. 1 7 4 4 different components, the addition of the edge (u, v) does not create a cycle. 7 6 6

Data Structures For Kruskal s Algorithm Each component of T is defined by the vertices in the component. Represent each component as a set of vertices. {1,,, 4}, {, 6}, {7, } Two vertices are in the same component iff they are in the same set of vertices. 1 7 7 4 6 4 6 4

Data Structures For Kruskal s Algorithm When an edge (u, v) is added to T, the two components that have vertices u and v combine to become a single component In our set representation of components, the set that has vertex u and the set that has vertex v are united. {1,,, 4} + {, 6} {1,,, 4,, 6} 1 7 7 4 6 4 6

Kruskal s Algorithm 1 1 17 17 1 1 1 1 1 1 1 1 17 17 1 1 1 1 1 1? 1? 1 17 17 1 1 1 1 1 1 6

Kruskal s Algorithm 1 1 17 17? 1 1 1 1 1 1 1 17? 1 17 1 1 1 1 1 1 1 17 1 17? 1 1 1 1 1 1 7

Kruskal s Algorithm 1 1 17 17 1 1 1 1 1? 1 1 1 17? 17 1 1 1 1 1 1 1 1 17 17? 1 1 1 1 1 1

Kruskal s Algorithm 1? 1 17 17 1 1 1 1? 1 1 1 1? 17 17 1 1 1 1 1 1 1 17 1 1 1

A B 4 6 C D 1 E 4 F 0

A B 4 6 C D 1 E 4 F 1

A B 4 6 C D 1 E 4 F

A B 4 6 C D 1 E 4 F

A B 4 6 C D 1 E 4 F 4

A B 4 6 C D cycle!! 1 E 4 F

A B 4 6 C D 1 E 4 F 6

A B 4 6 C D 1 E 4 F 7

minimum- spanning tree A B C D E 1 F

Kruskal's Algorithm MST-Kruskal(G,w) 1 A for each vertex v V[G] do // takes O(V) Make-Set(v) 4 sort the edges of E // takes O(E lg E) // takes O(E) for each (u,v)e, in nondecreasing of weight do 6 if Find-Set(u) Find-Set(v) then 7 A A {(u,v)} Union(Set(u),Set(v)) return A

Running Time of Kruskal s Algorithm Kruskal s Algorithm consists of two stages. Initializing the set A in line 1 takes O(1) time. Sorting the edges by weight in line 4. takes O(E lg E) Performing V MakeSet() operations for loop in lines -. E FindSet(), for loop in lines -. V - 1 Union(), for loop in lines -. which takes O(V + E) The total running time is O(E lg E) We have lg E = O(lg V) because # of E = V-1 So total running time becomes O(E lg V). 40

Prim s Algorithm The tree starts from an arbitrary root vertex r and grows until the tree spans all the vertices in V. At each step, Adds only edges that are safe for A. When algorithm terminates, edges in A form MST. Vertex based algorithm. Grows one tree T, one vertex at a time 41

Prim s Algorithm MST-Prim(G,w,r) //G: graph with weight w and a root vertex r 1 for each u V[G]{ key[u] p[u] NULL // parent of u } 4 key[r] 0 Q = BuildMinHeap(V,key); // Q vertices out of T 6 while Q do 7 u ExtractMin(Q) // making u part of T for each v Adj[u] do if v Q and w(u,v) key[v] then 10 p[v] u 11 key[v] w(u,v) 4 updating keys For each vertex v, key [v] is min weight of any edge connecting v to a vertex in tree. key [v]= if there is no edge and p [v] names parent of v in tree. When algorithm terminates the min-priority queue Q is empty.

Prim s Algorithm Lines 1- set the key of each vertex to (except root r, whose key is set to 0 first vertex processed). Also, set parent of each vertex to NULL, and initialize min-priority queue Q to contain all vertices. Line 7 identifies a vertex u є Q Removing u from set Q adds it to set Q-V of vertices in tree, thus adding (u, p[ u]) to A. The for loop of lines -11 update key and p fields of every vertex v adjacent to u but not in tree. 4

Run on example graph 6 4 10 1 44

Run on example graph 6 4 10 key[u] = 1 4

Run on example graph 6 4 r 10 0 Pick a start vertex r 1 46

Run on example graph 6 4 u 10 0 1 Red vertices have been removed from Q 47

Run on example graph 6 4 u 10 0 1 Red arrows indicate parent pointers 4

Run on example graph 6 4 u 10 0 1 4

Run on example graph 6 4 10 0 u 1 Extract_min from Q 0

Run on example graph 6 4 10 0 u 1 1

Run on example graph 6 4 10 10 0 u 1

Run on example graph 6 4 10 10 0 u 1

Run on example graph 6 4 10 10 0 u 1 4

Run on example graph 6 4 10 10 0 1 u 1

Run on example graph 6 4 10 10 0 1 u 1 6

Run on example graph 6 4 10 10 0 1 u 1 7

Run on example graph 4 6 4 10 10 0 1 u 1

Run on example graph 4 6 4 10 0 1 u 1

Run on example graph 4 6 4 10 0 1 u 1 60

Run on example graph u 4 6 4 10 0 1 1 61

Run on example graph 4 6 4 10 0 1 1 u 6

Run on example graph 4 6 4 10 0 1 1 u 6

Prim s Running Time What is the hidden cost in this code? Extract-Min is executed V times MST-Prim(G,w,r) 1 for each u V[Q] Decrease-Key is executed O( E ) times key[u] p[u] NULL while loop is executed V times 4 key[r] 0 Q = BuildHeap(V,key); //Q vertices out of T 6 while Q do 7 u ExtractMin(Q) // making u part of T for each v Adj[u] do if v Q and w(u,v) < key[v] then 10 p[v] u 11 key[v] w(u,v) DecreaseKey(v, w(u,v)); updating keys 64

Prim s Running Time Time complexity depends on data structure Q Binary heap: O(E lg V): BuildHeap takes O(log V) time number of while iterations: V ExtractMin takes O(lg V) time total number of for iterations: E DecreaseKey takes O(lg V) time Hence, Time = log V + V.T(ExtractMin) + E.T(DecreaseKey) Time = O(V lg V + E lg V) = O(E lg V) Since E V 1 (because G is connected) 6

Minimum bottleneck spanning tree A bottleneck edge is the highest weighted edge in a spanning tree. A spanning tree is a minimum bottleneck spanning tree (or MBST) if the graph does not contain a spanning tree with a smaller bottleneck edge weight. A MST is necessarily a MBST, but a MBST is not necessarily a MST. 66