V R N N N R f ] R S Itroductio to Algorithms September 12, 2003 Massachusetts Istitute of echology 6046J/18410J rofessors Shafi Goldwasser ad Silvio Micali Hadout 7 roblem Set 1 Solutios roblem 1-1 Recurrece Relatios Solve the followig recurreces Give a boud for each problem If you are uable to fid a boud, provide as tight upper ( or ) ad lower ( or ) bouds as you ca fid Justify your aswers You may assume that (a)! #"%$'&() - By part 1 of the Master Method (b) *,+-!!/1032 /4032* - By part 3 of the Master Method (c) 6, 6 87: ; < Substitute = for he resultig relatio is =>?@ BA C;= Clearly, this is => ad thus 6 (d) *D EF G H ( 1 1 1 3 + 4 + ) ( 1 1 1 3 + 4 + ) 2 NN I G %J K M M M M M M M M M M M MM R R R R 16 2 R S S S S S S S x S x16 S x2 S S S S S S 16x 16x16 16x2 2x 2x16 2x2 he figure above shows the recursio tree that helps us i guessig a solutio We have that U #"%$XW BY \^] a3bdc e \^] a3bdc g ïhj ( H gk l hus we guess that lm * ad we prove it by substitutio Assume that =>UDop = for a appropriate costat o ad for all =rqs he we have that
v ( \ 2 Hadout 7: roblem Set 1 Solutios t v ou sou a sou 3w a3b ox, c {z o yh a3b o h c o a-b ~} for o {z Hece, * the other had by ispectio we have g ƒ % * Hece l % (e) D hk{ * ˆ 87:? j (f) *D /1032s lš * (g) /40323 :* - By ispectio ad substitutio G k*œ & Œ /4032* - By part 2 of the Master Method roblem 1-2 Asymptotic Notatio Rak the followig fuctios by order of growth; that is, fid a arragemet }' }pžpžpž }' of the H fuctios satisfyig,,,, E k H artitio your list ito equivalece classes such that x ad are i the same class if ad oly if x* *' (he fuctio /40-2 is discussed o pages -6 of CRS) G 4 # & z ]%]%]%] z /4032! #"%$8 #"%$ /4032* #"%$ š/4032,{b /1032dC œ he followig are ordered asymptotically from smallest to largest, are as follows (two fuctios, ad are o the same lie if x *X ): ]%]%]%] z
Hadout 7: roblem Set 1 Solutios 3 /4032 š/4032 /4032d ž G 1 # & /40-2 #"%$ #"%$3 #"%$ z roblem 1-3 Sieve of Eratosthees DB he Sieve of Eratosthees, iveted circa 200 BC, is a algorithm to fid all prime umbers betwee 2 ad a iput umber Ÿ he algorithm works as follows: we begi with a list of all itegers from 2 to Ÿ For each = Ÿ, we cross out (ie mark as composite) each multiple of = (> = for @ ) that is less tha or equal to Ÿ Whe this process termiates, oly the prime umbers betwee 2 ad Ÿ are umarked
4 Hadout 7: roblem Set 1 Solutios Below, we give pseudocode for this algorithm At the begiig of the algorithm, every etry i the array is iitialized to true, ie is true for all } ˆŸ At the ed of the algorithm, is true iff is prime ERASHENES-SIEVE Ÿ : 1 et for all from 2 to Ÿ!ª «2 for = to Ÿ : 3 4 while y p= kÿ : l # {= `±6²! 6 D 7 edwhile 8 edfor he table below shows the values of the array elemets ³ŽpŽpŽXŸ (lie 7) durig a executio of the algorithm ru o iput Ÿ µ{z at the ed of each while loop i #¹ º #» º ¼ ½pº #¾ º # º ¼ ÁÀBº # º #à º ¼ œä)åæº œä{ä'º œä¹ º œä» º 1 2 F F F F F 3 F F F F F F 4 F F F F F F F F F F F F 13 F F F F F F rove that the ERASHENES-SIEVE algorithm is correct; that is, prove that upo termiatio, is true iff is prime Hit: You ca use the followig pre-coditio ad post-coditio ad you ca prove the suggested loop ivariat for the for loop (lie 2) et Ç is true iff is prime Ç Ç re-coditio: ŸÈ, ost-coditio: }ŠÉ such that ~ oop Ivariat: } É such that DŸ D= represet the statemet: l 6 We will use the give loop-ivariat for the for loop (lie 2) ad the relevat pre- ad postcoditios to prove the correctess of the algorithm We will ow prove the give loop ivariat Namely, we will prove by iductio that whe = is assiged the value (lie 2), Ç holds for all } Whe = is assiged the value 2, Ê is true sice it was iitialized to be true hus, we have proved that before we execute the for loop the first time, the base case of the loop ivariat holds
Hadout 7: roblem Set 1 Solutios Now we will assume that whe = use this iductive hypothesis to prove that whe = })~, is assiged the value, Ç holds for all }g is assiged the value µ, Ç We will holds for all et s cosider a executio of the while loop with =r Sice goes from to ËŸ =Ì, y = is always at least Sice oly elemets with ˆy {= are (re)set to false, o elemet l 6 6 with will chage value hus, Ç for }, will hold at the ed of the executio of the while loop It remais to show that Ç holds at the ed of the executio of the while loop If Í is true, the it caot have ay divisor Î such that ÏÎ the it does ot have a divisor ÎÐ ivariat is correct So k must be prime If k is prime, hus, it must be true hus, we have proved that the loop Whe =mƒÿ, o the fial executio of the for loop, we have that Ç due to the correctess of the loop ivariat holds for all, ƒÿ,