Design and Analysis of Algorithms Recurrences

12
Design and Analysis of Algorithms Recurrences Haidong Xue Summer 2012, at GSU

description

Design and Analysis of Algorithms Recurrences. Haidong Xue Summer 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. - PowerPoint PPT Presentation

Transcript of Design and Analysis of Algorithms Recurrences

Page 1: Design  and Analysis of Algorithms Recurrences

Design and Analysis of AlgorithmsRecurrences

Haidong XueSummer 2012, at GSU

Page 2: Design  and Analysis of Algorithms Recurrences

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);

Page 3: Design  and Analysis of Algorithms Recurrences

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

Page 4: Design  and Analysis of Algorithms Recurrences

How to solve recurrence?

• Three methods to solve recurrence– Substitution method– Recursion-tree method– Master method

Page 5: Design  and Analysis of Algorithms Recurrences

Substitution method-examples

1. Guess the solution2. Use mathematical induction to prove it

Page 6: Design  and Analysis of Algorithms Recurrences

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,

Page 7: Design  and Analysis of Algorithms Recurrences

Substitution method-examples

Proof:• When n=k+1, • Since , • • So, =O((k+1)lg(k+1)),

Page 8: Design  and Analysis of Algorithms Recurrences

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

Page 9: Design  and Analysis of Algorithms Recurrences

Recursion-tree

• Recursion tree: each node represents the known cost of a sub problem

• Help us to make a guess of the total cost

Page 10: Design  and Analysis of Algorithms Recurrences

Recursion-tree𝑇 (𝑛 )=2𝑇 (𝑛2 )+Θ (𝑛)

Θ (𝑛)

Θ (𝑛/2 ) Θ (𝑛/2 )

Θ (𝑛/4 ) Θ (𝑛/4 ) Θ (𝑛/4 ) Θ (𝑛/4 )

……

……

lg (𝑛)

Θ (𝑛)

=

=

= Θ (𝑛/2lg (𝑛)) Θ (𝑛/2lg (𝑛)) Θ (𝑛/2lg (𝑛)) Θ (𝑛/2lg (𝑛)) Θ (𝑛/2lg (𝑛))

Totally?

Page 11: Design  and Analysis of Algorithms Recurrences

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

Page 12: Design  and Analysis of Algorithms Recurrences

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 Θ()