CS170 Midterm 1 Solutions
Problem 1
1. (7 points)
17
18
15
16
E
20
19
14
10
5
D
13
12
11
J
Pre order numbers are to the upper left of each node, post order numbers are to the right.
Cross edges are BA, BC, EA, and EH.
Grading: 5 points for correct pre and post order numbers. 2 points for identifying any cross edges.
Points were deducted based on how many of the pre and post order numbers were wrong or for
identifying incorrect cross edges or not identifying any at all.
2. (4 points)
BEF
CG
HIJ
Grading: 4 points for the correct SCC DAG. 1-2 points were deducted for wrong edges in the DAG
based on how many edges were introduced or missed. 2 points were deducted for incorrectly identifying
the SCCs by at most one node, but having the graph otherwise correct.
3. (4 points)
The minimum number of SCCs in the resulting graph is two. We can achieve this number by adding
an edge from any of H, I, J or D to any of B, E, or F. The reason two is the minimum (and not one) is
that there are two sink nodes in the SCC DAG. Adding any single edge could only eliminate at most
one of the sink nodes.
Grading: 2 points for identifying two as the minimum number of nodes in the resulting SCC DAG.
Note that if your graph was incorrect for part 2, we tried to be consistent with your answer there. 2
points for justifying why two was the minimum number of SCCs after adding a single edge. Simply
stating what the edge is and showing that it reduces the graph to two SCCs does not tell us why we
cannot reduce the graph to one SCC.
Problem 2
1. (1 of 5 points) 51
= 17 (mod 21).
One way to compute this is to use the extended Euclidean algorithm.
21 =4 5 + 1
5 =5 1 + 0
We find 21 4 5 = 1, so 51
= 4
= 17 (mod 21).
Grading: 1 point for a correct answer, or 1/2 point for a good attempt or indicating that one is trying
to find x such that 5x
= 1 (mod 21).
2. (2 of 5 points) 12 of the 21 integers modulo 21 have inverses.
An integer has an inverse iff it is coprime to 21, iff it is divisible by neither 3 nor 7. Of the 21 integers,
7 are divisible by 3, 3 are divisible by 7, and one is divisible by both. 21 7 3 + 1 = 12.
Grading: 2 points for a correct answer, or 1 for saying an integer has an inverse iff it is coprime to 21.
3. (2 of 5 points) 5x
= 1 (mod 21) iff x is a multiple of 6.
This can be verified by trying the values 51 , 52 , . . . (mod 21) and seeing that the first 1 appears when
x = 6.
The easiest way to discover this is to use the Chinese Remainder Theorem: 5x
= 1 (mod 21) iff 5x
=1
x
(mod 7) and 5 = 1 (mod 3). By Fermats little theorem, if x is divisible by 6, then 5x
= 1 (mod 7).
Conversely, 52 and 53 are not
= 1 (mod 7), so we know 5x
= 1 (mod 7) iff x is a multiple of 6. In a
similar way, we can determine that 5x
= 1 (mod 3) iff x is even. By the Chinese Remainder Theorem,
then, 5x
= 1 (mod 21) iff x is a multiple of 6.
Grading: Two points for the correct answer. Otherwise, one point for noticing that 5x
= 1 (mod 21)
if x is a multiple of 12, or for stating other true facts about x, besides just saying 5x
= 1 (mod 21).
4. (5 points) We make five calls to divandconq(n/2), and then print n2 lines. On input n, call the number
of lines printed T (n). Then we have the recurrence
T (n) = 5T (n/2) + n2 .
Using the Master Theorem, T (n) = (nlog2 5 ).
Grading: Two points for stating the recurrence relation. Three points for finding the correct solution,
minus one point if the solution wasnt simplified.
5. (5 points)
(2 of 5 points) is any primitive eighth root of unity: = ei2/8 = cos(/4) + i sin(/4).
Grading: Two
any primitive eighth root of unity written as = cos(/4) sin(/4)
points for
or = 1/ 2 i 1/ 2 or similar. Otherwise, one point for writing = ei2/n or that is a
1/n rotation or similar.
(3 of 5 points) A2 = A( 2 ) = 1 i or 1 + i depending on which value for we choose.
Our polynomial is A(x) = 1 x + x2 x3 + x6 + x7 . 2 = i; the easiest way to tell is that 2
must be a primitive fourth root of unity. So A2 = A( 2 ) = A(i) = 1 i.
Grading: Three points for the correct answer, or for a wrong answer in x + i y form as long as
you wrote that 2 = i, followed some steps that should have worked but with an addition or
multiplication error. Otherwise, two points for the right idea but not written in x + i y form, or
for evaluating A( k ) for some k 6= 2.
Problem 3
It is instructive to compare formal proofs with brief arguments given below, and observe how the brief
arguments contain most of the ideas from and the general outline of the formal proofs (by mentioning key
players of the proofs, e.g. what exactly is the contradiction, which and how the cycle is formed, the use of
quantification every, etc). Note also that standard terminology (e.g. order preserving) are used whenever
possible, to avoid potential confusion. It is perfectly okay (and usually recommended in tests) to write a bit
more than the following briefest arguments, because often it makes your ideas clearer.
For grading, one point for correct True/False, and three points for explanations supporting the True/False.
We tried to grade answers based on the ideas, but for consistency we need to base our grading on what was
written on the midterm, rather than on subsequent clarifications of answers after midterm. Thats why it is
important to try to be precise in giving answers, to make the ideas clearer.
(A) If all weights of a graph are distinct, the minimum spanning tree is unique: True.
Remark: We present two ideas. The first one gives a clean proof using the cut property, and is
recommended. The second one gives a correct but less intuitive, iterative proof presented in Lecture.
The proof of the cut property shows that every MST must use
Formal Proof 1: Fix a cut (S, S).
When the edge weights are distinct, there is a unique lightest
some lightest edge crossing S and S.
edge crossing S and S, and so every MST must use this edge. It follows that MST is unique. (A bit
more verbosely, consider an MST T . Consider any edge e T , then T e creates a cut. Now e must
so any MST must also use e.)
be the lightest edge crossing S and S,
Brief Argument 1: The lightest edge crossing any cut is unique, and must be in every MST by (the
proof of) the cut property.
Formal Proof 2: Assume for contradiction that there are two different minimum spanning trees T
and T 0 . So there must be some edge in exactly one of T and T 0 (but not both), let e be the heaviest
one, and assume e T but e
/ T 0 . Take away e from T , and T breaks into two connected components
0
where e0 6= e.
S and S. In T , there must be at least one edge e0 crossing the cut between S and S,
0
00
Since T is a MST, we conclude that cost(e) cost(e ), for otherwise the tree T = T e + e0 would
have cost less than T (this is essentially the proof of the cut property). Distinctness of edge weights
gives cost(e) < cost(e0 ), but e0 is also in exactly one of T and T 0 , contradicting our choice for e (as the
heaviest such edge).
Brief Argument 2: Consider the two MSTs T and T 0 . Whenever T has an edge e that is not in T 0 ,
T 0 has a (strictly) heavier edge e0 that is not in T , where e0 crosses the cut created by T e. Iterating
this argument gives an infinite sequence of edges.
Remarks:
In both proofs, the main idea is to modify the proof of the cut property given in the textbook. Points
for explanations were not awarded if the (cut/cycle) structure is not implied in the answers.
Many answers mentioned that the output of a greedy MST algorithm (e.g. Kruskals, Prims)
is unique. This statement, while true, is not sufficient to show the uniqueness of the minimum
spanning tree, because those algorithms are not the only ways to find MSTs, a priori.
Some answers seemed to state that if there are a list of distinct numbers, then no two equal-length
sublists of them can have the same sum. This is not true.
The grading is based on whether and how clear the cut property is modified to lead to a valid
argument.
(B) If all weights of a graph are distinct, the second shortest spanning tree is unique. False.
The following graph has two different second MST.
4
2
Graph
MST
4
5
2nd MST
2
2nd MST
Remarks:
Some answers seemed to have confused shortest paths with spanning trees, and got points deducted.
Some answers mentioned and argued using a directed variant of MST, which is incompatible with
what is taught in class, and got points deducted.
(C) If all weights of a graph are distinct, the second shortest spanning tree contains the shortest edge.
True.
Formal Proof: Assume not, there exists a second MST T not containing the lightest edge (u, v).
Consider the path P from u to v on T , so P and (u, v) form a cycle. For every edge e on P, the
spanning tree Te = T e + (u, v) is lighter than T , since (u, v) is the unique lightest edge. P has at
least two edges, so there are at least two different spanning trees lighter than T , contradicting that T
is a second MST.
Brief Argument: The second MST T and the lightest edge e form a cycle of length at least three,
where by swapping e with edges of T on the cycle, we get (at least) two spanning trees lighter than T .
Remarks:
Some answers mentioned swapping the heaviest edge with the lightest edge for lighter spanning
trees. Without furthur quantification, this cannot be done, because the heaviest edge may be in
every spanning tree.
Some answers mentioned it is (always) possible to find shorter spanning tree than the second
MST, which is not sufficient for a contradiction.
(D) If we add the same number to all weights of a graph, the minimum spanning tree remains the same.
True.
Brief Argument: Every spanning tree has exactly |V | 1 edges, so after adding k to all weights,
every MST gets its weight increased by k(|V | 1), and the ordering of spanning trees is preserved.
Remarks:
A formal, one-line argument to justify the order-preserving property is as follows: a < b iff
a + c < b + c for any real numbers a, b, c. Some answers mentioned this, making the idea clearer.
Some answers just mentioned that the ordering of the edge weights are preserved. While true, this
is insufficient to show that the MST is the same, strictly speaking.
(E) If we add the same number to all weights of a graph, the shortest path from A to B remains the same.
False.
C
1
A
C
3
1
3
3
5
Original Graph
Add 2 to every edge
Shortest path: (A, C), (C, B)
Shortest path: (A, B)
Remarks:
Some answers essentially gave a family of well-defined counter-examples, and were also accepted.
Problem 4
P
Let T0 be the total sum of all the times for all the edges in the graph. In other words, T0 = eE T (e).
For every edge (u, v) in the graph, consider a single weight function w(u, v) = E(u, v) + (T (u, v)/100T0 ).
Running Dijkstras algorithms with this single weight function sufficed to get the shortest path according to
energy and if there were multiple shortest paths according to energy, then this one gives the shortest one
prioritized according to time.
two paths P1 and P2 such that P1 is more energy
P Proof sketch: Consider
P
P efficient. This implies
E(u,
v)
<
E(u,
v).
Because
all
the
energies
are
integers,
(u,v)P2
(u,v)P1 E(u, v) + 1/100 <
P(u,v)P1
(u,v)P2 E(u, v).
P
P
Now, consider the efficiency of P1 and P2 according to w. Note that (u,v)P2 w(u, v) (u,v)P2 E(u, v).
P
P
P
P
Similarly, (u,v)P1 w(u, v) = (u,v)P1 E(u, v) + ( (u,v)P1 T (u, v))/100T0 (u,v)P1 E(u, v) + 1/100
P
P
Thus, (u,v)P1 w(u, v) < (u,v)P2 w(u, v). Hence, paths maintain their ordering w.r.t. energy efficiency even under the new weighting function. Further, it is obvious that in case you have 2 paths with the
same energy efficiency, w(, ) assigns less weight to the one which consumes less time.