COSC413 Examination on Advanced Algorithms 2 October 2006, 2:10-4:10 pm Lecturer: Prof. T. Takaoka Open book, calculators are allowed (1) Transform the following satisfiablity problem into the corresponding colorability problem, and discuss the corresponding solutions. F = (x1 + x2 + x3’)(x1 + x2’)(x1‘+ x3) Note. x’ is the negation of x, multiplication is ‘and’, and addition is ‘or’. Do not seek possibility of attaching the special color to the outermost vertices of the graph. (2) Transform the above satisfiability problem into the corresponding clique problem, and discuss the corresponding solutions. (3) Let a difference equation be defined by x(0) = 0, x(1) = 1 x(n+2) = 2x(n+1) + 2x(n) (n=0, 1, ...) (3.1) Obtain the analytical solution for this equation. Hint. Solve the characteristic equation r2 –2r -2 =0. The general solution is given by c1 rn + c2 rn. Using the initial condition, determine c1 and c2 (3.2) Transform the equation into a vector-matrix form as follows: x(n+1) = 2x(n) + 2x(n-1) This is transformed to the following. (x(1), x(0)) = (1, 1) (x(n+1), x(n)) = (x(n), x(n-1)) a11 a12 a21 a22 = (x(n), x(n-1)) A Obtain the matrix A. Then we have (x(n+1), x(n)) = (x(1), x(0)) An. Obtain x(8) by using the analytical solution, repeated use of the equation 7 times and the repeated squaring of A three times, and confirm that you have the same results. Note. Any linear recurrence of the above type is O(logn) computable. (4) The Euclidean algorithm for greatest common divisors is given as follows: For a > b > 0 such that a = bq + r, let a = r(0), b = r(1), q = q(1) and r = r(2). We repeat division as follows: a = b*q(1) + r(2), 0 <= r(2) < b b = r(2)q(2) + r(3), 0 <= r(3) < r(2) ... r(i-1) = r(i)q(i) + r(i+1), 0 <= r(i+1) < r(i) ... r(n-1) = r(n)q(n) + r(n+1), r(n+1) = 0 gcd(a, b) = r(n) (4.1) Trace this algorithm with a = 105 and b = 56. (4.2) Define sequences c and d by c(0) = 0, c(1) = 1, c(i) = c(i-2) - q(i-1)c(i-1) d(0) = 1, d(1) = 0, d(i) = d(i-2) - q(i-1)d(i-1). Then we have a*d(i) + b*c(i) = r(i) for i=0, ..., n. By tracing sequences c and d, compute 8-1 mod 15 in the range of {1, ..., 14} and 15-1 mod 8 in the range of {1, ..., 7}. (5) We show the knapsack problem is solved in pseudo-polynomial time by dynamic programming as follows: Let positive 2n+1 integers a[1], a[2], …, a[n], w[1], w[2], …, w[n], and b be given. Obtain a subset I of the index set N = {1, ..., n} such that Max ∑ a[i] subject to iI ∑ w[i] ≦ b iI We can say the size of the problem is given by n and b. a[i] is the profit of item i and w[i] is the weight of item i. b is the maximum limit of the knapsack. We try to maximize the total profit by packing items into the knapsack. Let f[i][j] be the solution of the above problem with n=i and b=j, that is, f[i][j] is the solution for the problem of size i and j. That is, we use items 1, …, i, and the the limit of the knapsack is j. We compute f[n][b] by computing f[i][j] from smaller values of i and j step by step based on the following principle of optimality. f[0][j] = 0 (j=1, .., b), f[i][j]=0 for j = 0, f[i][j] = -999 for j < 0 f[i][j] = max{f[i-1][j], f[i-1][j-w[i]]+a[i]} If we pack item i for f[i][j], the optimal packing of items up to i-1 must give the optimal solution for the knapsack of limit j-w[i]. If item i is not packed, f[i][j] must be the optimal solution for the problem for items up to i-1, and the limit j, that is, f[i-1][j]. We take the maximum of these two. Example. a = (10, 6, 12, 9, 29), w = (3, 7, 2, 2, 3), b = 8. a w j 0 1 2 3 4 5 6 7 8 i -----------------------------------------------------------0 0 0 0 0 0 0 0 0 0 1 10 3 0 0 0 10 10 10 10 10 10 2 6 7 0 0 0 10 10 10 10 10 10 3 12 2 0 0 12 12 12 22 22 22 22 4 9 2 0 0 12 12 21 22 22 31 31 5 29 3 0 0 12 29 29 41 41 50 51 In the above table, we trace the bold face characters from the bottom right corner, that is, 51, the solution. As the weight of item 5 is 3, we reduce the limit of knapsack by 3, that is, 51 = max{f[4][8], f[4][5]+29} = max{31, 51}. By similar trace, we know items 1, 3, and 5 are used for the optimal solution. (a) Trace the algorithm with the same a and w, but b=10. Hint. Expand the above table. (b) The problem here is to develop a program that finds not only the maximum profit 51, but items 1, 3, and 5 that give this maximum. The following is the program for the maximum profit, in which declarations of variables are omitted. Enhance this program so that it can give the selected items for the maximum profit. The value -999 is for minus infinity. int max(int x, int y){ if(x>=y)return x; else return y; } main(){ for(j=1;j<=b;j++)f[0][j]=0; for(i=1;i<=n;i++){ for(j=0;j<=b;j++){ if(j-w[i]>=0) y=f[i-1][j-w[i]]; else y=-999; f[i][j]=max(f[i-1][j], y+a[i]); } } printf("\n %d ",f[n][b]); } Explain your idea in English, and then write a C progam. (c)Another program based on recursion is given below. Discuss the performance of the two algorithms in terms of the sizes of the problem. “sol” is the maximum profit. “vector” is to show which items to pack. binary(int k, int x, int y){ int i; if(k+1<=n){ if(y+w[k+1]<=b){ g[k+1]=1; binary(k+1, x+a[k+1], y+w[k+1]); } g[k+1]=0; binary(k+1, x, y); } else if(x>sol) {sol=x; for(i=1; i<=n; i++)vector[i]=g[i];} } main(){ sol=0; binary(0, 0, 0); printf("sol= %d ",sol); for(i=1; i<=n; i++)printf(“%d “, vector[i]); }