Chapter 7: Greedy Algorithms

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

O(N 1.5 ) divide-and-conquer technique for Minimum Spanning Tree problem Step 1: Divide the graph into  N sub-graph by clustering. Step 2: Solve each.
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.
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 Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
What is the next line of the proof? a). Let G be a graph with k vertices. b). Assume the theorem holds for all graphs with k+1 vertices. c). Let G be a.
Tree tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Is the following graph Hamiltonian- connected from vertex v? a). Yes b). No c). I have absolutely no idea v.
Minimum Spanning Trees CIS 606 Spring Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
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.
Design and Analysis of Algorithms Minimum Spanning trees
Minimum Spanning Trees
Tirgul 13 Today we’ll solve two questions from last year’s exams.
More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm.
Lecture 27 CSE 331 Nov 6, Homework related stuff Solutions to HW 7 and HW 8 at the END of the lecture Turn in HW 7.
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 Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Introduction to Graph Theory
Minimum Spanning Tree: Prim’s & Kruskal’s Algorithms Presenter: Michal Karpinski.
Spanning Tree Algorithms William T. Trotter and Mitchel T. Keller Math 3012 – Applied Combinatorics Spring 2009.
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
Prims Algorithm for finding a minimum spanning tree
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Kruskal’s Algorithm for Computing MSTs Section 9.2.
Greedy Algorithms General principle of greedy algorithm
Greedy function greedy { S <- S0 //Initialization
Minimum Spanning Tree Chapter 13.6.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Topological Sort (topological order)
Short paths and spanning trees
Data Structures & Algorithms Graphs
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
CS200: Algorithm Analysis
Connected Components Minimum Spanning Tree
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Data Structures – LECTURE 13 Minumum spanning trees
Kruskal’s Algorithm for finding a minimum spanning tree
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Autumn 2015 Lecture 10 Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSCI2100 Data Structures Tutorial
Richard Anderson Lecture 10 Minimum Spanning Trees
Weighted Graphs & Shortest Paths
Greedy Algorithms Comp 122, Spring 2004.
Autumn 2016 Lecture 10 Minimum Spanning Trees
Minimum Spanning Trees (MSTs)
Lecture 12 Algorithm Analysis
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
Minimum-Cost Spanning Tree
Presentation transcript:

Chapter 7: Greedy Algorithms Kruskal’s, Prim’s, and Dijkstra’s Algorithms

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

4 5 A B C 3 6 4 5 6 7 3 4 D E F G 7 6 4 5 3 5 5 H I J

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

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

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

kruskal(e, n) { ... count = 0; i = 1 A B C D E F G H I J 4 5 7 3 6 A B A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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 B C D E F G H I J n 10 count i 1 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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 B C DH E F G I J n 10 Count 1 i 2 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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 B C DH EF G I J n 10 Count 2 i 3 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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++; ADH B C EF G I J n 10 Count 3 i 4 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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++; ADH B C EFG I J n 10 Count 4 i 5 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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++; ADHB C EFG I J n 10 Count 5 i 6 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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++; ADHB CEFG I J n 10 Count 6 i 7 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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++; ADHB CEFGJ I n 10 Count 7 i 8 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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++; ADHBCEFGJ I n 10 Count 8 i 9 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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++; ADHBCEFGJ I n 10 Count 8 i 10 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

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++; ADHBCEFGJI n 10 Count 9 i 11 A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

4 5 A B C 3 6 4 5 6 7 3 4 D E F G 7 6 4 5 3 5 5 H I J A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

4 5 A B C 3 6 4 5 6 7 3 4 D E F G 7 6 4 5 3 5 5 H I J A E 5 F J 5 F I 6 B E 6 C G 6 F G 4 E F 3 A B 4 D H 3 I J 5 C F 4 G J 4 E H 7 A D 3 B C 5 H I 5 D E 7

4 5 A B C A 3 6 4 5 6 7 3 4 D E F G D E B 7 6 4 5 3 5 5 H I J H F G C J I

Theorem 7.2.5 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

G Theorem 7.2.5 pp. 280 4 5 A B C Minimal Spanning Tree of G 3 6 4 5 6 D E F G 7 6 4 5 3 D E B 5 5 H I J H F Theorem 7.2.5 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 G C J I

G C G’ Subset of Minimal Spanning Tree of G S Theorem 7.2.5 pp. 280 4 3 6 4 5 6 7 3 4 D E F G A 7 6 4 C 5 3 5 5 H I J D E Theorem 7.2.5 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. A B 4 A E 5 D H 3 D E 7 S

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

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.