Sunteți pe pagina 1din 2

CS 180, Summer 2014 Homework 1 Solution Outlines

1. Suppose youre helping to organize a summer sports camp, and the following problem comes up. The
camp is supposed to have at least one counselor who is skilled at each of the n sports covered by the
camp. They have received job applications from m potential counselors. For each of the n sports,
there is some subset of the m counselors qualified in that sport. Our goal is to hire as few counselors
as possible, subject to the constraint that we hire at least one counselor for each of the n sports.
Consider the following algorithm that attempts to solve this problem: for each candidate, count how
many sports he or she can cover for which we have not yet hired a counselor. Hire the counselor whose
count is the highest. If we havent yet covered every sport, update the counts and repeat.
Prove that this algorithm is not correct; that is, it cannot guarantee to always hire the fewest possible
counselors.
Solution There are plenty of counter-examples. Consider six sports, A-F, and four counselors. Their
knowledge is {A,B}, {C,D}, {E,F} and {A,C,E}. The algorithm would pick all four, while the first
three are sufficient for coverage.
2. Order the following functions in order from smallest asymptotic running time to greatest. Justify1
your answer.
fa (n) = log2 n2
fb (n) = 4log2 n
fc (n) = n7/4
fd (n) = 1.01n
fe (n) =

n2
log2 n

ff (n) = n9/4
fg (n) = log22 n
Solution: The order is fa (n), fg (n), fb (n), fc (n), fe (n), ff (n), fd (n).
The functions fit into three categories: sub-linear, polynomial (but at least linear), and exponential.
fa and fg are both sub-linear (O(n) but not (n)); fa simplifies to 2 log2 n, which is O(log n), while
fg is the square of that. Since logn > 1 for large enough n (in fact, for most values of n), the square
is larger. Accordingly fa < fg .
fb (n) simplifies, but this depends on what you assumed the base of the logarithm to be. When a
logarithm is in the non-exponent part, it doesnt matter which base: O(log10 n) = O(log2 n) (because
the change of base from 10 to 2 only involves multiplying by a constant). However, in the exponent,
.3 log2 n
it will affect how the function simplifies. If you assumed the base is 10, then 4log n = 22
=
.6 log2 n
.6
2
= n . This is asymptotically larger than the logarithm functions, but smaller than the other
polynomials.
It is helpful to compare the other two polynomials to O(n2 ) : ff is clearly larger ( 49 > 2), while fc
and fe are smaller. However, as n increases, fe approaches O(n2 ) (never quite reaching it, but getting
7
arbitrarily close: O(n2 ), while fc remains O(n 4 ). Accordingly, fc < fe < ff .
Lastly, fd is exponential, and as we saw in lecture and in the reading, these are always larger asymptotically than polynomials.

1 For

this class, when I ask you to justify (instead of prove) an answer, it is sufficient to give an intuitive explanation.

3. Consider the Subset Sum problem. You are given a set S of n positive integers and a target positive
integer T . The goal is to determine if there is a subset S 0 S whose elements sum is T .
Subset-Sum(S, n, T)
Initialize M [0 . . . n, 0 . . . T ]
for j = 1 . . . T do
M [0, j] = False
for i = 0 . . . n do
M [i, 0] = True
for i = 1 . . . n do
for j = 1 . . . T do
if si > j then
M [i, j] = M [i 1, j]
else
M [i, j] = M [i 1, j] OR M [i 1, j si ]
return M [n, T ]
(a) What is the (asymptotic) runtime of this algorithm in terms of n and T ? Solution: O(nT ).
(b) Is this a polynomial time algorithm, as per the definition from lecture and from the reading? Why
or why not? Solution This is not. The size of the input is O(n + log2 T ), and this is exponential
in the latter parameter.
4. Suppose have a directed acyclic graph G and we want to find out if there is a simple path that visits
every vertex. Give a linear time algorithm that determines if a given graph has such a path.
Claim: If G has such a path, it has a unique topological sort.
Proof of Claim: The first vertex in the path must be first in the topological sort. Every other vertex
has at least one incoming edge. The second element in the path must be second: its only incoming
edge can be from the first; if it had another, it would be from some vertex v. However, there is a path
from the second element to v, and an edge from v to that second element. This produces a cycle, thus
contradicting the acyclic nature of G. Future elements being later in the topological sort are argued
in a similar manner.
Once that claim has been established, it suggests a natural linear-time algorithm: take a topological
sort of G, then check if that topological sort constitutes a path. If it is a path, we have our answer.
Otherwise, we can correctly conclude that G lacks such a path, as if it had one, it would have been
produced by the topological sort.
What wont work : Depth-first search without marking vertices as visited. This will enumerate all paths
in the graph, which can be exponentially many.
What also doesnt work : While G having a single source and single sink are necessary conditions for
there to be a Hamiltonian Path in the DAG, they are not a sufficient condition: a graph can have
exactly one of each and still not have such a path.
5. We are given a graph G = (V, E) and two vertices s and t within G. We are told that the length of the
shortest path from s to t is more than n2 (where n is the number of vertices within G). Give a linear
time algorithm to find a vertex v such that every path from s to t contains v.
Solution Suppose we run breadth-first search from s to t to find the shortest path. Because there are
more than n2 layers, it isnt possible for each layer to have two vertices in it; if any vertex is in a layer
by itself (and at least one will be), that one is in every shortest path from s to t.

S-ar putea să vă placă și