Docsity

Study Sheet for Dynamics Programming - Analysis of Algorithms | CS 483, Study notes of Algorithms and Programming

Toggle sidebar
bg1
CS 483
Fall, 2004
H. Hamburger
Study Sheet for Chapter 8
Dynamic Programming (DP)
This is not a substitute for the usual study methods!
General Idea of DP:
• Many problems lead readily to Divide and Conquer.
• The resulting recursive algorithm may be inefficient.
• That inefficiency may arise from overlapping subproblems.
• DP overcomes this flaw by recording and reusing answers.
• Either recursion or iteration can use this technique.
• DP solves many instances, which can be efficient, if needed.
• You need to understand the algorithms and their efficiency.
Problems and Algorithms Studied
• Fibonacci numbers
• Binomial coefficients
• Warshall: all-pairs existence of paths
• Floyd: all-pairs shortest paths
• Optimal binary search trees
• Knapsack
Fibonacci Numbers
• The usual definition is recursive: fib(n) = fib(n-1) + fib(n-2).
• The definition leads directly to a recursive algorithm.
• That recursive algorithm is inefficient, with exponential execution time.
• Recursion with memos can be done in linear time, that is Q(n).
• Iteration is also linear, but faster.
• These methods compute fib for 1 to n.
• For a single value, there is a formula, which is Q(1).
Computing Binomial Coefficients
• The factorial formula takes about 2n multiplications for one instance.
• A variant of that formulat takes only 2min(k,n-k) multiplications.
• For k near n/2, the recursive formula takes time exponential in n.
• That's because each of the 2 subproblems is reduced only slightly in size.
• There is subproblem overlap, but the recursion doesn't notice that.
• DP keeps tack of solved subproblems and doesn't repeat work.
• DP should be done iteratively here.
• DP does just one addition per instance.
pf2

Partial preview of the text

Download Study Sheet for Dynamics Programming - Analysis of Algorithms | CS 483 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 483 Fall, 2004 H. Hamburger

Study Sheet for Chapter 8

Dynamic Programming (DP)

This is not a substitute for the usual study methods!

General Idea of DP:

  • Many problems lead readily to Divide and Conquer.
  • The resulting recursive algorithm may be inefficient.
  • That inefficiency may arise from overlapping subproblems.
  • DP overcomes this flaw by recording and reusing answers.
  • Either recursion or iteration can use this technique.
  • DP solves many instances, which can be efficient, if needed.
  • You need to understand the algorithms and their efficiency.

Problems and Algorithms Studied

  • Fibonacci numbers
  • Binomial coefficients
  • Warshall: all-pairs existence of paths
  • Floyd: all-pairs shortest paths
  • Optimal binary search trees
  • Knapsack

Fibonacci Numbers

  • The usual definition is recursive: fib(n) = fib(n-1) + fib(n-2).
  • The definition leads directly to a recursive algorithm.
  • That recursive algorithm is inefficient, with exponential execution time.
  • Recursion with memos can be done in linear time, that is Q(n).
  • Iteration is also linear, but faster.
  • These methods compute fib for 1 to n.
  • For a single value, there is a formula, which is Q(1).

Computing Binomial Coefficients

  • The factorial formula takes about 2n multiplications for one instance.
  • A variant of that formulat takes only 2min(k,n-k) multiplications.
  • For k near n/2, the recursive formula takes time exponential in n.
  • That's because each of the 2 subproblems is reduced only slightly in size.
  • There is subproblem overlap, but the recursion doesn't notice that.
  • DP keeps tack of solved subproblems and doesn't repeat work.
  • DP should be done iteratively here.
  • DP does just one addition per instance.

Existence of Paths & Warshall's Algorithm

  • Search can find paths, but is not efficient except for very sparse graphs.
  • Logical matrix multiplication (LMM) of adjacency matrix, A, yields paths.
  • Square of A using LMM gives paths of length 2.
  • (n-1)st^ LMM power gives longest non-cyclic paths.
  • Q(n) LMMs are used, each taking time Q(n^3 ), so execution time is Q(n^4 ).
  • For large n, repeated squaring with LMM gives Q(n^3 logn)
  • Warshall's algorithm does even better: Q(n^3 ).
  • It also computes Q(n) matrices but each takes only Q(n 2 ) to compute.
  • This is possible because each position in each array is done in time Q(1).
  • Warshall array D (k-1) confines intermediate path-vertices to 1, …, k-1.
  • D(k)^ permits a new i-to-j path if i-to-k and k-to-j were already possible.

Shortest Paths & Floyd's Algorithm

  • This problem is closely analogous to the preceding one.
  • The same four approaches to designing an algorithm are possible here.
  • Where logical–and of edges yields paths, edge weights are added here.
  • Where logical–or yields existence, here we want a minimum.
  • Floyd's and Warshall's algorithm differ only in these two ways.
  • Floyd's algorithm is Q(n 3 ).

Optimal Binary Search Trees (Optimal BSTs)

  • With n=0,1,2,3,4,5,…nodes, there are C(n) 1,1,2,5,14,42… binary trees.
  • For example: C(5) = 1x14 + 1x5 + 2x2 + 5x1 + 14x1 = 42
  • The first and last terms in the sum for C(n) are C(n-1), so C(n) > 2C(n-1).
  • This shows that C(n) = Q( n ).
  • Brute force means trying all C(n) trees, so execution time is exponential.
  • DP computes the optimal BST for each contiguous subsequence.
  • There are Q(n^2 ) of them, stored in a triangular array of cells.
  • In any subsequence each of at most n nodes is a candidate to be the root.
  • For each possible root, optimal subtrees have already been computed.
  • Execution time is Q(n 3 ).

Knapsack Problems

  • KP can be 1-, 2-, or higher dimensional; integer or continuous.
  • Continuous 1-D solution is easy: always use value-densest material.
  • We focus on integer 1-D version.
  • Any subset of the n items may be the best, so brute force takes Q(2n).
  • Consider items in an arbitrary but fixed sequence.
  • For new item (w,v), consider each integer capacity up to W, the given one.
  • Current item is either in or out. For out, result is stored directly above.
  • For in, add v to result that is w to the left in preceding row. Use if better.
  • Execution time = Q(nW) for DP with iteration.
  • For a lumpy instances of KP, recursive DP may be best.