# introduction to algorithms

Post on 22-Jan-2015

160 views

Category:

## Education

Embed Size (px)

DESCRIPTION

TRANSCRIPT

• 1. AlgorithmsSandeep Kumar Poonia Head Of Dept. CS/IT B.E., M.Tech., UGC-NET LM-IAENG, LM-IACSIT,LM-CSTA, LM-AIRCC, LM-SCIEI, AM-UACEE

2. Algorithms Introduction Proof By Induction Asymptotic notation 3. Pre Computer Era: In the pre computer era various models of computation were proposed. The models worked on a predefined set of operations. The primary focus in this era was on What can be computed and what cannot be computed? This field of study was called Computability Theory.Sandeep Kumar Poonia 4. Post Computer Era: With ultimate computability in terms of Turing Machines, the primary focus in the post computer era was Complexity Theory. People were more interested to find out How well can it be computed?Sandeep Kumar Poonia 5. Sandeep Kumar Poonia 6. Sandeep Kumar Poonia 7. Sandeep Kumar Poonia 8. As a corollary to the above mentioned result we can easily see that a number n must be halved floor(log2n) + 1 times to reach 0.Sandeep Kumar Poonia 9. Algorithms An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.An algorithm is thus a sequence of computational steps that transform the input into the output.Sandeep Kumar Poonia 10. AlgorithmsSandeep Kumar Poonia 11. AlgorithmsSandeep Kumar Poonia 12. Properties of AlgorithmsSandeep Kumar Poonia 13. Steps to develop an algorithmSandeep Kumar Poonia 14. Algorithms as a technology Suppose computers were infinitely fast and computer memory was free.Would you have any reason to study algorithms?......YES(See an example) let us assume a faster computer (computer A) running insertion sort against a slower computer (computer B) running merge sort. They each must sort an array of one million numbers. Suppose that computer A executes one billion instructions per second and computer B executes only ten million instructions per second, so that computer A is 100 times faster than computer B in raw computing power. insertion sort, takes time roughly equal to c1n2 to sort n items, merge sort, takes time roughly equal to c2n lg n Sandeep Kumar Poonia 15. Algorithms as a technology To sort one million numbers, Computer A takes Computer B takesBy using an algorithm whose running time grows more slowly, even with a poor compiler, computer B runs 20 times faster than computer A Sandeep Kumar Poonia 16. Review: Induction Suppose S(k) is true for fixed constant k o Often k = 0 S(n) S(n+1) for all n >= k Then S(n) is true for all n >= kSandeep Kumar Poonia 17. Proof By Induction Claim:S(n) is true for all n >= k Basis: Show formula is true when n = k Inductive hypothesis: Assume formula is true for an arbitrary n Step: Show that formula is then true for n+1Sandeep Kumar Poonia 18. Induction Example: Gaussian Closed Form Prove 1 + 2 + 3 + + n = n(n+1) / 2 Basis: o If n = 0, then 0 = 0(0+1) / 2 Inductive hypothesis: o Assume 1 + 2 + 3 + + n = n(n+1) / 2 Step (show true for n+1): 1 + 2 + + n + n+1 = (1 + 2 + + n) + (n+1) = n(n+1)/2 + n+1 = [n(n+1) + 2(n+1)]/2 = (n+1)(n+2)/2 = (n+1)(n+1 + 1) / 2Sandeep Kumar Poonia 19. Induction Example: Geometric Closed Form Prove a0 + a1 + + an = (an+1 - 1)/(a - 1) for all a 1 Basis: show that a0 = (a0+1 - 1)/(a - 1) a0 = 1 = (a1 - 1)/(a - 1) Inductive hypothesis: o Assume a0 + a1 + + an = (an+1 - 1)/(a - 1) Step (show true for n+1): a0 + a1 + + an+1 = a0 + a1 + + an + an+1 = (an+1 - 1)/(a - 1) + an+1 = (an+1+1 - 1)/(a - 1)Sandeep Kumar Poonia 20. Induction Weve been using weak induction Strong induction also holds Basis: show S(0) Hypothesis: assume S(k) holds for arbitrary k 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 28. An Example: Insertion Sort 301040201234i = j = key = A[j] = A[j+1] = InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 29. An Example: Insertion Sort 301040201234i=2 j=1 A[j] = 30key = 10 A[j+1] = 10InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 30. An Example: Insertion Sort 303040201234i=2 j=1 A[j] = 30key = 10 A[j+1] = 30InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 31. An Example: Insertion Sort 303040201234i=2 j=1 A[j] = 30key = 10 A[j+1] = 30InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 32. An Example: Insertion Sort 303040201234i=2 j=0 A[j] = key = 10 A[j+1] = 30InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 33. An Example: Insertion Sort 303040201234i=2 j=0 A[j] = key = 10 A[j+1] = 30InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 34. An Example: Insertion Sort 103040201234i=2 j=0 A[j] = key = 10 A[j+1] = 10InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 35. An Example: Insertion Sort 103040201234i=3 j=0 A[j] = key = 10 A[j+1] = 10InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 36. An Example: Insertion Sort 103040201234i=3 j=0 A[j] = key = 40 A[j+1] = 10InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 37. An Example: Insertion Sort 103040201234i=3 j=0 A[j] = key = 40 A[j+1] = 10InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 38. An Example: Insertion Sort 103040201234i=3 j=2 A[j] = 30key = 40 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 39. An Example: Insertion Sort 103040201234i=3 j=2 A[j] = 30key = 40 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 40. An Example: Insertion Sort 103040201234i=3 j=2 A[j] = 30key = 40 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 41. An Example: Insertion Sort 103040201234i=4 j=2 A[j] = 30key = 40 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 42. An Example: Insertion Sort 103040201234i=4 j=2 A[j] = 30key = 20 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 43. An Example: Insertion Sort 103040201234i=4 j=2 A[j] = 30key = 20 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 44. An Example: Insertion Sort 103040201234i=4 j=3 A[j] = 40key = 20 A[j+1] = 20InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 45. An Example: Insertion Sort 103040201234i=4 j=3 A[j] = 40key = 20 A[j+1] = 20InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 46. An Example: Insertion Sort 103040401234i=4 j=3 A[j] = 40key = 20 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 47. An Example: Insertion Sort 103040401234i=4 j=3 A[j] = 40key = 20 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 48. An Example: Insertion Sort 103040401234i=4 j=3 A[j] = 40key = 20 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 49. An Example: Insertion Sort 103040401234i=4 j=2 A[j] = 30key = 20 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 50. An Example: Insertion Sort 103040401234i=4 j=2 A[j] = 30key = 20 A[j+1] = 40InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 51. An Example: Insertion Sort 103030401234i=4 j=2 A[j] = 30key = 20 A[j+1] = 30InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 52. An Example: Insertion Sort 103030401234i=4 j=2 A[j] = 30key = 20 A[j+1] = 30InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }Sandeep Kumar Poonia 53. An Example: Insertion Sort 103030401234i=4 j=1 A[j] = 10key = 20 A[j+1] = 30InsertionSort(A, n

Recommended