0% found this document useful (0 votes)
19 views5 pages

Exam 1

This document outlines the instructions and content for Exam 1 of CS 660, taught by Instructor Hoa Vu. The exam consists of three parts: multiple choice questions, problem-solving questions related to grid paths and probabilities, and string manipulation with swaps. Students are permitted to use books and notes but cannot collaborate or use electronic devices, with a total time limit of 75 minutes to complete the exam.

Uploaded by

nakbeta4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Exam 1

This document outlines the instructions and content for Exam 1 of CS 660, taught by Instructor Hoa Vu. The exam consists of three parts: multiple choice questions, problem-solving questions related to grid paths and probabilities, and string manipulation with swaps. Students are permitted to use books and notes but cannot collaborate or use electronic devices, with a total time limit of 75 minutes to complete the exam.

Uploaded by

nakbeta4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS 660, EXAM 1

INSTRUCTOR: HOA VU

Instructions
• This exam is open book, open notes. You are allowed to use any resource including books,
notes, and lecture notes. No collaboration. No internet or any electronic device.
• Manage your time wisely. You have 75 minutes to complete the exam. Do not get stuck on
one problem for too long.
• There are 3 parts. For part 1, circle the correct answer answer on the multiple choice
ZipGrade sheet. For parts 2 and 3 write your answer on the blank papers provided.

1. Part 1 (40 points)


(1) (2 pts) T (n) = 3T (n/2) + O(n3 ).
(a) T (n) = Θ(n3 ).
(b) T (n) = Θ(n3 log n).
(c) T (n) = Θ(n3 log2 n).
(d) T (n) = Θ(nlog2 3 ).
(e) None of the above.
(2) (4 pts) T (n) = 2T (n/10) + T (n/20) + O(n).
(a) T (n) = Θ(2n/10 ).
(b) T (n) = Θ(2n ).
(c) T (n) = Θ(n log n).
(d) T (n) = Θ(n).
(e) None of the above. √
(3) (4 pts) f (n) = n0.1 and g(n) = 2 log n . Select all that apply.
(a) f (n) = ω(g(n)).
(b) f (n) = Ω(g(n)).
(c) f (n) = Θ(g(n)).
(d) f (n) = o(g(n)).
(e) f (n) = O(g(n)).
(4) (4 pts) Consider the knapsack problem with n items and a knapsack of capacity K. Assum-
ing all items have positive integer weights and positive values. Let dp[i, j] be the maximum
value that can be obtained using the first i items and a knapsack of capacity j.
Assume that that V [200] = 35, W [200] = 20. Furthermore, assume dp[199, 999] = 105,
dp[200, 999] = 115, dp[199, 1000] = 100, dp[180, 999] = 120, dp[199, 980] = 115. What is the
value of dp[200, 1000]?
(a) 140
(b) 135
(c) 120
(d) 150
(e) None of the above.
1
2 INSTRUCTOR: HOA VU

Figure 1. Figure 1

Recall the definition of quotient and remainder of a division. For integers a, b, if a = bq+r
where 0 ≤ r ≤ b − 1, then q is the quotient and r is the remainder when a is divided by
b. Note that r must be between 0 and b − 1. For example, if a = 7 and b = 5, then q = 1
and r = 2 since 7 = 5 × 1 + 2. When a = −7 and b = 5, then q = −2 and r = 3 since
−7 = 5 × (−2) + 3.
(5) (2 pts) Consider the directed acyclic graph in Figure 1. What is the length of the shortest
path from s to d modulo 5?
(a) 0
(b) 1
(c) 2
(d) 3
(e) 4
(6) (2 pts) Consider the directed acyclic graph in Figure 1. What is the length of the shortest
path from s to e modulo 5?
(a) 0
(b) 1
(c) 2
(d) 3
(e) 4
(7) (2 pts) Consider the directed acyclic graph in Figure 1. What is the length of the longest
path from s to c modulo 5?
(a) 0
(b) 1
(c) 2
(d) 3
(e) 4
(8) (2 pts) Consider the directed acyclic graph in Figure 1. What is the length of the longest
path from s to d modulo 5?
(a) 0
(b) 1
(c) 2
(d) 3
(e) 4
(9) (2 pts) It is not possible to compute the product of two n × n matrices using fewer than
Θ(n3 ) operations.
(a) True
(b) False
CS 660, EXAM 1 3

