MT2 Exam CMPE-371 10.01.2015 (120 min, 100 points) St. Name, Surname______________________________________ St.Id#_____________ Group___________________________ Instructors Gr1 - Asst. Prof. Dr. Adnan Acan Gr2 – Assoc. Prof. Dr. Alexander Chefranov Note: Use the last sheet for your calculations Q1 (30 points). For the matrices A1(3,4), A2(4,5), A3(5,4), A4(4,6) with sizes given in the parenthesis, use Dynamic programming to define minimal number of floating point multiplications to calculate the product of the matrix chain A1โA2โA3โA4. a) (5 points). Write out a recursive formula for calculation of the optimal complexity 0, ๐ = ๐ ๐๐๐ = { min (๐๐๐ + ๐๐+1,๐ + ๐๐−1 ๐๐ ๐๐ ) ๐≤๐<๐ b) (5 points). Write out array p=(3,4,5,4,6) c) (5 points). What two matrices are to be filled? What do they contain? What are the sizes of the matrices? How they are to be initialized? In what order their elements are to be filled? Matrices (๐๐๐ ) and (๐ ๐๐ ), the former matrix contains optimal cost values, and the latter one contains values of k giving minimum. The matrices have 4 rows and 4 columns. The matrix m is initialized by zeroes on the main diagonal. Elements of the matrices are filled by diagonals d) (5 points). Fill in the matrices m 0 60 120 0 80 0 s 192 176 120 0 1 2 2 3 3 3 I=1,j=2, k=1, m12=m11+m22+p0*p1*p2=0+0+3*4*5=60 I=2, j=3, k=2, m23=m22+m33+p1*p2*p3=0+0+4*5*4=80 I=3,j=4,k=3,m34=m33+m44+p2*p3*p4=0+0+5*4*6=120 I=1,j=3,k=1, m11+m23+p0*p1*p3=0+80+3*4*4=128 K=2,m12+m33+p0*p2*p3=60+0+3*5*4=120 M13=120, k=2 I=2, j=4, k=2, m22+m34+p1*p2*p4=0+120+4*5*6=240 K=3,m23+m44+p1*p3*p4=80+0+4*4*6=176 M24=176,k=3 1 I=1, j=4, k=1, m11+m24+p0*p1*p4=0+176+3*4*6=248 K=2, m12+m34+p0*p2*p4=60+120+3*5*6=270 K=3, m13+m44+p0*p3*p4=120+0+3*4*6=192 M14=192, k3 e) (5 points). What is the minimal computational complexity of the matrix chain? 192 f). (5 points). Show the optimal parenthesization (A1*A2)*A3)*A4 Q.2. (30 points). Let the knapsack capacity is W=50 kg, and five items are given Item# 1 2 3 4 Weight (wi, 10 30 20 10 kg) Value (vi, $) 20 40 60 45 5 30 20 a) (20 points). Use Dynamic programming to solve the 0-1 Knapsack problem given above a.1) (4 points). Write out the recursive formula used in the method ๐(๐, ๐ค) = { ๐(๐ − 1, ๐ค), ๐ค๐ > ๐ค maxโก(๐(๐ − 1, ๐ค), ๐(๐ − 1, ๐ค − ๐ค๐ ) + ๐ฃ๐ a.2) (4 points). Define the size of the matrix to be used in the method. What it shall contain? How it shall be initialized? In what order its elements are to be filled? The matrix has 6 rows for 0..5 items, and 6 columns for weights 0,10,20,30,40,50. It shall contain best solution value b(i,w) for i items and w weight of a knapsack. It shall be initialized by zeroes in the first row and column. It shall be filled by rows a.3) (4 points). Fill in the matrix i\w 0 10 0 0 0 1 0 20 2 0 20 3 0 20 4 0 45 5 0 45 20 0 20 20 60 65 65 30 0 20 40 80 105 105 40 0 20 60 80 125 125 50 0 20 60 100 125 125 2 a.4) (4 points).What is the maximal value (in $) the knapsack can have? 125 a.5) (4 points).What items are in the optimal solution of the 0-1 Knapsack problem? 4,3,1 b) (10 points). Use Greedy algorithm to the Fractional Knapsack problem given above b.1) (4 points). How the items are to be sorted? Show the items after sorting They shall be sorted by their relative value decreasing. After sorting (relative value in parenthesis): Item4(4.5), Item3(3),Item1(2), Item2(4/3),Item5(2/3) b.2) (4 points).What is the total value of the items placed in the knapsack? 125+40/3=138.33 b.3) (2 points). What items are placed in the knapsack? 4, 3, 1, and 1/3 of item 2 3 Q3 (30 points). Use Dynamic programming to find the Longest Common Subsequence of X=<a b c a d e f >, Y=<c d f e a b d> a) (5 points).Write out recursive formula for the problem solving 0, ๐๐โก๐ = 0โก๐๐โก๐ = 0 ๐๐๐ ๐−1,๐−1 + 1, ๐๐โก๐ฅ๐ = ๐ฆ๐ ๐๐๐ ๐๐ = { max(๐๐๐ ๐−1,๐ , ๐๐๐ ๐,๐−1 ) , ๐๐กโ๐๐๐ค๐๐ ๐ b) (5 points). What is the size of the matrix to be constructed? How its rows and columns shall be labeled? What is the contents of the matrix? Matrix is to have 8 rows (0..7) labeled by X characters and 8 columns (0..7) labeled by Y characters. It will contain lcs(I,i) values and arrows allowing reconstructing LCS c) (5 points). Fill the matrix x\y 0 c d 0 0 0 0 A 0 0↑ 0↑ B 0 0↑ 0↑ C 0 1\ 1← A 0 1↑ 1↑ D 0 1↑ 2\ E 0 1↑ 2↑ F 0 1↑ 2↑ f 0 0↑ 0↑ 1← 1↑ 2← 2↑ 3\ e 0 0↑ 0↑ 1← 1↑ 2← 3\ 3↑ a 0 1\ 1↑ 1↑ 2\ 2↑ 3← 3↑ b 0 1← 2\ 2↑ 2↑ 2↑ 3← 3↑ D 0 1← 2← 2↑ 2↑ 3\ 3↑ 3↑ d) (5 points). What is the length of the longest common subsequence? 3 e) (5 points). How the longest common subsequence is to be constructed using contents of the matrix? What is the meaning of signs”↑ “, “← “, “โ” used when filling the matrix? It is constructed from the end. We go out from the write bottom corner first by ↑ go up, second turn by ← go left until coming to a cell with \. A character marking that row and column is inserted before the last selected character, or just placed if it is the first selected. Then we go by diagonal to the left and above cell and the procedure repeats until we come to the first row or the first column. f) (5 points). Write out the longest common subsequence. Note: When constructing the longest common subsequence, use ”↑ “ first turn, and “← “ second turn Abd 4 Q4 (10 points). Consider the set of five activities Activity name a1 a2 a3 a4 Start 0 6 5 4 Finish 4 12 9 8 Use a greedy algorithm to select a maximal set of non-overlapping activities a5 4 6 a) (3 points). How the activities are to be sorted? They are sorted by the finish time b) (4 points). Write out sorted activities Activity name a1 Start 0 Finish 4 a5 4 6 a4 4 8 a3 5 9 a2 6 12 c) (3 points). What activities are selected? List their names as they are shown in the table above A1, a5, a2 5