Homework 1: You Are Allowed To Have at Most 4 Collaborators
Homework 1: You Are Allowed To Have at Most 4 Collaborators
Homework 1
Released 2/09/2020 Due 2/22/2020 11:59pm in Gradescope
Instructions. You may work in groups, but you must write solutions yourself. List collaborators on your
submission. You are allowed to have at most 4 collaborators. Also list any sources of help (including online
sources) other than the textbook and course staff.
If you are asked to design an algorithm, please provide: (a) the pseudocode or precise description in words
of the algorithm, (b) an explanation of the intuition for the algorithm, (c) a proof of correctness, (d) the
running time of your algorithm and (e) justification for your running time analysis.
Submissions. Please submit a PDF file. You may submit a scanned handwritten document, but a typed
submission is preferred. Please assign pages to questions in Gradescope.
1. (20) Latin Squares and Stable Matching. A Latin square is an n × n array of numbers, each in
the set {1, . . . , n}, such that no number appears twice in any row or any column. The following array
of numbers is an example of a Latin square:
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
If we have a preference list where each college ci ranks the n students, we can represent this list by an
n × n array where the ith row represents the peferences of college ci . Another n × n array can represent
the preferences of the n students.
a) Suppose that the preference list of the colleges is a Latin square. What is the largest and smallest
number of proposals in a run of the Gale-Shapley algorithm (with colleges proposing) under this
condition? (We assume nothing about the student preferences.) If you can’t get the exact number of
proposals as a function of n, find a function f such that you can show the number to be Θ(f ).
b) Now suppose that the preference list of the students is a Latin square. Find the exact minimum
possible number of proposals in any run of the Gale-Shapley algorithm (with colleges proposing) under
this condition? (We assume nothing about the college preferences.) This means finding a function
f such that for any n, there is a run with exactly f (n) proposals and no run with fewer than f (n)
proposals.
c) Find a function g such that the maximum number of proposals on a run of Gale-Shapley with the
student preference lists a Latin square and the colleges proposing is Θ(g).
d) (extra credit) Find the exact maximum number of proposals possible under this condition, with the
colleges proposing. This means finding a function g so that there is a run with g(n) for every n, and
no run ever has more than g(n).
2. (20 points) Big-O and big-Ω. For each function f (n) below, find (1) the smallest integer constant
H such that f (n) = O(nH ), and (2) the largest positive real constant L such that f (n) = Ω(nL ).
Otherwise, indicate that H or L do not exist. All logarithms are to base 2.
a) f (n) = (3n + 1)(2n − 3)(n + 2).
Pn
b) f (n) = k=1 k 2 .
c) f (n) = 4log(n) .
√
log n
d) f (n) = 2 .
1
Homework 1 2
3. (20 points) Running time analysis. In the lecture, we consider the following program, that takes
an array of n elements as the input.
SumOfProduct(A[1, . . . , n])
1. s ← 0; x ← 0; y ← 0
2. for i ← 1 to n
3. x ← x + 1/i
4. if x ≥ y + 1
5. y ←y+1
6. for j ← 1 to n
7. s ← s + A[i] · A[j]
8. return s
5. (20 points) Three-Color Search (after Jeff Erickson) One way to describe a generic graph search
is by marking each vertex using one of three colors: white, gray, or black.
ThreeColorStep()
v ← any gray vertex
ThreeColorSearch(s):
if v has no white neighbors then
color all nodes white
color v black
color s gray
else
while at least one vertex is gray do
w ← any white neighbor of v
ThreeColorStep()
parent(w) ← v
color w gray
a) Prove that ThreeColorSearch maintains the following invariant: no black vertex is a neighbor
of a white vertex.
b) Prove that after ThreeColorSearch(s) terminates, all vertices reachable from s are black, all
vertices not reachable from s are white, and that the parent edges v → parent(v) define a rooted
spanning tree of the component containing s.
Hint: as the search progresses, black nodes are “explored”, and gray nodes are the frontier (“discov-
ered”).
c) Prove that thee following variant of ThreeColorSearch, which maintains the set of gray vertices
in a stack, is equivalent to depth-first search. (Hint: the order of the last two highlighted lines matters.)
Homework 1 3
ThreeColorStackStep()
pop v from the stack
ThreeColorSearch(s): if v has no white neighbors then
color all nodes white color v black
color s gray else
push s onto the stack w ← any white neighbor of v
while at least one vertex is gray do parent(w) ← v
ThreeColorStackStep() color w gray
push v onto the stack
push w onto the stack