Grdy mthods Prudn Wong http://www.s.liv..uk/~pwong/thing/omp108/01617
Coin Chng Prolm Suppos w hv 3 typs of oins 10p 0p 50p Minimum numr of oins to mk 0.8, 1.0, 1.? Grdy mthod
Lrning outoms Undrstnd wht grdy mthod is Al to pply Kruskl s lgorithm to find minimum spnning tr Al to pply Dijkstr s lgorithm to find singlsour shortst-pths Al to pply grdy lgorithm to find solution for Knpsk prolm 3
Grdy mthods How to grdy? At vry stp, mk th st mov you n mk Kp going until you r don Advntgs Don t nd to py muh ffort t h stp Usully finds solution vry quikly Th solution found is usully not d Possil prolm Th solution found my NOT th st on
Grdy mthods - xmpls Minimum spnning tr Kruskl s lgorithm Singl-sour shortst-pths Dijkstr s lgorithm Both lgorithms find on of th BEST solutions Knpsk prolm grdy lgorithm dos NOT find th BEST solution 5
Kruskl s lgorithm
Minimum Spnning tr (MST) Givn n undirtd onntd grph G Th dgs r llld y wight Spnning tr of G tr ontining ll vrtis in G Minimum spnning tr of G spnning tr of G with minimum wight 7
Exmpls Grph G (dg ll is wight) 3 3 1 d Spnning trs of G 3 3 3 1 d d 1 d MST 8
Id of Kruskl's lgorithm - MST min-wight dg nd min-wight dg trs in forst my mrg until on singl tr formd 9
Kruskl s lgorithm - MST 8 7 d 9 11 1 i 7 8 10 h 1 g f Arrng dgs from smllst to lrgst wight (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 10
Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Choos th minimum wight dg (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 11
Kruskl s lgorithm - MST 8 7 d 9 11 1 i 7 8 10 h 1 g f Choos th nxt minimum wight dg (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 1
Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Continu s long s no yl forms (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 13
Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Continu s long s no yl forms (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 1
Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Continu s long s no yl forms (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 15
Kruskl s lgorithm - MST 8 11 h 7 8 7 i 1 g d 1 f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 Continu s long s no yl forms (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 16
Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g 1 (h,i) nnot inludd, othrwis, yl is formd d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 17
Kruskl s lgorithm - MST 8 7 d 9 11 1 i 7 8 10 h 1 g f Choos th nxt minimum wight dg (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 18
Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g 1 (,h) nnot inludd, othrwis, yl is formd d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 19
Kruskl s lgorithm - MST 8 7 d 9 11 1 i 7 8 10 h 1 g f Choos th nxt minimum wight dg (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 0
Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g 1 (f,) nnot inludd, othrwis, yl is formd d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 1
Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g (,h) nnot inludd, othrwis, yl is formd 1 d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn
Kruskl s lgorithm - MST 8 11 h 7 8 i 7 1 g 1 (d,f) nnot inludd, othrwis, yl is formd d f 9 10 (h,g) 1 (i,) (g,f) (,) (,f) (,d) 7 (h,i) 7 (,) 8 (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn 3
Kruskl s lgorithm - MST (h,g) 1 8 7 d (i,) 9 (g,f) 11 1 (,) i 7 (,f) 8 10 (,d) 7 h 1 g f (h,i) 7 (,) 8 MST is found whn ll dgs r xmind (,h) 8 (d,) 9 (f,) 10 (,h) 11 (d,f) 1 itli: hosn
Kruskl s lgorithm - MST Kruskl s lgorithm is grdy in th sns tht it lwys ttmpt to slt th smllst wight dg to inludd in th MST 5
Exris Find MST for this grph 10 3 6 3 10 d 6 f 5 ordr of (dgs) sltion: 6
Psudo od // Givn n undirtd onntd grph G=(V,E) T = nd E = E whil E do gin nd pik n dg in E with minimum wight if dding to T dos not form yl thn dd to T, i.., T = T { } rmov from E', i.., E = E \ { } Tim omplxity? Cn tstd y mrking vrtis 7
Dijkstr s lgorithm
Singl-sour shortst-pths Considr (un)dirtd onntd grph G Th dgs r llld y wight Givn prtiulr vrtx lld th sour Find shortst pths from th sour to ll othr vrtis (shortst pth mns th totl wight of th pth is th smllst) 9
Exmpl Dirtd Grph G (dg ll is wight) is sour vrtx 5 5 d 5 5 5 5 d thik lins: shortst pth dottd lins: not in shortst pth 30
Singl-sour shortst pths vs MST Shortst pths from 5 5 5 Wht is th diffrn twn MST nd shortst pths from? 5 5 d 5 d MST 31
Algorithms for shortst pths Algorithms thr r mny lgorithms to solv this prolm, on of thm is Dijkstr s lgorithm, whih ssums th wights of dgs r non-ngtiv 3
Id of Dijkstr s lgorithm sour hoos th dg lding to vrtx s.t. ost of pth to sour is min Mind tht th dg ddd is NOT nssrily th minimum-ost on 33
Dijkstr s lgorithm Input: A dirtd onntd wightd grph G nd sour vrtx s Output: For vry vrtx v in G, find th shortst pth from s to v Dijkstr s lgorithm runs in itrtions: in th i-th itrtion, th vrtx whih is th i-th losst to s is found, for vry rmining vrtis, th urrnt shortst pth to s found so fr (this shortst pth will updtd s th lgorithm runs) 3
Dijkstr s lgorithm Suppos vrtx is th sour, w now show how Dijkstr s lgorithm works 9 15 1 d 5 0 30 18 11 16 f 6 6 h 19 k 35
Dijkstr s lgorithm Evry vrtx v kps lls: (1) th wight of th urrnt shortst pth from ; () th vrtx lding to v on tht pth, initilly s (, -) 9 15 (, -) 1 d (, -) 5 (, -) 0 30 (, -) 18 11 16 f (, -) 6 6 (, -) h 19 k (, -) 36
(15, (, -) ) (, (9, ) -) (1, (, -) ) Dijkstr s lgorithm For vry nighor u of, updt th wight to th wight of (,u) nd th lding vrtx to. Choos from,, d th on with th smllst suh wight. hosn 9 15 nw vlus 1 d 5 0 30 ing onsidrd (, -) 18 shortst pth 11 16 f (, -) 6 6 (, -) h 19 k (, -) 37
Dijkstr s lgorithm For vry un-hosn nighor of vrtx, updt th wight nd lding vrtx. Choos from ALL un-hosn vrtis (i..,, d, h) th on with smllst wight. 9 nw vlus hosn 15 (15, ) 1 d (9, ) (1, ) 5 0 30 ing onsidrd (, -) 18 shortst pth 11 16 f (, -) 6 6 (33, (, -) ) h 19 k (, -) 38
Dijkstr s lgorithm If nw pth with smllst wight is disovrd,.g., for vrtis, h, th wight is updtd. Othrwis, lik vrtx d, no updt. Choos mong d,, h. 9 15 hosn (15, ) nw vlus 1 d (9, ) (1, ) 5 0 30 ing onsidrd 18 (, (,-) ) shortst pth 11 16 f (, -) 6 6 (33, (3, ) ) h 19 k (, -) 39
Dijkstr s lgorithm Rpt th produr. Aftr d is hosn, th wight of nd k is updtd. Choos mong, h, k. Nxt vrtx hosn is h. 9 15 (15, ) nw vlus 1 d (9, ) (1, ) 5 0 30 ing onsidrd 18 (, (35, ) d) shortst pth 11 16 hosn (3, ) f (, -) 6 6 h 19 k (59, (, -) d) 0
Dijkstr s lgorithm Aftr h is hosn, th wight of nd k is updtd gin. Choos mong, k. Nxt vrtx hosn is. (9, ) 9 (1, ) 1 30 15 5 0 (15, ) d nw vlus ing onsidrd 18 hosn (35, (3, d) h) shortst pth f 11 (, -) 16 (3, ) h 6 19 6 k (51, (59,d) h) 1
Dijkstr s lgorithm Aftr is hosn, th wight of f nd k is updtd gin. Choos mong f, k. Nxt vrtx hosn is f. (9, ) 9 (1, ) 1 30 15 5 0 (15, ) d nw vlus ing onsidrd 18 (3, h) shortst pth hosn f 11 (5, (, -) ) 16 (3, ) h 6 19 6 k (51, (50, h) )
Dijkstr s lgorithm Aftr f is hosn, it is NOT nssry to updt th wight of k. Th finl vrtx hosn is k. (9, ) 9 (1, ) 1 30 15 5 0 (15, ) d nw vlus ing onsidrd 18 (3, h) shortst pth f 11 (5, ) 16 hosn (3, ) h 6 19 6 k (50, ) 3
Dijkstr s lgorithm At this point, ll vrtis r hosn, nd th shortst pth from to vry vrtx is disovrd. (9, ) 9 (1, ) 1 30 15 5 0 (15, ) d nw vlus ing onsidrd 18 (3, h) shortst pth f 11 (5, ) 16 (3, ) h 6 19 6 k (50, )
Exris Shortst pths from (,-) 10 3 (,-) 6 3 10 d (,-) 6 f (,-) (,-) 5 ordr of (dgs) sltion: Compr th solution with slid #6 5
Dijkstr s lgorithm To dsri th lgorithm using psudo od, w giv som nottions Eh vrtx v is llld with two lls: numri ll d(v) indits th lngth of th shortst pth from th sour to v found so fr nothr ll p(v) indits nxt-to-lst vrtx on suh pth, i.., th vrtx immditly for v on tht shortst pth 6
Psudo od // Givn grph G=(V,E) nd sour vrtx s for vry vrtx v in th grph do Tim omplxity? st d(v) = nd p(v) = null st d(s) = 0 nd V T = whil V \ V T do // thr is still som vrtx lft gin hoos th vrtx u in V \ V T with minimum d(u) st V T = V T { u } for vry vrtx v in V \ V T tht is nighor of u do nd if d(u) + w(u,v) < d(v) thn // shortr pth is found st d(v) = d(u) + w(u,v) nd p(v) = u 7
Dos Grdy lgorithm lwys rturn th st solution?
Knpsk Prolm Input: Givn n itms with wights w 1, w,, w n nd vlus v 1, v,, v n, nd knpsk with pity W. Output: Find th most vlul sust of itms tht n fit into th knpsk Applition: A trnsport pln is to dlivr th most vlul st of itms to rmot lotion without xding its pity 9
Exmpl 1 pity = 50 w = 10 v = 60 w = 0 v = 100 w = 30 v = 10 itm 1 itm itm 3 knpsk totl totl sust wight vlu φ 0 0 {1} 10 60 {} 0 100 {3} 30 10 {1,} 30 160 {1,3} 0 180 {,3} 50 0 {1,,3} 60 N/A 50
Grdy pproh pity = 50 w = 10 v = 60 w = 0 v = 100 w = 30 v = 10 itm 1 itm itm 3 knpsk Grdy: pik th itm with th nxt lrgst vlu if totl wight pity. Rsult: itm 3 is tkn, totl vlu = 10, totl wight = 30 itm is tkn, totl vlu = 0, totl wight = 50 itm 1 nnot tkn Tim omplxity? Dos this lwys work? 51
Exmpl pity = 10 w = 7 v = w = 3 v = 1 w = v = 0 w = 5 v = 5 itm 1 itm itm 3 itm knpsk totl totl sust wight vlu φ 0 0 {1} 7 {} 3 1 {3} 0 {} 5 5 {1,} 10 5 {1,3} 11 N/A {1,} 1 N/A totl totl sust wight vlu {,3} 7 5 {,} 8 37 {3,} 9 65 {1,,3} 1 N/A {1,,} 15 N/A {1,3,} 16 N/A {,3,} 1 N/A {1,,3,} 19 N/A 5
Grdy pproh pity = 10 w = 7 v = w = 3 v = 1 w = v = 0 w = 5 v = 5 itm 1 itm itm 3 itm knpsk Grdy: pik th itm with th nxt lrgst vlu if totl wight pity. Rsult: itm 1 is tkn, totl vlu =, totl wight = 7 itm 3 nnot tkn itm nnot tkn itm is tkn, totl vlu = 5, totl wight = 10 not th st!! 53
Grdy pproh v/w = 6 v/w = v/w = 10 v/w = 5 pity = 10 w = 7 v = w = 3 v = 1 w = v = 0 w = 5 v = 5 itm 1 itm itm 3 itm knpsk Grdy : pik th itm with th nxt lrgst (vlu/wight) if totl wight pity. Rsult: itm 3 is tkn, totl vlu = 0, totl wight = itm 1 nnot tkn itm is tkn, totl vlu = 65, totl wight = 9 itm nnot tkn Work for Eg 1? 5
Grdy pproh v/w = 6 v/w=5 v/w = pity = 50 w = 10 v = 60 w = 0 v = 100 w = 30 v = 10 itm 1 itm itm 3 knpsk Grdy: pik th itm with th nxt lrgst (vlu/wight) if totl wight pity. Rsult: itm 1 is tkn, totl vlu = 60, totl wight = 10 itm is tkn, totl vlu = 160, totl wight = 30 itm 3 nnot tkn Not th st!! 55
Lsson Lrnd: Grdy lgorithm dos NOTlwys rturn th st solution