







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of divide and conquer algorithms, focusing on recurrence relations and their complexity analysis. Topics include recursive algorithms, such as tower of hanoi, and the process of figuring out an explicit formula for recurrence sequences using repeated substitution. The document also covers recurrence relations for binary search and the master theorem.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








1
2 Computation Time for Recursive Algorithms
int factorial(int n) { if n==0 return 1; else return factorial(n - 1) * n; } The number of operations required can be characterized by: f(n) = f(n-1)+ What series is generated by this recurrence relation? need an initial condition (corresponds to base case of recursion) f(0) = 1 f(n) = f(n-2) + 1 + 1 = f(n-3) + 1 + 1 + 1 = … 3 Recursive Algorithms
4 Recursive Algorithms
5 Recursive Algorithms
6 Recursive Algorithms
7 Recursive Algorithms
8 Recursive Algorithms
How to figure out an explicit formula for this sequence:
A pair of rabbits doesn’t breed until 2 months old. Thereafter each pair produces another pair each month Rabbits never die.
image from: http://www.jimloy.com/algebra/fibo.htm (^1314)
15
16
public static int binSearch (int myArray[], int first, int last, int value) { // returns the index of value or -1 if not in the array int index; if (first > last) { index = -1; } else { int mno = (first + last)/2; if (value == myArray[ mno ]) { index = mno; } else if (value < myArray[ mno ]) { index = binSearch(myArray, first, mno-1, value); } else { index = binSearch(myArray, mno+1, last, value); } } return index; }
17 Example: Finding Max in unsorted array Algorithm:
What are a, b, and g(n)? Solve by repeated substitution € f ( n ) = a ⋅ f ( n / b ) + g ( n ) 18 Estimating big-O (Master Theorem) € Let f be an increasing function that satisfies f ( n ) = a ⋅ f ( n / b ) + c ⋅ nd whenever n = bk^ , where k is a positive integer, a ≥ 1 , b is an integer > 1, and c and d are real numbers with c positive and d nonnegative. Then f ( n ) = O (^) ( n d ) if a < bd O (^) ( n d^ log n ) if a = bd O (^) ( n log b^ a ) if a > bd
19 Example: Binary Search using the Master Theorem f(n) = a f(n / b) + nd f(n) = O(?) € f ( n ) = O (^) ( n d ) if a < bd O (^) ( n d^ log n ) if a = bd O (^) ( n log b^ a ) if a > bd 20 Example: Max using the Master Theorem f(n) = a f(n / b) + nd f(n) = O(?) € f ( n ) = O (^) ( n d ) if a < bd O (^) ( n d^ log n ) if a = bd O (^) ( n log b^ a ) if a > bd
sortingDemo
25 26 Merge Sort: Space requirements Can divide be done in place?
Can merge sort be done in-place?
27 Merge using two arrays Data: Temp:
Step 1:^2^3 7 9 1^4^5 1 2 3 7 9 1 4 5 6 Step 2: 1 2 Step 3:^2 3^7^9 1^4^5 1 2 3 Step 4:^2 3^7^9 1 4^5^6
Merge using two arrays 2 3 7 9 1 4 5 6 1 2 3 4 Step 5:
Step 6: 2 3^7^9 1 4 5 1 2 3 4 5 6 Step 7: 2 3 7^9^1 4 5 1 2 3 4 5 6 7 Step 8: 2 3 7 9 1 4 5 6 1 2 3 4 5 6 7 9
29
O(n log(n)) mergeSort – Complexity
split merge
30 mergeSort: Recurrence Analysis a = b = d = € O(?) f ( n ) = a ⋅ f ( n / b ) + cn
€ f ( n ) = O (^) ( n d ) if a < bd O (^) ( n d^ log n ) if a = bd O (^) ( n log b^ a ) if a > bd 31 Stable Sorting Algorithms
32 MergeSort – Summary Efficient Predictable performance Needs extra space
37 Quick Sort Code // move one item at a time until unknown region is empty for (int u = first + 1; u <= last;++u) { // u: index of first unknown // Invariant: theArray[first+1..lastS1] < pivot // move item from unknown to proper region if (theArray[u].compareTo(pivot) < 0) { // item from unknown belongs in S ++lastS1; swap(theArray,u,lastS1); } // else item from unknown belongs in S } // place pivot in proper position and return its location swap(theArray,first,lastS1); return lastS1; } // let's go look at the demo 38 When things go bad… Worst case
39 quickSort – Algorithm Complexity
Work done at each depth
Total Work
40 quickSort: Best and average case a= b= d= € O() f ( n ) = a ⋅ f ( n / b ) + cn
€ f ( n ) = O (^) ( n d ) if a < bd O (^) ( n d^ log n ) if a = bd O (^) ( n log b^ a ) if a > bd
41 Strategies for Pivot Selection
but less likely Let's go look at the demo 42 Improvements Recursion incurs overhead
Hybrid sort algorithm
43 Space requirement for quicksort How much memory does quicksort require? 44 How fast can we sort? Observation: all the sorting algorithms so far are comparison sorts
Theorem: all comparison sorts are Ω(n log n) MergeSort is therefore an “optimal” algorithm but in practice quicksort is faster
49 Correctness LSD Radix Sort
If two digits at position i are different, ordering numbers by that digit is correct (lower-order digits irrelevant) If they are the same, numbers are already sorted on the lower-order digits. Since we use a stable sort, the numbers stay in the right order 50 Radix Sort Radix sort is
Can we use it for