Algorithm analysis examples

618 views
475 views

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
618
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
40
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Algorithm analysis examples

  1. 1. Algorithm Analysis E lExamples Muhammad Shoaib Farooq 1M.Shoaib Farooq
  2. 2. Complexity of Algorithm - Each instruction executes within const timetime e.g. Assignment Statement x=a ....................1x a ....................1 x= a+b*c/h-u ......1 a>b 1a>b ......................1 M.Shoaib Farooq 2
  3. 3. If Statement Complexity= ConstTime(bool exp) + max(ThanPart /else Part)max(ThanPart /else Part) e g if (a>b) than 1e.g. if (a>b) than .........1 a=2......................1 elseelse x=x+2*3 .................1 T(n)= 1+1 = 2 = O(1) Or O(c) M.Shoaib Farooq 3
  4. 4. If StatementIf Statement e.g.g if (a>b) than……..........1 a=2.......................1 b b+3*t 1b= b+3*t...............1 else x=x+2*3 ..............1x x 2 3 ..............1 T(n)= 1+2 = 3 = O(1) M.Shoaib Farooq 4
  5. 5. LOOP For n complete iterations - while ............n - for loop ..........n+1 - repeat..until ....n M.Shoaib Farooq 5
  6. 6. An Example: LOOP i=0 .............................1 while i<=n ..................n i++ n-1i++ ........................n-1 a=i ...........................1 T(n)=1+n+n-1+1( ) = 2n+1 = O(n)= O(n) M.Shoaib Farooq 6
  7. 7. An Example: LOOP i=1 ...........................1 while i<=n nwhile i<=n ................n i++ ........................1 a=i ............................1 T (n)= 1+n+1(n-1)+1 = 2n+1 = o(n)( ) ( ) ( ) OR T(n) = 1+ +1∑ n 1 T(n) = 1+ +1 = 1+ n+1 ∑=i 1 M.Shoaib Farooq = O(n) 7
  8. 8. An Example: LOOPp i=1 ...............................1 while (i<=n) nwhile (i<=n)....................n a=2+g........................n-1 i i+1 1i=i+1 ............. ..........n-1 if (i<=n)........... .............1 2 1a=2 ..........................1 else a=3............................1 T(n) = 1+ n+n-1+n-1+1+1 =3n+1 = O(n) M.Shoaib Farooq 8
  9. 9. An Example: LOOP i=1..............................1 while (i<=10)................10 i i 1 9i=i+1........................9 i=1 ..............................1 while (i<=n) nwhile (i<=n)..................n a=2+g......................n-1 i=i+1 .......................n-1 if (i<=n)........................1 a=2 ..........................1 T ( n) = ? else a=3...........................1 M.Shoaib Farooq 9
  10. 10. Linear Search LinearSearch(A, Key) i 1 1 while (A[i] !=key) and (i<=length[A]) i 1 n n-1 i 1 if ( i<=length[A]) 1 return true else 1 return false 1 M.Shoaib Farooq T(n) = 1 + n + n-1 + 1 +1 = 2n+2 10
  11. 11. Linear Search- Best-caseLinear Search Best case Best case occurs when key element occurs at first location of Array then loop executes only once T(n) = 1 + n + (n-1) + 1 +1T(n) 1 n (n 1) 1 1 = 1+1+0+1+1 O(1)= O(1) M.Shoaib Farooq 11
  12. 12. Linear Search- Worst-caseLinear Search Worst case Worst case occurs when key element is the last element in array or is not there at all loop executes n times T(n) = 1 + n + (n-1) + 1 +1 = 2n +22n 2 = O(n) M.Shoaib Farooq 12
  13. 13. Linear Search- Average-caseLinear Search Average case Key element is equally likely to occur at any position in the array. The number of comparisons can be any of the numbers 1,2,3…..n and each number occurs with probability p=1/n T(n) = 1.1/n + 2. 1/n +……….+ n.1/n = (1+2+3+……+n).1/n(1 2 3 …… n).1/n = [n(n+1)/2] 1/n = n+1/2 O( )= O(n) M.Shoaib Farooq 13
  14. 14. Binary Search BinarySearch(A, key) 1. mid (low + high)/2 2. while (A[mid]!=key) and ( low<=high) 3. if (key>A[mid]) 4 low mid +14. low mid +1 5. else 6 high mid - 16. high mid 1 7. mid (low + high)/2 8. if (low<=high) T(n)=1+k+(k-1)+(k-1)+(k-1)+1+1 4K 4 l O(l ) ( g ) 9. return true 10. else = 4K= 4 log2n O(log2n) n/2 + n/4 + n/8 + n/2k M.Shoaib Farooq 11. return false n/2 + n/4 + n/8 ……..+ n/2k 2K<=n k=log2n 14
  15. 15. An Example: Insertion SortAn Example: Insertion Sort InsertionSort(A, n)( , ) 1. for i = 2 to n 2. key = A[i] 3. j = i – 1 4. while (j > 0) and (A[j] > key) j 1 j5. A[j+1] = A[j] 6. j = j – 1 7 A[j+1] = key7. A[j+1] = key 15M.Shoaib Farooq
  16. 16. An Example: Insertion SortAn Example: Insertion Sort 30 10 40 20 i = ∅ j = ∅ key = ∅ A[j] = ∅ A[j+1] = ∅ InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = ∅ A[j+1] = ∅ for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 16M.Shoaib Farooq
  17. 17. An Example: Insertion Sort 30 10 40 20 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 10 An Example: Insertion Sort InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 10 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 17M.Shoaib Farooq
  18. 18. An Example: Insertion Sort 30 30 40 20 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 30 An Example: Insertion Sort InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 30 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 18M.Shoaib Farooq
  19. 19. An Example: Insertion SortAn Example: Insertion Sort 30 30 40 20 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 30 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 30 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 19M.Shoaib Farooq
  20. 20. An Example: Insertion SortAn Example: Insertion Sort 30 30 40 20 i = 2 j = 0 key = 10 A[j] = ∅ A[j+1] = 30 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = ∅ A[j+1] = 30 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 20M.Shoaib Farooq
  21. 21. An Example: Insertion SortAn Example: Insertion Sort 30 30 40 20 i = 2 j = 0 key = 10 A[j] = ∅ A[j+1] = 30 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = ∅ A[j+1] = 30 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 21M.Shoaib Farooq
  22. 22. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 20 i = 2 j = 0 key = 10 A[j] = ∅ A[j+1] = 10 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = ∅ A[j+1] = 10 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 22M.Shoaib Farooq
  23. 23. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 20 i = 3 j = 0 key = 10 A[j] = ∅ A[j+1] = 10 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = ∅ A[j+1] = 10 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 23M.Shoaib Farooq
  24. 24. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 20 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 24M.Shoaib Farooq
  25. 25. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 20 i = 4 j = 2 key = 40 A[j] = 30 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 25M.Shoaib Farooq
  26. 26. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 20 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 26M.Shoaib Farooq
  27. 27. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 20 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 27M.Shoaib Farooq
  28. 28. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 20 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 20 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 40 A[j+1] = 20 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 28M.Shoaib Farooq
  29. 29. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 20 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 20 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 40 A[j+1] = 20 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 29M.Shoaib Farooq
  30. 30. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 40 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 40 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 30M.Shoaib Farooq
  31. 31. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 40 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 40 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 31M.Shoaib Farooq
  32. 32. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 40 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 40 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 32M.Shoaib Farooq
  33. 33. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 40 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 33M.Shoaib Farooq
  34. 34. An Example: Insertion SortAn Example: Insertion Sort 10 30 40 40 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 40 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 34M.Shoaib Farooq
  35. 35. An Example: Insertion SortAn Example: Insertion Sort 10 30 30 40 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 30 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 30 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 35M.Shoaib Farooq
  36. 36. An Example: Insertion SortAn Example: Insertion Sort 10 30 30 40 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 30 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 30 A[j+1] = 30 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 36M.Shoaib Farooq
  37. 37. An Example: Insertion SortAn Example: Insertion Sort 10 30 30 40 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 30 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 10 A[j+1] = 30 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 37M.Shoaib Farooq
  38. 38. An Example: Insertion SortAn Example: Insertion Sort 10 20 30 40 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 20 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 10 A[j+1] = 20 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } }} 38M.Shoaib Farooq
  39. 39. An Example: Insertion SortAn Example: Insertion Sort 10 20 30 40 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 20 InsertionSort(A, n) { f i 2 t { 1 2 3 4 A[j] = 10 A[j+1] = 20 for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) {while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 }} A[j+1] = key } } Done!} Done! 39 M.Shoaib Farooq
  40. 40. Analysis: Insertion SortAnalysis: Insertion Sort M.Shoaib Farooq 40 Worst Case
  41. 41. Analysis: Insertion SortAnalysis: Insertion Sort tj = 1 n ∑= −= n j n 2 11 T(n)= c1n + c2(n - 1) + c4(n - 1) + c5(n - 1) + c8(n - 1) = (c1 + c2 + c4 + c5 + c8)n - (c2+ c4 + c5 + c8). M.Shoaib Farooq 41 Best Case
  42. 42. Selection Sort SelectionSort(A) f i 1 1 dfor i 1 to n-1 do small i for j i+1 to n do if (A[small]>A[j])if (A[small]>A[j]) small=j A[i] A[ ll]A[i] A[small] M.Shoaib Farooq 42
  43. 43. Sort (Which one?)Sort (Which one?) SORT ( A)SORT ( A) for i 1 to n-1 do f j i 1 t dfor j i+1 to n do if ( A[i]>A[j]) A[i] A[j] M.Shoaib Farooq 43
  44. 44. Bubble Sort BubbleSort (A) for i 1 to n-1 do for j 1 to n-1 dofor j 1 to n 1 do if ( A[j] > A[j+1]) A[j] A[j+1]A[j] A[j+1] M.Shoaib Farooq 44
  45. 45. An Example: LOOP Analysis (Bottom Up Approach) T(n)= ? M.Shoaib Farooq 45
  46. 46. Bottom Up Analysisp y • Bottom while Loop (line 5 and 6) ∑ j while(j) = • Inner For Loop (line 3 and 4) ∑= += k j 0 11 for(i) = ∑ ∑= = += i j i j jjwhile 2 1 2 1 1)( i i2 2 = = 2i(2i+1)/2 + 2i∑ ∑= = + i j i j j 2 1 2 1 1 = 2i2 + I + 2i 2i2 +3i M.Shoaib Farooq 46
  47. 47. Bottom Up Analysis Outer For Loop (Line 1 ) T( ) n n T(n) = =∑=i ifor 1 )( ∑= + n i ii 1 2 )32( ∑∑ nn 2 = = 2 [(2n3+3n2+n)/6] + 3 [n(n+1)/2] ∑∑ == + ii ii 11 2 32 Quadratic Series = 2 [(2n +3n +n)/6] + 3 [n(n+1)/2] = O (n3) M.Shoaib Farooq 47

×