VIEWS: 196 PAGES: 5 POSTED ON: 4/17/2010 Public Domain
Quiz #3 Introduction to Algorithms (5/20/2009) Name: ID#: Score: 1. (28%) Please use bottom-up recursive dynamic programming algorithm to find the optimal solution in the following problems. Please show your answers in the dynamic programming tables, filled by applying recurrence relations are shown in text book. a. (18%) Give a chain of matrix A1A2A3A4 their dimension as following, parenthesize the product A1A2A3A4 to minimize the number of scalar multiplications. Please fill m(i, j) table as the minimum number of multiplications to compute matrix, and s(i, j) table as value of k, where 1≦ k <4 such that the optimal parenthesization of A1A2A3A4 splits between Ak and Ak+1. matrix dimension A1 2x3 A2 3x5 A3 5x4 A4 4x2 Answer: 1. m(i, j) table j i 1 2 3 4 1 0 30 70 82 2 0 0 60 70 3 0 0 0 40 4 0 0 0 0 2. S(i, j) table j i 2 3 4 1 1 2 1 2 0 2 2 3 0 0 3 b. (10%) Let us consider the 0-1 knapsack problem given by the following data with knapsack’s capacity = 5, please find the most valuable subset of the items that fit into the knapsack. Item weight value 1 1 2 $12 2 1 $10 3 3 $20 4 2 $15 Answer: Capacity W Item 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 12 12 12 12 2 0 10 12 22 22 22 3 0 10 12 22 30 32 4 0 10 15 25 30 37 2. (30%) A carpenter has a piece of wood of a certain length that must be cut at positions a1, a2 ..., an where ai is the distance from the left end of the original piece of wood. Notice that after making the first cut, the carpenter now has two pieces of wood; after making the second cut, the carpenter has three pieces of wood, etc. Assume that the cost of making a cut in a piece of wood of length l is equal to l, and is the same no matter which position in that piece of wood is being cut. Let L be the length of the original piece of wood. a. [5 points] Derive the recurrence relation which could be used to design a recursive algorithm to find the minimum total cost for making all the cuts. We define that: C[i,j] is the minimum cost of the cut of the wood whose end positions are i and j respectively. lij is the length of the wood whose end positions are i and j respectively. A recurrence is defined as follows: 0 if i j 1 C[i, j ] min C[i, k ] C[k , j ] l if i j 1 . i k j ij b. [9 points] Describe a top-down recursive dynamic programming algorithm to find the minimum total cost for making all the cuts that minimizes the total cost. 2 TD-Carpenter(i, j) initialize c[i,j] = 0 if i≦j+1 otherwise c[i,j]= NIL if ( c[i,j] = NIL) temp1= TD-Carpenter (i,i+1)+ TD-Carpenter(i+1,j) for (k=i+2; k<j ;k++) temp2= TD-Carpenter (i,k)+ TD-Carpenter(k,j) if (temp2<temp1) temp1=temp2 c[i,j]=temp1+lij return c[i,j] c. [16 points] Give a 100 meter wood with 4 cut points at position a1, a2 ..., a4 = {12, 25, 56, 81} meter, use bottom-up recursive dynamic programming algorithm to find the minimum total cost for making all the cuts and the order in which to make the cuts that minimizes the total cost. Answer: 1. Cost table j i 0 (0) 1 (12) 2 (25) 3 (56) 4 (81) 5 (100) 0 0 0 25 81 162 225 1 0 0 0 44 103 176 2 0 0 0 0 56 119 3 0 0 0 0 0 44 4 0 0 0 0 0 0 2. Cut table j i 1 2 3 4 5 0 0 1 2 2 or 3 3 1 0 0 2 3 3 2 0 0 0 3 3 3 0 0 0 0 4 4 0 0 0 0 0 3. (10 points) Consider the following problem: You have two computers C1 and C2 , and a set of n programs, {P1, P2 ,..., Pn } to be run. Let Ti be the time required to run program Pi . Assume that 3 each computer runes one program at a time. You are to assign the programs to the computers so that the total execution time for all the programs is minimized. a. (7 %) Give a greedy algorithm to solve the problem. Answer: First, sort the programs by their execution times. Then, assign the programs one-by-one to the computers, starting from the longest program and proceeding to the shortest, each time placing the next longest program on the computer that has the least execution time. The running time of the greedy algorithm is dominated by the time required to sort the programs. b. (3 %) What is the running time of your proposed algorithm? Answer: If we use an O(n log n) sort, the running time of the algorithm is O(n log n) 4. (10%) Construct a Huffman tree and Huffman codes for the letter a, b, c, d, e, and f with corresponding to frequencies 6, 11, 7, 9, 18, and 2. a : 1011 b : 01 c : 100 d : 00 e : 11 f : 1010 5. (10%) The following statement is true or false. Please explain your answer. “If a dynamic programming problem satisfies the optimal substructure property, then a locally optimal solution is a globally optimal. Answer: False. A greedy problem has a property which a locally optimal solution is a globally optimal. 6. (12%) Let G = (V, E) be a connected, undirected graph with edge-weight function w : E -> R , and assume all edge weights are distinct. Consider a cycle (v1, v2,...,vk, vk+1) in G, where vk+1 = v1, and let (vi, vi+1) be the edge in the cycle with the largest edge weight. Prove that (vi, vi+1) does not belong to the minimum spanning tree T of G. (Hint: Proof by contradiction) 4 Answer: Proof by contradiction. Assume for the sake of contradiction that (vi,vi+1) does belong to the minimum spanning tree T. Removing (vi,vi+1) from T divides T into two connected components P and Q, where some nodes of the given cycle are in P and some are in Q. For any cycle, at least two edges must cross this cut, and therefore there is some other edge (vj ,vj+1) on the cycle, such that adding this edge connects P and Q again and creates another spanning tree T’. Since the weight of (vj,vj+1) is less than (vi,vi+1), the weight of T’ is less than T and T cannot be a minimum spanning tree. Contradiction. 5