(COMP3711) (2016) (F) Final Schenax 19574
(COMP3711) (2016) (F) Final Schenax 19574
(COMP3711) (2016) (F) Final Schenax 19574
Instructions
• This is a closed book exam. It consists of 24 pages and 9 questions .
• Please write your name, student ID and ITSC email and circle your
lecture section (L1 is Tu,Th 12-13:20 and L2 is Tu,Th 16:30-15:50) at
the top of this page.
• For each subsequent page that you write on, please write your student
ID at the top of the page in the space provided.
• Please sign the honor code statement on page 2.
• Answer all the questions within the space provided on the examination
paper. You may use the back of the pages for your rough work. The
last 2 pages are scrap paper and may also be used for rough work. Each
question is on a separate page most have at least one extra page for
writing answers This is for clarity and is not meant to imply that each
question requires all of the blank pages. Many can be answered using
much less space.
Questions 1 2 3 4 5 6 7 8 9 Total
Points 4 6 10 12 12 14 13 14 15 100
Score
Student ID:
Student’s Name:
Student’s Signature:
2
Student ID:
1. Sorting [4 pts]
The leftmost array gives 8 4-digit numbers. Run Radix sort on these
numbers. Show the result after sorting the array on each digit. The
rightmost array should be your final result.
9 1 6 2
8 2 3 4
1 2 3 4
4 3 2 1
3 5 2 1
3 6 2 1
7 1 8 2
2 9 8 5
Solution:
9 1 6 2 4 3 2 1 4 3 2 1 9 1 6 2 1 2 3 4
8 2 3 4 3 5 2 1 3 5 2 1 7 1 8 2 2 9 8 5
1 2 3 4 3 6 2 1 3 6 2 1 8 2 3 4 3 5 2 1
4 3 2 1 9 1 6 2 8 2 3 4 1 2 3 4 3 6 2 1
3 5 2 1 7 1 8 2 1 2 3 4 4 3 2 1 4 3 2 1
3 6 2 1 8 2 3 4 9 1 6 2 3 5 2 1 7 1 8 2
7 1 8 2 1 2 3 4 7 1 8 2 3 6 2 1 8 2 3 4
2 9 8 5 2 9 8 5 2 9 8 5 2 9 8 5 9 1 6 2
3
Student ID:
b
2
1
g 2 4
11
1
h
a 2
c 6 6
2
6 f
2
e
List the vertices by row in the order that Dijkstra’s algorithm takes
them out of the heap. Recall that v.d is the current value that Dijkstra’s
algorithm stores with that vertex. Fill in the table below. We have filled
in the first two rows for you.
a.d b.d c.d e.d f.d g.d h.d a.d b.d c.d e.d f.d g.d h.d
− 0 ∞ ∞ ∞ ∞ ∞ ∞ − 0 ∞ ∞ ∞ ∞ ∞ ∞
a 0 ∞ 2 6 ∞ ∞ ∞ a 0 ∞ 2 6 ∞ ∞ ∞
c 0 ∞ 2 4 ∞ 13 3
h 0 ∞ 2 4 9 13 3
e 0 ∞ 2 4 6 13 3
f 0 10 2 4 6 13 3
b 0 10 2 4 6 12 3
g 0 10 2 4 6 12 3
4
Student ID:
Preference
Consider the following preference listsLists
for the stable marriage problem
for men A, B, C, D and women a, b, c, d.
Man 1st 2nd 3rd 4th Woman 1st 2nd 3rd 4th
A b a c d a B D C A
B a d c b b A C D B
C a d b c c D B C A
D d b a c d A D C B
(a)
ManExplain why A-b,
Optimal Matching B-c,B-a,
is A-b, C-a,
C-c,D-d
D-d is NOT a stable matching.
5
Student ID:
4. BFS and DFS [12 pts] (Problem continues onto next page)
Consider the undirected Annulus Graph A7 with 14 vertices V = {u0 , . . . , u6 , v0 , . . . , v6 }
and the associated ordered adjacency lists as shown below:
v2 v1
v3 u2 u1
v5 v6
(a) Run BFS and DFS on A7 . Start both algorithms at u0 . Draw the
edges of the resulting BFS and DFS trees on the appropriate graph
below.
v2 v1 v2 v1
v3 u2 u1 v3 u2 u1
u3 u3
u0 v0 u0 v0
u4 u4
v4 v4
u5 u6 u5 u6
v5 v6 v5 v6
Draw BFS Tree edges connecting vertices above Draw DFS Tree edges connecting vertices above
6
Student ID:
(b) The graph A7 remains the same but we change the order of neigh-
bors in the adjacency lists so that they are now as below:
v2 v1
v3 u2 u1
v5 v6
v2 v1 v2 v1
v3 u2 u1 v3 u2 u1
u3 u3
u0 v0 u0 v0
u4 u4
v4 v4
u5 u6 u5 u6
v5 v6 v5 v6
Draw BFS Tree edges connecting vertices above Draw DFS Tree edges connecting vertices above
7
Student ID:
3 c 12 e
a 10
6 12 f g
b 8
d
the edges (c, e) and (c, d) have the bottleneck weight 12.
Design an O(E) algorithm that, given connected, undirected, weighted
graph G and value B, either returns a spanning tree with bottleneck
weight at most B or reports back that no such spanning tree exists.
You must explain why your algorithm is correct and justify its running
time.
You may use any algorithm given in class as a subroutine (without
having to describe the internal details of that algorithm). If you do
use such an algorithm, you must explicitly state the algorithm you are
using and its running time.
8
The problem is asking whether there is a spanning tree composed of
edges of weight B or less. This can be solved as follows:
(a) Preprocess by running through all the adjacency lists once, throw-
ing away all edges of weight > B.
(b) Run BFS or DFS starting from any vertex.
(c) Check if BFS/DFS sees all of the edges in one connected compo-
nent. If it does, return “YES” and report the spanning tree output
by the DFS or BFS. If it doesn’t, report “N”.
• The running time of both steps (a) and (b) are O(V + E).
• Since G is connected, V = O(E), so O(V + E) = O(E).
• Step (c) can either be done within the BFS/DFS call or in O(V ) =
O(E) time afterwards.
• Also note that (a) wasn’t actually necessary. It was also possible
to modfy BFS/DFS so that, in the loop that checks all neighbors
of a vertex, it would ignore all edges with weight > B.
9
Student ID:
For parts (b) and (c) you should assume that G has more than 3 ver-
tices. A proof of correctness may use the Cut-Lemma as a subroutine.
Any other lemma must be proven from scratch. A proof that one of
the statements is not correct requires a counter-example, e.g., find a
MST that does not contain the edge with the second or third smallest
weight.
10
Solution:
(a) Here is the proof of the cut Lemma from the class notes.
𝑒’
𝑆 𝑆 e is in the MST
𝑢 𝑢
𝑒 𝑒
𝑣 𝑣
𝑇∗ 𝑇 ∗∗
11
(b) True.
Let e1 = (u, v) be the edge with the smallest weight in the graph
and e2 = (u0 , v 0 ) be the edge with the second smallest weight.
There are two cases that need to be examined
(i) e1 and e2 do not share any vertices. In this case define
S = {u0 , u, v}. e2 is then the min-cost edge with exactly one
endpoint in S (because it is cheaper than every edge except for
e1 and e1 has both edges in S). Thus, by the Cut Lemma, e2
must be in the MST of G.
(ii) e1 and e2 do share a vertex. Since they can’t share both
vertices (otherwise they would be the same edge) they only
share one vertex. Without loss of generality suppose this is
u = u0 . Now define S = {u0 , v}. In this case again e2 is the
min-cost edge with exactly one endpoint in S (because it is
cheaper than every edge except for e1 and e1 has both edges in
S). Thus, by the Cut Lemma, e2 must be in the MST of G.
Since (i) and (ii) include all cases that can occur we have just
shown that, in all cases, e2 is in the MST of G.
An alternative proof is to consider what happens if e2 is not in T ∗
already and is inserted into T ∗ . Since T ∗ is connected, this creates
a cycle. Since a cycle must have at least three edges, one of those
three edges must be an edge e00 which is NOT e1 or e2 . Remov-
ing e00 leaves a new Tree T ∗∗∗ with lower cost than T ∗ , causing a
contradiction.
(c) False.
Consider the counterexample below. The three heavy edges are the
MST, which does not include the third cheapest edge (v2 , v3 ).
v2
1 3
v2
4
v1 v3
2
12
Student ID:
j1 = min{j 0 : yj 0 = x1 }
(a) i = 1; j = 1;
(b) While( (i ≤ m) AND (j ≤ n))
(c) If (xi == yj )
(d) i = i + 1; j = j + 1;
(e) ELSE j = j + 1
(f ) If i == m + 1 then
(g) print “success”
(h) ELSE print “Failure
13
Student ID:
14
to find a correct solution to the roster problem? Provide a brief
explanation as to why it will or will not be able to find a correct
solution. If the answer is that it will not, give a brief example
as part of your explanation, showing where your algorithm breaks
down.
Solution
p1 1 P1
2 3
p2 P2 3
s 1 t
2
2 1
3
pn Pm
15
(b) The Ford-Fulkerson algorithm runs in time O(|f ∗ |E) where |f ∗ | is
the value of the max-flow. In our example |f ∗ | ≤ 2n. Also, since
m = Θ(n2 ) (because 2n = 3m) we have E = O(mn + m + n) =
O(n2 ). So, the running time of the algorithm in terms of n is
O(|f ∗ |n) = O(nn2 ) = O(n3 ).
(c) The answer is YES. It will be able to solve the problem. The value
of the max-flow will be the same regardless of whether or not the
solution has integer flow values or not. So, even with the different
solver, al y ou need to do is check whether the max-flow = 2n or
not.
What it will NOT be able to do is find the assignments of the
people to patrols. This is because the algorithm for finding the
assignments assigns person pi to patrol Pj if and only if the flow
from pi to Pj in the max flow equals 1. If the algorithm you are
using does not guarantee that the flows are integers you can’t use
the flows to figure out which people get assigned to which patrol.
For example, if Ai,j = True for every pair i, j then we can set the
flow f (i, j) = m2 = n3 for every pair i, j. There would be no way
to figure out which person would go to which patrol from this.
Although the correct answer was YES, we gave full credits to any-
ome who said NO and gave an explanation as to why it could not
find the assignments.
16
Student ID:
Solution
17
• z14 = e = x9 but z14 6= y5 = s.
⇒ z14 matches to x9 .
• This means that either z13 = x8 = t or z13 = y5 = s.
• But z13 = p so neither of those is possible and therefore Z is
not a shuffle of X and Y.
(b) The boundary conditions are the values of f (i, 0) and f (0, j).
When j = 0, f (i, 0) = 1 is just the statement that Xi = Zi . This
can be checked recursively by setting f (1, 0) = 1 if and only if
x1 = z1 and, for i > 1 f (i, 0) = 1 if and only if f (i − 1, 0) = 1
and xi = zi . A similar set of basis cases holds for f (0, j).
For i, j > 1, if Xi and Yj can be shuffled to result in Zi+j then one
of the two following cases must occur
• The last item in Zi+j is the last item in Xi .
This can happen if and only if zi+j = xi and
Zi+j−1 is a shuffle of Xi−1 and Zj , i.e., f (i − 1, j) = 1.
• The last item in Zi+j is the last item in Xj .
This can happen if and only if zi+j = xj and
Zi+j−1 is a shuffle of Xi and Zj−1 , i.e., f (i, j − 1) = 1.
The initial conditions are then
1 if i = 1 AND x1 = z1
f (i, 0) = 1 if i > 1 AND f (i − 1, 0) = 1 AND xi = zi
0 otherwise
1 if j = 1 AND y1 = z1
f (0, j) = 1 if j > 1 AND f (0, j − 1) = 1 AND yj = zi
0 otherwise
18
To see that this is not correct, consider X = abc, Y = def and
Z = abdef f . For i = 3,, j = 3 this satisfies the condition above
which would imply f (3, 3) = 1 but f (3, 3) = 0
(c) i.Set all f (i, j) = 0;
ii.If x1 = z1 set f (1, 0) = 1;
iii. If y1 = z1 set f (0, 1) = 1
iv. For i = 2 to n
v. If (f (i − 1, 0) = 1) AND (xi = zi ))
vi. set f (i, 0) = 1;
vii. For j = 2 to m
viii. If (f (0, j − 1) = 1) AND (yj = zj ))
ix. set f (0, j) = 1;
x. For i = 1 to n
xi. For j = 1 to m
xii. If (f (i − 1, j) = 1 AND zi+j = xi )
OR (if f (i, j−) = 1 AND zi+j = yj )
xiii. set f (i, j) = 1
19