You are on page 1of 18

NATIONAL UNIVERSITY OF SINGAPORE

SCHOOL OF COMPUTING
FINAL EXAMINATION FOR
Semester 2 AY2013/14

CS2020 - Data Structures and Algorithms (Accelerated)

April 23, 2014 Time Allowed: 2 Hour

INSTRUCTIONS TO CANDIDATES
a. Write your Matriculation Number below, and on every page.
b. The exam contains 7 multi-part problems. You have 120 minutes to earn 100 points.
c. The exam contains 18 pages, including the cover page and 5 pages of scratch paper.
d. The exam is closed book. You may bring two double-sided sheet of A4 paper to the exam. You may
not use a calculator, your mobile phone, or any other electronic device.
e. Write your solutions in the space provided. If you need more space, please use the scratch paper at
the end of the exam. Do not put part of the answer to one problem on a page for another problem.
f. Show your work. Partial credit will be given. Please be neat.
g. You may use (unchanged) any algorithm or data structure that we have studied in class, without
restating it. If you want to modify the algorithm, you must explain exactly how your version works.
h. Draw pictures frequently to illustrate your ideas.
i. Good luck!

MATRICULATION NUMBER:

EXAMINERS USE ONLY


Question Mark Score
1 Minimum Spanning Trees 10
2 Dropbox 10
3 Java Java Java 10
4 Buggy Code 10
5 You Are the Computer 30
6 Mind the Gap 15
7 Maze Escape 15
TOTAL 100
CS2020 Final Exam Matric. Num.: 2

Problem 1. Minimum Spanning Trees. [10 points]

Problem 1.a. Consider the False Cycle Property of an MST: for graph G = (V, E) and minimum spanning
tree T : If C is a cycle in T , then the lightest edge in C is part of the MST T .
Assume that every edge has a unique weight. Give a (small) counterexample to this property. (Draw a graph
containing approximately 4 or 5 nodes that violates this property.) Indicate clearly the cycle and the edge
that violate the property.
Solution: (Needs a picture.) Consider a square with four nodes {A, B, C, D}. The edges of the square
have weights: (A, B) = 10, (B, C) = 40, (C, D) = 50, and (D, A) = 20. There is also one diagonal edge
(B, D) = 30. Notice that there are two cycles here: (A, B, D) and (B, C, D). They both contain edge (B, D)
with weight 30. However, edge (B, D) is the heaviest edge in cycle (A, B, D) and hence cannot be included
in the MSt. But it is the lightest edge in cycle (B, C, D).

Problem 1.b. Consider the problem of finding a minimum directed spanning tree on a directed graph.
The input is a directed graph G = (V, E) and a special node r, the root. The goal is to find a spanning tree
of minimum cost rooted at r such that there is a directed path from r to every node in the graph.
Unfortunately, neither Prims nor Kruskals algorithm works in a directed graph, because the Cut Property
of an (undirected) minimum spanning tree does not hold. Draw a graph consisting of no more than 5 nodes
which violates the cut property. Clearly indicate the cut and the violation.
Solution: (Needs a picture.) Consider three nodes A, B, and C. There is a directed edge from (A, B)
with cost 10, and an edge (A, C) with cost 5. There is also an edge (B, C) with cost 1. The minimum cost
spanning tree includes edges (A, B) and (B, C) for cost 11. Consider the cut separating A from B and C.
The smallest edge across the cut is (A, C) of cost 5, but no minimum cost spanning tree includes this edge.
CS2020 Final Exam Matric. Num.: 3

Problem 2. Dropbox. [10 points]

Dropbox was recently in the headlines, with the following tweet going viral:

It was discovered that whenever you share a file on Dropbox, they check it against a blacklist of known
copyrighted files. The way this process works is (likely) as follows: (1) companies who own copyrighted
files submit their files to Dropbox; (2) Dropbox calculates a hash of the file and stores it in a database (the
blacklist); (3) Whenever you share a file, Dropbox compares the hash of your shared file to the hashes in
the database, and disallows the share if any of the hashcodes match.

Problem 2.a. Explain (briefly) the impact of false positives and false negatives on this scheme.

Solution: Hash functions have false positives, but no false negatives. Thus you can never share a file that
is on the blacklist, but sometimes (due to a false positive), you will not be able to share a file that is not on
the blacklist.

