6
Most read
7
Most read
8
Most read
Dynamic Programming
General Strategy

   Used for optimization problems: often minimizing or maximizing.
   Solves problems by combining solutions to sub-problems.
   Sub-problems are not independent.
   Reduces computation by
    ◦ Solving sub-problems in a bottom-up fashion.
    ◦ Storing solution to a sub-problem the first time it is solved.
    ◦ Looking up the solution when subproblem is encountered again.
   Key: determine structure of optimal solutions
Steps in Dynamic Programming

 Characterize structure of an optimal solution.
 Define value of optimal solution recursively.
 Compute optimal solution values caching in bottom-
  up manner from table.
 Construct an optimal solution from computed values.
Principle of optimality
   Principle of optimality states that “ In an
    optimal sequence of decisions or choices each
    sub-sequences must be optimal.”
    e.g.                  D


          A



               C         B
   Suppose that in solving a problem, we have to make
    a sequence of decisions D1, D2,…, Dn. Then
    according to Principle of Optimality, if this
    sequence is optimal, then the last k decisions,
    1< k< n must be optimal.
Knapsack Problem
    A thief robbing a store and can carry a maximal weight of W into
    their knapsack. There are n items and ith item weight wi and is
    worth vi dollars. What items should thief take?

   Fractional Knapsack
    ◦ The items are allowed to be broken into smaller pieces so that
      knapsack contains fraction of xi of item i, where 0 ≤ xi ≤ 1

   0/1 Knapsack
    ◦ Either to include an item in knapsack or to leave it (binary
      choice), but may not take a fraction of an item i.e. 0 or 1.
0/1 Knapsack Problem
Let i=1,2,3,…,n are items available.
wi - weight of ith item
vi – value of ith item
m – capacity of knapsack
 Now the problem is to maximize the value or profit with
   capacity constraints
Let {x1,x2,…,xn} is the optimal solution
   Where xi ∈ {0,1}
=0          if i=0 or j=0
           =-∞           if j<0
C[i,j]     =c[i-1,j]   if wi ≥ 0

           =max{vi+c[i-1,j-wi],c[i-1,wi]}   if i>0 & j>wi

Consider the E.g. : w={1,2,5,6,7}
                     v={1,6,18,22,28}
                    m=11

How to calculate the cost table

C[1,1]=max{C[1-1,1-1]+1,C[1-1,1]}
      =max{1,0}=1
C[1,2]=max{C[1-1,2-1]+1,C[1-1,1]}
       =max{1,0}=1
C[3,3]=max{C[3-1,3-5]+18,C[3-1,3]}
       =max{-∞,C[2,3]}
       =C[2,3]
   Thus Optimal Solution Vector is, {0,0,1,1,0}
   And Optimal Profit is,   0+18+22+0=40
