0% found this document useful (0 votes)
28 views

5.1 Dynamic Programming

Uploaded by

Suraj kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

5.1 Dynamic Programming

Uploaded by

Suraj kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Department of Computer Science and Engineering (CSE)

UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE
ENGINEERING
Bachelor of Engineering
Design and Analysis of
Algorithms(CSH-311/ITH-311)

Topic: Dynamic Programming DISCOVER . LEARN . EMPOWER

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Learning Objectives & Outcomes


Objective:
• To understand the importance of dynamic programming
and its applications

Outcome:
• Student will understand
 Concept of dynamic programming
 Applications of DP

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Dynamic Programming

• Dynamic Programming is also used in optimization


problems. Like divide-and-conquer method, Dynamic
Programming solves problems by combining the
solutions of subproblems. Moreover, Dynamic
Programming algorithm solves each sub-problem just
once and then saves its answer in a table, thereby
avoiding the work of re-computing the answer every
time.
• Two main properties of a problem suggest that the given
problem can be solved using Dynamic Programming.
These properties are overlapping sub-problems and
optimal substructure.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Steps of Dynamic Programming Approach

• Characterize the structure of an optimal solution.


• Recursively define the value of an optimal solution.
• Compute the value of an optimal solution, typically in a
bottom-up fashion.
• Construct an optimal solution from the computed
information.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Applications of Dynamic Programming Approach

• Matrix Chain Multiplication


• Longest Common Subsequence
• Travelling Salesman Problem

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

0-1 Knapsack

Problem Statement:
A thief is robbing a store and can carry a maximal weight
of W into his knapsack. There are n items and weight
of ith item is wi and the profit of selecting this item is pi.
What items should the thief take?

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Dynamic-Programming Approach
• Let i be the highest-numbered item in an optimal
solution S for W dollars. Then S' = S - {i} is an optimal
solution for W - wi dollars and the value to the
solution S is Vi plus the value of the sub-problem.
• We can express this fact in the following formula:
define c[i, w] to be the solution for items 1,2, … , i and the
maximum weight w.
• The algorithm takes the following inputs
• The maximum weight W
• The number of items n
• The two sequences v = <v1, v2, …, vn> and w = <w1, w2, …,
wn>
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Dynamic-0-1-knapsack (v, w, n, W)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Dynamic Programming – Subset Sum Problem


• Objective: Given a set of positive integers, and a
value sum S, find out if there exist a subset in array whose
sum is equal to given sum S.

Example:
int[ ] A = { 3, 2, 7, 1}, S = 6
Output: True, subset is (3, 2, 1}

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Recursive Approach
• For every element in the array has two options, either we
will include that element in subset or we don’t include it.
• So if we take example as int[] A = { 3, 2, 7, 1}, S = 6
• If we consider another int array with the same size as A.
• If we include the element in subset we will put 1 in that
particular index else put 0.
• So we need to make every possible subsets and check if
any of the subset makes the sum as S.
• If we think carefully this problem is quite similar to
“Generate All Strings of n bits”
Time Complexity: O(2n).

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Dynamic Programming (Bottom –Up)


Base Cases:
•If no elements in the set then we can’t make any subset
except for 0.
•If sum needed is 0 then by returning the empty subset we
can make the subset with sum 0.
Given – Set = arrA[], Size = n, sum = S
•Now for every element in he set we have 2 options, either
we include it or exclude it.
•for any ith element-
•If include it => S = S-arrA[i], n=n-1
•If exclude it => S, n=n-1.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Change making problem/Coin Change Problem


• In the coin change problem, we are basically provided
with coins with different denominations like 1¢, 5¢ and
10¢. Now, we have to make an amount by using these
coins such that a minimum number of coins are used.
• Let's take a case of making 10¢ using these coins, we can
do it in the following ways:
» Using 1 coin of 10¢
» Using two coins of 5¢
» Using one coin of 5¢ and 5 coins of 1¢
» Using 10 coins of 1¢

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Approach to Solve the Coin Change Problem


• Let's say Mn is the minimum number of coins needed to make
the change for the value n.
• Let's start by picking up the first coin i.e., the coin with the
value d1. So, we now need to make the value
of n−d1 and Mn−d1 is the minimum number of coins needed
for this purpose. So, the total number of coins needed
are 1+Mn−d1 (1 coin because we already picked the coin with
value d1 and Mn−d1 is the minimum number of coins needed
to make the rest of the value).
• Similarly, we can pick the second coin first and then attempt to
get the optimal solution for the value of n−d2 which will
require Mn−d2 coins and thus a total of 1+Mn−d2.
• We can repeat the process with all the k coins and then the
minimum value of all these will be our answer. i.e., mini:di≤n
{Mn−di+1}.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

REFERENCES
Text books:
•Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of
India, 3rd edition 2012. problem, Graph coloring.
•Horowitz, Sahni and Rajasekaran, “Fundamentals of ComputerAlgorithms”,
University Press (India), 2nd edition

Websites:
• https://www.tutorialspoint.com/design_and_analysis_of_algorithms/
design_and_analysis_of_algorithms_01_knapsack.htm
• https://algorithms.tutorialhorizon.com/dynamic-programming-subset-sum-
problem/
• https://www.codesdope.com/course/algorithms-coin-change/

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Summary

Dynamic programming:
•The general method
•0/1 knapsack
•Subset Sum problem
•Change making problem

University Institute of Engineering (UIE)


THANK YOU

University Institute of Engineering (UIE)

You might also like