CSE 100 Minimum Spanning Trees Prim s and Kruskal

Similar documents
CSE 100: GRAPH ALGORITHMS

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

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

CS 310 Advanced Data Structures and Algorithms

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

CSE 100: GRAPH ALGORITHMS

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

The minimum spanning tree problem

Minimum Spanning Trees

CS Algorithms and Complexity

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Minimum Spanning Tree

Homework Assignment #3 Graph

The Shortest Path Problem

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Week 12: Minimum Spanning trees and Shortest Paths

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Minimum Spanning Trees. CSE 373 Data Structures

CS302 - Data Structures using C++

Minimum-Cost Spanning Tree. Example

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

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

Homework 5: Graphs, Minimum Spanning Trees, and Dijkstra Shortest-Path

Announcements. HW4 due Friday. Summer 2016 CSE373: Data Structures & Algorithms 1

Minimum Spanning Trees My T. UF

Priority queue ADT part 1

Week 11: Minimum Spanning trees

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Minimum Spanning Trees

Problem Score Maximum MC 34 (25/17) = 50 Total 100

LECTURE 26 PRIM S ALGORITHM

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Algorithm Design and Analysis

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

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

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

CSE 100: UNION-FIND HASH

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

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

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Minimum Spanning Trees and Shortest Paths

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP)

Complexity of Prim s Algorithm

Minimum spanning trees

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes

Announcements Problem Set 5 is out (today)!

Chapter 9 Graph Algorithms

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

Introduction: (Edge-)Weighted Graph

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Undirected Graphs. Hwansoo Han

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

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees

23.2 Minimum Spanning Trees

Minimum Spanning Trees

Lecture Summary CSC 263H. August 5, 2016

Minimum Spanning Trees Ch 23 Traversing graphs

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Lecture 25 Spanning Trees

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees

CHAPTER 13 GRAPH ALGORITHMS

looking ahead to see the optimum


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

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Shortest Path Algorithms

CSE 21: Mathematics for Algorithms and Systems Analysis

Minimum Spanning Trees and Shortest Paths

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

Data Structures and Algorithms

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

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

Lecture 18: Implementing Graphs

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

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

CS521 \ Notes for the Final Exam

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning trees. Suppose you have a connected undirected graph

Spanning Trees. Lecture 22 CS2110 Spring 2017

CS3383 Unit 2: Greedy

Exam 3 Practice Problems

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

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

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

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

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Minimum Spanning Trees

CS : Data Structures

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

CSE 373: Practice Final

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

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

Transcription:

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj: (V1,1), (V2,6), (V3,3) V1: dist= prev= done= adj: (V2,4) V2: dist= prev= done= adj: V3: dist= prev= done= adj: (V2,1) The priority queue (set start vertex dist=0, prev=-1, and insert it with priority 0 to start) V0 3 V3 6 1 V2 1 4 V1

Spanning trees We will consider spanning trees for undirected graphs A spanning tree of an undirected graph G is an undirected graph that... contains all the vertices of G contains only edges of G has no cycles is connected A spanning tree is called spanning because it connects all the graph s vertices A spanning tree is called a tree because it has no cycles (recall the definition of cycle for undirected graphs) What is the root of the spanning tree? you could pick any vertex as the root; the vertices adjacent to that one are then the children of the root; etc.

Spanning trees: examples Consider this undirected graph G: V0 V1 V2 V3 V4 V5 V6

Spanning tree? Ex. 1 V0 V1 V2 V3 V4 V5 V6 Is this graph a spanning tree of G? A. Yes B. No

Spanning tree? Ex. 2 V0 V1 V2 V3 V4 V5 V6 Is this graph a spanning tree of G? A. Yes B. No

Spanning tree? Ex. 3 Is this graph a spanning tree of G? A. Yes B. No

Minimum Spanning tree: Spanning tree with minimum total cost 1 V0 10 V1 1 V2 12 V3 2 V4 3 V5 4 1 V6 8 Is this graph a minimum spanning tree of G? A. Yes B. No

