Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/19/2016 Recurrences. 2/19/2016 Evaluating Run-time of Loops MergeSort(Array A) int n=A.length; int i=0; int t=1 for (t=1;t<n;t=2t) { for (i=0;i<n;i=i+2t)

Similar presentations


Presentation on theme: "2/19/2016 Recurrences. 2/19/2016 Evaluating Run-time of Loops MergeSort(Array A) int n=A.length; int i=0; int t=1 for (t=1;t<n;t=2t) { for (i=0;i<n;i=i+2t)"— Presentation transcript:

1 2/19/2016 Recurrences

2 2/19/2016 Evaluating Run-time of Loops MergeSort(Array A) int n=A.length; int i=0; int t=1 for (t=1;t<n;t=2t) { for (i=0;i<n;i=i+2t) M(A,i+1,i+t,i+2t) if (i+t < n) M(A,i+1,i+t,n) } MergeSort(A,i,j) if (j  i) return mid   (j – i)/2  MergeSort(A,i,mid) MergeSort(A,mid+1,j) Merge(A,i,mid,j)

3 2/19/2016 Iterative versus Recursive  Iterative functions generally more efficient  In algorithm design and analysis often prefer recursive version: Easier to analyze run-time Often easier to prove correct Function is often simpler and easier to read

4 2/19/2016 Recurrence Relations  Run time of recursive functions expressed as recurrence.  Simplified MergeSort Recurrence:

5 2/19/2016 Bounds for Recurrences  Methods for finding asymptotic bounds: Master Theorem Guess bound and prove  To make a ‘good guess’ use: Substitution Method Iteration Method Master Theorem  Use induction to prove guess

6 Substitution Method 2/19/2016

7 Substitution Method – Continued The ‘guess’

8 2/19/2016 Proving the Guess  Claim: T(n) = cnlgn + dn  Prove by induction on n  Hence, T(n)   (nlgn)

9 2/19/2016 Iteration Method  Draw a tree  Example: modified MergeSort splitting 4 ways instead of 2 ways merge takes steps  Recurrence:

10 Iteration Method – Example LevelCost T(n)T(n) 0 1 2 k 2/19/2016

11 Master Theorem 2/19/2016


Download ppt "2/19/2016 Recurrences. 2/19/2016 Evaluating Run-time of Loops MergeSort(Array A) int n=A.length; int i=0; int t=1 for (t=1;t<n;t=2t) { for (i=0;i<n;i=i+2t)"

Similar presentations


Ads by Google