Greey Algorithm Algorithm 0 Fll Semester
Optimiztion prolems An optimiztion prolem is one in whih you wnt to fin, not just solution, ut the est solution A greey lgorithm sometimes works well for optimiztion prolems A greey lgorithm works in phses. At eh phse: You tke the est you n get right now, without regr for future onsequenes You hope tht y hoosing lol optimum t eh step, you will en up t glol optimum /
Exmple: Counting money Suppose you wnt to ount out ertin mount of money, using the fewest possile ills n oins A greey lgorithm woul o this woul e: At eh step, tke the lrgest possile ill or oin tht oes not overshoot Exmple: To mke $.9, you n hoose: $5 ill $ ill, to mke $ 5 oin, to mke $.5 A 0 oin, to mke $.5 four oins, to mke $.9 For US money, the greey lgorithm lwys gives the optimum solution /
A filure of the greey lgorithm In some (fitionl) monetry system, krons ome in kron, 7 kron, n 0 kron oins Using greey lgorithm to ount out 5 krons, you woul get A 0 kron piee Five kron piees, for totl of 5 krons This requires six oins A etter solution woul e to use two 7 kron piees n one kron piee This only requires three oins The greey lgorithm results in solution, ut not in n optiml solution /
Exmple : Mking hnges 5 /
Huffmn oes Any inry tree with eges lele with 0 s n s yiels prefixfree oe of hrters ssigne to its leves Optiml inry tree minimizing the expete (weighte verge) length of oewor n e onstrute s follows Huffmn s lgorithm Initilize n one-noe trees with lphet hrters n the tree weights with their frequenies. Repet the following step n- times: join two inry trees with smllest weights into one (s left n right sutrees) n mke its weight equl the sum of the weights of the two trees. Mrk eges leing to left n right sutrees with 0 s n s, respetively. /
Huffmn enoing The Huffmn enoing lgorithm is greey lgorithm You lwys pik the two smllest numers to omine 00 5 7 5 7 9 A B C D E F A=00 B=00 C=0 D=00 E= F=0 7 /
0. 0.5 0. 0. 0.5 B _ C D A Exmple 0. C 0. D 0.5 0.5 A hrter A B C D _ 0. 0.5 frequeny 0.5 0. 0. 0. 0.5 B _ 0.5 0.5 A 0. 0. 0.5 0. 0. B _ C D oewor 00 00 0 0 0. 0. Enoe the text ADCB : 00000 0. C 0. D 0. B 0.5 0.5 _ 0.5 A.0 Deoe the text whose enoing is 000000000 : CDB_ACD 0 0. 0 0 0. 0. 0.5 C D 0 0. 0.5 A 0. B 0.5 _ 8 /
In-Clss Exerise Buil huffmn tree for the following: A x ={,,,, e } P x ={0.5, 0.5, 0., 0.5, 0.5} Define the oewor for eh lphet. Enoe the text - e 9 /
Minimum spnning tree A minimum spnning tree is lest-ost suset of the eges of grph tht onnets ll the noes Strt y piking ny noe n ing it to the tree Repetely: Pik ny lest-ost ege from noe in the tree to noe not in the tree, n the ege n new noe to the tree Stop when ll noes hve een e to the tree 5 The result is lest-ost (++++=) spnning tree If you think some other ege shoul e in the spnning tree: Try ing tht ege Note tht the ege is prt of yle To rek the yle, you must remove the ege with the gretest ost This will e the ege you just e 0 /
Trveling slesmn A slesmn must visit every ity (strting from ity A), n wnts to over the lest possile istne He n revisit ity (n reuse ro) if neessry He oes this y using greey lgorithm: He goes to the next nerest ity from wherever he is A B C D E From A he goes to B From B he goes to D This is not going to result in shortest pth! The est result he n get now will e ABDBCE, t ost of An tul lest-ost pth from A is ADBCE, t ost of /
Other greey lgorithms Dijkstr s lgorithm for fining the shortest pth in grph Alwys tkes the shortest ege onneting known noe to n unknown noe Prim s lgorithm for fining minimum-ost spnning tree Alwys tkes the lowest-ost ege etween noes in the spnning tree n noes not yet in the spnning tree Kruskl s lgorithm for fining minimum-ost spnning tree Alwys tries the lowest-ost remining ege /
Dijkstr : Exmple 7 5 e Tree verties (-,0) Remining verties (,) (-, ) (,7) e(-, ) 5 7 e (,) (,+) (,+) e(-, ) 5 7 e (,5) (,7) e(,5+) 5 7 e (,7) e(,9) e(,9) 5 7 e
In-Clss Exerise Apply Dijkstr s lgorithm to fin the shortest pth in the following grph. /
Kruskl s MST - Exmple 5 /
Prim s MST - Exmple /
In-Clss Exerise Fin minimum-ost spnning tree Kruskl Prim 7 /
Conneting wires There re n white ots n n lk ots, eqully spe, in line You wnt to onnet eh white ot with some one lk ot, with minimum totl length of wire Exmple: Totl wire length ove is + + + 5 = 8 Do you see greey lgorithm for oing this? Does the lgorithm gurntee n optiml solution? Cn you prove it? Cn you fin ounterexmple? 8 /
A sheuling prolem You hve to run nine jos, with running times of, 5,, 0,,, 5, 8, n 0 minutes You hve three proessors on whih you n run these jos You eie to o the longest-running jos first, on whtever proessor is ville P 0 0 P P 8 5 5 Time to ompletion: 8 + + = 5 minutes This solution isn t, ut we might e le to o etter 9 /
Another pproh Wht woul e the result if you rn the shortest jo first? Agin, the running times re, 5,, 0,,, 5, 8, n 0 minutes P 0 5 P P 5 8 0 Tht wsn t suh goo ie; time to ompletion is now + + 0 = 0 minutes Note, however, tht the greey lgorithm itself is fst All we h to o t eh stge ws pik the minimum or mximum 0 /
An optimum solution Better solutions o exist: P 0 P P 8 5 5 0 /
Tsk Sheuling Given: set T of n tsks, eh hving: A strt time, s i A finish time, f i (where s i < f i ) Gol: Perform ll the tsks using minimum numer of mhines. Mhine Mhine Mhine 5 7 8 9 /
Exmple Given: set T of n tsks, eh hving: A strt time, s i A finish time, f i (where s i < f i ) [,], [,], [,5], [,7], [,7], [,9], [7,8] (orere y strt) Gol: Perform ll tsks on min. numer of mhines /
Q & A