Professional Documents
Culture Documents
ASSIGNMENT-1
Submitted by__
Mahmuda Rahman
ID: CSE 202001027
Question-1: Compute the optimal price for the 0-1 knapsack problem for knapsack capacity, W=7.
Which items will be in the optimal solution? What will be their total price? Show how you calculate the
optimal solution using a matrix containing both price and arrows for each cell in the matrix.
Item 1 2 3 4 5
Weigh 1 2 3 3 2
t
Price 10 17 13 15 18
Solution:
Weight 0 1 2 3 4 5 6 7
Item
0
0 0 0 0 0 0 0 0
1
0 10 10 10 10 10 10 10
2
0 10 17 27 27 27 27 27
3
0 10 17 27 27 30 40 42
4
0 10 17 27 27 32 42 42
5
0 10 18 28 35 45 45 50
Here, {2,4,5} items are taken to get the optimal solution and their total price is 50.
Question-2: Compute the Longest Common Subsequence (LCS) of the strings X = "AGGTAB" and Y =
"GXTXAYB". Show how you calculated the LCS using a matrix containing both the cost and the arrows for
each cell in the matrix.
Solution:
Y G X T X A Y B
0 0 0 0 0 0 0 0
A
0 0 0 0 0 1 1 1
G
0 1 1 1 1 1 1 1
G
0 1 1 1 1 1 1 1
T
0 1 1 2 2 2 2 2
A
0 1 1 2 2 3 3 3
B 1
0 1 2 2 3 3 4
Here, the Longest Common Subsequence (LCS) of the strings X = "AGGTAB" and Y = "GXTXAYB" is
“GTAB” of length 4.