0% found this document useful (1 vote)
102 views21 pages

Recurrence Relations and The Master Theorem: Rosen Ch. 8.1 - 8.3

This document discusses recurrence relations and the master theorem. It begins with an overview of recurrence relations and examples. It then provides a formal definition and discusses solving recurrence relations through repeated substitution or finding a pattern. The document discusses modeling problems like compound interest and bacterial growth with recurrence relations. It also discusses recursively defined functions and their relationship to recurrence relations and sequences. The remainder of the document discusses solving recurrence relations, the connection to algorithm analysis, and using the master theorem to analyze divide-and-conquer algorithms like binary search and merge sort. It concludes with a brief discussion of tractable vs. intractable problems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
102 views21 pages

Recurrence Relations and The Master Theorem: Rosen Ch. 8.1 - 8.3

This document discusses recurrence relations and the master theorem. It begins with an overview of recurrence relations and examples. It then provides a formal definition and discusses solving recurrence relations through repeated substitution or finding a pattern. The document discusses modeling problems like compound interest and bacterial growth with recurrence relations. It also discusses recursively defined functions and their relationship to recurrence relations and sequences. The remainder of the document discusses solving recurrence relations, the connection to algorithm analysis, and using the master theorem to analyze divide-and-conquer algorithms like binary search and merge sort. It concludes with a brief discussion of tractable vs. intractable problems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CS200: Recurrence Relations

and the Master Theorem

Rosen Ch. 8.1 - 8.3

CS200 - Recurrence Relations 1


Recurrence Relations:
An Overview
n  What is a recurrence?
q  A recursively defined sequence …

q  … defined by a recurrence relation

n  Example
q  Arithmetic progression: a, a+d, a+2d, …, a+nd

n  a0 = a

n  an = an-1 + d

CS200 - Recurrence Relations 2


Formal Definition
A recurrence relation for the sequence {an } is an equation
that expresses an in terms of one of more of the previous
terms of the sequence, namely, a0 ,a1,...an−1, for all integers
n with n ≥ n 0 where n 0 is a nonnegative integer.
n  A Sequence is called a solution of a Recurrence
relation + Initial conditions (“base case”), if its
terms satisfy the recurrence relation
a1?, a2? a3?
n  Example: an = an-1 + 2, a1 = 1
solution?
an = 1 + 2(n-1) = 2n-1

CS200 - Recurrence Relations 3


Compound Interest
n  You deposit $10,000 in a savings account that
yields 10% yearly interest. How much money
will you have after 1,2, … years? (b is
balance, r is rate)

bn = bn−1 + rbn−1 = (1+ r)n b0


b0 = 10, 000
r = 0.1

CS200 - Recurrence Relations 4


Modeling with Recurrence

n  Suppose that the number of bacteria in a


colony triples every hour
q  Set up a recurrence relation for the number of
bacteria after n hours have elapsed.
q  100 bacteria are used to begin a new colony.

CS200 - Recurrence Relations 5


Recursively defined functions
and recurrence relations
n  A recursive function
f(0) = a (base case)
f(n) = f(n-1) + d for n > 0 (recursive step)

n  The above recursively defined function generates


the sequence
a0 = a

an = an-1 + d

n  A recurrence relation produces a sequence, an


application of a recursive function produces a
value from the sequence
CS200 - Recurrence Relations 6
How to Approach Recursive Relations

Recursive Functions Sequence of Values

f(0) = 0 (base case) f(0) = 0


f(n) = f(n-1) + 2 for n > 0 f(1) = f(0)+2 = 2

(recursive part) f(2) = f(1)+2 = 4
f(3) = f(2) +2 = 6

Closed Form?(solution,
explicit formula)

CS200 - Recurrence Relations 7


Find a recursive function

n  Give a recursive definition of f(n)=an, where a is


a nonzero real number and n is a nonnegative
integer.
f(0) = 1,
f(n) = a * f(n-1)

n  Give a recursive definition of factorial f(n) = n!


f(0) = 1
f(n) = n* f(n-1)

n  Rosen Chapter 5 example 3-2 pp. 346

CS200 - Recurrence Relations 8


Solving recurrence relations
Solve a0 = 2; an = 3an-1, n > 0

(1) What is the recursive function?
(2) What is the sequence of values?
a

Hint: Solve by repeated substitution, recognize


a pattern, check your outcome
n  a0 = 2; a1=3(2)=6; a2=3(a1)=3(3(2)); a3=…

CS200 - Recurrence Relations 9


Connection to Complexity…
Divide-and-Conquer
Basic idea:
Take large problem and divide it into smaller problems
until problem is trivial, then combine parts to make solution.

Recurrence relation for the number of steps required:


f(n) = a f(n / b) + g(n)
n/b : the size of the sub-problems solved
a : number of sub-problems
g(n) : steps necessary to split sub-problems and
combine solutions to sub-problems

CS200 - Recurrence Relations 10


