Dinition: GREEDY TECHNIQUE Gry thniqu is gnrl lgorithm sign strtgy, uilt on ollowing lmnts: onigurtions: irnt hois, vlus to in ojtiv untion: som onigurtions to ithr mximiz or minimiz Th mtho: Applil to optimiztion prolms ONLY Construts solution through squn o stps Eh stp xpns prtilly onstrut solution so r, until omplt solution to th prolm is rh. On h stp, th hoi m must Fsil: it hs to stisy th prolm s onstrints Lolly optiml: it hs to th st lol hoi mong ll sil hois vill on tht stp Irrvol: On m, it nnot hng on susqunt stps o th lgorithm NOTE: Gry mtho works st whn ppli to prolms with th gryhoi proprty A glollyoptiml solution n lwys oun y sris o lol improvmnts rom strting onigurtion. Gry mtho vs. Dynmi progrmming mtho: LIKE ynmi progrmming, gry mtho solvs optimiztion prolms. LIKE ynmi progrmming, gry mtho prolms xhiit optiml sustrutur UNLIKE ynmi progrmming, gry mtho prolms xhiit th gry hoi proprty vois ktring. Applitions o th Gry Strtgy: Optiml solutions: Chng mking Minimum Spnning Tr (MST) Singlsour shortst pths Humn os Approximtions: Trvling Slsmn Prolm (TSP) Frtionl Knpsk prolm
Dinition: Spnning Tr Spnning tr is onnt yli sugrph (tr) o th givn grph (G) tht inlus ll o G s vrtis Exmpl: Consir th ollowing grph Th spnning trs or th ov grph r s ollows: Wight (T ) = 8 Wight (T ) = Wight (T ) = 9 Minimum Spnning Tr (MST) Dinition: MST o wight, onnt grph G is in s: A spnning tr o G with minimum totl wight. Exmpl: Consir th xmpl o spnning tr: For th givn grph thr r thr possil spnning trs. Among thm th spnning tr with th minimum wight is th MST or th givn grph Qustion: Why n t w us BRUTE FORCE mtho in onstruting MST? Answr: I w us Brut or mtho Exhustiv srh pproh hs to ppli. Two srious ostls :. Th numr o spnning trs grows xponntilly with grph siz.. Gnrting ll spnning trs or th givn grph is not sy.
MST Applitions: Ntwork sign. Tlphon, ltril, hyruli, TV l, omputr, ro Approximtion lgorithms or NPhr prolms. Trvling slsprson prolm, Stinr tr Clustr nlysis. Ruing t storg in squning mino is in protin Lrning slint turs or rltim vriition Auto onig protool or Ethrnt riging to voi yls in ntwork, t Prim s Algorithm to in minimum spnning tr Som usul initions: Fring g: An g whih hs on vrtx is in prtilly onstrut tr Ti n th othr is not. Unsn g: An g with oth vrtis not in Ti Algorithm: ALGORITHM Prim (G) //Prim s lgorithm or onstruting MST //Input: A wight onnt grph G = { V, E } //Output: ET th st o gs omposing MST o G // th st o tr vrtis n initiliz with ny vrtx V T { v 0 } E T Ø or i to V o rturn E T Fin minimumwight g * = (v*, u*) mong ll th gs (v, u) suh tht v is in V T n u is in V V T V T V T U { u*} E T E T U { *}
Th mtho: STEP : Strt with tr, T 0, onsisting o on vrtx STEP : Grow tr on vrtx/g t tim Construt sris o xpning sutrs T, T, T n. At h stg onstrut T i + rom T i y ing th minimum wight g onnting vrtx in tr (Ti) to on vrtx not yt in tr, hoos rom ring gs (this is th gry stp!) Algorithm stops whn ll vrtis r inlu Exmpl: Apply Prim s lgorithm or th ollowing grph to in MST. 8 Solution: Tr vrtis Rmining vrtis Grph (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, )
(, ) (, ) (, ) (, ) (, ) (, ) Algorithm stops sin ll vrtis r inlu. Th wight o th minimum spnning tr is Eiiny: Eiiny o Prim s lgorithm is s on t strutur us to stor priority quu. Unorr rry: Eiiny: Θ(n ) Binry hp: Eiiny: Θ(m log n) Minhp: For grph with n nos n m gs: Eiiny: (n + m) log n Conlusion: Prim s lgorithm is vrtx s lgorithm Prim s lgorithm Ns priority quu or loting th nrst vrtx. Th hoi o priority quu mttrs in Prim implmnttion. o Arry optiml or ns grphs o Binry hp ttr or sprs grphs o Fioni hp st in thory, ut not in prti
Algorithm: ALGORITHM Kruskl (G) Kruskl s Algorithm to in minimum spnning tr //Kruskl s lgorithm or onstruting MST //Input: A wight onnt grph G = { V, E } //Output: ET th st o gs omposing MST o G Sort E in sning orr o th g wights // initiliz th st o tr gs n its siz E T Ø g_ountr 0 //initiliz th numr o pross gs k 0 whil g_ountr < V k k + i E T U { i k } is yli E T E T U { i k } g_ountr g_ountr + rturn E T Th mtho: STEP : Sort th gs y inrsing wight STEP : Strt with orst hving V numr o trs. STEP : Numr o trs r ru y ONE t vry inlusion o n g At h stg: Among th gs whih r not yt inlu, slt th on with minimum wight AND whih os not orm yl. th g will ru th numr o trs y on y omining two trs o th orst Algorithm stops whn V gs r inlu in th MST i. : whn th numr o trs in th orst is ru to ONE.
Exmpl: Apply Kruskl s lgorithm or th ollowing grph to in MST. Solution: Th list o gs is: Eg Wight 8 Sort th gs in sning orr: Eg Wight 8 8 Eg Wight Insrtion sttus Insrtion orr YES Eg Wight Insrtion sttus Insrtion orr YES 7
Eg Wight Insrtion sttus Insrtion orr YES Eg Wight Insrtion sttus Insrtion orr YES Eg Wight Insrtion sttus NO Insrtion orr Eg Wight Insrtion sttus NO Insrtion orr Eg Wight Insrtion sttus Insrtion orr YES Algorithm stops s V gs r inlu in th MST 8
Eiiny: Eiiny o Kruskl s lgorithm is s on th tim n or sorting th g wights o givn grph. With n iint sorting lgorithm: Eiiny: Θ( E log E ) Conlusion: Kruskl s lgorithm is n g s lgorithm Prim s lgorithm with hp is str thn Kruskl s lgorithm. Som usul initions: Dijkstr s Algorithm to in Singl Sour Shortst Pths Shortst Pth Prolm: Givn onnt irt grph G with nonngtiv wights on th gs n root vrtx r, in or h vrtx x, irt pth P (x) rom r to x so tht th sum o th wights on th gs in th pth P (x) is s smll s possil. Algorithm By Duth omputr sintist Esgr Dijkstr in 99. Solvs th singlsour shortst pth prolm or grph with nonngtiv g wights. This lgorithm is otn us in routing. E.g.: Dijkstr's lgorithm is usully th working prinipl hin linkstt routing protools ALGORITHM Dijkstr(G, s) //Input: Wight onnt grph G n sour vrtx s //Output: Th lngth Dv o shortst pth rom s to v n its pnultimt vrtx Pv or vry vrtx v in V //initiliz vrtx priority in th priority quu Initiliz (Q) or vry vrtx v in V o D v ; P v null // Pv, th prnt o v insrt(q, v, D v ) //initiliz vrtx priority in priority quu s 0 //upt priority o s with s, mking s, th minimum Drs(Q, s, s ) V T 9
or i 0 to V o u* DltMin(Q) //xpning th tr, hoosing th lolly st vrtx V T V T U {u*} or vry vrtx u in V V T tht is jnt to u* o i Du* + w (u*, u) < Du Du Du + w (u*, u); Pu u* Drs(Q, u, Du) Th mtho Dijkstr s lgorithm solvs th singl sour shortst pth prolm in stgs. Stg : A gry lgorithm omputs th shortst istn rom sour to ll othr nos in th grph n svs in t strutur. Stg : Uss th t strutur or ining shortst pth rom sour to ny vrtx v. At h stp, n or h vrtx x, kp trk o istn D(x) n irt pth P(x) rom root to vrtx x o lngth D(x). Sn irst rom th root n tk initil pths P( r, x ) = ( r, x ) with D(x) = w( rx ) whn rx is n g, D(x) = whn rx is not n g. For h tmporry vrtx y istint rom x, st D(y) = min{ D(y), D(x) + w(xy) } Exmpl: Apply Dijkstr s lgorithm to in Singl sour shortst pths with vrtx s th sour. 8 Solution: Lngth Dv o shortst pth rom sour (s) to othr vrtis v n Pnultimt vrtx Pv or vry vrtx v in V: 0
D = 0, P = null D =, P = null D =, P = null D =, P = null D =, P = null D =, P = null Tr vrtis (, 0 ) Rmining vrtis (, ) (, ) (, ) (, ) (, ) Distn & Pth vrtx D = 0 P = D = P = [, ] D = P = null D = P = null D = P = [, ] D = P = [, ] Grph (, ) (, ) (, ) (, + ) (, ) (, ) (, ) (, + ) (, ) (, ) (, 0 ) (, ) D = 0 P = D = P = [, ] D = P = [,,] D = P = null D = P = [, ] D = P = [, ] D = 0 P = D = P = [, ] D = P = [,,] D=0 P = [,,,] D = P = [, ] D = P = [, ] D = 0 P = D = P = [, ] D = P = [,,] D=0 P = [,,,] D = P = [, ] D = P = [, ] (, ) (, 0 ) D = 0 P = D = P = [, ] D = P = [,,] D=0 P = [,,,] D = P = [, ] D = P = [, ] (, 0) Algorithm stops sin no gs to sn
Conlusion: Dosn t work with ngtiv wights Applil to oth unirt n irt grphs Us unorr rry to stor th priority quu: Eiiny = Θ(n ) Us minhp to stor th priority quu: Eiiny = O(m log n) Som usul initions: Humn Trs Co wor: Enoing txt tht ompriss n hrtrs rom som lpht y ssigning to h o th txt s hrtrs som squn o its. This its squn is ll o wor Fix lngth noing: Assigns to h hrtr it string o th sm lngth. Vril lngth noing: Assigns o wors o irnt lngths to irnt hrtrs. Prolm: How n w tll how mny its o n no txt rprsnt ith hrtr? W n us prix r os Prix r o: In Prix r o, no owor is prix o owor o nothr hrtr. Binry prix o : o Th hrtrs r ssoit with th lvs o inry tr. o All lt gs r ll 0 o All right gs r ll o Cowor o hrtr is otin y roring th lls on th simpl pth rom th root to th hrtr s l. o Sin, thr is no simpl pth to l tht ontinus to nothr l, no owor n prix o nothr owor Humn lgorithm: Construts inry prix o tr By Dvi A Humn in 9. Humn s lgorithm hivs t omprssion y ining th st vril lngth inry noing shm or th symols tht our in th il to omprss. Humn oing uss rqunis o th symols in th string to uil vril rt prix o o Eh symol is mpp to inry string o Mor rqunt symols hv shortr os o No o is prix o nothr o Humn Cos or Dt Comprssion hivs 090% Comprssion
Constrution: Stp : Initiliz n onno trs n ll thm with th hrtrs o th lpht. Ror th rquny o h hrtr in its tr s root to init th tr s wight. (Mor gnrlly th wight o tr will qul to th sum o th rqunis in th tr s lvs) Stp : Rpt th ollowing oprtion until singl tr is otin. Fin two trs with smllst wight. Mk thm th lt n right sutr o nw tr n ror th sum o thir wights in th root o th nw tr s its wight Exmpl: Construt Humn o or th ollowing t: Chrtr A B C D proility 0. 0. 0. 0. 0. Eno th txt ABACABAD using th o. Do th txt whos noing is 00000000 Solution: A 0. B 0. C 0. D 0. 0. B 0. D 0. 0. C 0. A 0. C 0. A 0. 0. B D 0. 0. 0.
0. 0. A 0. B 0. D 0. 0. C 0. 0. A 0. 0. 0. B 0. D 0. 0. C 0..0 0 A 0. 0 B 0. 0. 0 D 0. 0. 0 0. 0. C 0. Algorithm STOPS s SINGLE TREE is otin Co wors r: Chrtr A B C D proility 0. 0. 0. 0. 0. Co wor 0 00 0 0 Eno txt or ABACABAD using th o wors: 000000000 Do txt or no txt 00000000 is: BADADA Comput omprssion rtio: Bits pr hrtr = Cowor lngth * Frquny = ( * 0. ) + ( * 0.) + ( * 0. ) + ( * 0. ) + ( * 0. ) =.0 Comprssion rtio is = (.0 )/. 00% =.%