Lecture 3 - Analyzing Algorithms

Lecture 3 - Analyzing Algorithms - Analyzing...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Analyzing Iterative (Non-recursive) and Recursive Algorithms
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Analyzing Iterative Algorithms -I- Simple statement sequence: s 1 ; s 2 ; …. ; s k (1) as long as k is constant Simple loops: for(i = 1; i n ; i ++ ) { s; } where s is in (1) – Time complexity is 1 = n is in (n) Nested loops: for(i = 1; i n ; i ++ ) for(j = 1; j n ; j ++ ) { s; } – Complexity is 1 = n = n 2 is in (n 2 ) i = 1 n i = 1 n j = 1 n i = 1 n
Background image of page 2
Loop index depends on outer loop index: for(i = 1; i n; i ++ ) for(k = 1; k i; k ++ ) { s; } – Complexity is 1 = i = n(n + 1)/2 is in (n 2 ) T(n) = 1 + 2 + 3 + …. + n = n (n + 1) / 2 Loop index doesn’t vary linearly: h = 1; while ( h n ) { s; h = 2 x h } – h takes values 1, 2, 4, … until it exceeds n ( n = 2 k ) – h takes values 2 0 ,2 1 ,2 2 ,…,2 k there are k + 1 iterations – k = log n so 1 + log n iterations is in (log n) Analyzing Iterative Algorithms -II- i = 1 n k = 1 i i = 1 n Assume n is a power of 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
• Time complexity of following algorithm: for (i = 1; i n; i ++ ) { j = n; while (j 1) { s; j = j/2; } } Inner loop runs for values of j: (n = 2 k ), 2 k-1 , …, 2 1 , 2 0 , there are k + 1 iterations where k = lg n. Since the outer loop runs n times, the complexity is T(n) = n (1 + lg n). T(n) = (k + 1) = k + 1 = n k + n = n lgn + n = n (lgn + 1) Analyzing Iterative Algorithms: Examples -I- i = 1 n i = 1 n i = 1 n
Background image of page 4
• Time complexity of following algorithm: i = n; while (i 1) { j = i; while (j n) { s; j = 2 x j; } i = i/2; } Outer loop runs for values of i: (n = 2 k ), 2 k-1 , …, 2 1 , 2 0 , there are k + 1 iterations where k = lg n. T(n) = 1 + 2 + 3 + + (k + 1) = (k + 1) (k + 2) / 2 Analyzing Iterative Algorithms: Examples -II- The inner while loop begins with j = i. j doubled each time until greater than n. 1 st time j is doubled only once (2 k to 2 k+1 ). 2 nd time, j doubled twice (2 k-1 , 2 k to 2 k+1 ). 3 rd time through, j is doubled 3 times. Etc. The last time through, which is the (k+1) st time through, j is doubled k+1 times.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
• Time complexity of following algorithm: i = n; while(i 1) { for (j = 1; j i; j ++ ) { s; } i = i / 2; } Outer loop runs for values of i: (n = 2 k ), 2 k-1 , …, 2 1 , 2 0 , there are k + 1 iterations where k = lg n. T(n) = n/2 i = n 1/2 i = n (2 - (1/2 k )) = 2n - 1 is in (n) Analyzing Iterative Algorithms: Examples -III- i = 0 k i = 0 k The inner for loop is executed i times. 1 st time it is executed n times. 2 nd time it is executed n/2 times. 3 rd time it is executed n/4 times. Etc. Last time it is executed (1=n/2 k ) times.
Background image of page 6
• Time complexity of the following algorithm: for (i = 1; i n; i ++ ) { m = n/i; for (j = 1; j m; j ++ ) { s; } } Outer loop run n times Inner loop executed n, n/2, n/3, … , n/n times T(n) <= n + n/2 + n/3 + + n/n T(n) <= n/i = n 1/i = n (log n) is in (n log n)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 37

Lecture 3 - Analyzing Algorithms - Analyzing...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online