2c03-review - 00030

2c03-review - 00030 - begin for i:=1 to n do if i mod 3 = 1...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
4 b.[5] procedure P2(n:integer); var i,j,k : integer; begin for i:=1 to 100000000 do for j:=i-1 to 20*n do for k:=1 to j do {some statement requiring O(1) time} end T(n) = Σ 100000000 i=1 T’ = Σ 100000000 i=1 ( Σ 20n j=i-1 T’’) = Σ 100000000 i=1 ( Σ 20n j=i-1 ( Σ j k=1 O(1))) = Σ 100000000 i=1 ( Σ 20n j=i-1 O(j)) = Σ 100000000 i=1 O((20n-i+2)*(20n+i-1)) = Σ 100000000 i=1 O((20n-i)*(20n+i)) = Σ 100000000 i=1 O(400n 2 -i 2 ) = O(400n 2 - Σ 100000000 i=1 i 2 ) = O(n 2 ) Now we have only two nested loops since “for i:=1 to 100000000” is a constant. c.[5] procedure P3(n:integer); var i,j,x,y : integer;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: begin for i:=1 to n do if i mod 3 = 1 then begin for j:=i to n do x:=x+1; for j:=20000 downto n do x:=x+y; for j:=1 to i do y:=y+1 end end T(n) = n i=1 T = n i=1 ( n j=i O(1) + 20000 j=n O(1) + i j=1 O(1))/3 = n i=1 (O(n-i+1) +O(i)) = O(n(n+1)/2) + O(n(n+1)/2) = O(n 2 ) Only two level of nesting, each with O(n). d.[15] function F(n:integer):integer; begin if n<=1 then return(1) else return(F(n-1)+F(n div 2)) end We have to solve the following recurrence equation....
View Full Document

Ask a homework question - tutors are online