CS 310 Unit 15 Dynamic Programming

# CS 310 Unit 15 Dynamic Programming - CS 310 Unit 15 Dynamic...

This preview shows pages 1–9. Sign up to view the full content.

CS 310 Unit 15 Dynamic Programming Furman Haddix Ph.D. Assistant Professor Minnesota State University, Mankato Spring 2008

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dynamic Programming Objectives What is Dynamic Programming Fibonacci algorithm top-down, bottom-up, memoized Factory Example Matrix Multiplication Example Memoization Example Text, Chapter 15.1-15.3
Dynamic Programming Dynamic programming is a methodology rather than an algorithm Like Divide and Conquer, dynamic programming is a means of solving problems efficiently and effective Dynamic Programming is generally concerned with optimization problems Dynamic Programming applications have two characteristics: Overlapping subproblems: subproblem a is used in solving both subproblem b and subproblem c. A recursive solution will contain many instances of the same subproblem. The (optimal) solution contains (optimal) solutions to a subset of subproblems BEWARE the fallacy of suboptimization: Not all problems can be decomposed into subproblems for which optimal subproblem solutions are parts of the optimal problem solution Where applicable, dynamic programming may provide a more efficient solution than divide and conquer because

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Three methods of computing Fibonacci numbers Divide and Conquer – classic recursion top-down Dynamic Programming – back side recursion bottom-up Memoization – classic recursion with a memo table lookup top-down with table
Divide and Conquer: Classic Recursion Example Fibonacci Naïve Recursive Algorithm (n = 7) int fib(int n) { if (n < 3) return (n--); else return(fib(n-1) + fib(n-2)); } fib(5); fib(4); fib(3); fib(2); r = 1; fib(1); r = 0; r = 1; fib(2); r = 1; r = 2; fib(3); fib(2); r = 1; fib(1); r = 0; r = 1; r = 3; fib(6); fib(4); fib(3); fib(2); r = 1; fib(1); r = 0; r = 1; fib(2); r = 1; r = 2; r = 5; fib(5); fib(4); fib(3); r = 1; fib(2); r = 1; r = 2; fib(3); fib(2); r = 1; fib(1); r = 0; r = 1; r = 3; fib(2); r = 1; fib(1); r = 0; fib(7); r = 8; Consider number of calls to fib(3)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Fibonacci Numbers: Dynamic Programming Style Calculation Bottom-up Algorithm: 0, -- if n = 1 F(n), F(n-1) = 1, 0 if n = 2 F(n-1) + F(n-2), F(n-1) if n 3 Pseudocode: switch n { case 1:fn = 0; break; case 2:fn = 1; fnm1 = 0; break; default: fibo(n-1, fn, fnm1); swap(fn, fnm1); // fn = fn + fnm1; fn += fnm1; // fnm1 = fn – fnm1; } } T(n) = T(n-1) + (1) Θ n levels, (1) at each level Θ T(n) = O(n) Computation is done on the back side eliminating redundant function calls for subproblem solution
Execution of Fibonacci Numbers void fibo(int n, int &fn, int &fnm1) { switch n { case 1: fn = 0; break; case 2: fn = 1; fnm1 = 0; break; default: fibo(n-1, fn, fnm1); fn += fnm1; fnm1 = fn - fnm1; } fibo(5); fibo(4); fibo(3); fibo(2); fn = 1; fnm1 = 0; fn = 1; fnm1 = 1; fn = 2; fnm1 = 1; fn = 3; fibo(6); fnm1 = 2; fn = 5; fnm1 = 3; fnm1 = 5; fibo(7); fn = 8;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Memoization Algorithm:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 40

CS 310 Unit 15 Dynamic Programming - CS 310 Unit 15 Dynamic...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online