Problem 2.b. A researcher suggests using multiple hash functions (as in a Bloom filter). Explain both
the positives and negatives of this change.

Solution: Pros: lower false positive rate


Cons: larger database (i.e., more space), and slightly slower verification

Problem 2.c. Assume you want to share the file anyways1 , despite the Dropbox policy. Give a simple
solution for circumventing the Dropbox mechanism.

Solution: Append a single bit, e.g., a 0 to the end of the file before sharing it, and then strip off the
extra bit once the file has been shared.
1 In CS2020 we discourage illegal behavior, but we believe in understanding the underlying technology.
CS2020 Final Exam Matric. Num.: 4

Problem 3. Java Java Java. [10 points]

Circle all the statements below that are true, and cross out all the statements that are false.

In Java, you can inherit only one interface. The String class is immutable.
FALSE TRUE

You can only use the keyword super in a con- A Java class may not have more than one con-
structor. structor.
FALSE FALSE

Because of inheritance, a superclass inherits all An object is created from a class by calling new
the functionality found in its subclasses. and is destroyed by calling delete.
FALSE FALSE

The Java HashMap implementataion of If class Child extends class Parent, then you can
containsValue is inefficient, i.e., it is not assign an object of type Child to a variable of
O(1) (even when the hash function is good). type Parent without a typecast.
TRUE TRUE

A protected member variable cannot be accessed A constructor can be static if it is in a static


outside the package that contains it. nested class, but not in any other situation.
FALSE FALSE
CS2020 Final Exam Matric. Num.: 5

Problem 4. Buggy Code: Harmonic Sequence. [10 points] Consider the following piece of (buggy)
code. It is designed to output the sum HN = 1 + 1/2 + 1/3 + . . . + 1/N .
1: public class Harmonic
2: {
3: public static double H(int N)
4: {
5: if (N = 0) return 0;
6: return H(N-1) + 1/N;
7: }
8:
9: public static void main (String[] args)
10: {
11: int N = 37;
12: System.out.println(H(N));
13: }
14: }

Identify two problems with this program and how to fix them. (Do not include stylistic errors, or other
issues that are not best practices. Focus only on problems that cause a compile-time error, a run-time
error, or return a wrong answer.)

Problem 1.
Solution: On line 5, the assignment (N = 0) should be a test for equality (N == 0), or even better,
(N <= 0).

Problem 2.
Solution: On line 6, the calculation 1/N is done on integers, and hence rounds down to 0 for all N > 1.
This should be changed to 1.0/N, or the values should be otherwise cast to a double.
CS2020 Final Exam Matric. Num.: 6

Problem 5. You are the computer [30 points]

Problem 5.a. Below is a directed acyclic graph (DAG) consisting of seven nodes. In this problem, you
will run a topological sort on this DAG. Perform the topological sort by executing a post-order depth-first
search on the graph. (Do not use an alternate technique for finding a topological order.)

A" B"

C" D" E"

F" G"

Start your topological sort at node A. Assume that at every node, the adjacency list is sorted alphabetically
from smaller to larger letters. (Thus, when enumerating edges adjacent to a node, always enumerate edges
in alphabetical order, choosing smaller letters over larger letters.)

In what order do you first visit the nodes? A


Solution: A,B,D,F,G,E,C

What is the final topological order discovered?


Solution: A,C,B,E,D,G,F
CS2020 Final Exam Matric. Num.: 7

Problem 5.b. Below is a state of the Union-Find algorithm (for implementing the Disjoint Sets ADT),
with 16 elements in two disjoint sets. Assume that Union-Find is implemented with Quick-Union, path
compression, and union-by-weight. (The below state is actually impossible, but assume that it is really the
state.)

A" K"

B" C" D" L"

E" F" G" M"

H" I" N"

J" O"

P"

The program executes a Union(K,I) operation. Draw (neatly) the state of the algorithm after the union
operation completes.
Solution:

B C D G I K

E F H L
J

P
CS2020 Final Exam Matric. Num.: 8

Problem 5.c. Below is a depiction of a SkipList containing 10 keys. Draw on the diagram below the
search path followed when searching for the key 42. Clearly indicate which edges are on the search path.

0 18

0 18 30

0 16 18 30 45

0 7 16 18 22 25 30 42 45 76

Solution:

0 18

0 18 30

0 16 18 30 45

