CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Design and Analysis of Algorithms Minimum Spanning trees
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.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Minimum Spanning Trees
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.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
COMP108 Algorithmic Foundations Greedy methods
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees
Lecture 26 CSE 331 Nov 2, 2016.
Lecture 22 Minimum Spanning Tree
Minimum Spanning Trees and Shortest Paths
Topological Sort (topological order)
Data Structures & Algorithms Graphs
Minimum-Cost Spanning Tree
Spanning Trees.
Minimum Spanning Tree.
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
Minimum Spanning Trees
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
Data Structures – LECTURE 13 Minumum spanning trees
Lecture 26 CSE 331 Nov 1, 2017.
Hannah Tang and Brian Tjaden Summer Quarter 2002
Kruskal’s Minimum Spanning Tree Algorithm
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Lecture 27 CSE 331 Oct 31, 2014.
Lecture 28 CSE 331 Nov 7, 2012.
Lecture 27 CSE 331 Nov 2, 2010.
Minimum Spanning Tree.
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Lecture 19: Minimum Spanning Trees
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSE 417: Algorithms and Computational Complexity
Minimum Spanning Trees (MSTs)
CSE 373: Data Structures and Algorithms
Winter 2019 Lecture 10 Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
CSE 332: Minimum Spanning Trees
Lecture 23: Minimum Spanning Trees
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 24: Graphs VI

Minimum Spanning Tree Problem Input: Undirected Graph G = (V, E) and a cost function C from E to non-negative real numbers. C(e) is the cost of edge e. Output: A spanning tree T with minimum total cost. That is: T that minimizes

Observations about Spanning Trees For any spanning tree T, inserting an edge enew not in T creates a cycle But Removing any edge eold from the cycle gives back a spanning tree If enew has a lower cost than eold we have progressed!

Find the MST A C B D F H G E 1 7 6 5 11 4 12 13 2 3 9 10 The previous problem is a minimum spanning tree problem. We have two primary ways of solving this problem deterministically. BTW, Which of these three is the minimum spanning tree? (The one in the middle)

Two Different Approaches Prim’s Algorithm Looks familiar! Kruskals’s Algorithm Completely different! One node, grow greedily Forest of MSTs, Union them together. I wonder how to union…

Prim’s algorithm Idea: Grow a tree by adding an edge from the “known” vertices to the “unknown” vertices. Pick the edge with the smallest weight. G v Pick one node as the root, Incrementally add edges that connect a “new” vertex to the tree. Pick the edge (u,v) where u is in the tree, v is not AND where the edge weight is the smallest of all edges (where u is in the tree and v is not). known

Prim’s algorithm A B C D F E 10 1 5 8 3 6 2 4 Starting from empty T, choose a vertex at random and initialize V = {A}, T ={} G 6

Prim’s algorithm A Choose the vertex u not in V such that edge weight from u to a vertex in V is minimal (greedy!) V = {A,C} T = { (A,C) } 10 5 1 3 8 B C D 1 1 6 4 2 F E G 6

Prim’s algorithm Repeat until all vertices have been chosen V = {A,C,D} T= { (A,C), (C,D)} A B C D F E 10 1 5 8 3 6 2 4 G

Prim’s algorithm V = {A,C,D,E} T = { (A,C), (C,D), (D,E) } A B C D F E 10 1 5 8 3 6 2 4 G 6

Prim’s algorithm V = {A,C,D,E,B} T = { (A,C), (C,D), (D,E), (E,B) } A F E 10 1 5 8 3 6 2 4 G 6

Prim’s algorithm V = {A,C,D,E,B,F} T = { (A,C), (C,D), (D,E), (E,B), (B,F) } A B C D F E 10 1 5 8 3 6 2 4 G 6

Prim’s algorithm V = {A,C,D,E,B,F,G} T = { (A,C), (C,D), (D,E), (E,B), (B,F), (E,G) } A B C D F E 10 1 5 8 3 6 2 4 G 6

Prim’s algorithm Final Cost: 1 + 3 + 4 + 1 + 1 + 6 = 16 A B C D F E 10 5 8 3 6 2 4 G 6

Prim's Algorithm Implementation for each vertex v: // Initialization v's distance := infinity. v's previous := none. mark v as unknown. choose random node v1. v1's distance := 0. List := {all vertices}. T := {}. while List is not empty: v := remove List vertex with minimum distance. add edge {v, v's previous} to T. mark v as known. for each unknown neighbor n of v: if distance(v, n) is smaller than n's distance: n's distance := distance(v, n). n's previous := v. return T.

Prim’s algorithm Analysis How is it different from Djikstra's algorithm? If the step that removes unknown vertex with minimum distance is done with binary heap the running time is: O(|E|log |V|)

Kruskal’s MST Algorithm Idea: Grow a forest out of edges that do not create a cycle. Pick an edge with the smallest weight. G=(V,E) v

Example of Kruskal 1 1 6 5 4 7 2 3 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 2 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 2 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 3 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 4 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 5 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 6 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 7 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 7 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Example of Kruskal 8,9 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

Kruskal's Algorithm Implementation sort edges in increasing order of length (e1, e2, e3, ..., em). T := {}. for i = 1 to m if ei does not add a cycle: add ei to T. return T. But how can we determine that adding ei to T won't add a cycle?