Download presentation
Presentation is loading. Please wait.
1
Design and Analysis of Algorithms
Chp 4 Solving Recurrences
2
Recurrent Algorithms BINARY – SEARCH
For an ordered array A, finds if x is in the array A[lo…hi] Alg.:BINARY-SEARCH (A, lo, hi, x) if (lo > hi) return FALSE mid (lo+hi)/2 if x = A[mid] return TRUE if ( x < A[mid] ) BINARY-SEARCH (A, lo, mid-1, x) if ( x > A[mid] ) BINARY-SEARCH (A, mid+1, hi, x) 12 11 10 9 7 5 3 2 1 4 6 8 mid lo hi Recurrences
3
Analysis of BINARY-SEARCH
Alg.:BINARY-SEARCH (A, lo, hi, x) if (lo > hi) returnFALSE mid (lo+hi)/2 ifx = A[mid] return TRUE if ( x < A[mid] ) BINARY-SEARCH (A, lo, mid-1, x) if ( x > A[mid] ) BINARY-SEARCH (A, mid+1, hi, x) T(n) = c + T(n) – running time for an array of size n constant time: c1 constant time: c2 constant time: c3 same problem of size n/2 same problem of size n/2 T(n/2) Recurrences
4
Recurrences and Running Time
Recurrences arise when an algorithm contains recursive calls to itself What is the actual running time of the algorithm? Need to solve the recurrence Find an explicit formula of the expression (the generic term of the sequence) Recurrences
5
Example Recurrences T(n) = T(n-1) + n Θ(n2) T(n) = T(n/2) + c Θ(lgn)
Recursive algorithm that loops through the input to eliminate one item T(n) = T(n/2) + c Θ(lgn) Recursive algorithm that halves the input in one step T(n) = T(n/2) + n Θ(n) Recursive algorithm that halves the input but must examine every item in the input T(n) = 2T(n/2) Θ(n) Recursive algorithm that splits the input into 2 halves and does a constant amount of other work Recurrences
6
Methods for Solving Recurrences
Iteration method Recursion tree method Recurrences
7
Some Simple Summation Formulas
5/4/2018 Some Simple Summation Formulas Arithmetic series: Geometric series: Special case: x < 1: Harmonic series: Other important formulas:
8
The Iteration Method T(n) = c + T(n/2) = c + c + T(n/4)
= c + c + c + T(n/8) Assume n = 2k k = lgn T(n) = c + c + … + c + T(1) = k . C + T(1) So T(n) = clgn + T(1) = Θ(lgn) T(n/2) = c + T(n/4) T(n/4) = c + T(n/8) k times Recurrences
9
Iteration Method – Example
T(n) = n + 2T(n/2) = n + 2(n/2 + 2T(n/4)) = n + n + 4T(n/4) = n + n + 4(n/4 + 2T(n/8)) = n + n + n + 8T(n/8) … = in + 2iT(n/2i) = kn + 2kT(1) = nlgn + nT(1) = Θ(nlgn) Assume: n = 2k T(n/2) = n/2 + 2T(n/4) Recurrences
10
Iteration Method – Example
T(n) = n + T(n-1) = n + (n-1) + T(n-2) = n + (n-1) + (n-2) + T(n-3) … = n + (n-1) + (n-2) + … T(1) = n(n+1)/ T(1) = n2+ T(1) = Θ(n2) Recurrences
11
s(n) = c + s(n-1) c + c + s((n-1)-1) c + c + s(n-2) 2c + s(n-2)
… kc + s(n-k) = ck + s(n-k) Recurrences
12
So far for n >= k we have What if k = n?
s(n) = ck + s(n-k) What if k = n? s(n) = cn + s(0) = cn Recurrences
13
So far for n >= k we have What if k = n? So Thus in general
s(n) = ck + s(n-k) What if k = n? s(n) = cn + s(0) = cn So Thus in general s(n) = cn Recurrences
14
= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)
= n + s(n-1) = n + n-1 + s(n-2) = n + n-1 + n-2 + s(n-3) = n + n-1 + n-2 + n-3 + s(n-4) = … = n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k) Recurrences
15
= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k) =
= n + s(n-1) = n + n-1 + s(n-2) = n + n-1 + n-2 + s(n-3) = n + n-1 + n-2 + n-3 + s(n-4) = … = n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k) = Recurrences
16
So far for n >= k we have
Recurrences
17
So far for n >= k we have
What if k = n? Recurrences
18
So far for n >= k we have
What if k = n? Recurrences
19
So far for n >= k we have
What if k = n? Thus in general Recurrences
20
T(n) = 2T(n/2) + c 2(2T(n/2/2) + c) + c 22T(n/22) + 2c + c
… 2kT(n/2k) + (2k - 1)c Recurrences
21
So far for n > 2k we have What if k = lg n?
T(n) = 2kT(n/2k) + (2k - 1)c What if k = lg n? T(n) = 2lg n T(n/2lg n) + (2lg n - 1)c = n T(n/n) + (n - 1)c = n T(1) + (n-1)c = nc + (n-1)c = (2n - 1)c Recurrences
22
The recursion-tree method
Convert the recurrence into a tree: Each node represents the cost incurred at that level of recursion Sum up the costs of all levels Used to “guess” a solution for the recurrence Recurrences
23
Example 1 W(n) = 2W(n/2) + n2 Subproblem size at level i is: n/2i
Subproblem size hits 1 when 1 = n/2i i = lgn Cost of the problem at level i = (n/2i)2, No. of nodes at level i = 2i Total cost: W(n) = O(n2) Recurrences
24
Example 1 W(n) = 2W(n/2) + n2 Subproblem size at level i is: n/2i
Subproblem size hits 1 when 1 = n/2i i = lgn Cost of the problem at level i = (n/2i)2, No. of nodes at level i = 2i Total cost: W(n) = O(n2) Recurrences
25
Example 2 Solve T(n) = T(n/4) + T(n/2)+ n2 Recurrences
26
Example 2 Solve T(n) = T(n/4) + T(n/2)+ n2 Recurrences
27
Example 2 Solve T(n) = T(n/4) + T(n/2)+ n2 Recurrences
28
Example 2 Solve T(n) = T(n/4) + T(n/2)+ n2 Recurrences
29
Example 2 Solve T(n) = T(n/4) + T(n/2)+ n2 Recurrences
30
Example 2 Solve T(n) = T(n/4) + T(n/2)+ n2 Recurrences
31
Example 3 T(n) = 2T(n/2) + cn Recurrences
32
Using the recursion tree to visualize a recurrence.
Recurrences
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.