0 7 16 18 22 25 30 42 45 76
CS2020 Final Exam Matric. Num.: 9

Problem 6. Mind the Gap [15 points]

Assume you are given a set of n points on a line. The points are given to you as an array of real numbers
x[0 . . n 1] where x[0] is the leftmost point on the line, x[1] is the next point, x[2] is the next point, etc.
The goal, in the first part of the problem, is to find the pair of consecutive points with the largest gap, i.e.,
to find an i such that |x[i + 1] x[i]| is largest. Then, the second part asks about finding a gap that is at
least as large as the average gap. Here is an example input:

x[0]$ x[1]$ x[2]$ x[3]$ x[4]$ x[5]$ x[6]$ x[7]$ x[8]$

%9.8$ %7.3$ %5.9$ %3.6$ %2.1$ 3.8$ 5.2$ 6.5$ 7.9$

largest$gap:$5.6$

Problem 6.a. Give an efficient algorithm to find the largest gap. Your algorithm should return a pair
(i, i + 1) that maximizes |x[i + 1] x[i]|.

In less than four words, what is your basic approach: Linear search

What is the running time of your approach? O(n)

Describe your algorithm briefly, in 2-3 sentences, giving pseudocode as needed (but only if
needed) to clarify the details.
Solution: In this case, you cannot do better than linear search. The solution, then, is to iterate through
all the values of i from 0 to n 2 and return the value with the maximum gap.
CS2020 Final Exam Matric. Num.: 10

Problem 6.b. Give an efficient


Pn2 algorithm to find a gap that is at least as big as the average gap. The
average gap is of size A = n1 i=0 |x[i + 1] x[i]|. Your algorithm should return a pair (i, i + 1) such that
|x[i + 1] x[i]| A.

In less than four words, what is your basic approach: Binary search

What is the running time of your approach? O(log n)

Describe your algorithm briefly, in 2-3 sentences, giving pseudocode as needed (but only if
needed) to clarify the details.
Solution: In this case, we can use binary search. Initially, we are searching the range of points from
0 to n 1, and the average gap A = (X[n 1] X[0])/n. We examine the middle point m = bn/2c,
and calculate the average of the left half AL = (X[m] X[0])/(m + 1) and the average of the right half
AR = (X[n 1] X[m])/(n m). If AL is larger, then we recurse to the left, otherwise we recurse to the
right. We terminate when the range of points contains only two points.

Pseudocode follows:
AveragePointSearch(float[] X, int begin, int end)
// Base case: only two points left
if (end <= begin+1) return begin;

// Calculate midpoint and averages


A = (X[end] - X[begin]) / (end-begin+1);

// Note: (end - begin) >= 2, so mid > begin and mid < end.
// Beware the case where there are only 3 points.
mid = begin + floor((end - begin)/2);
Aleft = (X[mid] - X[begin]) / (mid-begin+1);
Aright = (X[end] - X[mid]) / (end-mid+1);

// Recurse on the left or right half


if (Aleft > Aright) return AveragePointSearch(X, begin, mid);
else return AveragePointSearch(X, mid, end);
CS2020 Final Exam Matric. Num.: 11

Problem 7. Maze Escape, The Sequel [15 points]

Suddenly, there is a flash of light, and you are no longer where you were before. This is part of a maze of
twisty little passages, all alike. It is pitch black. You are likely to be eaten by a Grue. Luckily, you have a
fairy godmother to help out in times of need. Your fairy godmother gives you three useful items:
A map of the maze, with the current location designated S and the exit designated D. She tells you
that the door to the exit will open only after you have visited every room in the maze.

A magic device with a big red button. Whenever you press the button, you are immediately teleported
back from wherever you are to the start location S.
A bag of Zing, a magical substance that will help you to escape.
Looking at the map, you see a lot of rooms, connected by doors. You notice two strange things:

Many (but not all) of the doors are drawn in red and labeled with a number. Your fairy godmother
informs you that unlocking these doors will cost you Zing, and if you run out of Zing before unlocking
all the doors, you will be stuck forever. A door labeled with an x costs x Zing to unlock. Once the
door is open, it remains open forever and can be used as much as you like, in either direction.
Some of the rooms are colored in green and labeled with a large letter T . Your fairy godmother
informs you that these rooms contain a transporter. You can transport, for free, from any room with a
transporter to any other room with a transporter. (When you activate a transporter, you must specify
the destination of the transporter from among all the rooms labeled with the letter T .)
You start out with Z Zing, and you can escape when you have first visited every room, and then found your
way to the exit. Your task is to give an efficient algorithm to find a route of escape that uses no more than
Z Zing, or outputs IMPOSSIBLE if it is impossible. (All that matters is that you find a feasible route of
escape; further optimization is unnecessary.)

