Algorithm Homework Help
Algorithm Homework Help
• MAKE-SET(u): Θ(1)
• WEAK-UNION(u, v): Θ(1) (where u, v are the roots of two trees)
• FIND-SET(u): Θ(lg n)
• UNION(u, v) = WEAK-UNION(FIND-SET(u), FIND-SET(v)): Θ(1) + 2Θ(lg n)
= Θ(lg n)
Thus, using this data structure, a series of m operations will take O(m lg
n) time.
(b) Explain how you can use the data structure in (a) to achieve
o(m lg n) run time on m total operations.
Solution:
Suppose that there are n1 classes you are thinking of taking. You
have a list of which of your n2 friends are taking which classes.
To figure out your schedule for the semester, you’ve decided to
learn as much as you can about all of the classes. You can learn
something about a class in two ways: (1) attend the class
yourself, or (2) ask a friend who is taking the class what it’s like.
Attending a class takes one hour. Speaking to one friend also
takes one hour, but during that hour you can quiz your friend
about all of the classes they are taking.
(a) For each available class, you would like to either attend
the class in question, or speak to all of your friends in the
class to get their personal opinion. You only have k hours
to devote to this process. Either devise a polynomial-time
algorithm for picking out k friends and classes that satisfy
these constraints, or show that the problem is NP-hard.
Solution:
Suppose that we have some cut (S, V −S) of the graph whose
capacity is finite. Then the only edges crossing from S to V − S
must be edges of capacity 1. Each such edge corresponds to either
attending a class or seeing a friend. We must determine two
things about this schedule: the number of hours required, and
whether the schedule lets us learn about all classes. The number
of hours required is the same as the capacity of the cut.
Now suppose that we have a schedule that lets us learn about all
classes. We wish to show that this corresponds to a cut with
capacity equal to the amount of time required for the schedule.
For each friend f we talk to, remove (s, xf ) from the graph. For
each class c we attend, remove (yc, t) from the graph. Let S be the
set of vertices reachable from s in this modified graph.
First, we wish to show that S is a cut: namely, that t ∈/ S.
Suppose for the sake of contradiction that t ∈ S. Then there
exists some path (s, xf , yc, t) from s to t in the modified
graph. Consider the class c. Because (yc, t) exists in the
modified graph, we were not taking c in the original schedule.
So it must be that we talked to all friends who were attending
c, including f. But that would mean that (s, xf ) was removed
from the graph, which contradicts the existence of the path
(s, xf , yc, t). So S must be a cut that separates s from t.
Solution:
S = {v | fv ∈ A} ∪ {u | c(u,v) ∈ B}
This means that |S| ≤ |A| + |B| = k = ℓ. (If |S| < ℓ, add arbitrary
nodes until |S| = ℓ.) Now we wish to show that S is a vertex
cover. Consider some edge e = (u, v) ∈ E. In order to learn
about class ce, we must have fu ∈ A, fv ∈ A, or ce ∈ B.
Therefore, the constructed set S must either contain u or v. As a
result, S is a vertex cover.