


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Spring 2001;
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



CS231 Algorithms Handout # Prof Lyn Turbak April 12, 2001 Wellesley College
PROBLEM SET 7 Due: Thursday, April 19
Reading: CLR Chapter 16 (Dynamic Programming); CLR Chapter 17, Sections 17.1-- 17.3 (Greedy Algorithms)
Suggested Problems: 16.1-1; 16.3-1; 16.3-6; 16-2; 16-3; 16-4; 17.1-1, 17.1-3, 17.2-2, 17.2-3, 17.2-4, 17.2-5, 17.2-6; 17.3-1, 17.3-8; 17-
Problem 1 [15] Use the memoized longest common subsequence (LCS) algorithm presented in class to determine all of the longest common subsequences of the sequences BACCABABC and ACBABCCAB.
As a concrete example of a concise way to present your results, here is a solution to CLR 16.3-1, which asks for the LCS of 10010101 and 010110110:
0 0 0
0
0
1
1
1
1 1 1
2 2 2
2
2 3 3
(^33)
3 4
5
(^4 )
4
5
(^55)
6
(^66)
4
In the table, each diagonal line corresponds to a "prepend" step, and each pair of a vertical and horizontal line corresponds to a "max" step. The numbers in each box indicate the length of an LCS starting from that point. Note that only those slots of the table actually visited by the memoized algorithm are actually filled in.
From the table it is possible to read of the following length-6 LCSs of the two sequences:
010101 101101 101011 101010 100110
Problem 2 [25] Solve CLR 16.3-5 (p. 319) using the two strategies described below. Assume that the input is stored in an array A[1..n]. For each strategy:
a[10] Develop a solution that uses the Θ(mn) LCS algorithm as a black box as part of the solution. Note: you can express a solution of this form in just a few lines!
b[15] Develop a solution that does not use the LCS algorithm as a black box, but instead uses an auxiliary array M[1..n], where each M[i] stores the longest monotonically increasing sequence in A[i..n] that begins with A[i].
Problem 3 [30]
A binary tree is either (1) a leaf or (2) a node with left and right subtrees. (In this definition, nodes do not carry values.) The following function counts the number of distinct binary trees with n nodes:
Count-Trees (n) = if n = 0 then return 1 {There is one leaf} else count <- 0 for i = 0 to n-1 do count <- count + (Count-Trees(i) * Count-Trees((n-1)-i)) return count
For example,
Count-Trees(0) = 1 Count-Trees(1) = 1 Count-Trees(2) = 2 Count-Trees(3) = 5
Count-Trees(n) is also known as the n th Catalan number. It can be shown that Count- Trees(n) takes time exponential in n (CLR 13-4, p. 262).
a[10] Write pseudocode for a memoized version of Count-Trees.
b[10] Write pseudocode for a dynamic programming solution to Count-Trees.
c[5] Use the solution to part (b) to calculate Count-Trees(8).
d[5] What is the running time of your solutions to parts (a) and (b). (They should be the same!) Justify your answer.
Problem Set Header Page Please make this the first page of your hardcopy submission.
In the Time column, please estimate the time you spent on the parts of this problem set. Please try to be as accurate as possible; this information will help me to design future problem sets. I will fill out the Score column when grading your problem set.
General Reading
Problem 1 [15]
Problem 2 [25]
Problem 3 [30]
Problem 4 [10]
Problem 5 [20]
Extra Credit 1 [25]
Extra Credit 2 [25]
Total