D"
T" 9"
13"

T"
T"
3"
5"

7" 6"

4"

S"
CS2020 Final Exam Matric. Num.: 12

In one sentence, what is the basic idea behind your approach?


Solution: Construct a graph and run a MST algorithm to find a spanning tree, which you then traverse
in any order.

What is the running time of your solution, if there are n rooms and m doors?
Solution: The new graph may have (m2 ) edges (since you need to connect every pair of rooms labelled
with a T ), and so the running time of the MST algorithm is O(m2 log n). The tree traversal costs O(m2 + n),
which is domianted by the MST algorithm.

Provide any additional details explaining how your solution works. (Pseudocode is not re-
quired.)
Solution: First, construct a graph G where each room is a node. If there is a door connecting two rooms,
add an edge between the appropriate nodes. If the door has no Zing-cost attached to it, then give it a cost
of zero. Otherwise, label the edge with the Zing-cost of opening that door. Finally, for all pairs of nodes
labelled with a T , add a zero-cost edge connecting them. The new graph has n nodes and m2 edges if the
original map has n rooms and m doors. (If you interpreted the exit as a room, omit the exit room from this
graph, as you cannot enter it until you have visited every room. Later, reconnect it to the tree by its least
cost door.)
Second, run Prims Algorithm (or your favorite MST algorithm) to find an MST of the graph. The
spanning tree covers every room, opening the minimum cost set of doors.
Third, perform a DFS traversal of the spanning tree, and output that as the order in which the rooms
should be visited. (The order in which you traverse the spanning tree does not matter.) Finally, find a route
from the end of your traversal to the exit along the spanning tree.
Note: the ability to teleport back to the start from any node is irrelevant, in that all the edges are
undirected. If you have arrived at a room, you can always walk back to the start. In addition, if you add
edges representing the teleporter back to the start, these are directed edges, and hence it is now harder to
find an MST.
CS2020 Final Exam Matric. Num.: 13

Problem 8. Just for fun: Prisoners in Hats [0 points]

The entire class of CS2020 students has just been captured by a supervillain, the Mad Hatter, who wants
to punish them for continuing to escape from mazes (and for not wearing nice hats to class). Like all
supervillains, he talks too much. And like all supervillains, he is certain that he is much, much smarter than
his victims. So he suggests a game of wits. Actually, he suggests two gamesyou can choose your favorite!
If you win, then you go free. If you lose, then you die. Want to play? Which game do you prefer?

Problem 8.a. (Game 1.) The students are arranged in a line from shortest to tallest. Each student can
see all the shorter students in front of him/her, and none of the taller students behind. The Mad Hatter
places either a red hat or a blue hat on each students head.
Beginning at the end of the line, one at a time, each student guesses the color of his/her hat. The
guess is made out loud, for all to hear. Aside from guessing the color of their hats, no other communication
is allowed. If at least half the students guess the color of their hats correctly, then everyone goes free.
Otherwise, everyone is executed.
You have five minutes to agree on a strategy. What strategy will you choose? How many students can
you guarantee will survive?

Problem 8.b. (Game 2.) The students are arranged in a circle. All the students can see all the other
students. As before, the Mad Hatter places either a red hat or a blue hat on each students head. On
a given signal, all the students guess the color of their own hat simultaneously, with no opportunity for
communication. If at least half the students guess the color of their hats correctly, then everyone goes free.
Otherwise, everyone is executed.
You have five minutes to agree on a strategy. What strategy will you choose? How many students can
you guarantee will survive?
CS2020 Final Exam Matric. Num.: 14

Scratch Paper
CS2020 Final Exam Matric. Num.: 15

Scratch Paper
CS2020 Final Exam Matric. Num.: 16

Scratch Paper
CS2020 Final Exam Matric. Num.: 17

Scratch Paper
CS2020 Final Exam Matric. Num.: 18

Scratch Paper

End of Paper

You might also like