CSC 801- Dsign n Anlysis of Algorithms Ltur 11 Gry Thniqu Exmpl: Chng-Mking Prolm Givn unlimit mounts of oins of nomintions 1 > > m, giv hng for mount n with th lst numr of oins Exmpl: 1 = 25, 2 =10, = 1 Gry solution for n = 8: Gry solution for n = 0: 2 1
Gry Thniqu Construts solution to n optimiztion prolm pi y pi through squn of hois tht r: fsil (stisfis th prolm s onstrints) lolly optiml (st hoi in h stp) irrvol ( hoi n t hng in susqunt stps) For som prolms, yils n optiml solution for vry instn. For most, it os not, ut n usful for fst pproximtions. Applitions of th Gry Strtgy Optiml solutions: hng mking for norml oin nomintions minimum spnning tr (MST) singl-sour shortst pths simpl shuling prolms Huffmn os Approximtions: trvling slsmn prolm (TSP) knpsk prolm othr omintoril optimiztion prolms 5 2
Minimum Spnning Tr (MST) Spnning tr of onnt grph G: onnt yli sugrph of G tht inlus ll of G s vrtis Minimum spnning tr of wight, onnt grph G: spnning tr of G of minimum totl wight Exmpl: 2 1 Prim s MST lgorithm Strt with tr T 1 onsisting of on (ny) vrtx n grow tr on vrtx t tim to prou MST through sris of xpning sutrs T 1, T 2,, T n On h itrtion, onstrut T i+1 from T i y ing vrtx not in T i tht is losst to thos lry in T i (this is gry stp!) Stop whn ll vrtis r inlu 7
Tring Prim s MST lgorithm 2 1 5 8 Prim s Implmnttion: Priority Quu Tr vrtis (-,-) Rmining vrtis (,2) (,) (-, ) (-, ) 2 1 5 nrst tr vrtx g wight 9
Nots out Prim s lgorithm Proof y inution tht this onstrution tully yils MST Ns priority quu for loting losst fring vrtx Effiiny O(n 2 ) for wight mtrix rprsnttion of grph n rry implmnttion of priority quu O(m log n) for jny list rprsnttion of grph with n vrtis n m gs n min-hp implmnttion of priority quu 10 Anothr gry lgorithm for MST: Kruskl s Sort th gs in nonrsing orr of lngths Grow tr on g t tim to prou MST through sris of xpning forsts F 1, F 2,, F n-1 On h itrtion, th nxt g on th sort list unlss this woul rt yl. (If it woul, skip th g.) 11 5
Kruskl s Implmnttion Tr gs Sort list of gs 5 2 1 12 Nots out Kruskl s lgorithm Algorithm looks sir thn Prim s ut is hrr to implmnt (hking for yls!) Cyl hking: yl is rt iff g onnts vrtis in th sm onnt omponnt Union-fin lgorithms s stion 9.2 Effiiny: O(m log m) for grph with m gs 1
Minimum spnning tr vs. Stinr tr 1 1 1 vs 1 1 Applition of Gry Strtgy Singl-Sour Shortst Pths 7
Exmpls Sour = 5 2 1 Sour = 5 2 1 1 Shortst pths Dijkstr s lgorithm Singl Sour Shortst Pths Prolm: Givn wight onnt grph G, fin shortst pths from sour vrtx s to h of th othr vrtis Dijkstr s lgorithm: Similr to Prim s MST lgorithm, with iffrnt wy of omputing numril lls: Among vrtis not lry in th tr, it fins vrtx u with th smllst sum v + w(v,u) whr v is vrtx for whih shortst pth hs n lry foun on pring itrtions (suh vrtis form tr) v is th lngth of th shortst pth form sour to v w(v,u) is th lngth (wight) of g from v to u 17 8
Dijkstr s Algorithm 7 2 5 Tr vrtis (-,0) Rmining vrtis prnt istn to root 18 Dijkstr s Algorithm 7 2 5 Tr vrtis Rmining vrtis (-,0) (,) (-, ) (,7) (-, ) (,) (,+) (,+2) (-, ) (,5) (,7) (,5+) 2 5 7 2 5 7 2 5 7 (,7) (,9) 2 5 7 (,9) 9
20 Nots on Dijkstr s lgorithm Dosn t work for grphs with ngtiv wights Applil to oth unirt n irt grphs Effiiny sm s Prim s: O( V 2 ) for grphs rprsnt y wight mtrix n rry implmnttion of priority quu O( E log V ) for grphs rprsnt y j. lists n min-hp implmnttion of priority quu Don t mix up Dijkstr s lgorithm with Prim s lgorithm! 21 10
Summry 22 Applition of Gry Strtgy Huffmn Trs n Cos 11
Prfix-Fr Cos Coing: ssignmnt of it strings to lpht hrtrs Cowors: it strings ssign for hrtrs of lpht Two typs of os: fix-lngth noing (.g., ASCII) vril-lngth noing xmpl: Symol... Aritrry o Prfix-fr o Prfix-fr os: no owor is prfix of nothr owor 2 Coing Prolm Try this: using prfix-fr os, no th txt Coing Prolm: If frqunis of th hrtr ourrns r known, wht is th st inry prfix-fr o? 25 12
Huffmn os Any inry tr with gs ll with 0 s n 1 s yils prfix-fr o of hrtrs ssign to its lvs Optiml inry tr minimizing th xpt (wight vrg) lngth of owor n onstrut s follows Huffmn s lgorithm Initiliz n on-no trs with lpht hrtrs n th tr wights with thir frqunis. Rpt th following stp n-1 tims: join two inry trs with smllst wights into on (s lft n right sutrs) n mk its wight qul th sum of th wights of th two trs. Mrk gs ling to lft n right sutrs with 0 s n 1 s, rsptivly. 2 Exmpl hrtr A B C D _ frquny 0.5 0.1 0.15 27 1
Exmpl hrtr A B C D _ frquny 0.5 0.1 0.15 0.1 B 0.15 _ C D 0.5 A owor 11 100 00 01 101 C D 5 0.5 A vrg its pr hrtr: 2*0.5+*0.1+2*+2*+*0.15 = 2.25 for fix-lngth noing: omprssion rtio: (-2.25)/*100% = 25% 0.1 B 5 0.15 _ 0.1 B 0.5 A 0.15 _ C 0. D 1.0 0. 0. 0 1 0. 0 1 0 5 C D 0 1 0. 1 0.5 A C D 5 0.1 0.15 B _ 0.5 A 0.1 B 0.15 _ 28 Homwork Ring: Chptr 9 Exriss 9.1:, 5, 9 9.2: 1,, 5, 11 9.: 2,, 7 9.: 1, 29 1