CS 478 Week 04 (Recurrences)
CS 478 Week 04 (Recurrences)
Recurrences
• Each recursive call would take constant time plus the time for
the recursive calls it makes, yielding the recurrence
• where a>1 b>1, and f(n) is a given function. Such recurrences arise frequently.
• Creates a subproblems, each of which is 1/b the size of the original problem, and in
which the divide and combine steps together take f(n) time
Technicalities in recurrences
• In practice, we neglect certain technical details when we state
and solve recurrences. For example, if we call MERGE-SORT
on n elements when n is odd, we end up with subproblems of
size
Recursive FIND_MAXIMUM_SUBARRAY
• We make the simplifying assumption that the original problem
size is a power of 2, so that all subproblem sizes are integers.
• For starters, line 1 takes constant time. The base case, when
n=1, is easy: line 2 takes constant time, and so on
• We spend T(n/2) time solving each of them. Because we have to solve two
subproblems—for the left subarray and for the right subarray
• The contribution to the running time from lines 4 and 5 comes to 2T(n/2).
Motivation
Introduction
• A recurrence is an equation or inequality that describes a
function in terms of its value on smaller inputs
T ( n) (1)
2T ( n / 2) ( n )
If n = 1
If n > 1
Θ(nlgn)
Substitution Method
3. Choose an n0 for which the guess works and such that Step 2
for any n > n0 does not depend on the claim for some n' < n0
Basic concept
• Useful when it is easy to guess the form of the answer
• Example:
T (n) 2T ( n / 2) n
T(n) = O(n lg n) ?
T(3)=2T(1) + 3 = 5
cn Technology,
Ghulam Ishaq Khan Institute of Engineering Sciences and lg n = c * 3Topi
* lg 3
Week 04: Recurrences CS478
Recursion-Tree Method
Basic concept
• Recursion tree
– Each node represents the cost of a single subproblem somewhere in the set of
recursive function invocations
– Sum the costs within each level of the tree to obtain a set of per-level costs
– Sum all the per-level costs to determine the total cost of all levels of the recursion
• Useful when the recurrence describes the running time of a divide-and-
conquer algorithm
• Useful for generating a good guess, which is then verified by the
substitution method
– Sloppiness are allowed
• Example:
– Ignore the floors and write out the implicit coefficient c > 0 (sloppiness)
2
T (n) 3T ( n / 4) (n )
T(n)=3T(n/4) + cn2
• The subproblem size for a node at depth i is n/4i
– When the subproblem size is 1 n/4i = 1i=log4n
– The tree has log4n+1 levels (0, 1, 2,.., log4n)
• The cost at each level of the tree (0, 1, 2,.., log4n-1)
– Number of nodes at depth i is 3i
– Each node at depth i has a cost of c(n/4i)2
– The total cost over all nodes at depth i is 3i c(n/4i)2=(3/16)icn2
• The cost at depth log4nlog n log 4 3
– Number of nodes is
3 4
n
– Each contributing cost T(1)
– The total cost n log 4 3T (1) (n log 4 3 )
3 2 3 2 2 3 log 4 n 1 2
2
T (n) cn cn ( ) cn ... ( ) cn (n log 4 3 )
16 16 16
log 4 n 1
3 i 2
( ) cn (n log 4 3 )
i 0 16
3 i 2
( ) cn (n log 4 3 )
i 0 16
1 log 4 3 16 2
2
cn (n ) cn (n log 4 3 )
3 13
1
16
O(n 2 )
2
( )x n 1
3
n (3 / 2) x
x log (3 / 2) n
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Week 04: Recurrences CS478
Basic concept
• Cookbook for solving
T (n) aT (n / b) f (n)
– Describe the running time of an algorithm that divides a problem of size
n into a subproblems, each of size n/b
– a 1 and b > 1 (constant), f(n): asymptotically positive
– Require memorization of three cases
– Floor and ceiling functions are omitted
Master Theorem
Consider a recurrence of the form
T(n) = a T(n/b) + f(n)
with a>=1, b>1, and f(n) eventually positive.
a)If f(n) = O(nlogb(a)-), then T(n)=(nlogb(a)).
b)If f(n) = (nlogb(a) ), then T(n)=(nlogb(a) log(n)).
c) If f(n) = (nlogb(a)+) and f(n) is regular, then
T(n)=(f(n))
[f(n) regular iff eventually af(n/b)<= cf(n) for some
constant c<1]
Master Theorem
• Let T(n) be a monotonically increasing function that
satisfies
T(n) = a T(n/b) + f(n)
T(1) = c
where a 1, b 2, c>0. If f(n) is (nd) where d 0
then
if a < bd
T(n) = If a = bd
if a > bd
• Note that the Master Theorem does not solve the recurrence
equation