Example: Binary Search
public 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 mid = (first + last)/2;!
! if (value == myArray[mid]) { index = mid; }!
! else if (value < myArray[mid]) {!
! ! index = binSearch(myArray, first, mid-1, value); !
}!
! ! else { !
index = binSearch(myArray, mid+1, last, value); !
}!
!} !
!return index;!
}
What are a, b, and g(n)? f (n) = a ⋅ f (n /b) + g(n)
CS200 - Recurrence Relations 11
Estimating big-O (Master Theorem)
Let f be an increasing function that satisfies
f (n) = a ⋅ f (n /b) + c ⋅ n d
whenever n = b k , 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
⎧O(n d ) if a < b d ⎫
⎪ ⎪ Section 8.3 in Rosen
f (n) = ⎨O(n log n ) if a = b ⎬ Proved using induction
d d

⎪ d ⎪
⎩O(n ) if a > b ⎭
logb a

CS200 - Recurrence Relations 12


Binary Search using the Master Theorem

For binary search ! %


# ( )
d d
O n if a < b #
f(n) = a f(n / b) +c .nd # #
= 1 f(n / 2) + c f (n) = " O ( n d log n ) if a = b d &
# #
# O ( n logb a ) if a > b d #
$ '
Therefore, d = 0 (to make nd a constant), b = 2, a = 1.
bd = 20 = 1
It  sa,sfies  the  second  condi,on  of  the  Master  theorem.  
So, f(n) = O(ndlog2n) = O(n0log2n) = O(log2n)

CS200 - Recurrence Relations 13


Complexity of MergeSort with Master
Theorem
public void mergesort(Comparable[] theArray, int first, int last){
// Sorts the items in an array into ascending order.
// Precondition: theArray[first..last] is an array.
// Postcondition: theArray[first..last] is a sorted permutation
if (first < last) {
int mid = (first + last) / 2; // midpoint of the array
mergesort(theArray, first, mid);
mergesort(theArray, mid + 1, last);
merge(theArray, first, mid, last);
}// if first >= last, there is nothing to do
}

n  M(n) is the number of operations performed by mergeSort on an array
of size n

n  M(0)=M(1) = 1 M(n) = 2M(n/2) + c.n
WHY + n ?
the cost of merging two arrays of size n/2 into one of size n
CS200 - Recurrence Relations 14
Complexity of MergeSort
Master theorem

M(n) = 2M(n/2) + c.n
"O( n d )
for the mergesort algorithm if a < b d &
$ $
f (n) = #O( n log n ) if a = b '
d d
f(n) = a f(n / b) + c.nd $ d$
%O( n ) if a > b (
log b a
= 2 f(n / 2) + c.n1
Notice that c does not play a role(big O)
d = 1, b = 2, a = 2. Therefore bd = 21 = 2
It  sa,sfies  the  second  condi,on  of  the  Master  theorem.
So, f(n) = O(ndlog2n)

= O(n1log2n)
= O(nlog2n)

CS200 - Recurrence Relations 15


Best Case QuickSort Recurrence
d
f (n) = a ⋅ f (n /b) + cn
Best case: assume perfect division in equal sized partitions

n  a=
n  b= "O( n d ) if a < b d &
n  c= $ $
€ f (n) = #O( n log n ) if a = b '
d d

n  d= $ d$
%O( n ) if a > b (
log b a

n  O(?)
Worst Case: n + (n-1) + … +3 + 2+ 1= O(n2)

€ 16
CS320 Excursion: Tractability

n  A problem that is solvable using an algorithm


with polynomial worst-case complexity is
called tractable.

n  If estimation has high degree or if the


coefficients are extremely large, the algorithm
may take an extremely long time to solve the
problem.

CS200 - Recurrence Relations 17


Intractable vs Unsolvable problems

n  If the problem cannot be solved using an


algorithm with worst-case polynomial time
complexity, such problems are called
intractable. Have you seen such problems?

n  If it can be shown that no algorithm exists for


solving them, such problems are called
unsolvable.

CS200 - Recurrence Relations 18


Hanoi
// pegs are numbers, via is computed
// number of moves are counted
// empty base case Recurrence for
public void hanoi(int n, int from, int to){ number of moves?
if (n>0) { Solution?
int via = 6 - from - to; How did we prove
hanoi(n-1,from, via); this earlier?
System.out.println("move disk " + n +
" from " + from + " to " + to);
count++;
hanoi(n-1,via,to);
}
}

CS200 - Recurrence Relations 19


Permutations
public void permute(int from) {
if (from == P.length-1) {// suffix size one, nothing to permute
System.out.println(Arrays.toString(P));
else { // put every item in first place and recur
for (int i=from; i<P.length;i++) {
swapP(from,i); // put i in first position of suffix
permute(from+1); // permute the rest
swapP(from,i); // PUT IT BACK
}
}
}

complexity? number of permutations? recurrence relation?

CS200 - Recurrence Relations 20


Interesting Intractable Problems

Boolean Satisfiability 2n
n 

(A v ~B v C) ^ (~A v C v ~D) ^ (B v ~C v D)

n  TSP n!

n  only solution:


trial and error

how many options for these problems?


CS200 - Recurrence Relations 21

You might also like