Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Assignment of Dynamic Programming

Assignment 1: Rod Cutting Problem


Given a rod of length n, and an array that contains the prices of all the pieces smaller than n,
determine the maximum profit you could obtain from cutting up the rod and selling its pieces.

Suppose that we have a rod of length 8, and an array containing the length(1,2,3,….. 7 ) and
price(4,3,2,1,5,2,4) of the pieces.

Solve the above problem using dynamic programming.

Assignment 2: Longest Common Subsequence(LCS)

Find LCS between two given strings X=’AABCABB’ and Y=’BCABABB’

Assignment 3: Longest Palindromic Subsequence(LPS)

Find LPS between two given strings X=’AABCABB’

Assignment 4: Increasing Subsequence with maximum sum

Find the longest subsequence from X=[11,5,13,2,1,7,8,21,23,12,15] whose sum is maximum


and elements are in ascending order.

Assignment 5: Matrix Chain Multiplication(MCM)

Find optimal matrix chain multiplication of 4 matrices ABCD whose dimensions are 5x7, 7x9,
9x3 and 3x5.

Assignment 6: Shortest Path

Imagine that you have 5 friends: A, B, C, D, and E. You know a few roads that connect some
of their houses, and you know the lengths of those roads.

a. Find shortest path from A


b. Find shortest path between every pair of vertices

13 B
6
6
A 2
C

E D
5

Assignment 7: Shortest Path

Imagine that you have 5 friends: A, B, C, D, and E. You know a few roads that connect some
of their houses, and you know the lengths of those roads.

a. Find shortest path from A


b. Find shortest path between every pair of vertices

13 B

6
6
A 2
C

1
-13
E D
5

Assignment 8: Travelling Salesman Problem

Imagine that you have 5 friends: A, B, C, D, and E. You know all roads are connected, and you
know the lengths of those roads. Find shortest tour in given graph.

B
A
C
E

Distances are: A=[2 5 1 3 7


7 1 3 9 15
12 3 6 8 1
2 7683
4 6 8 15 9]

Assignment 9: 0/1 Knapsack

Assume that we have a knapsack with max weight capacity W = 5


Fill the knapsack with items such that the benefit (value or profit) is maximum.

Following table contains the items along with their value and weight.

item i 1 2 3 4

value val 100 20 60 40

weight wt 3 2 4 1

Total items n = 4
Total capacity of the knapsack W = 5

Assignment 10: Optimal binary search tree

Find OBST for given probabilities

• Probability table (pipi is the probabilty of key kiki:

ii 1 2 3 4 5
kiki k1k1 k2k2 k3k3 k4k4 k5k5
pipi 0.25 0.20 0.05 0.20 0.30
Assignment 11: Given two strings, s1 and s2 and edit operations (given below). Find minimum
number operations required to convert string s1 into s2.

Allowed Operations:

▪ Insertion – Insert a new character.


▪ Deletion – Delete a character.
▪ Replace – Replace one character by another.

Strings are:
S1=’Kitten’ S2=’Sitting’
S1=’Intension’ S2=’Execution’

Assignment 12: Maximum Subarray Problem(Don’t do it with Kadane’s algorithm)


find the sum of contiguous subarray within a one-dimensional array of numbers which has the
largest sum.
A=[3 50 45 -21 -33 0 9 -56 9 80 76 -88 56 78 69]

Assignment 13: Given a matrix of size MxN whose elements are non-negative. Count number
of paths to reach last cell M-1xN-1 to first cell 0x0 along with cost of path and cost of path
should be optimal. We can move one unit down or one unit right at a time.

A=[ 4 7 8 6
6739
3812
7 1 7 3]

Assignment 14: Given an array of integers, find the element which appears maximum number
of times in the array.
Example:

int [] arrA = {4, 1, 5, 2, 1, 5, 9, 8, 6, 5, 3, 2, 4, 7}

Assignment 15: Given an array of integers, find the sum of any three elements which is closest
to zero. The array may contain positive and negative elements.

Example:

Given Input: [-1, 4, -2, 5, 10, -5]

Minimum Sum with three elements is: 1

Explanation: -1, 4, -2 sums to -1


Given Input: [-1, 4, -2, 5, 10, -5, 1]

Minimum Sum with three elements is: 0

Explanation: 1, 4, -5 sums to 0

You might also like