CSC 401 Analysis of Algorithms Lecture Notes 11
- Slides: 16
CSC 401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: • Introduce the Divide-and-conquer paradigm • Review the Merge-sort algorithm • Solve recurrence equations • • Iterative substitution Recursion trees Guess-and-test The master method • Discuss integer and matrix multiplications 1
Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: – Divide: divide the input data S in two or more disjoint subsets S 1, S 2, … – Recur: solve the subproblems recursively – Conquer: combine the solutions for S 1, S 2, …, into a solution for S The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations 2
Merge-Sort Review Merge-sort on an input sequence S with n elements consists of three steps: Algorithm merge. Sort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S. size() > 1 (S 1, S 2) partition(S, n/2) merge. Sort(S 1, C) merge. Sort(S 2, C) S merge(S 1, S 2) – Divide: partition S into two sequences S 1 and S 2 of about n/2 elements each – Recur: recursively sort S 1 and S 2 – Conquer: merge S 1 and S 2 into a unique sorted 7 2 9 4 2 4 7 9 sequence 7 2 2 7 7 7 2 2 9 4 4 9 9 9 4 4 3
Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b. Likewise, the basis case (n < 2) will take at b most steps. Therefore, if we let T(n) denote the running time of merge-sort: We can therefore analyze the running time of mergesort by finding a closed form solution to the above equation. – That is, a solution that has T(n) only on the left-hand side. 4
Iterative Substitution In the iterative substitution, or “plug-and-chug, ” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern Note that base, T(n)=b, case occurs when 2 i=n. That is, i = log n. So, Thus, T(n) is O(n log n). 5
The Recursion Tree Draw the recursion tree for the recurrence relation and look for a pattern: depth T’s size time 0 1 n bn 1 2 n/ 2 bn i 2 i n/2 i bn … … Total time = bn + bn log n (last level plus all previous levels) 6
Guess-and-Test Method In the guess-and-test method, we guess a closed form solution and then try to prove it is true by induction: Guess: T(n) < cnlogn. Wrong: we cannot make this last line be less than cn log n 7
Guess-and-Test Method, Part 2 Recall the recurrence equation: Guess #2: T(n) < cn log 2 n. – if c > b. So, T(n) is O(n log 2 n). In general, to use this method, you need to have a good guess and you need to be good at induction proofs. 8
Master Method Many divide-and-conquer recurrence equations have the form: The Master Theorem: 9
Master Method, Examples The form: The Master Theorem: Example 1: – Solution: a=4, b=2, logba=2, f(n)=n, so case 1 says T(n) is O(n 2). Example 2: – Solution: a=b=2, logba=1, f(n)=nlogn, so case 2 says T(n) is O(n log 2 n). 10
Master Method, Examples The form: The Master Theorem: Example 3: – Solution: a=1, b=3, logba=0, f(n)=nlogn, so case 3 says T(n) is O(nlogn). Example 4: – Solution: a=2, b=8, logba=3, f(n)=n 2, so case 1 says T(n) is O(n 3). 11
Master Method, Examples The form: The Master Theorem: Example 5: – Solution: a=9, b=3, logba=2, f(n)=n 3, so case 3 says T(n) is O(n 3). Example 6: (binary search) – Solution: a=1, b=2, logba=0, f(n)=1, so case 2 says T(n) is O(log n). Example 7: (heap construction) – Solution: a=2, b=2, logba=1, f(n)=logn, so case 1 12 says T(n) is O(n).
Iterative “Proof” of the Master Theorem Using iterative substitution, let us see if we can find a pattern: We then distinguish the three cases as – – – The first term is dominant Each part of the summation is equally dominant The summation is a geometric series 13
Integer Multiplication Algorithm: Multiply two n-bit integers I and J. – Divide step: Split I and J into high-order and low-order bits – We can then define I*J by multiplying the parts and adding: – So, T(n) = 4 T(n/2) + n, which implies T(n) is O(n 2). – But that is no better than the algorithm we learned in grade school. 14
An Improved Integer Multiplication Algorithm: Multiply two n-bit integers I and J. – Divide step: Split I and J into high-order and low-order bits – Observe that there is a different way to multiply parts: – So, T(n) = 3 T(n/2) + n, which implies T(n) is O(nlog 23), by the Master Theorem. 15 – Thus, T(n) is O(n 1. 585).
Matrix Multiplication Algorithm Problem: Given n×n matrices X and Y, find their product Z=X×Y, General algorithm: O(n 3) Strassen’s Algorithm: – Rewrite the matrix product as: – Where I=AE+BG, J=AF+BH K=CE+DG, L=CF+DH – Define 7 submatrix products: S 1=A(F-H), S 2=(A+B)H, S 3=(C+D)E, S 4=D(G-E) S 5=(A+D)(E+H), S 6=(B-D)(G+H), S 7=(A-C)(E+F) – Construct: I=S 4+S 5+S 6 -S 2, J=S 1+S 2, K=S 3+S 4, L=S 1 -S 7 -S 3 -S 5 – Running time: T(n) = 7 T(n/2)+bn 2 for some constant b, 16 which gives: O(nlog 7), better than O(n 3)
- Analysis of algorithms lecture notes
- Introduction to algorithms lecture notes
- Csc 401
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Exploratory data analysis lecture notes
- Sensitivity analysis lecture notes
- Factor analysis lecture notes
- Tiu lecture notes medical analysis
- Power system analysis lecture notes
- Digital signal processor architecture
- Project procurement management lecture notes
- Theology proper lecture notes
- Lecture notes on public sector accounting ghana pdf
- Software project management notes
- Magnetism
- Classical mechanics
- Physical science lecture notes