CS331 Advanced Data Structures – Spring 2016 Due: Feb. 24

advertisement
CS331 Advanced Data Structures – Spring 2016
Homework 2 – 55 points
Due: Feb. 24
1. (10 points) Modify the O(nm) algorithm that we wrote to solve the n-m string problem so
that it runs in time O(n). HINT: In order to do this you must do O(1) work when filling in
each location of the array.
2. (10 points) In the dynamic programming algorithm to solve the matrix chain multiplication
problem, we repeatedly evaluate expressions of the form N (i, k) + N (k + 1, j) + di dk+1 dj+1 .
Develop a summation formula for the number of times these expressions are evaluated. Obtain
an exact closed form and big-O form for this summation.
3. (5 points) In the above problem, the number of multiplications used by the algorithm is twice
the number of steps, since at each step it takes two multiplications to evaluate di dk+1 dj+1 .
Explain how this number can be reduced without increasing the O(n2 ) space requirements of
the algorithm. Determine the number of multiplications your optimized algorithm uses.
4. (10 points) In the matrix chain multiplication problem, we can recover the optimal parenthesization by storing the optimal value of k in each table location along with the optimal number
of multiplications. Write a simple recursive procedure that uses these k values to output the
parenthesization. For example, with four matrices, your output might look like ((1(23))4)
or ((12)(34)). You may assume that the value of k is stored in an array paren[][].
5. (20 points) Consider the following problem: You are in the upper-left hand corner of an n×m
grid and need to get to the lower-right hand corner, where a valuable treasure is stored. You
can only move down and to the right. You would like to know how many paths there are to
get to the lower-right hand corner. We’ll use the convention that you start in location (n, m)
and are trying to get to (1, 1).
(a) Let C(i, j) be the number of paths from square (i, j) to the lower-right hand corner.
Develop a recurrence relation for C(i, j), complete with one or more base cases.
(b) Write a program to solve this problem.
(c) Determine C(20, 20). Determine the smallest area grid which has over one million paths
in it (you may want to use an Excel spreadsheet for this).
(d) Developed closed forms for C(1, j), C(2, j), C(3, j) and C(4, j).
Download