ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo

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

Minimum Spanning Trees and Shortest Paths

Chapter 9 Graph Algorithms

Minimum Spanning Trees

Minimum Spanning Tree

Chapter 9 Graph Algorithms

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

Week 11: Minimum Spanning trees

CS 6783 (Applied Algorithms) Lecture 5

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

Greedy Approach: Intro

looking ahead to see the optimum

Minimum Spanning Trees and Shortest Paths

Chapter 9 Graph Algorithms

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

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

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

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

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

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

Week 12: Minimum Spanning trees and Shortest Paths

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

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

Lecture 18: Implementing Graphs

Spanning Trees. Lecture 20 CS2110 Spring 2015

6.1 Minimum Spanning Trees

Lecture 6 Basic Graph Algorithms

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

18 Spanning Tree Algorithms

Chapter 23. Minimum Spanning Trees

Minimum spanning trees

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

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

Minimum Spanning Tree

Minimum spanning trees

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

Spanning Trees. Lecture 22 CS2110 Spring 2017

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

Minimum-Cost Spanning Tree. Example

Exam 3 Practice Problems

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

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

LECTURE 26 PRIM S ALGORITHM

Note. Out of town Thursday afternoon. Willing to meet before 1pm, me if you want to meet then so I know to be in my office

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

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

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

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

COMP 355 Advanced Algorithms

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

Data Structures and Algorithms

Parallel Graph Algorithms

CHAPTER 13 GRAPH ALGORITHMS

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

Minimum Spanning Trees

Analysis of Algorithms, I

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

Minimum Spanning Trees

Design and Analysis of Algorithms

Data Structures and Algorithm Analysis in C++

Chapter 1 Introduction

SPANNING TREES. Lecture 21 CS2110 Spring 2016

Spanning Trees. CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Motivation. Observations. Spanning tree via DFS

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

Kruskal s MST Algorithm

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Undirected Graphs. Hwansoo Han

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

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

Graph Algorithms. 1 Preliminary Denitions. CSci 335 Software Design and Analysis III Chapter 9 Graph Algorithms. Prof.

CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

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

Minimum Spanning Trees My T. UF

tree follows. Game Trees

Algorithms for Minimum Spanning Trees

Parallel Graph Algorithms

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Homework Assignment #3 Graph

Graphs & Digraphs Tuesday, November 06, 2007

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

Design and Analysis of Algorithms

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Minimum Spanning Trees Shortest Paths

Priority queue ADT part 1

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

The minimum spanning tree problem

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

CSE 100: GRAPH ALGORITHMS

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

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

CS261: Problem Set #2

CS 4349 Lecture October 23rd, 2017

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

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

END-TERM EXAMINATION

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Transcription:

ALORITHM DSIN RDY ALORITHMS University of Waterloo

LIST OF SLIDS - List of Slides reedy Approaches xample: Making Change 4 Making Change (cont.) 5 Minimum Spanning Tree 6 xample 7 Approaches that Don t Work Prim s Algorithm 9 xample 0 xample (cont.) ADTs needed for Prim s Algorithm Kruskal s Algorithm xample 4 Which one to use? 5 Finding Path(s) in a raph 6 Dijkstra s Algorithm 7 xample Why does all of this Work? 9 Summary

reedy Algorithms: reedy Approaches IDA: iteratively make the best local choice no undoing of decisions made locality guarantees reasonable performance in general may lead to non-optimal solutions proofs of correctness xamples: making change finding a minimum spanning tree Prim s and Kruskal s algorithms finding a shortest path Dijkstra s algorithm CS 4 University of Waterloo

reedy Algorithms: xample: Making Change Input: A natural number k. Output: A minimum-sized collection of coins whose values sum to k. The allowed coin values s are, 5, 0, 5, and 00. reedy solution: while k > 0 output the maximum coin value v s. t. v k k := k v CS 4 University of Waterloo

reedy Algorithms: 4 Making Change (cont.) xample : Change for 4: 5, 0, 0,,, xample : Change for 0 but assume that there are no 5-coins : 5,,,,, NOT OPTIMAL (0, 0, 0 is optimal) Questions:. what sets of coins guarantee optimal solution?. how hard is it really to make change optimally? CS 4 University of Waterloo

