120 likes | 292 Views
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.
E N D
Design and Analysis of AlgorithmsRecurrences HaidongXue 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. • 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 • Guess the solution • Use mathematical induction to prove it
Substitution method-examples Guess: • Basis • When n=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 = = …… …… = 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 Θ() Θ(lg(n)) (cannot use master method) Θ()