CS2020 1617 Semester 2
CS2020 1617 Semester 2
(Semester 2 AY2016/17)
Instructions
• Write your Student Number below, and on every page. Do not write your name.
• The assessment contains 7 multi-part problems. You have 120 minutes to earn 100 points.
• The assessment contains 28 pages, including the cover page and 5 pages of scratch paper.
• The assessment is closed book. You may bring two double-sided sheet of A4 paper to the assessment.
You may not use a calculator, your mobile phone, or any other electronic device.
• Write your solutions in the space provided. If you need more space, please use the scratch paper.
• Show your work. Partial credit will be given. Please be neat.
• 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.
• Draw pictures frequently to illustrate your ideas.
• Good luck!
Student Number.:
Total: 100
CS2020 Final Assessment Student Num.: 2
Circle the image that best represents how you feel right now.
CS2020 Final Assessment Student Num.: 3
True False
You are given a hash table that resolves collisions with chaining. The
table is of size m and contains n elements. Assume that the hash
function satisfies the Simple Uniform Hashing Assumption. Then the
expected time for a search operation is O(1).
We will call the inner loop of Bellman-Ford (where each edge in the
graph is relaxed exactly once) an “iteration.” Assume you run Bellman-
Ford on a graph G with no negative weight cycles. Then at the end
of i iterations, every node within i hops of the source has an estimate
equal to its correct distance from the source.
True False
Assume you have a graph with no positive weight cycles. Then you can
find the longest path in the graph by negating all the edge weights and
running Dijkstra’s algorithm.
Assume you have an AVL tree, and let u be a node in the AVL tree of
height h. Then, after every operation completes, the subtree rooted at
node u contains at most 2h+1 nodes.
Every directed acyclic graph has exactly one valid topological ordering.
Problem 2.a. Find a topological ordering of the directed acyclic graph below using depth-first
search (DFS). In the table below, for each node, fill in the order in which the DFS visits the node,
and its position in the topological order. Assume the DFS begins at node A. When there is more
than once possible choice of which node to visit next, assume the DFS chooses the node with the
earliest alphabetic id (e.g., X before Y before Z.).
5
A C
4
11
3
2
7 D E
1
1 2
B 4
F 3
G H
3
4
Visit order: (Please include every time a node is seen on the traversal, not just the first time.)
Topological order:
CS2020 Final Assessment Student Num.: 6
Problem 2.b. Below is a state of the Union-Find algorithm (for implementing the Disjoint
Sets ADT), with 17 elements in two disjoint sets. Assume that Union-Find is implemented with
Quick-Union and path compression. (The below state is actually impossible, but assume that it is
really the state.)
A K
B C D L
E F N M
G H O
I J P
The program executes a connected(K,H) operation that determines whether K and H are con-
nected using the Union-Find algorithm. (In this case, it should return the boolean false.) Draw
(neatly) the state of the algorithm after the operation completes.
CS2020 Final Assessment Student Num.: 7
Problem 2.c. Below is given a flow network with a source S and a target T . Each edge has a
capacity, and each edge is assigned a non-negative flow. The label a/b for an edge indicates that
the edge has flow a and capacity b. (For three of the edges, the flow is not given and is shown only
as a variable x, y, or z.) The given flow respects all the requirements for a legal st-flow.
Please answer all the questions on the next page regarding this flow network.
S
30 /30
10 /30
20/20
a c
x /10 0/20
b
10/10
y/50
30/60 0/10
d 30 /70
10/50 e
f 10/10
40/50
g
z/10
/
50/80
T
CS2020 Final Assessment Student Num.: 8
Notice that for three of the edges ((a, b), (a, f ), (f, t)) the flow is unspecified. Use the rest of the
provided information to deduce the values of the flow on these edges:
(a, b) : x =
(a, f ) : y =
(f, T ) z =
• If the array has fewer than k elements, then do a linear search to look for the value v.
• Otherwise, choose k elements spaced evenly apart in the array. Call these elements set S.
• If any of the items in set S is equal to the value v you are looking for, then return true.
• Otherwise, let i be the index of the largest item in S that is < v, and let j be the index of
the smallest item in S that is > v.
1. static boolean karySearch(int v, int[] A, int begin, int end, int k){
2. int size = (end - begin + 1);
3. if (size < k) {
4. for (int i=begin; i<=end; i++) {
5. if (A[i] == v) return true;
6. }
7. return false;
8. }
9. int lastProbe = 0;
10. // Loop through probe points
11. for (int i=1; i<k; i++) {
12. int currentProbe = begin + (size*i)/k;
13. if (A[currentProbe] == v) return true;
14. else if (A[currentProbe] > v) {
15. return karySearch(v, A, lastProbe, currentProbe, k);
16. }
17. lastProbe = currentProbe;
18. }
19.
20. return karySearch(v, A, lastProbe, end, k);
21. }
The code is designed to sort an array of integers, and it is designed to be used in the following
manner, e.g., to search for the value 17 (where k = 5):
Problem 3.a. Unfortunately there is a bug in the code that will prevent it from working correctly
for at least one value of k. Which of the following is a problem with this code?
Problem 3.b.
Explain (briefly) what the bug is and how to fix it.
Problem 3.c. Let T (n, k) be the running time of the k-ary search on an array of n elements.
What is the recurrence that describes the performance of this algorithm?
T(n, k) =
Problem 3.d. What is the solution to the recurrence in the previous part? (Give your answer
in terms of n and k.)
T(n) =
CS2020 Final Assessment Student Num.: 12
Problem 4.a. Explain briefly (in one to two sentences) the differences between the Iterator
interface and the Iterable interface. When do you use one and when do you use the other?
Problem 4.b. Explain briefly (in one to two sentences) the differences between the HashMap
class and the TreeMap class. When do you use one and when do you use the other? What are the
trade-offs?
Problem 4.c. Explain briefly (in one to two sentences) what is the Comparable interface and
why would you use it?
CS2020 Final Assessment Student Num.: 13
Problem 4.d. In the LastTrip class on the previous page, important parts of the code have
been replaced with the strings AAA, BBB, CCC, DDD, and EEE. Fill in below proper Java code
to replace these placeholders.
5, 7 BBB =
8 CCC =
Problem 4.e. On executing the main method, at each of the indicated lines of the program,
indicate the name of the trip and the name of the nextTrip (or null, if there is no nextTrip).
line 42 a
line 42 b
line 42 c
line 46 a
line 46 b
line 46 c
line 46 d
Problem 4.f. What is printed out when the program completes? (You may not need all the
lines provided.)
Output line 1 :
Output line 2 :
Output line 3 :
Output line 4 :
Output line 5 :
CS2020 Final Assessment Student Num.: 16
Problem 5.a. Each node u in the graph is labelled with a value du . The graph expert,
Prof. D. S. Tance, claims that these values represent the shortest distance from S to the node.
For example, if node u has label du , then the shortest path from S to u had length du . You want
to decide whether Tance is correct.
Give the most efficient algorithm to decide whether the labels are correct, i.e., return true if and
only if the labels represent the lengths of the shortest paths from S:
Assume the graph has n nodes and m edges. What is the running time of your algorithm:
Time :
CS2020 Final Assessment Student Num.: 17
Problem 5.b. Prof. D. S. Tance loves finding shortest paths, and has now given each node u a
label representing the shortest distance from every node in the graph. Each node u in the graph has
a label du [v] representing the shortest path from v to u, for every node v. Assume that these labels
are correct. (You may assume that Tance calculated these labels using an All-Pairs-Shortest-Path
algorithm.)
Some of the edges in the graph are useless, in that they do not contribute to any shortest path.
For every edge (u, v), we say that it is useless if it is not part of a shortest path from some node x
to another node y. For simplicity, assume that for every pair of nodes x, y there is a single unique
shortest path between x and y.
Give the most efficient algorithm you can to find all the useless edges in the graph:
Assume the graph has n nodes and m edges. What is the running time of your algorithm:
Time :
CS2020 Final Assessment Student Num.: 18
S S
5 10
5 10
da = 5 a b db = 10
da = 5 a b db = 9
3 6
3 6
9 1
9 1
c 4 g c 4 g
dc = 12 dc = 15 dg = 11
dg = 8
Part a: Each of the distances from the source Part a: The distances from the source S
S is correctly labelled. For example, the are not correctly labelled. For example, the
distance from S to node b is 9 (via the distance from S to node b is 9 (via the path
path (S, a, g, b)). For part (a), the algorithm (S, a, g, b)), not 10 (as labelled). For part (a),
should return true. the algorithm should return false.
5 8
3 7
9 2
Part b: The solid edges are useful, while the dotted edges are useless. For example, the edge (S, a)
is useful, because it is on the shortest path from S to a. The edge (a, c) is not useful because it is
not part of any shortest path. For part (b), the algorithm should return the edges (a, c) and (b, c).
CS2020 Final Assessment Student Num.: 19
Exit d
+20L
L0 L 60 L 60 L0 L 60
In this example, R = 20. You
+20L
need to have initial level 40 in
L0 L 60 L 70 L 70 L 70
order to exit the maze. This
is sufficient to reach either the
L 30 L 10 L0 L0 L0 relic in the bottom right corner
or the top left corner, which
L0 L 200 L0 L0 L0 boosts you to level 60, en-
Entrance s
+20L abling you to exit the maze.
L0 L0 L0 L0 L0
CS2020 Final Assessment Student Num.: 20
Problem 6.a. Explain, succinctly in two to three sentences, the main idea of how you plan
to solve the problem.
Problem 6.c. Assume that the graph has n nodes, m edges, and k relics. What is the time
complexity of your algorithm? Use asymptotic (big-O) notation.
Time complexity :
CS2020 Final Assessment Student Num.: 21
Problem 6.d. Give the complete details of your algorithm. Explain why you achieve the
performance claimed in the previous part.
CS2020 Final Assessment Student Num.: 22
Circle the image that best represents how you feel right now.
CS2020 Final Assessment Student Num.: 24
Scratch Paper
CS2020 Final Assessment Student Num.: 25
Scratch Paper
CS2020 Final Assessment Student Num.: 26
Scratch Paper
CS2020 Final Assessment Student Num.: 27
Scratch Paper
CS2020 Final Assessment Student Num.: 28
Scratch Paper
End of Paper