Dknap(i,j,m)
{
  w :=[w1,w2,…,wn] ; // array of weights
  v := [v1,v2,…,vn]; // array of values
  C:= [1,2,…,n,1,2,…,m]; // Knapsack array(2-D)
  // 1,2,…i,…,n are objects
  // 1,2,…,j,…,m are capacities of knapsack
for i :=1 to n do
  C[i,0]:=0;            //Knapsack with capacity 0
  for i:=1 to n do
  {
       for j:=1 to m do
       {
               if (i=1 and j<w[i]) then
                        C[i,j]=0;
               else
               if (i=1) then
                        C[i,j]=v[i];
               else
               if(j<w[i]) then
                        C[i,j]=C[i-1,j];
               else
               if(C[i-1,j] > C[i-1,j-w[i]]+v[i]) then
                        C[i,j]= C[i-1,j];
               else
                        C[i,j]= C[i-1,j-w[i]]+v[i];
       }
  }
  Traceback(C)
}
Traceback(C)
{      //opt[] array is used to store optimal solution vector
  i:=n; j:=m;
  while(i≥1)
  { k:=i; //k is the index of opt[] array
       while(j≥0)
       {
                if(C[i,j]=C[i-1,j]) then
                {
                         i:=i-1;
                         opt[k]:=0;
                else
                {
                         opt[k]=1;
                         i:=i-1;
                         j:=j-w[i];
                }
}
Analysis of 0/1Knapsack
 Time Complexity is Θ(nm) as there are n m
 entries in table.

 Using Dynamic Programming determine the
 optimal profit and solution vector
 w={2,3,4} v ={1,2,5} m=6



  Optimal solution vector is {1,0,1} and Profit is 6

More Related Content

PPT
Asymptotic notations
PDF
P, NP, NP-Complete, and NP-Hard
PPTX
Greedy Algorithm - Knapsack Problem
PPT
Randomized algorithms ver 1.0
PPT
Dinive conquer algorithm
PPTX
Knowledge representation and Predicate logic
PPTX
Artificial Intelligence Searching Techniques
PDF
Algorithms Lecture 2: Analysis of Algorithms I
Asymptotic notations
P, NP, NP-Complete, and NP-Hard
Greedy Algorithm - Knapsack Problem
Randomized algorithms ver 1.0
Dinive conquer algorithm
Knowledge representation and Predicate logic
Artificial Intelligence Searching Techniques
Algorithms Lecture 2: Analysis of Algorithms I

What's hot (20)

PPTX
State space search
PDF
I. AO* SEARCH ALGORITHM
PPT
AI Lecture 3 (solving problems by searching)
PPTX
Asymptotic notations
PPTX
Graph coloring using backtracking
PPTX
Hashing
PPTX
Performance analysis(Time & Space Complexity)
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PDF
Daa notes 1
PPTX
Forward and Backward chaining in AI
PPTX
First order logic
PDF
I.BEST FIRST SEARCH IN AI
PPTX
Knapsack Problem
PPT
Branch and bound
PPTX
Data Structures : hashing (1)
PPTX
Lecture optimal binary search tree
PPTX
First order predicate logic (fopl)
PPTX
Job sequencing with Deadlines
PPT
Algorithm analysis
State space search
I. AO* SEARCH ALGORITHM
AI Lecture 3 (solving problems by searching)
Asymptotic notations
Graph coloring using backtracking
Hashing
Performance analysis(Time & Space Complexity)
DESIGN AND ANALYSIS OF ALGORITHMS
Daa notes 1
Forward and Backward chaining in AI
First order logic
I.BEST FIRST SEARCH IN AI
Knapsack Problem
Branch and bound
Data Structures : hashing (1)
Lecture optimal binary search tree
First order predicate logic (fopl)
Job sequencing with Deadlines
Algorithm analysis
Ad

Viewers also liked (20)

PPT
Dynamic pgmming
PPTX
Dynamic Programming
PPTX
Dynamic Programming
PPTX
Elements of dynamic programming
PPT
Dynamic programming
PPT
Dynamic Programming
DOCX
Unit 7 dynamic programming
PPT
Lecture 8 dynamic programming
PPT
Dynamic programming in Algorithm Analysis
PPTX
Dynamic programming - fundamentals review
PDF
Dynamic programing 2
PDF
A software approach to mathematical programming
PPT
Mathematical analysis of Graph and Huff amn coding
PPT
Prim Algorithm and kruskal algorithm
PDF
PPT
Appendix b 2
PPTX
Dynamic programming
PPT
PPTX
Dynamic programming class 16
PPTX
0 1 knapsack problem using dynamic programming
Dynamic pgmming
Dynamic Programming
Dynamic Programming
Elements of dynamic programming
Dynamic programming
Dynamic Programming
Unit 7 dynamic programming
Lecture 8 dynamic programming
Dynamic programming in Algorithm Analysis
Dynamic programming - fundamentals review
Dynamic programing 2
A software approach to mathematical programming
Mathematical analysis of Graph and Huff amn coding
Prim Algorithm and kruskal algorithm
Appendix b 2
Dynamic programming
Dynamic programming class 16
0 1 knapsack problem using dynamic programming
Ad

Similar to Daa:Dynamic Programing (20)

PPT
Knapsack problem and Memory Function
PPT
Knapsack Problem Analysis of Algorithm.ppt
PPTX
01 Knapsack using Dynamic Programming
PPT
Knapsack problem
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
PPT
lecture 25
PPT
4 greedy methodnew
PDF
Knapsack Problems in Algorithms and Design
PPTX
Dynamic programming (dp) in Algorithm
DOC
Data structure notes
PPT
Elak3 need of greedy for design and analysis of algorithms.ppt
PPTX
Dynamic Programming in design and analysis .pptx
PPT
Design and analysis of Algorithms - Lecture 15.ppt
PPTX
Knapsack problem
PDF
knapsackusingbranchandbound
PPTX
0 1 knapsack using branch and bound
PPTX
THE GREEDY METHOD notes related to education.pptx
PPT
DynProg_Knapsack.ppt
PPT
0/1 knapsack
Knapsack problem and Memory Function
Knapsack Problem Analysis of Algorithm.ppt
01 Knapsack using Dynamic Programming
Knapsack problem
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
lecture 25
4 greedy methodnew
Knapsack Problems in Algorithms and Design
Dynamic programming (dp) in Algorithm
Data structure notes
Elak3 need of greedy for design and analysis of algorithms.ppt
Dynamic Programming in design and analysis .pptx
Design and analysis of Algorithms - Lecture 15.ppt
Knapsack problem
knapsackusingbranchandbound
0 1 knapsack using branch and bound
THE GREEDY METHOD notes related to education.pptx
DynProg_Knapsack.ppt
0/1 knapsack

Recently uploaded (20)

PDF
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PPTX
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
Electrocardiogram sequences data analytics and classification using unsupervi...
PPTX
agenticai-neweraofintelligence-250529192801-1b5e6870.pptx
PPTX
Module 1 Introduction to Web Programming .pptx
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
PDF
SaaS reusability assessment using machine learning techniques
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
PPTX
SGT Report The Beast Plan and Cyberphysical Systems of Control
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PDF
LMS bot: enhanced learning management systems for improved student learning e...
PDF
NewMind AI Weekly Chronicles – August ’25 Week IV
PDF
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
DOCX
Basics of Cloud Computing - Cloud Ecosystem
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
Training Program for knowledge in solar cell and solar industry
Co-training pseudo-labeling for text classification with support vector machi...
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
Convolutional neural network based encoder-decoder for efficient real-time ob...
Electrocardiogram sequences data analytics and classification using unsupervi...
agenticai-neweraofintelligence-250529192801-1b5e6870.pptx
Module 1 Introduction to Web Programming .pptx
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
SaaS reusability assessment using machine learning techniques
Connector Corner: Transform Unstructured Documents with Agentic Automation
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
SGT Report The Beast Plan and Cyberphysical Systems of Control
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
LMS bot: enhanced learning management systems for improved student learning e...
NewMind AI Weekly Chronicles – August ’25 Week IV
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
Basics of Cloud Computing - Cloud Ecosystem

Daa:Dynamic Programing

  • 2. General Strategy  Used for optimization problems: often minimizing or maximizing.  Solves problems by combining solutions to sub-problems.  Sub-problems are not independent.  Reduces computation by ◦ Solving sub-problems in a bottom-up fashion. ◦ Storing solution to a sub-problem the first time it is solved. ◦ Looking up the solution when subproblem is encountered again.  Key: determine structure of optimal solutions
  • 3. Steps in Dynamic Programming  Characterize structure of an optimal solution.  Define value of optimal solution recursively.  Compute optimal solution values caching in bottom- up manner from table.  Construct an optimal solution from computed values.
  • 4. Principle of optimality  Principle of optimality states that “ In an optimal sequence of decisions or choices each sub-sequences must be optimal.” e.g. D A C B
  • 5. Suppose that in solving a problem, we have to make a sequence of decisions D1, D2,…, Dn. Then according to Principle of Optimality, if this sequence is optimal, then the last k decisions, 1< k< n must be optimal.
  • 6. Knapsack Problem  A thief robbing a store and can carry a maximal weight of W into their knapsack. There are n items and ith item weight wi and is worth vi dollars. What items should thief take?  Fractional Knapsack ◦ The items are allowed to be broken into smaller pieces so that knapsack contains fraction of xi of item i, where 0 ≤ xi ≤ 1  0/1 Knapsack ◦ Either to include an item in knapsack or to leave it (binary choice), but may not take a fraction of an item i.e. 0 or 1.
  • 7. 0/1 Knapsack Problem Let i=1,2,3,…,n are items available. wi - weight of ith item vi – value of ith item m – capacity of knapsack  Now the problem is to maximize the value or profit with capacity constraints Let {x1,x2,…,xn} is the optimal solution Where xi ∈ {0,1}
  • 8. =0 if i=0 or j=0 =-∞ if j<0 C[i,j] =c[i-1,j] if wi ≥ 0 =max{vi+c[i-1,j-wi],c[i-1,wi]} if i>0 & j>wi Consider the E.g. : w={1,2,5,6,7} v={1,6,18,22,28} m=11 How to calculate the cost table C[1,1]=max{C[1-1,1-1]+1,C[1-1,1]} =max{1,0}=1 C[1,2]=max{C[1-1,2-1]+1,C[1-1,1]} =max{1,0}=1 C[3,3]=max{C[3-1,3-5]+18,C[3-1,3]} =max{-∞,C[2,3]} =C[2,3]
  • 9. Thus Optimal Solution Vector is, {0,0,1,1,0}  And Optimal Profit is, 0+18+22+0=40
  • 10. Dknap(i,j,m) { w :=[w1,w2,…,wn] ; // array of weights v := [v1,v2,…,vn]; // array of values C:= [1,2,…,n,1,2,…,m]; // Knapsack array(2-D) // 1,2,…i,…,n are objects // 1,2,…,j,…,m are capacities of knapsack
  • 11. for i :=1 to n do C[i,0]:=0; //Knapsack with capacity 0 for i:=1 to n do { for j:=1 to m do { if (i=1 and j<w[i]) then C[i,j]=0; else if (i=1) then C[i,j]=v[i]; else if(j<w[i]) then C[i,j]=C[i-1,j]; else if(C[i-1,j] > C[i-1,j-w[i]]+v[i]) then C[i,j]= C[i-1,j]; else C[i,j]= C[i-1,j-w[i]]+v[i]; } } Traceback(C) }
  • 12. Traceback(C) { //opt[] array is used to store optimal solution vector i:=n; j:=m; while(i≥1) { k:=i; //k is the index of opt[] array while(j≥0) { if(C[i,j]=C[i-1,j]) then { i:=i-1; opt[k]:=0; else { opt[k]=1; i:=i-1; j:=j-w[i]; } }
  • 13. Analysis of 0/1Knapsack Time Complexity is Θ(nm) as there are n m entries in table. Using Dynamic Programming determine the optimal profit and solution vector w={2,3,4} v ={1,2,5} m=6 Optimal solution vector is {1,0,1} and Profit is 6