SlideShare a Scribd company logo
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
1
S. Hassan Adelyar, Ph.D
Instructor of Computer Science Faculty
Kabul University
December 2020
Lesson XIV: Dynamic Algorithm Part III
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
2
Learning Outcome
 By the end of this lesson, students will be
able to:
 Understand dynamic programming
approach for solving problems.
 Understand the effectiveness of dynamic
Algorithms for solving problems.
 Implement dynamic Algorithms for
solving the Knapsack problem.
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
3
The Knapsack Problem
 Set of n items, where each item i is specified
by a size si and a value vi.
 We are also given a size bound S (the size of
our knapsack).
 The goal is to find the sub set of items of
maximum total value such that sum of their
sizes is at most S (they all fit into the
knapsack).
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
4
The Knapsack Problem
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
5
 Imagine you have an assignment with different
parts labeled A through G.
 Each part has a “value” (in points) and a “size”
(time in hours to complete).
 For example, say the values and times for our
assignment are:
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
6
 You have a total of 15 hours:
 Which parts should you do?
 If there was partial credit that was proportional to
the amount of work done (e.g., one hour spent on
problem C earns you 2.5 points) then the best
approach is to work on problems in order of
points/ hour (a greedy strategy).
 But, what if there is no partial credit? In that case,
which parts should you do, and what is the best
total value possible?
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
7
 We can solve the knapsack problem in exponential
time by trying all possible subsets.
 With Dynamic Programming, we can reduce this
to time O(nS).
 Using dynamic programming to solve this integer
programming problem is now straightforward.
 We use an (n+1) x (C+1) table to evaluate the
values of V[i,j].
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
8
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
9
Example
 Suppose that we have a knapsack of capacity
9.
 We have items of four different sizes 2, 3, 4,
and 5 and values 3, 4, 5 and 7, respectively.
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
10
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
11
 Example 2:
 Weights : 2, 3, 3, 4, 6
 Values : 1, 2, 5, 9, 4
 Knapsack Capacity (W) = 10
 From the above input, the capacity of the
knapsack is 10 kgs and there are 5 items to choose
from.
 A very simple idea is to evaluate each item and
find out if it should be included in the knapsack or
not in order to maximize the value.
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
12
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
13
class Knapsack {
// A utility function that returns maximum of two integers
static int max(int a, int b) {
return (a > b)? a : b;
}
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
14
static int knapSack(int W, int wt[], int val[], int n){
if (n == 0 || W == 0)
return 0;
if (wt[n-1] > W)
return knapSack(W, wt, val, n-1);
else return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-
1),
knapSack(W, wt, val, n-1));
}
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
15
public static void main(String args[]){
int val[] = new int[]{7,9,5,12,14,6,12};
int wt[] = new int[]{3,4,2,6,7,3,5};
int W = 15;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
}
}
Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
16
Summary
 In this presentation, we studied the
effectiveness of dynamic Algorithms for
solving problems. We implemented dynamic
Algorithms for solving the Knapsack problem.
 In the Knapsack problem, we have a set of n
items, & we are also given a size bound S. The
goal is to find the sub set of items of
maximum total value such that sum of their
sizes is at most S.
End of Lesson 14
Any Question?

More Related Content

PPTX
Greedy+Day-3.pptx
PDF
D05511625
PPTX
Lesson no 3 - Algorithm Analysis - II.pptx
PDF
Ada lab manual
PDF
Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...
PDF
DAA-LAB-MANFSDSDFSDFSDSDSFDSDAA LAB MANUAL FOR BTECH STUDENTSUAL2020-1.pdf
PDF
362939660 proyecto-matematica-basica-informe-docx
PDF
Recommendation Systems in banking and Financial Services
Greedy+Day-3.pptx
D05511625
Lesson no 3 - Algorithm Analysis - II.pptx
Ada lab manual
Comparison of Dynamic Programming Algorithm and Greedy Algorithm on Integer K...
DAA-LAB-MANFSDSDFSDFSDSDSFDSDAA LAB MANUAL FOR BTECH STUDENTSUAL2020-1.pdf
362939660 proyecto-matematica-basica-informe-docx
Recommendation Systems in banking and Financial Services

Similar to Lesson 14 - Dynamic Programming III.pptx (20)

PPTX
Mechanical Engineering Homework Help
PPT
Design and Analysis of Algorithm Brute Force 1.ppt
PDF
IRJET- Unabridged Review of Supervised Machine Learning Regression and Classi...
PPTX
Machine Learning for Modern Developers
PDF
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
PPTX
Dynamic Programming - Part 1
PDF
complexity analysis.pdf
PPT
5.3 dynamic programming 03
PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
PPTX
Design and Analysis of Algorithm-Lecture.pptx
PDF
PacMan Slides
PDF
Neural networks with python
PPT
data unit notes from department of computer science
PPT
topic27_classes_objects_1.ppt for beginners and idiots
PPT
UnitI (1).ppt
PPT
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
PPTX
Ms nikita greedy agorithm
PPTX
Presentation_23953_Content_Document_20240906040454PM.pptx
PPTX
3.3 the math object
Mechanical Engineering Homework Help
Design and Analysis of Algorithm Brute Force 1.ppt
IRJET- Unabridged Review of Supervised Machine Learning Regression and Classi...
Machine Learning for Modern Developers
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
Dynamic Programming - Part 1
complexity analysis.pdf
5.3 dynamic programming 03
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Design and Analysis of Algorithm-Lecture.pptx
PacMan Slides
Neural networks with python
data unit notes from department of computer science
topic27_classes_objects_1.ppt for beginners and idiots
UnitI (1).ppt
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
Ms nikita greedy agorithm
Presentation_23953_Content_Document_20240906040454PM.pptx
3.3 the math object
Ad

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
RMMM.pdf make it easy to upload and study
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Computing-Curriculum for Schools in Ghana
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Paper A Mock Exam 9_ Attempt review.pdf.
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
History, Philosophy and sociology of education (1).pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
Supply Chain Operations Speaking Notes -ICLT Program
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
What if we spent less time fighting change, and more time building what’s rig...
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
Weekly quiz Compilation Jan -July 25.pdf
Anesthesia in Laparoscopic Surgery in India
Computing-Curriculum for Schools in Ghana
Ad

Lesson 14 - Dynamic Programming III.pptx