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