Theory of Algorithms Assignment #6 -Sridhar Godavarthy (U32086851) A)a) 5 4 3 If we go by the algorithm, we would remove 5 and hence we would have an independent set of weight 5. However, the maximum possible weight is 7. Hence false. 8 3 2 9 b) If we go by the algorithm we would have got: even = 12, odd = 10 and would have chosen even( =12) as the independent set with maximum weight. However, the correct solution would have been nodes 8,9 = 17. Hence false. c) The recursion can be written as : OPT(j) = max[wj + OPT(j – 2), OPT(j – 1)] if j >1 0, if j <= 0 The algorithm: BuildIndependentSet M[j] -> 0 for all j <=0 For i = 1 to n M[i] -> max[wi + M(i – 2) , End GetIndependentSet Create Set I to store independent If n < 1 Return I Endif If M[n] = M[n – 1] Return GetIndependentSet(n – Else Return GetIndependentSet(n – End Run time analysis: M(i – 1)] set 1) 2) BuildIndependentSet calculates each element exactly once and has a run time O(n). GetIndependentSet either considers n-1th element or the n -2th element and in the worst case access each element of the array and has a run time of O(n). When we run both of them together, we have a complexity of O(n). Theory of Algorithms Assignment #6 -Sridhar Godavarthy (U32086851) B) a) 2 1 3 4 5 6 Maximum length path from v1 to v6 = 4. By the algorithm = 2 Hence false. b) Recurrence can be stated as: OPT(j) = 1 + max(OPT(i)) where i is every node that has an incoming edge into j such that there is a edge from i->j. 0 if no edges into j ( if j <= 1) Algorithm: Set M[i] = 0 for all i to n For i = 2 to n max (1 OPT ( j )) M[i] = j |e ( j ,i ) End For Run time analysis: The loop runs once for every node. The operation within the loop depends on the number of edges going out of this node and in the worst case can be run n times when every node has an edge into every larger node. The complexity can be given by O(n) x O(n) = O(n2) Theory of Algorithms Assignment #6 -Sridhar Godavarthy (U32086851) C) Algorithm: For every edge ij rij -> - log rij end Run Bellman ford algorithm. Perform Relaxation once more to see if there is a negative weight cycle.If there is a cycle with negative weight, we have an opportunity cycle, return the cycle. Run time analysis: The initial reassignment takes O(E) time. The Bellman Ford algorithm is polynomial in time. Hence, the overall run time is polynomial. Correctness: Effectively, we are trying to find a cycle such that the product of the edge weights > 1. This is the case where we get a profit. In a cycle with 4 edges, we need: r1 x r2 x r3 x r4 > 1 Taking log, log r1 + log r2 + log r3 + log r4 > 0 In order to apply the Bellman Ford algorithm we need negative weights and the weight of the cycle should be negative.. Hence we negate the values to be able to apply this algorithm, without impacting the result: -logr1 –log r2 –log r3 –log r4 < 0 Theory of Algorithms Assignment #6 -Sridhar Godavarthy (U32086851) D) Recurrence: OPTin(j) = max[aj + OPTin(j – 1), aj] OPT(j) = max[OPTin(j), OPT(j – 1)] Algorithm: Set Min[i] = 0 for i-> 0…n Set M[i] = 0 for i-> 0…n For i = 1 to n Min[i] = max( a[i] + Min[i – 1), a[i]) End For i = 1 to n M[i] = max( Min[i], M[i - 1]) End M[n] is the maximum possible sum. Define Set S to contain the elements forming the maximum subsequence and set it to empty. Set Smax-> M[n] In the Min array, locate element with index i that has value = Smax If Smax <=0// all elements are negative or zero. Add the maximum of the input sequence to S. else while a[i] >= 0 Add element a[i] to set S i-> i -1 end while end if return S as the maximum subsequence. Run time analysis: Determination of Min array takes O(n) time as we go through each element exactly once and the comparison and addition are constant time operations Determination of M array also takes O(n) time as we go through each element of the Min array exactly once. The comparison operation is constant time. If the sum is zero, we have an O(n) operation to find the maximum of the input sequence. If the sum is non-zero, in the worst case scenario, if the whole input sequence is to be included, we would have to traverse the array exactly twice. Hence it takes O(n). Overall complexity = O(n) + O(n) + O(n) = O(n) and is hence linear Theory of Algorithms Assignment #6 -Sridhar Godavarthy (U32086851) I found another approach which reeks of Dynamic programming, but could not find the recursion. I am describing it below in the hope that it is dynamic programming too: Let A -> Array of numbers Smax -> 0 , Scurr->0 , L->0 , R->0 , Lmax->0 , Rmax->0 While R <= a.length Scurr-> Sum of all elements from a[L] to a[R] If Scurr >= 0 If scurr > smax Smax -> scurr Lmax->L Rmax->R Endif R->R+1 Else Scurr -> 0 L-> R + 1 R -> R + 1 Endif End while If Smax = 0 // All elements are <= 0 Return maximum(A) and the index of this elelement. else Return Lmax and Rmax Run Time Analysis: The loop visits each element of the array exactly once has a order O(n) The comparisons within the loop are of constant time. At the end, if all elements are <=0, finding the maximum element and the index is O(n). Return is a constant time op. Overall time complexity = O(n) and is linear in time. Theory of Algorithms Assignment #6 -Sridhar Godavarthy (U32086851) E) Recurrence: OPT(i,j) = max[OPT(i + 1,j), OPT(i, j – 1) ] if ai != aj max[OPT(i + 1,j), OPT(i, j – 1), 2 + OPT(i +1, j – 1)] if ai = aj 1 if i = j Algorithm: Let A be the input string and n be the length of A Set M[i,j] = 1 for i = 1..n and j = 1..n For i = 2 to n For j = i to n If A[i] = A[j] M[i,j]=max(M[i,j – 1],M[i -1, j],2+M[i – 1, j – 1]) Else M[i,j] = max(M[i, j – 1], M[i -1, j ]) End End End Return M[1,n] Run Time Analysis: Initial assignment is O(n2) Outer Loop runs n times with O(n) Inner loop iterates over the outer loop and hence has a worst case time of O(n). Inside the inner loop, there can be a maximum of three comparisons which is a constant time op O(3) Return is only a memory lookup and hence is a constant time op. Overall complexity = O(n2) + O(n) x O(n) x O(3) + O(1) = O(n2)