1 / 42

CS 3343: Analysis of Algorithms

CS 3343: Analysis of Algorithms. Analyzing recursive algorithms. Problem of the day. How do you find a coffee shop if you don’t know on which direction it might be?. Analyzing recursive algorithms. Recursive algorithms. General idea: Divide a large problem into smaller ones

cbrannigan
Download Presentation

CS 3343: Analysis of Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 3343: Analysis of Algorithms Analyzing recursive algorithms

  2. Problem of the day How do you find a coffee shop if you don’t know on which direction it might be?

  3. Analyzing recursive algorithms

  4. Recursive algorithms • General idea: • Divide a large problem into smaller ones • By a constant ratio • By a constant or some variable • Solve each smaller onerecursively or explicitly • Combine the solutions of smaller ones to form a solution for the original problem Divide and Conquer

  5. MERGE-SORTA[1 . . n] • If n = 1, done. • Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . • “Merge” the 2 sorted lists. Merge sort Key subroutine:MERGE

  6. Merging two sorted arrays Subarray 1 Subarray 2 20 13 7 2 12 11 9 1

  7. Merging two sorted arrays Subarray 1 Subarray 2 20 13 7 2 12 11 9 1

  8. Merging two sorted arrays 20 13 7 2 12 11 9 1

  9. Merging two sorted arrays 20 13 7 2 12 11 9 1

  10. Merging two sorted arrays 20 13 7 2 12 11 9 1 1

  11. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 1

  12. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 1 2

  13. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 1 2

  14. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 1 2 7

  15. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 1 2 7

  16. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 1 2 7 9

  17. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 1 2 7 9

  18. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 1 2 7 9 11

  19. Merging two sorted arrays 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 20 13 12 1 2 7 9 11

  20. 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 20 13 12 1 2 7 9 11 12 Merging two sorted arrays

  21. How to show the correctness of a recursive algorithm? • By induction: • Base case: prove it works for small examples • Inductive hypothesis: assume the solution is correct for all sub-problems • Step: show that, if the inductive hypothesis is correct, then the algorithm is correct for the original problem.

  22. MERGE-SORTA[1 . . n] • If n = 1, done. • Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . • “Merge” the 2 sorted lists. Correctness of merge sort • Proof: • Base case: if n = 1, the algorithm will return the correct answer because A[1..1] is already sorted. • Inductive hypothesis: assume that the algorithm correctly sorts smaller suarrays, i.e., A[1.. n/2 ] and A[n/2+1..n]. • Step: if A[1.. n/2 ] and A[n/2+1..n] are both correctly sorted, the whole array A[1.. n] is sorted after merging. • Therefore, the algorithm is correct.

  23. How to analyze the time-efficiency of a recursive algorithm? • Express the running time on input of size n as a function of the running time on smaller problems

  24. Sloppiness:Should be T( n/2 ) + T( n/2) , but it turns out not to matter asymptotically. Analyzing merge sort T(n) Θ(1) 2T(n/2) f(n) MERGE-SORTA[1 . . n] • If n = 1, done. • Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . • “Merge” the 2 sorted lists

  25. Analyzing merge sort • Divide: Trivial. • Conquer: Recursively sort 2 subarrays. • Combine: Merge two sorted subarrays T(n) = 2T(n/2) + f(n) +Θ(1) # subproblems Dividing and Combining subproblem size • What is the time for the base case? • What is f(n)? • What is the growth order of T(n)? Constant

  26. 20 13 7 2 12 11 9 1 20 13 7 2 12 11 9 20 13 7 12 11 9 20 13 12 11 9 20 13 12 11 20 13 12 1 2 7 9 11 12 Merging two sorted arrays Θ(n)time to merge a total of n elements (linear time).

  27. T(n) = Θ(1)ifn = 1; 2T(n/2) + Θ(n)ifn > 1. Recurrence for merge sort • Later we shall often omit stating the base case when T(n) = Q(1) for sufficiently small n, but only when it has no effect on the asymptotic solution to the recurrence. • But what does T(n) solve to? I.e., is it O(n) or O(n2) or O(n3) or …?

  28. Example: Find 9 3 5 7 8 9 12 15 Binary Search To find an element in a sorted array, we • Check the middle element • If ==, we’ve found it • else if less than wanted, search right half • else search left half

  29. Example: Find 9 3 5 7 8 9 12 15 Binary Search To find an element in a sorted array, we • Check the middle element • If ==, we’ve found it • else if less than wanted, search right half • else search left half

  30. Example: Find 9 3 5 7 8 9 12 15 Binary Search To find an element in a sorted array, we • Check the middle element • If ==, we’ve found it • else if less than wanted, search right half • else search left half

  31. Example: Find 9 3 5 7 8 9 12 15 Binary Search To find an element in a sorted array, we • Check the middle element • If ==, we’ve found it • else if less than wanted, search right half • else search left half

  32. 3 5 7 8 9 12 15 Example: Find 9 Binary Search To find an element in a sorted array, we • Check the middle element • If ==, we’ve found it • else if less than wanted, search right half • else search left half

  33. 3 5 7 8 9 12 15 Example: Find 9 Binary Search To find an element in a sorted array, we • Check the middle element • If ==, we’ve found it • else if less than wanted, search right half • else search left half

  34. Binary Search BinarySearch (A[1..N], value) { if (N == 0) return -1; // not found mid = (1+N)/2; if (A[mid] == value) return mid; // found else if (A[mid] < value) return BinarySearch (A[mid+1, N], value) else return BinarySearch (A[1..mid-1], value); } What’s the recurrence for the running time of BinarySearch?

  35. Recurrence for binary search

  36. Recursive Insertion Sort RecursiveInsertionSort(A[1..n]) 1. if (n == 1) do nothing; 2.RecursiveInsertionSort(A[1..n-1]); 3. Find index i in A such that A[i] <= A[n] < A[i+1]; 4. Insert A[n] after A[i];

  37. Recurrence for insertion sort

  38. Compute factorial Factorial (n) if (n == 1) return 1; return n * Factorial (n-1); • Note: here we use n as the size of the input. However, usually for such algorithms we would use log(n), i.e., the bits needed to represent n, as the input size.

  39. Recurrence for computing factorial • Note: here we use n as the size of the input. However, usually for such algorithms we would use log(n), i.e., the bits needed to represent n, as the input size.

  40. What do these mean? Challenge: how to solve the recurrence to get a closed form, e.g. T(n) = Θ (n2) or T(n) = Θ(nlgn), or at least some bound such as T(n) = O(n2)?

  41. Solving recurrence • Running time of many algorithms can be expressed in one of the following two recursive forms or Both can be very hard to solve. We focus on relatively easy ones, which you will encounter frequently in many real algorithms (and exams…)

  42. Solving recurrence • Recursion tree / iteration method • Substitution method • Master method

More Related