complexity
play

Complexity Divide a program in basic blocks and count the - PDF document

6/7/10 Complexity Divide a program in basic blocks and count the order of magnitude number of basic block CS420 lecture four execu6ons Recurrence


  1. 6/7/10 ¡ Complexity ¡ • Divide ¡a ¡program ¡in ¡basic ¡blocks ¡and ¡count ¡the ¡ order ¡of ¡magnitude ¡ number ¡of ¡basic ¡block ¡ CS420 ¡lecture ¡four ¡ execu6ons ¡ Recurrence ¡Rela6ons ¡ – basic ¡block : ¡code ¡fragment ¡that ¡takes ¡constant ¡ 6me ¡to ¡execute ¡ wim ¡bohm, ¡cs.colostate ¡ – careful : ¡some ¡language ¡constructs ¡may ¡take ¡more ¡ than ¡constant ¡6me ¡ • contains ¡in ¡Java ¡ • + ¡in ¡APL ¡/ ¡C ++ ¡ • paMern ¡matching ¡in ¡SNOBOL ¡/ ¡Python ¡ ¡ Recurrence ¡Rela6ons ¡ Repeated ¡subs6tu6on ¡ • Simple ¡recurrence ¡rela6ons ¡(one ¡recurrent ¡ term ¡in ¡the ¡rhs) ¡can ¡some6mes ¡be ¡solved ¡ • The ¡complexity ¡of ¡an ¡algorithm ¡can ¡oRen ¡be ¡ using ¡ repeated ¡subs3tu3on ¡ expressed ¡in ¡a ¡ recurrence ¡rela3on ¡ with ¡the ¡ • Two ¡types: ¡ ¡ Linear ¡and ¡ DivCo ¡ input ¡size ¡as ¡parameter. ¡Eg ¡(merge ¡sort) ¡ – Linear ¡F(n) ¡= ¡aF(n-­‑d)+g(n), ¡base: ¡F(1)=v 1 ¡ – Divco ¡ ¡F(n)= ¡aF(n/d)+g(n), ¡base: ¡F(1)=v 1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡T(n) ¡= ¡2T(n/2)+n ¡ • Two ¡ques6on: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡T(1) ¡= ¡1 ¡ – what ¡is ¡the ¡paMern ¡ – how ¡oRen ¡is ¡it ¡applied ¡un6l ¡we ¡hit ¡the ¡base ¡case ¡ 1 ¡

  2. 6/7/10 ¡ Linear ¡Example ¡ DivCo ¡example ¡ ¡ ¡ ¡Hanoi: ¡ ¡M(n)=2M(n-­‑1)+1, ¡ ¡M(1)=1 ¡ Merge ¡sort: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡T(n) ¡= ¡2T(n/2) ¡+ ¡n, ¡ ¡T(1)=1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡n ¡= ¡2 k ¡ ¡ ¡ ¡ ¡ ¡ ¡M(n) ¡= ¡2M(n-­‑1)+1 ¡= ¡2(2M(n-­‑2)+1)+1 ¡= ¡4M(n-­‑2)+2+1 ¡= ¡ ¡ ¡ ¡ ¡T(n)=2(2(T(n/4)+n/2)+n ¡= ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡4(2M(n-­‑3)+1)+2+1= ¡8M(n-­‑3)+4+2+1= ¡... ¡ ¡ ¡ ¡ ¡4T(n/4) ¡+ ¡2n ¡= ¡8T(n/8) ¡+ ¡3n ¡... ¡= ¡ ¡2 k T(n/2 k )+kn ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡2 k M(n-­‑k)+2 k-­‑1 +2 k-­‑2 +...+2+1= ¡ hit ¡base ¡for ¡k ¡= ¡logn ¡ ¡ ¡hit ¡base ¡for ¡k ¡= ¡n-­‑1: ¡ ¡ ¡ ¡= ¡2 k T(n/2 k )+kn ¡= ¡ ¡n+kn ¡= ¡O(nlogn) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2 n-­‑1 M(1)+2 n-­‑1 +2 n-­‑2 +...+2+1 ¡= ¡2 n -­‑1 ¡ Another ¡one: ¡binary ¡search ¡ DivCo ¡repeated ¡subs6tu6on ¡paMern ¡ n ¡= ¡b k ¡ f(n)= ¡af(n/b)+g(n) ¡= ¡ ¡a(af(n/b 2 )+g(n/b))+g(n) ¡= ¡ Let ¡n ¡= ¡2 k ¡ ¡ ¡and ¡f(1)=1 ¡ ¡and ¡ ¡f(n) ¡= ¡f(n/2)+c ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a 2 f(n/b 2) +ag(n)+g(n) ¡= ¡a 2 (af(n/b 2) +g(n)+ag(n)+g(n) ¡= ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a 3 f(n/b 3 )+a 2 g(n/b 2 )+ag(n/b)+g(n) ¡= ¡... ¡ f(n)=f(n/2)+c ¡= ¡f(n/4)+2c ¡= ¡ ¡ k − 1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f(n/8)+3c ¡= ¡f(n/2 k )+kc ¡= ¡ ¡ f ( n ) = a k f ( n / b k ) + ∑ a i g ( n / b i ) hit ¡base ¡for ¡k=lg ¡n: ¡ i = 0 n = b k , so ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡f(1)+lgn.c ¡= ¡O(lg ¡n) ¡ k − 1 f ( n ) = a k f (1) + ∑ a i g ( n / b i ) i = 0 2 ¡

  3. 6/7/10 ¡ Linear ¡repeated ¡subs6tu6on ¡paMern ¡ Master ¡Method ¡ Cookbook ¡approach ¡to ¡solu6on, ¡recognizing ¡the ¡ ¡ n ¡= ¡kb+1 ¡ form. ¡Cormen ¡et.al.'s ¡method ¡is ¡more ¡complex ¡ ¡ f(n)= ¡af(n-­‑b)+g(n) ¡= ¡a(f(n-­‑2b)+g(n-­‑b)+g(n)= ¡ than ¡Rosen: ¡ a 2 f(n-­‑2b)+ag(n-­‑b)+g(n) ¡=a 2 (af(n-­‑3b)+g(n))+ag(n-­‑b)+g(n)= ¡ ¡ ¡ ¡ ¡ ¡A n ¡ = ¡C ¡ A n/d +kn p ¡ a 3 f(n-­‑3b)+a 2 g(n-­‑2b)+ag(n-­‑b)+g(n) ¡= ¡... ¡ – A n ¡= ¡O(n p ) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡C ¡< ¡d p ¡ ¡ ¡ ¡ ¡eg ¡ ¡ ¡A n ¡ = ¡3 ¡ A n/2 +n 2 ¡ k − 1 f ( n ) = a k f ( n − kb ) + ∑ – A n ¡= ¡O(n p log(n)) ¡if ¡C ¡= ¡d p ¡ ¡ ¡ ¡ ¡eg ¡ ¡A n ¡ = ¡2A n/2 +n ¡ a i g ( n − ib ) – A n ¡= ¡O(n logdc ) ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡C ¡> ¡d p ¡ ¡ ¡ ¡ ¡eg ¡ ¡A n ¡ = ¡3 ¡ A n/2 +n ¡ i = 0 k − 1 f ( n ) = a k f (1) + ∑ a i g ( n − ib ) Proof ¡based ¡on ¡repeated ¡subs6tu6on ¡plus ¡induc6on ¡ i = 0 Let's ¡prove ¡a ¡simpler ¡version ¡ App: ¡mul6plying ¡two ¡n ¡digit ¡numbers ¡ Let ¡f(n)=af(n/b)+c, ¡n=b k , ¡ ¡ ¡then ¡ f ( n ) = O ( n log b a ) if a > 1 ¡ ¡ ¡A ¡x ¡B ¡ ¡= ¡ ¡ ¡a n-­‑1 a n-­‑2 ..a 1 a 0 ¡ ¡X ¡ ¡ ¡b n-­‑1 b n-­‑2 ..b 1 b 0 ¡ proof: ¡ f ( n ) = O (log n ) if a = 1 • high ¡school ¡method: ¡ ¡O(n 2 ) ¡ ¡ k − 1 f ( n ) = a k f (1) + ∑ a j c from repeated subst • divide ¡and ¡conquer ¡approach ¡ i = 0 k − 1 ∑ ¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡= ¡A 1 10 n/2 +A 2 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ B ¡= ¡B 1 10 n/2 +B 2 ¡ case a = 1: f ( n ) = f (1) + c 1 i = 0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ AxB ¡= ¡A 1 B 1 10 n +(A 1 B 2 +A 2 B 1 )10 n/2 + ¡A 2 B 2 ¡ k = lg b n so f ( n ) = f (1) + c log b n = a k − 1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ A 1 B 2 +A 2 B 1 ¡ = ¡(A 1 +A 2 )(B 1 +B 2 )-­‑A 1 B 1 -­‑A 2 B 2 ¡ k − 1 ∑ a i case a > 1: geometric progression a − 1 ¡ ¡ ¡ ¡ ¡ ¡ ¡so ¡we ¡ only ¡need ¡3 ¡n/2 ¡digit ¡mul3plica3ons : ¡ ¡ ¡ ¡ i = 0 f ( n ) = a k f (1) + c a k − 1 c c a − 1 = C 1 a k + C 2 a − 1 = a k ( f (1) + a − 1) − ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡A 1 B 2 , ¡A 2 B 1 ¡ ¡and ¡(A 1 +A 2 )(B 1 +B 2 ) ¡ because a k = a log b n = n log b a : f ( n ) = C 1 n log b a + C 2 3 ¡

  4. 6/7/10 ¡ Complexity ¡of ¡DivCo ¡n ¡digit ¡mul6ply ¡ Complexity ¡of ¡DivCo ¡n ¡digit ¡mul6ply ¡ ¡ ¡T n =3T n/2 +O(n) ¡ ¡ ¡T n =3T n/2 +O(n) ¡ Look ¡up ¡in ¡master ¡table: ¡ Look ¡up ¡in ¡master ¡table: ¡ ¡ ¡ ¡ ¡ ¡T n =O(n lg3 )~O(n 1.6 ) ¡ Similar ¡trick ¡for ¡ ¡matrix ¡mul6ply ¡ Block ¡matrix ¡mul6ply ¡ ¡ ¡A 1,1 ¡ ¡ ¡A 1,2 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ B 1,1 ¡ ¡ ¡B 1,2 ¡ n C ¡= ¡AxB, ¡ ¡ ∑ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡* ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡ C i , j = A i , k * B k , j simple ¡O(n 3 ) ¡code ¡ k = 1 ¡ ¡A 2,1 ¡ ¡A 2,2 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ B 2,1 ¡ ¡B 2,2 ¡ ¡ ¡ ¡for ¡i=1 ¡to ¡n ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡j ¡= ¡1 ¡to ¡n ¡ ¡A 1,1 ¡B 1,1 ¡+ ¡A 1,2 ¡B 2,1 ¡ ¡ ¡ ¡ ¡ ¡A 1,1 ¡B 1,2 ¡+ ¡A 1,2 ¡B 2,2 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C[i,j]=0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡k ¡= ¡1 ¡to ¡n ¡ ¡C[i,j]+=A[i,k]*B[k,j] ¡ ¡ ¡A 2,1 ¡B 1,1 ¡+ ¡A 2,2 ¡B 2,1 ¡ ¡ ¡ ¡ ¡ ¡A 2,1 ¡B 1,2 ¡+ ¡A 2,2 ¡B 2,2 ¡ ¡ ¡ ¡ ¡ 4 ¡

  5. 6/7/10 ¡ naive ¡block ¡matmult ¡ naive ¡block ¡matrix ¡mul6ply ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡T(n) ¡= ¡8T(n/2) ¡+ ¡O(n 2 ) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡T(n) ¡= ¡8T(n/2) ¡+ ¡O(n 2 ) ¡ master ¡table: ¡ master ¡table: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡T(n) ¡= ¡O(n log8 )=O(n 3 ) ¡ Linear ¡homogeneous ¡recurrence ¡ Strassen ¡ rela6ons ¡(source: ¡Rosen ¡chapter ¡7) ¡ • a n ¡= ¡c 1 .a n-­‑1 ¡+ ¡c 2 .a n-­‑2 ¡+ ¡.. ¡+ ¡c r .a n-­‑r ¡ Strassen ¡shows ¡that ¡we ¡only ¡need ¡7 ¡block ¡mul6plies ¡ ¡ ¡ ¡ ¡ ¡ ¡a 0 ¡= ¡v 0 , ¡.. ¡, ¡a r-­‑1 ¡= ¡v r-­‑1 ¡: ¡ini6al ¡values ¡ (plus ¡O(n 2 ) ¡ ¡work: ¡matrix ¡adds/subtracts ¡) ¡ ¡ • Guess ¡individual ¡solu6ons ¡of ¡form ¡ α n ¡ ¡ see ¡notes ¡ – give ¡rise ¡to ¡ characteris3c ¡equa3on ¡ – solve ¡to ¡obtain ¡ characteris3c ¡roots ¡ • General ¡solu3on ¡ T(n) ¡= ¡ 7 T(n/2) ¡+ ¡O(n 2 ) ¡ – linear ¡combina6on ¡of ¡roots ¡ • Specific ¡solu3on ¡ master ¡table: ¡ ¡T(n) ¡= ¡O(n lg7 )~O(n 2.81 ) ¡ – find ¡coefficients ¡of ¡general ¡solu6on ¡using ¡ini6al ¡values ¡ 5 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend