Design and Analysis of AlgorithmsRecurrences
Haidong XueSummer 2012, at GSU
What is a recurrence?
• “an equation or inequality that describes a function in terms of its value on smaller inputs”
• Time complexity of divide and conquer algorithms can be represented as recurrence equations.
• What is the recurrence of fact2
fact2(n)if(n<=1) return 1;return n*fact2(n-1);
How to solve recurrences?
• What can be omitted?– Floor, ceiling and boundary conditions– Since the solution typically doesn’t change by
more than a constant factor• Example: merge sort
How to solve recurrence?
• Three methods to solve recurrence– Substitution method– Recursion-tree method– Master method
Substitution method-examples
1. Guess the solution2. Use mathematical induction to prove it
Substitution method-examples
Guess: 1. Basis– When n=2,
2. Inductive steps– Inductive hypothesis: when n<=k, k>=2, – The statement needs to be proved: when n=k+1,
Substitution method-examples
Proof:• When n=k+1, • Since , • • So, =O((k+1)lg(k+1)),
Substitution method-examples
• It could be very hard prove even if one made the right guess
• 4.3-2
• How to make a guess?– Recursion-tree
Recursion-tree
• Recursion tree: each node represents the known cost of a sub problem
• Help us to make a guess of the total cost
Recursion-tree𝑇 (𝑛 )=2𝑇 (𝑛2 )+Θ (𝑛)
Θ (𝑛)
Θ (𝑛/2 ) Θ (𝑛/2 )
Θ (𝑛/4 ) Θ (𝑛/4 ) Θ (𝑛/4 ) Θ (𝑛/4 )
……
……
lg (𝑛)
Θ (𝑛)
=
=
= Θ (𝑛/2lg (𝑛)) Θ (𝑛/2lg (𝑛)) Θ (𝑛/2lg (𝑛)) Θ (𝑛/2lg (𝑛)) Θ (𝑛/2lg (𝑛))
Totally?
The master theorem
Let a>=1 and b>1 be constants, let f(n) be a function, and let T(n) be defined on the nonnegative integers by the recurrence:
• If for some constant >0, then • If , then • If for some constant >0, and if for some
constant c<1 and all sufficiently large n, then
The master theorem
𝑇 (𝑛 )=𝑎𝑇 (𝑛𝑏 )+ 𝑓 (𝑛)Recurrence f(n) T(n)
𝑇 (𝑛)=9𝑇 (𝑛 /3)+𝑛𝑇 (𝑛)=𝑇 (2𝑛/3)+1
𝑇 (𝑛)=2𝑇 (𝑛/2)+𝑛𝑙𝑔𝑛𝑇 (𝑛)=2𝑇 (𝑛/2)+Θ(𝑛)
𝑛 𝑛𝑙𝑜𝑔 39=𝑛2 Θ()
1 𝑛𝑙𝑜𝑔 3/21=1 Θ(lg(n))
𝑛𝑙𝑔(𝑛) 𝑛𝑙𝑜𝑔 22 (cannot use master method)
Θ(𝑛) 𝑛𝑙𝑜𝑔 22 Θ()