(10) (4 pts) Consider two strings A[1 . . . n] and B[1 . . . m] where A[n] = B[m]. Let ed[i, j]
be the edit distance between A[1 . . . i] and B[1 . . . j]. Suppose that ed[n − 1, m] = 90,
ed[n, m − 1] = 101, ed[n − 1, m − 1] = 88. What is the value of ed[n, m]?
(a) 91
(b) 102
(c) 99
(d) 100
(e) None of the above.
(11) (4 pts) Consider an n × n board with some given dark squares. Starting at the top left,
at each step you can move down or to the right without crossing a dark square. Your goal
is to count the number of paths from the top left to the bottom right. For example, the
number of paths for the board below is 3 ((down, down, right, right), (down, right, down,
right), (down, right, right, down)).

How many paths are there for the board below modulo 5?

(a) 0
(b) 1
(c) 2
(d) 3
(e) 4
(12) (2 pts) Consider the AVL tree in Figure 2. After inserting 150, what is the parent of 100?
(a) 150
(b) 50
(c) 200
(d) 20
(e) None of the above.
(13) (2 pts) Consider the AVL tree in Figure 2. After deleting 40, what is the parent of 100?
(a) 50
(b) 20
(c) 200
(d) 10
(e) None of the above.
(14) (2 pts) Consider the graph in Figure 3. What is the weight of the minimum spanning tree
modulo 5?
(a) 0
4 INSTRUCTOR: HOA VU

(b) 1
(c) 2
(d) 3
(e) 4
(15) (2 pts) The edge cb must be in some minimum spanning tree of the graph in Figure 3.
(a) True
(b) False

2. Part 2 (30 points)


• (15 pts) You are given an n × n grid in the form of a 2D array A[1 . . . n][1 . . . n]. Each
A[i, j] is an integer between 0 and n − 1. If A[i, j] = x, then you can move down or to the
right x steps (without stepping out of the grid). For example, if A[1, 1] = 2, then you can
move to (1, 3) or (3, 1) from (1, 1). Compute the number of distinct paths from (1, 1) to all
other cells in the grid. Analyze the running time of your algorithm.
• (15 pts) You are given an n × n grid in the form of a 2D array A[1 . . . n][1 . . . n]. Each
A[i, j] is either 0 or 1. If you are at (i, j) you can:
– Move to A[i + 1, j] or A[i, j + 1] if A[i + 1, j] = A[i, j + 1] = 1 randomly with probability
1/2 each.
– Move to A[i + 1, j] with probability 1 if A[i + 1, j] = 1 and A[i, j + 1] = 0.
– Move to A[i, j + 1] with probability 1 if A[i + 1, j] = 0 and A[i, j + 1] = 1.
– Get stuck and stop if A[i + 1, j] = A[i, j + 1] = 0.
– You may assume A[n + 1, j] = A[i, n + 1] = 0 for all i, j (to avoid moving out of the
grid).
In summary you can go down or to the right one square with probability 1/2 each if both
squares hold a 1, or you can go down with probability 1 if the square below holds a 1 and
the square to the right holds a 0, or you can go right with probability 1 if the square to
the right holds a 1 and the square below holds a 0. Design an algorithm that computes the
probabilties that you reach each cell in the grid starting from (1, 1). Analyze the running
time of your algorithm.

3. Part 3 (30 points)


You are given a string s[1 . . . n] and a list of pairs P = {(a1 , b1 ), (a2 , b2 ), . . . , (am , bm )} where
1 ≤ ai ≤ bi ≤ n. For each pair ai , bi , you can swap letters in s between positions ai and bi
for as many times as you want. If s = “cba” and P = {(1, 2), (2, 3)}, then you can obtain
cba → cab → acb → abc.
Your goal is to find the lexicographically smallest string that can be obtained after performing
all swaps.
• Example 1: s = “dcab” and P = {(1, 4), (1, 2)} then the output should be “bcad”.
• Example 2: s = “dcaf be” and P = {(1, 4), (2, 3), (2, 6)} then the output should be “dacf be”.
Design an algorithm that takes s and P as input and outputs the lexicographically smallest string
that can be obtained after performing the allowed swaps. Let m be the number of pairs in P . Your
algorithm should run in O(m log n + n log n) time. Here, we do not care about minimizing the
number of swaps. We only care about the lexicographically smallest string that can be obtained.
Follow these steps.
• (10 pts) Justify/show that if we have the pairs (x1 , x2 ), (x2 , x3 ), . . . , (xk−1 , xk ), then we can
perform a series of swaps such that sx1 and sxk are swapped while keeping sx2 , sx3 , . . . , sxk−1
fixed.
• (20 pts) Based on the statement above (you may assume that it’s true if you can’t prove it),
design an algorithm that outputs the lexicographically smallest string that can be obtained
CS 660, EXAM 1 5

after performing swaps. Your algorithm should run in O(m log n + n log n) time. Consider
using a data structure that we covered in class. Justify the running time. If you come
up with an algorithm that is correct using the above observation but does not run in the
required time, you will receive partial credit.

You might also like