reedy Algorithms: 5 Minimum Spanning Tree Def: A tree is a connected acyclic undirected graph. Def: Let = (V, ) be a connected undirected graph. A spanning tree for is a subset such that (V, ) is a tree. We are looking for an algorithm: Input: A connected undirected graph, each of whose edges has an associated positive integer weight. Output: A spanning tree for whose total weight is minimized. solution is not necessarily unique (example?) CS 4 University of Waterloo

xample reedy Algorithms: 6 iven raph: A B 5 F C 4 D Minimal Spanning Tree: A B 5 F C 4 D Another Minimal Spanning Tree: A B 5 F C 4 D CS 4 University of Waterloo

reedy Algorithms: 7 Approaches that Don t Work Pick a node, follow the shortest edge greedily similar to BFS/DFS guarantees to produce a minimal spanning tree NOT optimal (example?) Do an exhaustive search modify DFS/BFS to generate all trees (how?) pick the one with minimal total weight guaranteed to find a correct solution too inefficient (exponential; why?) CS 4 University of Waterloo

reedy Algorithms: Prim s Algorithm IDA: row the tree greedily in successive stages pick a node and mark it visited select a minimum-weight edge e from edges between a visited and an unvisited node and add it to mark both endpoints of e visited and repeat. CS 4 University of Waterloo

reedy Algorithms: 9 xample Starting from C: A B 5 F D 4 C A B 5 F C 4 D A B 5 F C 4 D A B 5 F C 4 D CS 4 University of Waterloo

reedy Algorithms: 0 xample (cont.) A B 5 F C 4 D A B 5 F C D 4 A B 5 F C D 4 CS 4 University of Waterloo

reedy Algorithms: ADTs needed for Prim s Algorithm How do we store visited nodes? insert, retrieve [dictionary?] How to store edges? get in increasing order of cost [ordered list?] This gives us O( V ) algorithm optimal for dense graphs ( V ) for sparse graphs ( << V ) we can use heaps CS 4 University of Waterloo

reedy Algorithms: Kruskal s Algorithm IDA: Always select a minimum-weight edge from all of the edges in the graph that does not close a cycle. use a sorted list of edges Needs to keep a forest of trees initially all nodes are separate trees Use union-find data structure/algorithm (chapter ) CS 4 University of Waterloo

reedy Algorithms: xample A B 5 F C 4 D A B 5 F C 4 D A B 5 F C 4 D A B 5 F C D 4 CS 4 University of Waterloo

reedy Algorithms: 4 Which one to use? Kruskal s algorithm (O(a log n)) is better if the graph is sparse. Prim s algorithm (O(n )) is better if the graph is dense. CS 4 University of Waterloo

reedy Algorithms: 5 Finding Path(s) in a raph Def: A directed graph is an ordered pair (V, ) where V is a finite set of nodes, and is a set of ordered pairs of distinct nodes from V. how to find a shortest path between nodes u and v? is there a path at all? Single-Source Shortest Paths: Input: A directed graph with positive integer edge weights and a start node u V. Output: For each v V, the length of a minimum-weight path from u to v. negative cycles? CS 4 University of Waterloo

reedy Algorithms: 6 Dijkstra s Algorithm IDA: compute the path length by adding the next closest vertex (to vertices seen so far) until all vertices are added. S := {u}, D(u) = 0 while there is an edge from S to V S let v V S be a node that minimizes D(w) + d wv for all w S D(v) := min{d(w) + d wv : w S} for all w S do if D(w) > D(v) + d vw then D(w) := D(v) + d vw S := S {v} CS 4 University of Waterloo

xample reedy Algorithms: 7 5 4 0 5 4 0 5 5 4 0. 5 0 7 5 7 4 CS 4 University of Waterloo

reedy Algorithms: Why does all of this Work? OBSRVATION: Dijkstra s Algorithm: a b c min{a,b+c} Prim s Algorithm: b a c min{b,c} + induction CS 4 University of Waterloo

reedy Algorithms: 9 Summary reedy approach gives optimal solutions but only if set up correctly Other typical problems with greedy solutions:. simple (non-preemptive) scheduling. Huffman codes. approximate bin packing not optimal! (e.g., at least extra 4/ bins) 4.... CS 4 University of Waterloo