The Greedy Method Introducton We have completed data structures. We now are gong to look at algorthm desgn methods. Often we are lookng at optmzaton problems whose performance s exponental. For an optmzaton problem, we are gven a set of constrants and an optmzaton functon. o Solutons that satsfy the constrants are called feasble solutons. o A feasble soluton for whch the optmzaton functon has the best possble value s called an optmal soluton. Cheapest lunch possble: Works by gettng the cheapest meat, frut, vegetable, etc. In a greedy method we attempt to construct an optmal soluton n stages. o At each stage we make a decson that appears to be the best (under some crteron) at the tme. o A decson made at one stage s not changed n a later stage, so each decson should assure feasblty. Consder gettng the best major: What s best now, may be worst later. Consder change makng: Gven a con system and an amount to make change for, we want mnmal number of cons. o A greedy crteron could be, at each stage ncrease the total amount of change constructed as much as possble. In other words, always pck the con wth the greatest value at every step. o A greedy soluton s optmal for some change systems. Machne schedulng: o Have a number of jobs to be done on a mnmum number of machnes. Each job has a start and end tme. o Order jobs by start tme. o If an old machne becomes avalable by the start tme of the task to be assgned, assgn the task to ths machne; f not assgn t to a new machne. o Ths s an optmal soluton. Note that our Huffman tree algorthm s an example of a greedy algorthm: o Pck least weght trees to combne. Heurstc we are not wllng to take the tme to get an optmal soluton, but we can be satsfed wth a pretty good soluton. 0/1 Knapsack Problem Problem descrpton: o Pack a knapsack wth a capacty of c. The Greedy Method Page 1
o From a lst of n tems, we must select the tems that are to be packed n the knapsack. o Each object has a weght of w and a proft of p. In a feasble knapsack packng, the sum of the weghts packed does not exceed the knapsack capacty. n An optmal packng s a feasble one wth maxmum proft p x subject to the constrants w x c n = 1 and { 0,1}, 1 n x o We are to fnd the values of x where x = 1 f object s packed nto the knapsack and x = 0 f object s not packed. Greedy strateges for ths problem: o From the remanng objects, select the object wth maxmum proft that fts nto the knapsack. o From the remanng objects, select the one that has mnmum weght and also fts nto the knapsack. o From the remanng objects, select the one wth maxmum p / w that fts nto the knapsack. Consder a knapsack nstance where n = 4, w = [ 2, 4,6,7], p = [ 6,10,12,13], and c = 11. o Look at the above three strateges. None of the above algorthms can guarantee the optmal soluton. o Ths s not surprsng snce ths problem s a NP-hard problem. Of the above three algorthms, the thrd one s probably the best heurstc. = 1 Topologcal Orderng Greedy crtera for the unnumbered nodes, assgn a number to a node for whch all predecessors have been numbered. Algorthm Intally, for each node wth predecessor count of zero, put t n a contaner (stack, queue, anythng wll do). 1. Remove a node wth predecessor count of zero from the contaner, number t, then lst t. 2. For each successor, decrement ts predecessor count. 3. If a successor now has a predecessor count of zero, add t to the contaner. Complexty assume an adjacency lst. 1. Fnd predecessor count. Ο (e), where e s number of edges. 2. Lst node for each successor, update predecessor count. Ο ( e + n), snce f have lots of nodes wth no predecessors, n could be more than e. Matchngs and Coverngs n a Graph The Greedy Method Page 2
Matchng a set of edges, no two of whch have a vertex n common. A perfect match s one n whch all vertces are matched. A maxmum matchng s matchng that cannot be extended by the addton of an edge. For an example see Fgure 1. Fgure 1 Matchng Many problems that seem unrelated to matchng can be formulated ths way. Edge coverng fnd mnmum number of edges (not necessarly ndependent) to cover (touch) all vertces. Stable marrage problem we want to fnd a matchng such that there exsts no stuaton n whch two ndvduals would rather be matched wth each other than the people they are currently matched wth. Bpartte Cover Bpartte graph (two parts) two ndependent sets of vertces such that all edges are between elements of dfferent sets. Complete bpartte every vertex of one set s adjacent to every vertex of second set. Examples of bpartte graphs: o Would-work-for graph n whch one set of nodes s jobs and another set s employees. o Want to marry (n Utah) where the node sets are men and women. Cover a subset A' of the set A s sad to cover the set B ff every vertex n B s connected to at least one vertex of A'. o Sze of the cover the number of vertces n A'. o Mnmum cover there s no smaller subset of A that covers B. Bpartte cover gven two types of nodes X and Y, X' s a node cover f X' s a subset of X such that every node n Y s connected to a node n X' va an edge. We want the cover of the mnmum sze. The Greedy Method Page 3
A coverng for Fgure 2 s {1, 2, 3, 17}. Show the mnmum cover for the bpartte graph shown n Fgure 2. o The cover s {1, 16, 17}. Fgure 2 Bpartte Graph Examples: o Suppose you have a set of nterpreters and a set of languages that need to be nterpreted. Edges represent can nterpret. We want to fnd the mnmal number of nterpreters to hre so that you can nterpret all languages. o Set of people and a set of categores (male, Asan, CS major, sngle,...). We want to form a commttee wth representaton from all groups. Set cover gven a collecton of subsets, fnd a mnmum number of subsets such that ther unon s the unverse. o Can you see how to map ths problem to the bpartte cover problem? Nodes for each subset. Nodes for each element. Edges represent contans. Ths ablty to map one problem nto another s key to usng knowledge of algorthms communty. Ths s lkely the reason why I always try to fnd smlartes between thngs that seem dfferent (e.g., cleanng up for company). NP-hard no one has developed a polynomal tme algorthm. o Also called ntractable or exponental. o We use greedy algorthms to approxmate. Greedy crteron Select the vertex of A that covers the largest number of uncovered vertces of B. Also keep a Boolean array that tells whether each node s covered or not. o Ths dea could be mplemented va a max tournament tree of wllcover take the bggest. Shortest Paths Gven a dgraph wth each edge n the graph havng a nonnegatve cost (or length). Try to fnd the shortest paths (n a dgraph) from a node to all other nodes. Greedy crteron From the vertces to whch the shortest path has not been generated, select one that results n the least path length (the smallest one). Have a reached set of nodes and an unreached set of nodes. 1. Intally, only the start node s reached. The Greedy Method Page 4
2. Use a mn prorty queue to store the total path lengths of each of the reached nodes to ts successors. 3. Whle prorty queue s not empty Pck the shortest total length node. If t has already been reached, dscard. Else count t as reached. Enqueue the total path lengths of each of the reached nodes to ts successors. For example, consder the dgraph Fgure 3 n and calculate the shortest path. Fgure 3 Shortest Path Mnmal Spannng Trees Problem select a subset of the edges such that a spannng tree s formed and the weght of the edges s mnmal. Example need to connect all stes to sewer system - want to mnmze the amount of ppe. Consder the spannng tree shown n Fgure 4. Fgure 4 Spannng Tree Prm s algorthm o Greedy crteron From the remanng edges, select a least-cost edge whose addton to the set of selected edges forms a tree. Tree set of vertces already n MST Succ set of vertces not n Tree but n Succ[Tree] Place any vertex (to serve as root) n MST. The Greedy Method Page 5
1. Fnd the smallest edge that connects Tree wth Succ 2. Add the edge to the MST and update Tree and Succ Uses a greedy method obtan a globally optmal soluton by means of a locally optmal soluton. Usng Fgure 4 and Prm s algorthm, we add the edges n the followng order: (1,6), (6,5), (5,4), (4,3), (3,2), and (2, 7). How do we fnd smallest edge? (sort or prorty queue) Kruskal s algorthm o Greedy crteron From the remanng edges, select a least-cost edge that does not result n a cycle when added to the set of already selected edges. Let each vertex be a separate component. 1. Examne each edge n ncreasng order of cost. 2. If edge connects vertces n same component, dscard t. Otherwse, add edge to MST. Update components. Usng Fgure 4 and Kruskal s algorthm, we add the edges n the followng order: (1,6), (3,4), (2,7), (2,3), (4,5), and (5,6). How do we fnd smallest edge? (sort or prorty queue) More Traversals of a Graph or Dgraph Tour vst each vertex n a graph exactly once and fnsh at the vertex started from. Euleran tour fnd a path/tour through the graph such that every edge s vsted exactly once. (Easy check nodal degree; f all are even, t s possble.) Hamltonan tour fnd a path through the graph such that every vertex s vsted exactly once. (NP complete) See Fgure 5 for dfferent tours. Fgure 5 Dfferent Tours The Greedy Method Page 6