UCS415 – Design and Analysis of Algorithms
Lab Assignment 3: Dynamic Programming
1. Given two strings, s1 and s2, the objective is to determine the Longest Common Subsequence (LCS).
Input: s1 = “AGGTAB”, s2 = “GXTXAYB”
Output: Longest Common Subsequence = “GTAB” .
2. Given the dimension of a sequence of matrices in an array arr[], where the dimension of the ith matrix
is (arr[i-1] * arr[i]), the task is to find the most efficient way to multiply these matrices together such
that the total number of element multiplications is minimum. Also, print the number of multiplications
performed. When two matrices of size m*n and n*p when multiplied, they generate a matrix of size
m*p and the number of multiplications performed is m*n*p.
Input: arr[] = {2, 1, 3, 4}
Output: Efficient way : (M1 x (M2 x M3)) and Multiplications performed= 20
Explanation: There are 3 matrices of dimensions 2×1, 1×3, and 3×4,
Let the input 3 matrices be M1, M2, and M3. There are two ways to multiply ((M1 x M2) x M3) and
(M1 x (M2 x M3)),
Please note that the result of M1 x M2 is a 2 x 3 matrix and result of (M2 x M3) is a 1 x 4 matrix.
((M1 x M2) x M3) requires (2 x 1 x 3) + (2 x 3 x 4) = 30
(M1 x (M2 x M3)) requires (1 x 3 x 4) + (2 x 1 x 4) = 20
The minimum of these two is 20.
3. Given N items where each item has some weight and profit associated with it and also given a bag
with capacity W, [i.e., the bag can hold at most W weight in it]. The task is to put the items into the
bag such that the sum of profits associated with them is the maximum possible. The constraint here
is we can either put an item completely into the bag or cannot put it at all. It is not possible to put a
part of an item into the bag.
Input: N = 4 (number of items), W = 7 (capacity of the bag), profit[] = {5, 3, 8, 6} (profits associated
with each item), weight[] = {2, 3, 4, 5} (weights associated with each item)
Output: Items selected: Item 1 and Item 3, Maximum profit: 13
4. Given a binary matrix mat of size n * m, the task is to find out the maximum length of a side of a
square sub-matrix with all 1s.
Input:
mat = [
[0, 1, 1, 0, 1],
[1, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0] ]
Output: 3
Explanation: The maximum length of a side of the square sub-matrix is 3 where every element is 1
Additional Questions
1. Given an array arr[] of length n and an integer target, the task is to find the subsets with a sum equal
to target.
Input: arr[] = {2, 3, 7, 8, 10}, target = 10
Output: All the possible subsets are [2, 8], [3, 7], [10]
https://www.geeksforgeeks.org/count-of-subsets-with-sum-equal-to-x/
2 . Given an array arr[] of size n, the task is to find the Longest Increasing Subsequence (LIS) i.e., the
longest possible subsequence in which the elements of the subsequence are sorted in increasing order.
Input: arr[] = [3, 10, 2, 1, 20]
Output: The longest increasing subsequence is [3, 10, 20] .
https://www.geeksforgeeks.org/longest-increasing-subsequence-dp-3/
3. Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird
person and follows a certain rule when looting the houses. According to the rule, he will never loot
two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows
which house has what amount of money but is unable to come up with an optimal looting strategy.
He asks for help to find the maximum money he can get if he strictly follows the rule. ith house has
hval[i] amount of money present in it.
Input: hval[] = {5, 5, 10, 100, 10, 5}
Output: Selected: {5, 100, 5}
https://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/
4. Given a fence with n posts and k colors, the task is to find out the number of ways of painting the
fence so that not more than two consecutive posts have the same color.
Input: n = 3, k = 2
Output: 6
Explanation: The following image depicts the 6 possible ways of painting 3 posts with 2 colors
https://www.geeksforgeeks.org/painting-fence-algorithm/
5. Given an array arr[] containing n positive integers, a subsequence of nums is called bitonic if it
is first strictly increasing, then strictly decreasing. The task is to find the longest bitonic
subsequence. Note: A strictly increasing or a strictly decreasing sequence should not be
considered as a bitonic sequence.
Input: arr[]= [12, 11, 40, 5, 3, 1]
Output : The Longest Bitonic Subsequence is {12, 40, 5, 3, 1}.
https://www.geeksforgeeks.org/longest-bitonic-subsequence-dp-15/