Practice Problems for Midterm Exam 2 | Data Structures and Algorithms | CMPSC 465, Exams of Computer Science

Material Type: Exam; Class: Data Structures and Algorithms; Subject: Computer Science; University: Penn State - Main Campus; Term: Spring 2009;

Typology: Exams

Pre 2010

Uploaded on 09/24/2009

koofers-user-tgr
koofers-user-tgr 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Second Midterm Exam, CMPSC 465, Spring
2009
Practice problems
Midterm will be on Tuesday, March 31, 8:15 PM, in 60 and 61 Willard.
This will be open book exam, you can also have notes (several sheets or one notebook).
The use of communication equipment and calculators is not permitted the exam.
When you describe an algorithm, you should also justify its correctness and your estimate
of the running time.
In problems where you show the working of an algorithm on diagrams each diagram
is repeated to provide you with a “spare one”. Indicate which diagram contains your
solution.
1.Balanced trees, including B-trees
Given a list of numbers and priorities, draw the corresponding treap, a binary search
tree in which parent cannot have a smaller priority (thus the number with the largest
priority is on the root).
Example: (number, priority): (0,8), (1,28), (2,3), (3,25), (4,1), (5,17), (6,0), (7,9), (8,12),
(9,6), (10,19), (11,17).
3
1
0 2
10
115
4 8
97
6
8
28
3
25
1
17
0
9
12
6
19
17
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Practice Problems for Midterm Exam 2 | Data Structures and Algorithms | CMPSC 465 and more Exams Computer Science in PDF only on Docsity!

Second Midterm Exam, CMPSC 465, Spring

Practice problems

  • Midterm will be on Tuesday, March 31, 8:15 PM, in 60 and 61 Willard.
  • This will be open book exam, you can also have notes (several sheets or one notebook). The use of communication equipment and calculators is not permitted the exam.
  • When you describe an algorithm, you should also justify its correctness and your estimate of the running time.
  • In problems where you show the working of an algorithm on diagrams each diagram is repeated to provide you with a “spare one”. Indicate which diagram contains your solution.
  1. Balanced trees, including B-trees Given a list of numbers and priorities, draw the corresponding treap, a binary search tree in which parent cannot have a smaller priority (thus the number with the largest priority is on the root). Example: (number, priority): (0,8), (1,28), (2,3), (3,25), (4,1), (5,17), (6,0), (7,9), (8,12), (9,6), (10,19), (11,17). 3

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).

  1. Problem Reduction Page 246, exercises 5, 9, 11. Also, 3 together with 4a. Exercise 5. Are all points P 1 ,... , Pn contained in a triangle with the vertices from that sequence? We can find the convex hull, and then we check if it is a triangle. We know that a convex hull can be found in time O(n log n). Exercise 9. We have a graph with edges e 0 ,... , em− 1 and we want to find C[1],... , C[m] so if edges ei, ej have a common endpoint then C[i] 6 = C[j]. We can translate this to a node coloring problem: we create a graph with nodes 0,... , m− 1 and edges {i, j} such that ei and ej share an endpoint. A valid coloring (array C[m]) for the nodes of this graph is also a valid coloring of the edges of the previous graph. Because colorings are the same, an algorithm producing the minimum number of colors for nodes of the new graph will produce the minimum number of colors for the edges of the first graph. Exercise 11. I though that there is a nice reduction from a problem with k couples to a problem with k − 1 couples, but the solution I got for 3 couples strongly relies on the fact that there are at most 3 men. One can show that if k > 3 there is no solution. So this puzzle, at the very least, is too hard to be given during an exam. I will post a solution on Sunday, so the curious among you can check. Solution for k = 3: h is a husband, w is a wife, B is the boat. Columns indicate who and what is on each of the two river banks. 0 hw · hw · hw B 1 hw · hh ww B 2 hw · hw · h B w 3 hhh www B 4 hw · hh B ww 5 hw hw · hw B 6 hw · hw B hw 7 ww hw · hh B 8 www B hhh 9 w hw · hw · h B 10 ww B hw · hh 11 hw · hw · hw B However, if k > 3, then we cannot reach a state that either has two (or more) husbands on the far side of the river and the boat on the near side, or three (or more) husbands on the far side of the river, and the boat there. This is because when we have one husband on each side, we must have all wives with husbands, so wives cannot move alone (away from husbands) and husbands cannot move alone either, UNLESS all husbands leave a river bank together.

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 ).

  1. Dynamic programming Compute N(5,5,6), the number monotone paths from (0,0) to (5,5) with area 6. A monotone path can be represented as a non-decreasing sequence that indicates how many grid squares are below the path in the columns of the grid. As the path is monotone, these numbers cannot decrease, and they add to the area under the path. 0 0 0 1 5 0 0 0 2 4 0 0 1 1 4 0 0 0 3 3 0 0 1 2 3 0 1 1 1 3 0 0 2 2 2 0 1 1 2 2 1 1 1 1 2 page 292, exercise 1, 9. Exercise 1: the graph is a path 0 → 1 → 2 → 3. Allowing paths of length 0 changes the matrix to 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 Allowing 0 as intermediate node does not change the matrix, because there are no edges to 0. Allowing 1 as an intermediate node adds path 0 → 1 → 2 so the new matrix is 1 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1

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).