0% found this document useful (0 votes)
17 views19 pages

0 1 Knapsack

Uploaded by

hardtech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views19 pages

0 1 Knapsack

Uploaded by

hardtech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1

DYNAMIC PROGRAMMING
SYEDA HIRA FATIMA
NAQVI
ASSISTANT PROFESSOR
IMCS

2
INTRODUCTION
• DYNAMIC PROGRAMMING IS THE MOST POWERFUL DESIGN TECHNIQUE FOR SOLVING OPTIMIZATION
PROBLEMS.
• DIVIDE & CONQUER ALGORITHM PARTITION THE PROBLEM INTO DISJOINT SUBPROBLEMS SOLVE THE
SUBPROBLEMS RECURSIVELY AND THEN COMBINE THEIR SOLUTION TO SOLVE THE ORIGINAL
PROBLEMS.
• DYNAMIC PROGRAMMING IS USED WHEN THE SUBPROBLEMS ARE NOT INDEPENDENT, E.G. WHEN
THEY SHARE THE SAME SUBPROBLEMS. IN THIS CASE, DIVIDE AND CONQUER MAY DO MORE WORK
THAN NECESSARY, BECAUSE IT SOLVES THE SAME SUB PROBLEM MULTIPLE TIMES.
• DYNAMIC PROGRAMMING SOLVES EACH SUBPROBLEMS JUST ONCE AND STORES THE RESULT IN A
TABLE SO THAT IT CAN BE REPEATEDLY RETRIEVED IF NEEDED AGAIN.
• DYNAMIC PROGRAMMING IS A BOTTOM-UP APPROACH- WE SOLVE ALL POSSIBLE SMALL
PROBLEMS AND THEN COMBINE TO OBTAIN SOLUTIONS FOR BIGGER PROBLEMS.
• DYNAMIC PROGRAMMING IS A PARADIGM OF ALGORITHM DESIGN IN WHICH AN OPTIMIZATION
PROBLEM IS SOLVED BY A COMBINATION OF ACHIEVING SUB-PROBLEM SOLUTIONS AND APPEARING3
TO THE "PRINCIPLE OF OPTIMALITY".
CHARACTERISTICS OF DYNAMIC
PROGRAMMING:
• DYNAMIC PROGRAMMING WORKS WHEN A PROBLEM HAS THE FOLLOWING
FEATURES:-
• OPTIMAL SUBSTRUCTURE: IF AN OPTIMAL SOLUTION CONTAINS OPTIMAL
SUB SOLUTIONS THEN A PROBLEM EXHIBITS OPTIMAL SUBSTRUCTURE.
• OVERLAPPING SUBPROBLEMS: WHEN A RECURSIVE ALGORITHM WOULD
VISIT THE SAME SUBPROBLEMS REPEATEDLY, THEN A PROBLEM HAS
OVERLAPPING SUBPROBLEMS.

4
ELEMENTS OF DYNAMIC PROGRAMMING

1.SUBSTRUCTURE: DECOMPOSE THE GIVEN PROBLEM INTO SMALLER SUBPROBLEMS.


EXPRESS THE SOLUTION OF THE ORIGINAL PROBLEM IN TERMS OF THE SOLUTION FOR
SMALLER PROBLEMS.
2.TABLE STRUCTURE: AFTER SOLVING THE SUB-PROBLEMS, STORE THE RESULTS TO THE
SUB PROBLEMS IN A TABLE. THIS IS DONE BECAUSE SUBPROBLEM SOLUTIONS ARE
REUSED MANY TIMES, AND WE DO NOT WANT TO REPEATEDLY SOLVE THE SAME
PROBLEM OVER AND OVER AGAIN.
3.BOTTOM-UP COMPUTATION: USING TABLE, COMBINE THE SOLUTION OF SMALLER
SUBPROBLEMS TO SOLVE LARGER SUBPROBLEMS AND EVENTUALLY ARRIVES AT A
SOLUTION TO COMPLETE PROBLEM.
5
CONT..

• NOTE: BOTTOM-UP MEANS:-


i. START WITH SMALLEST SUBPROBLEMS.
ii.COMBINING THEIR SOLUTIONS OBTAIN THE SOLUTION TO SUB-PROBLEMS OF
INCREASING SIZE.
iii.UNTIL SOLVING AT THE SOLUTION OF THE ORIGINAL PROBLEM.

6
APPLICATIONS OF DYNAMIC PROGRAMMING

1.0/1 KNAPSACK PROBLEM


