UNIVERSITY OF NAIROBI COURSE: MASTER OF SCIENCE IN COMPUTER SCIENCE LEVEL: YEAR I SEMESTER III NAME: VINCENT KIPNG’ETICH KOECH REG NO: P58/76055/2012 LECTURER: MR. BRIAN OMWENGA UNIT: DESIGN AND ANALYSIS OF ALGORITHMS ASSIGNMENT III ACADEMIC YEAR: 2012/2013 1 QUESTION I What is the time complexity of the bottom-up string alignment algorithm? Introduction: Once we formulate the solution to a problem recursively as in terms of its sub problems, we can try reformulating the problem in a bottom-up fashion: try solving the sub problems first and use their solutions to build-on and arrive at solutions to bigger sub problems. This is also usually done in a tabular form by iteratively generating solutions to bigger and bigger sub problems by using the solutions to small sub problems Time Complexity: Time complexity of optimal alignment is O(nm). o Proof: when computing value of the cell (i, j), only cells (i − 1, j − 1), (i, j − 1), (i − 1, j) are examined) constant time. There are (n + 1)(m+ 1) cells ) O(nm) time complexity. o When computing the value for a specific cell (i; j) only cells (i -1; j- 1), (i; j -1) and (i - 1; ) are examined, along with two characters Si and Tj. Hence, to fill in one cell takes a constant number of cell examinations and comparisons. There are n*m cells in the table. So the time complexity is also O(nm). Space complexity: Space complexity is O(n), if only M(x, y) is required, and O(nm) for the reconstruction of the alignment. o Proof: Row-wise computation of the matrix, for computing row k, only row k−1 must be stored) O(n) space. For reconstructing the alignment, all trace back pointers must be stored) O(nm) space. o Using the algorithm, computing the value of cell (i; j) involves one cell in row j (i - 1; j) and two cells in the previous row ((i - 1; j - 1) and (i; j - 1)). Since the computation is performed one row at a time, when computing the values in row k only row k - 1 has to be stored, using, O(n +m) space. In order to reconstruct the alignment from the recursion, pointers must be set for allowing the back-tracing, therefore, the space complexity is O(nm). 2 QUESTION II Create a top-down algorithm for the string alignment problem Introduction: This is the direct fall-out of the recursive formulation of any problem. If the solution to any problem can be formulated recursively using the solution to its sub problems, and if its sub problems are overlapping, then one can easily memoize or store the solutions to the sub problems in a table. Whenever we attempt to solve a new sub problem, we first check the table to see if it is already solved. If a solution has been recorded, we can use it directly, otherwise we solve the sub problem and add its solution to the table. Problem: To find the longest subsequence common to all sequences in two sets of sequences. If the two set were “DYNAMIC PROGRAMMING” and “ALGORITHM” the longest common subsequence is “AORI”. Let two sequences be defined as the following: a = (a1, a2, …, ax) b = (b1, b2, …, by) We could define the recursive relation by: The answer would be lcs(ax,by ). Initial Layout: 3 After a couple of iterations the table looks like the following. We are just about to fill the blue colored cell. a[i-1] and b[j-1] are the same alphabet ‘I’ so the blue cell is filled with 1+c[i-1, j-1] which is “2”. Finally, the table looks like this. Our optimal solution is “4” (the red colored cell). Expressing the Recursive Relation: function lcs(a, b) begin lcsr(a, b, 0, 0) end function lcsr(a, b, i, j) begin if a[i] = null or b[j] = null then return 0 else if a[i] = b[j] then return 1 + lcsr(i+1, j+1) else return max(lcsr(i+1, j), lcsr(i, j+1)) end 4 Using memoization to eliminate redundancy: function lcs(a, b) begin lcsr(a, b, 0, 0) end var m = 2D array (size of first set * size of second set) initialized to -1 function lcsr(a, b, i, j) begin if m[i, j] = -1 then if a[i] = null or b[j] = null then m[i, j] = 0 else if a[i] = b[j] then m[i, j] = 1 + lcsr(i+1, j+1) else m[i, j] = max(lcsr(i+1, j), lcsr(i, j+1)) end return m[i, j]** end QUESTION III What is the time complexity of the algorithm? In the memoized version above, our running time is now just O(mn). One easy way to see this is as follows. First, notice that we reach line (**) at most (I,j) times (at most once for any given value of the parameters). This means we make at most 2mn recursive calls total (at most two calls for each time we reach that line). Any given call of LCS involves only O(1) work (performing some equality checks and taking a max or adding 1), so overall the total running time is O(mn). Conclusion: Comparing bottom-up and top-down dynamic programming, both do almost the same work. The topdown (memoized) version pays a penalty in recursion overhead, but can potentially be faster than the bottom-up version in situations where some of the sub problems never get examined at all. These differences, however, are minor: you should use whichever version is easiest and most intuitive for you for the given problem at hand. 5 References: Wester, M., Kessens, J.M., and Strik, H. “Improving the Performance of a Dutch CSR by Modeling Pronunciation Variation,” Proc. of the Workshop Modeling Pronunciation Variation for Automatic Speech Recognition, Kerkrade, 145-150, 1998. Strik, H. and Cucchiarini, C. “Modeling Pronunciation Variation for ASR: Overview and Comparison of Methods,” Proc. of the Workshop Modeling Pronunciation Variation for Automatic Speech Recognition, Kerkrade, 137-144, 1998. Kessens, J.M., Wester, M., Cucchiarini, C., and Strik, H.“The Selection of Pronunciation Variants: Comparing the Performance of Man and Machine”, Proc. ICSLP, Sydney, Australia, 1998. Strik H., Russel A., Van den Heuvel, H., Cucchiarini, C., Boves, L. ”A Spoken Dialogue System for the Dutch Public Transport Information Service”, Int. Journal of Speech Technology, Vol. 2, No. 2, pp. 119-129, 1997. Booij G., The Phonology of Dutch: Clarendon press, Oxford, 1995. Cucchiarini, C., van den Heuvel, H. “/r/ Deletion in Standard Dutch”, Proc. of the Dept. of Language & Speech, University of Nijmegen, Vol. 19, pp. 59-65, 1995. Cucchiarini, C. “Assessing Transcription Agreement: Methodological Aspects”, Clinical Linguistics & Phonetics, Vol. 10, no.2, pp. 131-155, 1996. 6
© Copyright 2026 Paperzz