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

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Analysis of Algorithms CS 477/677
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Analysis of Algorithms
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm Binary Search Trees1.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
Minimum- Spanning Trees
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
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),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
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
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Lecture 12 Algorithm Analysis
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum spanning tree Shortest path algorithms
Greedy Algorithms Comp 122, Spring 2004.
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Finding Minimum Spanning Trees
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Presentation transcript:

1 Minimum Spanning Trees

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

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 Concrete example

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

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. oror oror oror Some Spanning Trees from Graph A Graph A Spanning Trees

All 16 of its Spanning Trees Complete Graph

10 Total Number of Spanning Trees  A complete graph with n vertices has n (n-2) spanning trees.(Cayley's formula)  5 3 is is is 100 million is Compare: there are *10 6 seconds in a year.  A nanosecond is one billionth (10 -9 ) of a second. (An electrical signal can travel about 30cm in a nanosecond.) There are  *10 15 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.

11 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

12 Hallmark for “greedy” algorithms 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. Greedy-choice property A locally optimal choice is globally optimal.

13 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

14 GENERIC-MST –Loop in lines 2-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 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

17 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 23.1 (c, f) is the light edges crossing S and V-S and will be a safe edge Illustration of Theorem 23.1

18 Example: MST

19 Example: MST

20 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

21 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

22 Kruskal's Algorithm MST-Kruskal(G,w) 1 A   2 for each vertex v  V[G] do 3 Make-Set(v) // creates set containing v (for initialization) 4 sort the edges of E 5 for each (u,v)  E do 6 if Find-Set(u)  Find-Set(v) then // different component 7 A  A  {(u,v)} 8 Union(Set(u),Set(v)) // merge 9 return A

23 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.  different components, the addition of the edge (u, v) does not create a cycle

24 Data Structures For Kruskal’s 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, 2, 3, 4}, {5, 6}, {7, 8}   Two vertices are in the same component iff they are in the same set of vertices

25 Data Structures For Kruskal’s 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, 2, 3, 4} + {5, 6}  {1, 2, 3, 4, 5, 6}

26 Kruskal’s Algorithm

27 Kruskal’s Algorithm

28 Kruskal’s Algorithm

29 Kruskal’s Algorithm

30 C FE AB D

31 C FE AB D

32 C FE AB D

33 C FE AB D

34 C FE AB D

35 C FE AB D cycle!!

36 C FE AB D

37 C FE AB D

38 C FE AB D minimum- spanning tree

39 Kruskal's Algorithm MST-Kruskal(G,w) 1 A   takesO(V) 2 for each vertex v  V[G] do // takesO(V) 3 Make-Set(v) takesO(E lg E) 4 sort the edges of E // takesO(E lg E) //takesO(E) // takesO(E) 5 for each (u,v)  E, in nondecreasing of weight do 6 if Find-Set(u)  Find-Set(v) then 7 A  A  {(u,v)} 8 Union(Set(u),Set(v)) 9 return A

40 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.  takesO(E lg E)  Performing  |V| MakeSet() operationsfor loop in lines 2-3.  |E| FindSet(),for loop in lines 5-8.  |V| - 1 Union(), for loop in lines 5-8.  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).

41 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

42 MST-Prim(G,w,r) //G: graph with weight w and a root vertex r 1 for each u  V[G]{ 2 key[u]   3p[u]  NULL // parent of u } 4 key[r]  0 5 // Q – vertices out of T 5 Q = BuildMinHeap(V,key); // Q – vertices out of T 6 while Q   do 7 u  ExtractMin(Q) // making u part of T 8 for each v  Adj[u] do 9 if v  Q and w(u,v)  key[v] then 10 p[v]  u 11 key[v]  w(u,v) Prim’s Algorithm 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. When algorithm terminates the min-priority queue Q is empty.

43 Prim’s Algorithm  Lines 1-5 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 8-11 update key and p fields of every vertex v adjacent to u but not in tree.

44 Run on example graph

45 Run on example graph

46 Run on example graph

47 Run on example graph

48 Run on example graph

49 Run on example graph

50 Run on example graph Extract_min from Q

51 Run on example graph

52 Run on example graph

53 Run on example graph

54 Run on example graph

55 Run on example graph

56 Run on example graph

57 Run on example graph

58 Run on example graph

59 Run on example graph

60 Run on example graph

61 Run on example graph

62 Run on example graph

63 Run on example graph

64 What is the hidden cost in this code? MST-Prim(G,w,r) 1 for each u  V[Q] 2 key[u]   3 p[u]  NULL 4 key[r]  0 // 5 Q = BuildHeap(V,key); //Q – vertices out of T 6 while Q   do 7 u  ExtractMin(Q) // making u part of T 8 for each v  Adj[u] do 9 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)); Prim’s Running Time updating keys while loop is executed |V| times Decrease-Key is executed O(|E|) times Extract-Min is executed |V| times

65 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)

66 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.