Cs141 Home/Lecture5

Cs141 Home | Cs141 Home | recent changes | Preferences

Showing revision 2

/Lecture4 /Lecture6

Introduction to Dynamic Programming

Computing the nth Fibonacci number using recursion (fib(n) = fib(n-1)+fib(n-2)) takes time at least 2n/2. We improve this by noting that this computation is highly redundant. For example, fib(3) is recomputed many times. To see this, look at the recursion tree for, say, fib(8):

upload:fib_call_graph_8.tiff

Using the "show_call_graph" utility from /Prog1, applied to call graphs generated by running the fib() routine given to you there.

To improve this, we can use dynamic programming. This is implemented one of two ways:

  1. caching results of computations for later re-use
  2. filling out a table bottom up.

For details, see S04_CS141:FibonacciByDP .

We also considered counting paths: S04_CS141:CountingPathsByDP, and did a class exercise about counting the number of subsets of {1,2,..,n} that have size k: S04_CS141:NChooseKByDP .

For more examples: S04_CS141:DynamicProgramming


Cs141 Home | Cs141 Home | recent changes | Preferences
This page is read-only | View other revisions | View current revision
Edited January 18, 2005 1:55 pm by Neal (diff)
Search: