next up previous
Next: About this document Up: My Home Page

lec4-1.au Argue the solution to

is tex2html_wrap_inline261 by appealing to the recursion tree.   Draw the recursion tree.

rectree422-Lrectree422-R

How many levels does the tree have? This is equal to the longest path from the root to a leaf.

The shortest path to a leaf occurs when we take the heavy branch each time. The height k is given by tex2html_wrap_inline265 , meaning tex2html_wrap_inline267 or tex2html_wrap_inline269 .

The longest path to a leaf occurs when we take the light branch each time. The height k is given by tex2html_wrap_inline273 , meaning tex2html_wrap_inline275 or tex2html_wrap_inline277 .

The problem asks to show that tex2html_wrap_inline279 , meaning we are looking for a lower bound

On any full level, the additive terms sums to n. There are tex2html_wrap_inline283 full levels. Thus tex2html_wrap_inline285 lec4-2.au Use iteration to solve T(n) = T(n-a) + T(a) + n, where tex2html_wrap_inline289 is a constant. Note iteration is backsubstitution.  

lec3-3.au Recurrence Relations

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations.  

A recurrence relation is an equation which is defined in terms of itself.

Why are recurrences good things?

  1. Many natural functions are easily expressed as recurrences:

  2. It is often easy to find a recurrence as the solution of a counting problem. Solving the recurrence can be done for many special cases as we will see, although it is somewhat of an art.

lec3-4.au Recursion is Mathematical Induction!

In both, we have general and boundary conditions, with the general condition breaking the problem into smaller and smaller pieces.   

The initial or boundary condition terminate the recursion.  

As we will see, induction provides a useful tool to solve recurrences - guess a solution and prove it by induction.

tabular59

Guess what the solution is?

Prove tex2html_wrap_inline295 by induction:

  1. Show that the basis is true: tex2html_wrap_inline297 .
  2. Now assume true for tex2html_wrap_inline299 .
  3. Using this assumption show:

lec3-5.au Solving Recurrences

No general procedure for solving recurrence relations is known, which is why it is an art. My approach is:  

Realize that linear, finite history, constant coefficient recurrences always can be solved

Check out any combinatorics or differential equations book for a procedure.

Consider tex2html_wrap_inline301 , tex2html_wrap_inline303 , tex2html_wrap_inline305

It has history = 2, degree = 1, and coefficients of 2 and 1. Thus it can be solved mechanically! Proceed:

Systems like Mathematica and Maple have packages for doing this.   

lec3-6.au

Guess a solution and prove by induction

To guess the solution, play around with small values for insight.

Note that you can do inductive proofs with the big-O's notations - just be sure you use it right.  

Example: tex2html_wrap_inline307 .

Show that tex2html_wrap_inline309 for large enough c and n. Assume that it is true for n/2, then

Starting with basis cases T(2)=4, T(3)=5, lets us complete the proof for tex2html_wrap_inline321 .

lec3-7.au Try backsubstituting until you know what is going on

Also known as the iteration method. Plug the recurrence back into itself until you see a pattern.  

Example: tex2html_wrap_inline323 .

Try backsubstituting:

The tex2html_wrap_inline325 term should now be obvious.

Although there are only tex2html_wrap_inline327 terms before we get to T(1), it doesn't hurt to sum them all since this is a fast growing geometric series:

lec3-8.au Recursion Trees

Drawing a picture of the backsubstitution process gives you a idea of what is going on.  

We must keep track of two things - (1) the size of the remaining argument to the recurrence, and (2) the additive stuff to be accumulated during this call.

Example: tex2html_wrap_inline331

recursion-tree-Lrecursion-tree-R3.0in

The remaining arguments are on the left, the additive terms on the right.

Although this tree has height tex2html_wrap_inline333 , the total sum at each level decreases geometrically, so:

The recursion tree framework made this much easier to see than with algebraic backsubstitution.

lec3-9.au

See if you can use the Master theorem to provide an instant asymptotic solution