Minimum spanning trees in a weighted graph A single graph can have many different spanning trees They all must have the same number of edges, but if it is a weighted graph, they may differ in the total weight of their edges Of all spanning trees in a weighted graph, one with the least total weight is a minimum spanning tree (MST) It can be useful to find a minimum spanning tree for a graph: this is the least-cost version of the graph that is still connected, i.e. that has a path between every pair of vertices How to do it?

Prim s MST Algorithm V2 3 1 V0 12 V5 4 Start with any vertex and grow like a mold, one edge at a time Each iteration choose the cheapest crossing edge 10 V3 1 2 V1 V6 1 8 V4

Fast implementation of Prim s MST Algorithm 10 V0 V1 1 V2 12 4 3 V5 V3 2 1 V6 1 8 V4 Iteration 1

Finding a minimum spanning tree: Prim s algorithm As we know, minimum weight paths from a start vertex can be found using Djikstra s algorithm At each stage, Djikstra s algorithm extends the best path from the start vertex (priority queue ordered by total path cost) by adding edges to it To build a minimum spanning tree, you can modify Djikstra s algorithm slightly to get Prim s algorithm At each stage, Prim s algorithm adds the edge that has the least cost from any vertex in the spanning tree being built so far (priority queue ordered by single edge cost) Like Djikstra s algorithm, Prim s algorithm has worst-case time cost O( E log V ) We will look at another algorithm: Kruskal s algorithm, which also is a simple greedy algorithm Kruskal s has the same big-o worst case time cost as Prim s, but in practice it can be made to run faster than Prim s, if efficient supporting data structures are used

Fast Implementation of Prim s MST Algorithm 1. Initialize the vertex vector for the graph. Set all done fields to false. Pick an arbitrary start vertex s. Set its prev field to -1, and its done field to true. Iterate through the adjacency list of s,and put those edges in the priority queue. 2. Is the priority queue empty? Done! 3. Remove from the priority queue the edge (v, w, cost) with the smallest cost. 4. Is the done field of the vertex w marked true? If Yes: this edge connects two vertices already connected in the spanning tree, and we cannot use it. Go to 1. 5. Accept the edge: Mark the done field of vertex w true, and set the prev field of w to indicate v. 6. Iterate through w s adjacency list, putting each edge in the priority queue. 7. Go to 1. The resulting spanning tree is then implicit in the values of prev fields in the vertex objects.

Weighted minimum spanning tree: Kruskal s algorithm Prim s algorithm starts with a single vertex, and grows it by adding edges until the MST is built: Kruskal s algorithm starts with a forest of single-node trees (one for each vertex in the graph) and joins them together by adding edges until the MST is built; Why Kruskal?

Kruskal s algorithm:

Kruskal s algorithm: output Show the result here: What is the total cost of this spanning tree? Is there another spanning tree with lower cost? With equal cost?

Ref: Tim Roughgarden (stanford) Naïve Implementation of Kruskal s algorithm: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E If T U {e i } has no cycles Add e i to T

Running Time of Naïve Implementation of Kruskal s algorithm: 1. Sort edges in increasing order of cost 2. Set of edges in MST, T={} 3. For i= 1 to E If T U {e i } has no cycles Add e i to T Ref: Tim Roughgarden (stanford)

Towards a fast implementa<on for Kruskal s Algorithm What is the work that we are repeatedly doing with Kruskal s Algo? 19

Towards a fast implementa<on for Kruskal s Algorithm What is the work that we are repeatedly doing with Kruskal s Algo? Checking for cycles: Linear with BFS, DFS Union-Find Data structure allows us to do this in constant time! (Next time) 20

Graph algorithm time costs: a summary The graph algorithms we have studied have fast algorithms: Find shortest path in unweighted graphs Solved by basic breadth-first search: O( V + E ) worst case Find shortest path in weighted graphs Solved by Dijkstra s algorithm: O( E log V ) worst case Find minimum-cost spanning tree in weighted graphs Solved by Prim s or Kruskal s algorithm: O( E log V ) worst case The greedy algorithms used for solving these problems have polynomial time cost functions in the worst case since E V 2, Dijkstra s, Prim s and Kruskal s algorithms are O( V 3 ) As a result, these problems can be solved in a reasonable amount of time, even for large graphs; they are considered to be tractable problems However, many graph problems do not have any known polynomial time solutions...!