2.MATHEMATICAL OPTIMIZATION PROBLEM
3.ALL PAIR SHORTEST PATH PROBLEM
4.RELIABILITY DESIGN PROBLEM
5.LONGEST COMMON SUBSEQUENCE (LCS)
6.FLIGHT CONTROL AND ROBOTICS CONTROL
7.TIME-SHARING: IT SCHEDULES THE JOB TO MAXIMIZE CPU USAGE.
7
DIVIDE & CONQUER METHOD VS DYNAMIC
PROGRAMMING.
Divide & Conquer Method Dynamic Programming
1.It deals (involves) three steps at each level of •1.It involves the sequence of four
recursion: steps:Characterize the structure of optimal
Divide the problem into a number of solutions.
subproblems. •Recursively defines the values of optimal
Conquer the subproblems by solving them solutions.
recursively. •Compute the value of optimal solutions in a
Combine the solution to the subproblems into the Bottom-up minimum.
solution for original subproblems. •Construct an Optimal Solution from computed
information.
2. It is Recursive. 2. It is non Recursive.
3. It does more work on subproblems and hence 3. It solves subproblems only once and then stores
has more time consumption. in the table.

4. It is a top-down approach. 4. It is a Bottom-up approach.


5. In this subproblems are independent of each 5. In this subproblems are interdependent.
other.
8
6. For example: Merge Sort & Binary Search etc. 6. For example: Fibonacci séries.
DYNAMIC PROGRAMMING:

• The word “ programming “ in the name of this technique stands for “


planning “ and does not refer to computer programming.
• Dynamic programming is a technique for solving problems with
overlapping subproblems. Rather than solving subproblems again and
again , dynamic programming suggest to solve each of the sub
problems only once and recording the result in a table from which a
solution to the original problem can be obtained.
• Dynamic programming requires an additional array .
• It is probably one of the easiest to implement because it doesn’t
require the use of any additional substructures. 9
KNAPSACK PROBLEM:

• In knapsack problem we have a knapsack that has a weight


limit (capacity) M.
• There are several items I1 , I2, I3,……,In .
• each item having weight W1 ,W2 ,W3,….,Wn and
• some benefit (profit/value) associated with it V1 , V2, V3,…..,Vn .

• Our objective
• to fill the knapsack with items without crossing the knapsack
weight limit.
• The total benefit is maximize.
10
KNAPSACK PROBLEM:
• There are two versions of knapsack
problem.

O-1 KNAPSACK : We either take an entire item or reject


it completely . We can’t break an item to fill the knapsack.
Fractional knapsack: if the item can’t fit in to the
knapsack , break it and pick it partially to fil the knapsack.

11
DIFFERENT TECHNIQUES TO SOLVE 0-1
KNAPSACK PROBLEM:
 DYNAMIC PROGRAMMING O (N * CAPACITY)
 BRUTE FORCE ALGORITHM O(N*2^N)
 GREEDY ALGORITHM O(NLOGN)
 BRANCH AND BOUND
 GENETIC ALGORITHM

12
• LETS TAKE AN EXAMPLE AND SOLVE THEM
THROUGH DYNAMIC PROGRAMMING:

ite 1 2 3
4
ms
100 20 60
valu 40
es 3 2 4
weights 1

Max capacity

5
13
SOLVING KNAPSACK PROBLEM THROUGH
DYNAMIC PROGRAMMING:
• Create value table v[ i, w] where i (rows) denotes items and
w (columns) denotes weight.
• As there are 4 items so we have 5 rows from 0 to 4.
• Weight limit is 5 so we have 6 columns 0 to 5.
• Fill the first row i =0 with 0 this means when 0 item is
considered then value earned is 0
• Fills the first column w=0 with 0 this means when capacity
of knapsack is 0 then item considered is 0 and value earned
is 0. 14
ALGORITHM (RULES TO FILL TABLE):
for i=0 to N
for w=0 to M
If i=0 || w = 0 then,
V[ i , w ] = 0
else if wt [ i ] <= w then
V [ i , w ] = max (v[ i-1, w ] , val [i] + v [ i-1 , W – wt[ i ] ]
else wt [ i ] > w then ,
V [ i , w ] =V [ i-1 , w ]

15
Return V[ N , M ]
Value Table

V[i, 0 1 2 3 4 5 Max weight


w] 0 0 0 0 0 0
0
0 0 0 100 100 100
1
0 0 20 100 100 120
2
0 0 20 100 100 120
3
0 40 40 100 140 140 Max value
4

16
ITEMS THAT WERE PUT INSIDE THE KNAPSACK
ARE FOUND USING THE FOLLOWING RULE:

Set i = N & w=M


While i > 0 and w > 0 do
If v [ i , w] != v [ i-1,w] then ,
Mark the ith item
Set w = w - wt [ i ]
Set i = i-1
Else
Set i = i – 1 17
item 1 2 3
4
value
100 20 60
weight 40
3 2 4
1
V[i, 0 1 2 3 4 5
w]
0 0 0 0 0 0 So the items
0
0 0 0 100 100 100
are putting
1 inside the
0 0 20 100 100 120
2 knapsack are
0 0 20 100 100 120 4 &1
3
0 40 40 100 140 140
4

18
THE END
19

You might also like