The Master Theorem:   Let tex2html_wrap_inline335 and b>1 be constants, let f(n) be a function, and let T(n) be defined on the nonnegative integers by the recurrence

where we interpret n/b as tex2html_wrap_inline345 or tex2html_wrap_inline347 . Then T(n) can be bounded asymptotically as follows:

  1. If tex2html_wrap_inline351 for some constant tex2html_wrap_inline353 , then tex2html_wrap_inline355 .
  2. If tex2html_wrap_inline357 , then tex2html_wrap_inline359 .
  3. If tex2html_wrap_inline361 for some constant tex2html_wrap_inline363 , and if tex2html_wrap_inline365 for some constant c<1, and all sufficiently large n, then tex2html_wrap_inline371 .

lec3-10.au

Examples of the Master Theorem

Which case of the Master Theorem applies?

lec3-11.au

Why should the Master Theorem be true?

Consider T(n) = a T(n/b) + f(n).

Suppose f(n) is small enough

Say f(n)=0, ie. T(n) = a T(n/b).

Then we have a recursion tree where the only contribution is at the leaves.  

There will be tex2html_wrap_inline429 levels, with tex2html_wrap_inline431 leaves at level l.

rectree-case12.0in

so long as f(n) is small enough that it is dwarfed by this, we have case 1 of the Master Theorem!

lec3-12.au

Suppose f(n) is large enough

If we draw the recursion tree for T(n) = a T(n/b) + f(n).

rectree-case36.0in

If f(n) is a big enough function, the one top call can be bigger than the sum of all the little calls.

Example: tex2html_wrap_inline441 . In fact this holds unless tex2html_wrap_inline443 !

In case 3 of the Master Theorem, the additive term dominates.

In case 2, both parts contribute equally, which is why the log pops up. It is (usually) what we want to have happen in a divide and conquer algorithm.

lec3-13.au

Famous Algorithms and their Recurrence

Matrix Multiplication

The standard matrix multiplication algorithm for two tex2html_wrap_inline445 matrices is tex2html_wrap_inline447 .    

matrix-multiplication-Lmatrix-multiplication-R

Strassen discovered a divide-and-conquer algorithm which takes tex2html_wrap_inline449 time.

Since tex2html_wrap_inline451 dwarfs tex2html_wrap_inline453 , case 1 of the master theorem applies and tex2html_wrap_inline455 .

This has been ``improved'' by more and more complicated recurrences until the current best in tex2html_wrap_inline457 .

lec3-14.au

Polygon Triangulation

Given a polygon in the plane, add diagonals so that each face is a triangle None of the diagonals are allowed to cross.   

triang-Ltriang-R

Triangulation is an important first step in many geometric algorithms.

The simplest algorithm might be to try each pair of points and check if they see each other. If so, add the diagonal and recur on both halves, for a total of tex2html_wrap_inline459 .

However, Chazelle gave an algorithm which runs in tex2html_wrap_inline461 time. Since tex2html_wrap_inline463 , by case 1 of the Master Theorem, Chazelle's algorithm is linear, ie. T(n) = O(n).

Sorting

The classic divide and conquer recurrence is Mergesort's T(n) = 2 T(n/2) + O(n), which divides the data into equal-sized halves and spends linear time merging the halves after they are sorted.  

Since tex2html_wrap_inline469 but not tex2html_wrap_inline471 , Case 2 of the Master Theorem applies and tex2html_wrap_inline473 .

In case 2, the divide and merge steps balance out perfectly, as we usually hope for from a divide-and-conquer algorithm.

Mergesort Animations

Approaches to Algorithms Design

Incremental

Job is partly done - do a little more, repeat until done.  

A good example of this approach is insertion sort

Divide-and-Conquer

A recursive technique  

A good example of this approach is Mergesort.




next up previous
Next: About this document Up: My Home Page

Steve Skiena
Tue Sep 15 15:27:46 EDT 1998