Title: CS 312: Algorithm Analysis
1CS 312 Algorithm Analysis
- Lecture 07 Recurrence Relations
- a.k.a. Difference Equations
2Announcements
- HW 5 Due Today
- Questions about the Master Theorem
- Project 1
- Help Session with Michael D.
- Howd it go?
- Another Help Session with David M.
- 1/25/07 (Thu)
- 1pm
- 1121 TMCB
- Questions?
- Early Day is next Friday
- Due Date is the Monday thereafter
3Objectives for Today
- Lecture 6 Divide and Conquer
- Introduce Divide and Conquer
- Apply to Multiplication
- Analyze by defining a recurrence relation
- Analyze for Big-theta bound
- Introduce the Convex Hull problem
- If time permits, analyze binary search using
Master Theorem
- Lecture 7 Recurrence Relations
- Go beyond the Master Theorem
- Analyze Recursive Algorithms, especially for
Divide and Conquer - Recurrence Relations
- Also known as Difference Equations
- Today Special Type
- Homogeneous
- Linear
- Constant Coefficients
4Recall Analysis Framework
- Define measure of input size
- Identify algorithms elementary operations
- Time efficiency is measured by counting the
number of times an elementary operation is
executed. - Space efficiency is measured by counting the
number of memory units consumed by the algorithm - The efficiency of some algorithms may differ
significantly for inputs of the same size. - Distinguish between worst-case, average-case, and
best-case efficiencies by plotting efficiency vs.
input size - Establish order of growth. Use asymptotic
notation.
5 Asymptotic Notation
6Nonrecursive Algorithms
- Example Element Uniqueness Problem, check
whether all elements in an array are distinct.
function UniqueElements(A0n-1) for i0
to n-2 for j i1 to n-1
if AiAj return false return true
- Define input size
- Define elementary operation
- Distinguish worst-case
7Recursive Algorithms
- Example Compute Factorial function N!
function Factorial(N) if n0 return 1 else
return Factorial(N-1)N
- Define input size
- Define elementary operation
- Distinguish worst-case
What series is generated by this recurrence
relation?
In order to answer that question we need an
initial condition!
Appeal to Theory of Recurrence Relations /
Difference Equations
8Recursive Algorithms
- Example Tower of Hanoi, move all disks to third
peg without ever placing a larger disk on a
smaller one.
9Recursive Algorithms
- Example Tower of Hanoi, move all disks to third
peg without ever placing a larger disk on a
smaller one.
10Recursive Algorithms
- Example Tower of Hanoi, move all disks to third
peg without ever placing a larger disk on a
smaller one.
11Recursive Algorithms
- Example Tower of Hanoi, move all disks to third
peg without ever placing a larger disk on a
smaller one.
12Recursive Algorithms
- Example Tower of Hanoi, move all disks to third
peg without ever placing a larger disk on a
smaller one.
13Recursive Algorithms
- Example Tower of Hanoi, move all disks to third
peg without ever placing a larger disk on a
smaller one.
Can you figure out an explicit formula for this
sequence?
- Substitution
- Linear Analysis (Appeal to Theory of Recurrence
Relations)
14Recurrence Relations
- Given a sequence of (real) numbers y(k), a
recurrence relation is an equation relating the
value y(k), at a point k, to values at other
points (usually neighboring points).
15Linear Recurrence Relations
- Theorem a recurrence relation of order n is
linear if and only if it can be written as - where x(k) is arbitrary and ai(k) are functions
independent of y for all i0,1,2,.
16Linear Recurrence Relations
- Theorem a recurrence relation of order n is
linear if and only if it can be written as - where x(k) is arbitrary and ai(k) are functions
independent of y for all i0,1,2,.
17Terminology
- The ai(k)s in these equations are called the
coefficients of the linear equation. - If these coefficients do not depend on k, the
equation is said - to have constant coefficients
- or to be time-invariant.
- The function x(k) is called
- the forcing term
- the driving term
- the system input
- or simply the right-hand side.
- A solution of the recurrence relation is a
function y(k) (in terms of k) that satisfies the
recurrence relation. - A recurrence relation is said to be homogeneous
ify(k) 0 (for all k) is a solution of the
equation.
18Big Picture
- Bear with me now while we look at the big picture
19Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
A function is like a table that takes a number in
and gives a number out.
20Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
Functions can also take in a vector of numbers or
yield a vector out.
21Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
What if the input and output vectors of a
function were infinitely long?
Then x and y themselves are functions!
f becomes a map from one function to another!
We call maps that take functions as inputs or
generate functions as outputs operators, or
systems.
An operator is like a rule, its the mathematical
description of a subroutine!
22Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
What if the input and output vectors of a
function were infinitely long?
Then x and y themselves are functions!
y(k)
x(k)
S
f becomes a map from one function to another!
We call maps that take functions as inputs or
generate functions as outputs operators, or
systems.
An operator is like a rule, its the mathematical
description of a subroutine!
23Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
What if the input and output vectors of a
function were infinitely long?
Then x and y themselves are functions!
y(k)
x(k)
S
f becomes a map from one function to another!
We call maps that take functions as inputs or
generate functions as outputs operators, or
systems.
Unlike a simple function, an operator can have
local variables that store intermediate
calculationsjust like a subroutine. In other
words, S is a map that can have memory!
An operator is like a rule, its the mathematical
description of a subroutine!
24Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
What if the input and output vectors of a
function were infinitely long?
Then x and y themselves are functions!
y(k)
x(k)
S
f becomes a map from one function to another!
We call maps that take functions as inputs or
generate functions as outputs operators, or
systems.
Subroutine Description function S(x,k) if
k0 then y f yo else y f w x
w f y return y
An operator is like a rule, its the mathematical
description of a subroutine!
25Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
What if the input and output vectors of a
function were infinitely long?
Then x and y themselves are functions!
y(k)
x(k)
S
f becomes a map from one function to another!
We call maps that take functions as inputs or
generate functions as outputs operators, or
systems.
An operator is like a rule, its the mathematical
description of a subroutine!
26Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
What if more than one number is stored in memory?
y(k)
x(k)
S
Subroutine Description function S(x,k) if
k0 then y f yo else if k1 then y f y1
else y f a1w1 aow0x w0 f w1 w1
f y return y
27Order of Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
What if more than one number is stored in memory?
y(k)
x(k)
S
We call the number of values stored in
memory the order of the system.
Subroutine Description function S(x,k) if
k0 then y f yo else if k1 then y f y1
else y f a1w1 aow0x w0 f w1 w1
f y return y
28Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system
What if more than one number is stored in memory?
y(k)
x(k)
S
We call the number of values stored in
memory the order of the system.
29Recurrence Relations
So why should we care about recurrence relations?
Generalizes the idea of a function to an operator
or dynamic system.
Characterizes a function y(k) by rules instead of
by a table of its values!
Example Complexity of solution for Tower of
Hanoi problem
Example Capital growth in your savings account
y(n) - in account starting day n i -
interest earned each day D(n) - Deposit on day
n W(n) - Withdraw on day n
30Big Picture
31Terminology
32Example
33Solution?
34Assignment
- Read Recurrence Relations Notes, Part II
- HW 6 Part I Exercises (Section 1.3)