




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: Exam; Class: Data Structures and Algorithms; Subject: Computer Science; University: Penn State - Main Campus; Term: Spring 2009;
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!





1
0 2
10
5 11
4 8
7 9
6
8
28
3
25
1
17
0
9
12
6
19
17
Page 222, exercise 7a (unstable/illegal nodes are in red) C C O M
C O
M
C O P
M
C O
P
U
M
C O
P
T U M
C I O
P
T U
M
C I N O
P
T U
M
C G I N O
P
T U G M P
C I N O T U
M
G
C I
P
N O T U
Page 222, exercise 10 (pseudocode). We can simply insert the list elements to an initially empty 2-3 tree. We can do it faster if the list is already sorted. I will put a recursive code later (Saturday night).
But if they go together to the near side of the river, we are back at the initial state. If they go together to the far side of the river, there are only two of them on the near side, so at least two on the far side, without a boat there. We can never reach that state. Exercise 3 and 4a. Exercise 3: For k = 1 the number of paths with k edges from i to j is zero if there is no edge, and one if there is an edge, so it is Aij where A is the adjacency matrix. For k = 0 the number of paths with k edges from i to j is zero if i 6 = j and one if i = j, so it is given by the identity matrix Iij , or A^0. For larger k, the number of paths with k edges from i to j is the sum over possible second nodes of paths of the products: (1 if there is an edge from i to m, 0 otherwise) times (the number of paths from m to j with k − 1 edges), and this sum is the product of i-th row of A with j-th column of Ak−^1 , so it is an entry of Ak. Exercise 4a: we need to check if A^3 has a non-zero. It suffices to compute A^3. Thus it suffices to compute twice a product of two n × n matrices. We reduced the problem of fast checking if there is a triangle to the problem of fast matrix multiplication, and we know about the Strassen algorithm that works in time O(nlog^2 3 which is faster than O(n^3 ).
Allowing 2 as an intermediate node adds 0 → 2 → 3 and 1 → 2 → 3 so the new matrix is 1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1
which is the final result. Exercise 9. To make Floyd algorithm incorrect, we need a negative cost cycle, e.g. the graph could be a directed cycle 0 →^1 1 →^1 2 − →^3 0.
Example of a problem: Subset Sum. Given are numbers x 0 ,... , x 1 and target number t, the question is if there exists a subset S ⊂ { 0 ,... , n − 1 } such that ∑ i∈S xi = t. Recursive subproblem: for k ≤ n, s ≤ n we define SP (k, s) (abbreviation of “Sum Possible”) which is true if and only if there exists S ⊂ { 0 ,... , k− 1 } such that ∑ i∈S xi = s. Basic case: SP (0, s) = [s = 0] (here [s = 0] is true or false). Recurence: SP (k + 1, s) = SP (k, s) or SP (k, s − xk). Resulting code: for (i = 1; i < n; SP[i++] = 0); SP[0] = 1; for (k = 0; k < n; k++] = 0) for (s = t; s >= x[k]; s--] = 0) SP[s] ||= SP[s-x[k]];
Running time O(nt), memory neede O(n). Example of a problem: Longest Common Subsequence. Given are two sequences, x 0 , x 1 ,... , xm− 1 and y 0 , y 1 ,... , yn− 1. A subsequence is obtained from a sequence by deleting some of its entries without chang- ing the order of the remaining elements. We want to know the length of the longest common subsequence. E.g. if we have CABAB and ABBACBACA, ABAB is the longest common subsequence so we should compute 4. Recursive subproblem: LCS(a, b), the length of the longest common subsequence of x 0 , x 1 ,... , xa− 1 and y 0 , y 1 ,... , yb− 1. Basic case: LCS(0, b) = LCS(a, 0) = 0 (one of the sequences is empty). Recurrence: LCS(a + 1, b + 1) = max
LCS(a + 1, b) (yb not used) LCS(a, b + 1) (xa not used) LCS(a, b) + [xa = yb] ([xa = yb] is 0 or 1)
This recurrence can be easily converted into a program that fills two-dimensional array LCS in time O(mn). One can observe that it suffices to keep two rows or two columns in the memory, because entries in column i are computed using entries from column i and i − 1. This would give mememory requirement of O(m).