Greedy Algorithms. COMP 215 Lecture 6

Similar documents
Chapter 23. Minimum Spanning Trees

Kruskal s MST Algorithm

CS 6783 (Applied Algorithms) Lecture 5

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

Data Structures and Algorithms

Minimum Spanning Trees My T. UF

Outline. Computer Science 331. Analysis of Prim's Algorithm

Lecture 19. Broadcast routing

CS 310 Advanced Data Structures and Algorithms

Minimum Spanning Trees

Greedy Algorithms. Previous Examples: Huffman coding, Minimum Spanning Tree Algorithms

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

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

Minimum Spanning Trees

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Announcements Problem Set 5 is out (today)!

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

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Partha Sarathi Manal

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

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

6.1 Minimum Spanning Trees

Lecture 34 Fall 2018 Wednesday November 28

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

Week 12: Minimum Spanning trees and Shortest Paths

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

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

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Week 11: Minimum Spanning trees

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

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

Design and Analysis of Algorithms

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

Greedy Algorithms CSE 780

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

Lecture 13: Minimum Spanning Trees Steven Skiena

Design and Analysis of Algorithms

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

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

Introduction to Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

The minimum spanning tree problem

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

Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Minimum Spanning Trees

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

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

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Outline. Graphs. Divide and Conquer.

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

COMP 355 Advanced Algorithms

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

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

Greedy Algorithms CSE 6331

Homework Assignment #3 Graph

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

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Lecture 6 Basic Graph Algorithms

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

Greedy Algorithms Part Three

Review of course COMP-251B winter 2010

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Minimum spanning trees

Minimum spanning trees

Minimum Spanning Trees

Introduction to Algorithms

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

Minimum Spanning Trees

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

Introduction to Algorithms

Autumn Minimum Spanning Tree Disjoint Union / Find Traveling Salesman Problem

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

Single Source, Shortest Path Problem

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

CS 161: Design and Analysis of Algorithms

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo

23.2 Minimum Spanning Trees

Minimum-Cost Spanning Tree. Example

The Algorithms of Prim and Dijkstra. Class 19

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

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

CS : Data Structures

looking ahead to see the optimum

18 Spanning Tree Algorithms

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

Weighted Graphs and Greedy Algorithms

Lecture 10 Graph Algorithms

Introduction: (Edge-)Weighted Graph

Subsequence Definition. CS 461, Lecture 8. Today s Outline. Example. Assume given sequence X = x 1, x 2,..., x m. Jared Saia University of New Mexico

Lecture Notes for Chapter 23: Minimum Spanning Trees

What is a minimal spanning tree (MST) and how to find one

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

Transcription:

Greedy Algorithms COMP 215 Lecture 6

Wrapping Up DP A few words on traveling salesperson problem. The problem. Brute force algorithm. Dynamic programming algorithm.

Greedy Algorithms If we can view our algorithm as making a series of choices, greedy algorithms: Always make the choice that currently seems best. Never go back to undo a previous choice. Change example... Greedy algorithms tend to be very efficient. It's not always possible to find a greedy algorithm. Greedy algorithms often make good approximate algorithms.

Minimum Spanning Trees Today we will consider undirected graphs. A tree is a graph with no cycles. In a connected graph, there is a path from every node to every other node. A spanning tree of a graph G, is a connected subgraph of G that contains all of the vertices in G and is a tree. A minimum spanning tree is a spanning tree with minumum weight. We will look at two minimum spanning tree algorithms.

Let's go over an example... Prim's Algorithm

Prim's Pseudocode Call the set of vertices already considered Y. We will maintain two arrays, nearest[i] = index of the vertex in Y closest to v i. distance[i] = weight on edge between v i and the vertex indexed by nearest[i]. (or 1 if i is in Y).

Prim's Pseudocode Void prim(int n, number W[][], set_of_edges& F) { index vnear; number min; edge e; index nearest[2..n]; number distance [2..n]; F = EMPTYSET; for (i = 2; i<=n i++) { nearest[i] = 1; distance[i] = W[i][1]; } CONTINUED ON NEXT SLIDE...

} repeat (n 1 times) { min = ; for (i = 2; i <=n; i++) { if (0 <= distance[i] < min) { min = distance[i]; vnear = i; } } } e = (vnear, nearest[vnear]); add e to F; distance[vnear] = 1; for (i = 2; i<=n; i++) { if (W[i][vnear] < distance[i]) { distance[i] = W[i][vnear]; nearest[i] = vnear; } } Find the next vertex to add to Y. Add the edge from our new vertex to Y to our MST. Update distance and nearest to reflect the new Y.

Prim's Analysis n 2 every case. Why? Do you see any room for improvement? Is it guaranteed to find a minimum spanning tree?

Prim's Correctness Proof A subset of edges F is promising if edges can be added to it to create a minimum spanning tree. Proof outline: The empty set is promising. Show (by induction) that with every edge Prim's adds, the set of edges remains promising. First a lemma (lemma 4.1 in the book): Given a graph G = (V, E) let F be a promising subset of E. Let Y be the vertices connected by the edges in F. If e is an edge of minimum weight that connects a vertex in Y to a vertex in V Y, then F {e} is promising.

Proof of Lemma 4.1 F is promising, which means there must be some subset of edges F' such that Two cases: F F ' and (V, F') is an MST. FIRST: If {e} F ' then F {e} F ' and F {e} is promising. SECOND: If {e} F ' then F ' {e} must contain a cycle. Let's draw a picture... There must be a vertex {e'} F ' that also connects Y with V Y. The weight of e and e' must be the same. F ' {e} {e'} is an MST. F {e} F ' {e} {e'}, so F {e} is promising.

Prim Proof Continued

Let's do an example... Kruskal's Algorithm

Handling Disjoint Sets We need a data structure for maintaining disjoint sets. initial(n) create n disjoint subsets, each of which contains exactly one of the indices between 1 and n. p = find(i) retrieve a pointer to the set containing i. merge(p,q) merge the two sets pointed to by p and q. equal(p,q) check to see if two pointers refer to the same set. If we initially create n disjoint sets, then call find, merge and equal c * t times the cost is:. t lg t The implementation and analysis of this is interesting in it's own right, but we won't talk about it today.

Kruskal's Pseudocode int kruskal(int n, int m, set_of_edges E, set_of_edges& F) { set_pointer p, q; edge e; sort the m edges in E by weight; initial(n); while (# of edges in F < n 1) { e = next lowest weight edge; i,j = e.indices(); p = find(i); q = find(j); if (! equal(p,q)) { merge(p,q); add e to F; } } }

Kruskal's Analysis We want the complexity in terms of number of edges m, and number of vertices n. Three potentially time consuming phases: Sorting. Initializing the disjoint sets. The while loop.

Kruskal's Analysis Three potentially time consuming phases: Sorting. m lg m Initializing the disjoint sets. n The while loop. m lg m m lg m Overall:, or n 2 lg n Is it guaranteed to find the minimum spanning tree?

If time... Dijkstra's Algorithm