Cse 421 Midterm
Cse 421 Midterm
a. True. n3.05 = n3 * n.05. If we divide by n3 on both sides, it is a matter of comparing
log(n) and n.05, and log(n) grows faster than n.05. C being 1 in this case would
fulfill the requirement of f(n) < cg(n), at large values of n.
b. False. 22n = 2n * 2n. The amount that 2n is multiplied by will grow exponentially,
and so there is no constant that can lower 22n enough so that 2n will be greater than
it at high values of n.
c. True. In order to determine if there is an odd length cycle, first, all of the
edges/vertices must be examined, giving a runtime of O(m + n), and there does
exist an algorithm that finds a cycle and reports it in that time (according to HW 2
Q1). After that, it’s just a matter of counting how many nodes there are in a cycle.
That could take up to O(m) time to reiterate over it, so each edge will be looked at
twice. The maximum number of edges in a graph is m = n(n-1)/2. Thus, O(2m +
n) = O(2(n(n-1)/2) + n) = O(n(n-1) + n) = O(n2 - n + n) = O(n2). An algorithm is
said to run in polynomial time if it runs in time O(nd) for some constant d on
inputs of size n. In this case, d is 2 and n is the number of vertices.
d. False. The solved recurrence has a = 3, b = 2, and d = 5, which means a < bd. The
recurrence is thus n5. There is no constant to multiply n3 by that will make n5 less
than n3 at high levels of n.
e. False: Counter example is as follows to the right
If this is our graph with our cycle and our vertice
in the top right with two equal heaviest edges,
any MST for this graph would require at least
one of those edges. As those are the only two
edges connected to the top right vertice, you have
to use at least one of them to reach that vertex in
the MST.
f. True. If we were to treat the vertices on each end
of the edge as one vertex cuts, then by the cut
property, the smallest edge must be in every
MST.
g. False. The fastest we saw in lecture was Karatsuba’s which was around O(n1.46)...?
No matter how small you split up the bits I don’t think you could get it to n1.1, you
have to combine them afterwards anyways, even though at the smallest level “1
bit numbers” cost a constant amount you still have to add them all up later.
h. False. For a vertex cover, you’d need at least one of the vertices from each of the
edges in the matching because a vertex from an edge in the matching cannot cover
more than one edge found in the matching, due to the fact that all the matching
edges are separated by at least one edge in between them all.
i. True. If at least n sets are required, to cover n elements, then each of the n points
requires a unique set to cover it. If you simply just looped through all m sets,
adding them to the set cover if they covered a new node. By the time the set
covers the universe, if you have at least n sets, you’ll know that’s the case; if it’s
any less, then it’s the other case. This happens in O(m), which is well within
polynomial time if the input is the number of sets.
2. This algorithm adds the edges of vertices with a degree of one. These edges must be part
of the matching because there is no other way to saturate those vertices. Furthermore,
after adding this edge, the vertex on the other end is removed, along with any other edges
it’s connected to, because they cannot possibly be part of the matching as one of their
ends is already saturated. Thus, the edges added to ME must all be part of the perfect
matching. Through this process of adding and eliminating vertices/edges, the matching is
built up. Obviously this runs in O(n) times, which is well within polynomial run time, as
each node can only be removed once.
Base case: The algorithm holds for a graph of size 2
Assume the algorithm holds for a graph G with 2n vertices.
Now consider a graph G` with 2(n + 1) vertices. Since the graphs have no cycles, there
must be at least one vertex with a degree of 1. The edge connecting this vertex to the rest
of G`, E, must be in the graph’s perfect matching because there’s no other way to saturate
that vertex. If we were to remove this edge and those two vertices, we have a graph G
with 2n vertices, which the algorithm already works for as we hypothesized. If G had a
perfect matching and G`did as well, then the two extra vertices would have to be
connected to each other (as all the other vertex pairs are taken). Additionally, if all the
other vertices are saturated, that means that at the end of the algorithm, regardless of how
many there were, no edges remain between those two extra vertices and all of the other
vertices in the graph, as those vertices have been removed. The algorithm would then
correctly identify the last two vertices with degree 1 as saturated, add the edge to ME,
and return. G`’s perfect matching is G’s perfect matching plus the extra edge, and vice
versa. Through the hypothesis, the algorithm holds for all values of n.
Let there be an array, ME, containing the edges in the matching, which is empty at the
start
while(T has vertices)
if(Vertex V has a degree of 1)
Add V’s Edge, E to ME
Remove both ends of E from T.
Else
Return no matching
Return ME
3. A: O(nlog2(5))
This recurrence ends up being T(n) = 5T(n/2) + n. Since a, 5, is greater than bd, 21, our
runtime is O(nlogb(a)), which is O(nlog2(5)).
B: ᘯ(2n+1)
This recurrence ends up being T(n) = 2T(n - 1) + 1. The summation is as follows:
∑ 2iT(n - i) + 1 from i = 0 to i = n
n + ∑ 2iT(n-i) from i = 0 to i = n
n + (2n + 1 - 1)/(2-1)∑ T(n - i) from i = 0 to i = n, based on the identity we learned in class
n + 2n + 1 ∑ T(n - i) from i = 0 to i = n, 2n + 1 > 2n + 1 - 1
At this point we know the run time is at least n + 2n + 1, so it’s ᘯ(2n+1 + n) at least.
C: O(n2log3(n))
This recurrence ends up being T(n) = 9T(⅓) + n2. Since a, 9, is equal to bd, 32, our
runtime is O(ndlogb(n)), or O(n2log3(n)).
4. My algorithm divides the input arrays into smaller subgroups until they are in pairs, and
naively sorts each pair with pointers into one merged array. This is repeatedly done with
resulting arrays until all the arrays have merged into one. This runs in O(nklog(k)) time
because all nk elements are traversed log(k) times, the number of times we’ll have to
merge on each level.