More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm.

Slides:



Advertisements
Similar presentations
Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select.
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320.
10.4 Spanning Trees. Def Def: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G See handout.
Discussion #36 Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Tree Algorithms
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
3/29/05Tucker, Sec Applied Combinatorics, 4th Ed. Alan Tucker Section 4.2 Minimal Spanning Trees Prepared by Amanda Dargie and Michele Fretta.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Design and Analysis of Algorithms Minimum Spanning trees
Minimum Spanning Trees
Tirgul 13 Today we’ll solve two questions from last year’s exams.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
COSC 2007 Data Structures II Chapter 14 Graphs III.
7.1 and 7.2: Spanning Trees. A network is a graph that is connected –The network must be a sub-graph of the original graph (its edges must come from the.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Spanning Trees. A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:
Introduction to Graph Theory
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
Nattee Niparnan. Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long.
Minimum Bottleneck Spanning Trees (MBST)
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Kruskal’s Algorithm for Computing MSTs Section 9.2.
Graph Search Applications, Minimum Spanning Tree
COMP108 Algorithmic Foundations Greedy methods
May 12th – Minimum Spanning Trees
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Spanning Trees.
Minimal Spanning Trees
Minimum Spanning Trees and Shortest Paths
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
Spanning Trees.
Minimum Spanning Tree.
Connected Components Minimum Spanning Tree
Minimum Spanning Tree.
Chapter 7: Greedy Algorithms
Spanning Trees.
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
MA/CSSE 473 Day 33 Student Questions Change to HW 13
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Minimum Spanning Tree.
CSE 373: Data Structures and Algorithms
Minimum Spanning Trees (MSTs)
Lecture 12 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Presentation transcript:

More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm.

Minimum Spanning Tree (MST) Problem Given a weighted graph, i.e. a graph with edge weights… try to find a sub-graph that (i) connects all the nodes and (ii) the sum of the edge weights is minimal. This sub-graph will always be a tree. ABC DEF G H I J

MST Example Given the following graph, find the MST First, we want all the nodes connected Second, we want to pick the lowest weight edges. ABC DEF G H I J

MST Example Greedy step 1: Start with A and select the minimum edge This would connect D. ABC DEF G H I J Total Weight: 3

MST Example Greedy step 2: From D select the minimum edge This would connect H. ABC DEF G H I J Total Weight: 6

MST Example Greedy step 3: From H select the minimum edge This would connect I. ABC DEF G H I J Total Weight: 11

MST Example Greedy step 4: From I select the minimum edge This would connect J. ABC DEF G H I J Total Weight: 16

MST Example Greedy step 5: From J select the minimum edge This would connect G. ABC DEF G H I J Total Weight: 20

MST Example Greedy step 6: From G select the minimum edge This would connect F. ABC DEF G H I J Total Weight: 24

MST Example Greedy step 6: From G select the minimum edge This would connect F. ABC DEF G H I J Total Weight: 24

MST Example What is the running time of the algorithm? ABC DEF G H I J Total Weight: 27

MST Example N steps Each step must find the minimum edge from a node Worst case: N-1 + N-1 + N-1 + … + N-1 = O(N 2 ) ABC DEF G H I J

MST Example You might think the worst case is: N-1 + N-2 + N-3 + … = O(N 2 ) because at every step you connect a node and don’t have to consider it’s edges. However, think about how the algorithm would actually be implemented, and how you would keep track of this info?

MST Example You might think the worst case is: N-1 + N-2 + N-3 + … = O(N 2 ) pick node v from the node_list. while node_list is not empty { mark v as visited. min_hop = infinity; foreach of v’s edges (v,w) if (w is not visited) if (edge (v,w) < min_hop) { min_hop = edge(v,w) min_edge = w; } remove v from node from node_list v = w; } The first while loop will always take N iterations The foreach loop could take N-1 iteration in a complete graph

MST Example Greedy step 8: From E select the minimum edge This would connect B. ABC DEF G H I J Total Weight: 33

MST Example Greedy step 9: From B select the minimum edge This would connect C. ABC DEF G H I J Total Weight: 38

MST Example This greedy algorithm failed Why? ABC DEF G H I J Total Weight: 38

MST Example It makes a local decision. From E, it chooses to go to B We have to consider other options. ABC DEF G H I J Total Weight: 38

Kruskal’s Algorithm Solves the Minimum Spanning Tree Problem using a better Greedy Approach Input: –List of edges in a graph –n – the number of vertices Output: –Prints the list of edges in the Minimum Spanning Tree

ABC DEF G HI J

Kruskal’s kruskal(e, n) { sort(e); ABC DEF G H I J

Kruskal’s kruskal(e, n) { sort(e); ABC DEF G H I J A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7

kruskal(e, n) { sort(e); for (i = A to J) makeset(i) ABC DEF G H I J A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDEFGHIJ

kruskal(e, n) {... count = 0; i = 1 ABC DEF G H I J A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDEFGHIJ Count 0 i1i1

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDEFGHIJ count 0 i1i1 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDHEFGIJ Count 1 i2i2 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABCDHEFGIJ Count 2 i3i3 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGIJ Count 3 i4i4 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGIJ Count 4 i5i5 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGIJ Count 5 i6i6 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGIJ Count 6 i7i7 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGJI Count 7 i8i8 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGJI Count 8 i9i9 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGJI Count 8 i 10 n 10

kruskal(e, n) { while (count < n-1) { if (findset(e[i].v) != findset(e[i].w)) { print(e[i].v + “ ”+ e[i].w); count++; union(e[i].v, e[i].w); } i++; } A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ADHBCEFGJI Count 9 i 11 n 10

A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABC DEF G H I J

A D 3 D H 3 EF 3 AB 4 C F 4 F G 4 G J 4 A E 5 H I 5 I J 5 F J 5 BC 5 B E 6 C G 6 F I 6 DE 7 E H 7 ABC DEF G H I J

ABC DEF G H I J A B C DE F G H I J

Theorem pp. 280 Let G be a connected, weighted graph, and let G’ be a sub-graph of a minimal spanning tree of G. Let C be a component of G’, and let S be the set of all Edges with one vertex in C and the other not in C. If we add a minimum weight edge in S to G’, the resulting graph is also contained in a minimal spanning tree of G

Theorem pp. 280 Let G be a connected, weighted graph, and let G’ be a sub-graph of a minimal spanning tree of G. Let C be a component of G’, and let S be the set of all Edges with one vertex in C and the other not in C. If we add a minimum weight edge in S to G’, the resulting graph is also contained in a minimal spanning tree of G ABC DEF G H I J G A B C DE F G H I J Minimal Spanning Tree of G

Theorem pp. 280 G’ be a sub-graph of a minimal spanning tree of G. Let C be a component of G’, and let S be the set of all Edges with one vertex in C and the other not in C. ABC DEF G H I J G G’ Subset of Minimal Spanning Tree of G S C A DE AB 4 A E 5 DE 7 D H 3

Theorem pp. 280 If we add a minimum weight edge from S to G’, the resulting graph is also contained in a minimal spanning tree of G ABC DEF G H I J G G’ Subset of Minimal Spanning Tree of G S C A DE AB 4 A E 5 DE 7 D H 3

Theorem 7.2.6: Kruskal’s Algorithm finds minimum spanning tree Proof by induction G’ is a sub-graph constructed by Kruskal’s Algorithm G’ is initially empty but each step of the Algorithm increases the size of G’ Inductive Assumption: G’ is contained in the MST.

Theorem 7.2.6: Kruskal’s Algorithm finds minimum spanning tree Proof by induction Let (v,w) be the next edge selected by Kruskal’s Algorithm Kruskal’s algorithm finds the minimum weight edge (v,w) such that v and w are not already in G’ C can be any subset of the MST, so you can always construct a C such that v is in C and w is not. Therefore, by Theorem 7.2.5, when (v,w) is added to G’, the resulting graph is also contained in the MST.