KEMBAR78
Elements of Dynamic Programming | PPTX
University Visvesvaraya College Of Engineering,
Bengaluru-01
TOPIC
ON
ELEMENTS OF DYNAMIC PROGRAMMING
Presented by
VISHWAJEET SHABADI
20GACS2014,
Branch: Information Technology,
UVCE, Bengaluru.
Under The Guidance of
Dr. Venkatesh
Professor,
Dept. of CSE,
UVCE, Bengaluru.
INTRODUCTION OF DYNAMIC PROGRAMMING
• Dynamic Programming is the most powerful design technique
for solving optimization problems. Example: Matrix Chain
Multiplication.
• Dynamic Programming is a Bottom-up approach.
• We typically apply dynamic programming to optimization
problems.
CHARACTERISTICS OF DYNAMIC PROGRAMMING
• Optimal Substructure: If an optimal solution contains
optimal sub solutions then a problem exhibits optimal
substructure. Example: Bellman-Ford
• Overlapping subproblems: When a recursive algorithm
would visit the same subproblems repeatedly, then a
has overlapping subproblems. Example: Fibonacci
Series
OPTIMAL SUBSTRUCTURE
• A given problems has Optimal Substructure Property if optimal
solution of the given problem can be obtained by using optimal
solutions of its subproblems.
• The Shortest Path problem has following Optimal Substructure
property:
If a node x lies in the shortest path from source node u to destination
node v then, the shortest path from u to v is the combination of shortest
path from u to x and shortest path from x to v.
• All Pair Shortest Path
ON THE OTHER HAND
• The Longest path problem doesn’t have the Optimal Substructure
property.
• Here, by Longest Path we mean longest simple path(path without
cycle) between two nodes.
• Longest paths from q to t:
q -> r -> t
q -> s -> t
• Longest path from q to r:
q -> s -> t -> r
• Longest path from r to t:
OVERLAPPING SUBPROBLEMS
• Dynamic Programming is mainly used when solutions of same
subproblems are needed again and again.
• If we take an example of following recursive program for Fibonacci
Numbers, there are many subproblems which are solved again and
again.
USE MEMORY
• “Remember the results.”
• Do not solve the same problem again, just recall it from
• Two methods of storing the results in memory:
Memoization
Tabulation
MEMOIZATION
• Initialize a lookup array/table with all its elements as NIL.
NIL is simply a constant value, that signifies absence of a solution.
• Call the recursive function f(n) to solve for ‘n’ using memoization.
MEMOIZATION
• At every step i, f(i) performs the following steps:
1. Checks whether table[i] is NIL or not.
2. If it’s not NIL, f(i) returns the value ‘table[i]’.
3. If it’s NIL and ‘i’ satisfies the base condition, we update the lookup table with
the base value and return the same.
4. If it’s NIL and ‘i’ does not satisfy the base condition, then f(i) splits the
problem ‘i’ into subproblems and recursively calls itself to solve them.
5. After the recursive calls return, f(i) combines the solution to subproblems,
update the lookup table and returns the solution for problem ‘i’.
MEMOIZATION
MEMOIZATION
MEMOIZATION
MEMOIZATION
MEMOIZATION
MEMOIZATION
MEMOIZATION
TABULATION
• STEPS
a. We begin with initializing the base values of ‘i’.
b. Next, we run a loop that iterates over the remaining
c. At every iteration i, f(n) updates the ith entry in the
by combining the solutions to previously solved
d. Finally, f(n) returns table[n].
TABULATION
int fib(int n){
if(n <= 1)
return n;
f[0] = 0; f[1] = 1;
for(int i = 2; I <= n; i++){
f[i] = f[i-2] + f[i-1];
}
return n;
}
PERFORMANCE
• Time taken for calculating the 40th Fibonacci number:
1. Recursive: 14 seconds
2. Memoization: 0.17 seconds
3. Tabulation: 0.30 seconds
THANK
YOU!

Elements of Dynamic Programming

  • 1.
    University Visvesvaraya CollegeOf Engineering, Bengaluru-01 TOPIC ON ELEMENTS OF DYNAMIC PROGRAMMING Presented by VISHWAJEET SHABADI 20GACS2014, Branch: Information Technology, UVCE, Bengaluru. Under The Guidance of Dr. Venkatesh Professor, Dept. of CSE, UVCE, Bengaluru.
  • 2.
    INTRODUCTION OF DYNAMICPROGRAMMING • Dynamic Programming is the most powerful design technique for solving optimization problems. Example: Matrix Chain Multiplication. • Dynamic Programming is a Bottom-up approach. • We typically apply dynamic programming to optimization problems.
  • 3.
    CHARACTERISTICS OF DYNAMICPROGRAMMING • Optimal Substructure: If an optimal solution contains optimal sub solutions then a problem exhibits optimal substructure. Example: Bellman-Ford • Overlapping subproblems: When a recursive algorithm would visit the same subproblems repeatedly, then a has overlapping subproblems. Example: Fibonacci Series
  • 4.
    OPTIMAL SUBSTRUCTURE • Agiven problems has Optimal Substructure Property if optimal solution of the given problem can be obtained by using optimal solutions of its subproblems. • The Shortest Path problem has following Optimal Substructure property: If a node x lies in the shortest path from source node u to destination node v then, the shortest path from u to v is the combination of shortest path from u to x and shortest path from x to v. • All Pair Shortest Path
  • 5.
    ON THE OTHERHAND • The Longest path problem doesn’t have the Optimal Substructure property. • Here, by Longest Path we mean longest simple path(path without cycle) between two nodes. • Longest paths from q to t: q -> r -> t q -> s -> t • Longest path from q to r: q -> s -> t -> r • Longest path from r to t:
  • 6.
    OVERLAPPING SUBPROBLEMS • DynamicProgramming is mainly used when solutions of same subproblems are needed again and again. • If we take an example of following recursive program for Fibonacci Numbers, there are many subproblems which are solved again and again.
  • 7.
    USE MEMORY • “Rememberthe results.” • Do not solve the same problem again, just recall it from • Two methods of storing the results in memory: Memoization Tabulation
  • 8.
    MEMOIZATION • Initialize alookup array/table with all its elements as NIL. NIL is simply a constant value, that signifies absence of a solution. • Call the recursive function f(n) to solve for ‘n’ using memoization.
  • 9.
    MEMOIZATION • At everystep i, f(i) performs the following steps: 1. Checks whether table[i] is NIL or not. 2. If it’s not NIL, f(i) returns the value ‘table[i]’. 3. If it’s NIL and ‘i’ satisfies the base condition, we update the lookup table with the base value and return the same. 4. If it’s NIL and ‘i’ does not satisfy the base condition, then f(i) splits the problem ‘i’ into subproblems and recursively calls itself to solve them. 5. After the recursive calls return, f(i) combines the solution to subproblems, update the lookup table and returns the solution for problem ‘i’.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
    TABULATION • STEPS a. Webegin with initializing the base values of ‘i’. b. Next, we run a loop that iterates over the remaining c. At every iteration i, f(n) updates the ith entry in the by combining the solutions to previously solved d. Finally, f(n) returns table[n].
  • 18.
    TABULATION int fib(int n){ if(n<= 1) return n; f[0] = 0; f[1] = 1; for(int i = 2; I <= n; i++){ f[i] = f[i-2] + f[i-1]; } return n; }
  • 19.
    PERFORMANCE • Time takenfor calculating the 40th Fibonacci number: 1. Recursive: 14 seconds 2. Memoization: 0.17 seconds 3. Tabulation: 0.30 seconds
  • 20.