EECS 336: Algorithms
1
Homework 7 Solutions
Zoo Tycoon
Algorithm: Construct a source s and a sink t. For each food j, create a vertex xj , and add an
edge from s to xj with capacity Tj . For each animal i, add a vertex yi , and for each food j not in
Si ,

Northwestern University
Electrical Engineering and Computer Science
EECS359/459: Art of Multicore Programming
Prof. Hai Zhou
Sept 29, 2016
Handout #2
Due: Oct 6
Homework 2
The problems starting with Exercise are problems in the textbook. You can discuss p

Northwestern University
Electrical Engineering and Computer Science
EECS359/459: Art of Multicore Programming
Prof. Hai Zhou
Sept 22, 2016
Handout #1
Due: Sept 29
Homework 1
Questions will usually be given as exercise nubers from the textbook, but this fi

EECS 336
Homework 1
Problem 1
Solution:
(a): The ordering of these six functions:
(n + 1)! = (en ), en = (lgn)2lgn ), (lgn)2lgn = (42lgn ), 42lgn = (2lgn ), 2lgn = (lgn)
The following justifications explain the rankings:
(lgn)2lgn = n2lglgn , since algb =

EECS 336
Homework 4
Problem 1
Solution:
This algorithm is incorrect.
We can use a counter example to explain this:
X:BCDDDD
Y:DDDDCB
If we use the greedy approach mentioned in this paper to fine the LCS of sequence X and
Y, we will get the steps as follow

EECS 336
Homework 3
Problem 1
Solution:
We can use the Dynamic Programming to find the Shortest maximumweight common subsequence of X and Y that has the most occurrences of s1 among all shortest maximumweight
common subsequences of X and Y.
The pseudo Cod

EECS 336
Homework 5
Problem 1
Solution:
(a) In this problem, define i be the i-th operation in the min-heap, ni be number of elements
in the heap after the i-th operation.
ni
P
Let the potential function of this binary min-heap be (Di ) =
lgm
m=1
INSERT:

EECS 336
Homework 2
Problem 1
Solution:
Algorithm:
a) We can spilt the array X into 2 subarrays: X1 and X2, each of subarrays are in half size.
b) After that, we do a linear time equality operation to decide whether it is possible to find
second majority

EECS 336
Homework 8
Problem 1
Solution:
Algorithm:
The algorithm to deal with this problem is same as the randomized 2-approximation algorithm for MAX-3-CNF problem. Let us set every variable to 1 with probability 21 and to
0 with probability 12 independe

EECS 336
Homework 6
Problem 1
Solution:
The 0-1 knapsack problem is not always a polynomial-time problem.
This problem can be solved by dynamic programming algorithm.
Algorithm:
we can use i represent the i th item,
wi to represent the weight of i th item

EECS 336
Homework 7
Problem 1
Solution:
We define v1 , v2 , ., vn as the n variables, c1 , c2 , ., cm as the m clause, ki represents the number of literals in the i-th clause, and Lj represents the j-th literal in the i-th clause.
Algorithm:
DNF-SAT(v1 ,

Problem Set 1 Solutions
Prepared by Rachel Cummings
January 24, 2013
Problem 1
There are many examples
m1 m2 m3 m4 w1
w1 w4 w3 w2 m4
w2 w1 w4 w3 m3
w3 w2 w1 w4 m2
w4 w3 w2 w1 m1
with
w2
m3
m2
m1
m4
n=4
w3
m2
m1
m4
m3
and at least three stable matchings. B

Partial Solutions to Homework 7
Prepared by Rachel Cummings
Problem 2
This solution only presents the correct algorithm. For full credit, you needed to complete all parts of the
problem.
Input: Strings S1 , S2 , S3 , S4 of lengths n1 , n2 , n3 , n4 respec

Problem Set 3 Solutions
Prepared by Rachel Cummings
February 8, 2013
Problem 3
A dag is a directed acyclic graph. This means that your graph should have directed edges and no cycles.
A topological ordering is one where if there is a (directed) edge from s

Partial Solutions for Homework 8
Prepared by Rachel Cummings
Problem 1
Independent Set
(a)
An instance s of this problem is a graph G = (V, E) and a number k. The certificate t is a list of at least k
vertices that form an independent set
Certifier B(s,t)

1
Exercise 47. Prove Lemma ?.
Solution Let A0 , . . . , An1 be the n threads. Consider the sequence of initial states s0 , . . . , sn where A0 , . . . , , Ai1 have inputs 1 and Ai , . . . , An1 have
inputs 0. (That is, s0 has all inputs 0 and sn all input

EECS395/495 Solution #4
29.
Yes. Every method call must finish in finite steps to be wait-free. We
prove it by contradiction. If there is a method call that takes infinite
steps to finish. The thread calling the method is only possible to finish
finite nu

EECS 336: Algorithms
1
Homework 2 Solutions
Myth Boosters
Algorithm:
int j = 1
for i from 1 to n
if wi = tj
j+
if j = m + 1
return True
else return False
Proof of correctness: We must argue that our algorithm returns True if and only if T is a
valid secre

EECS 336: Algorithms
1
Homework 1 Solutions
Asymptotics
a. f (n) = (g(n)
b. f (n) = O(g(n)
c. f (n) = (g(n)
d. f (n) = (g(n)
e. f (n) = O(g(n)
f. f (n) = (g(n)
g. f (n) = (g(n)
h. f (n) = O(g(n)
i. f (n) = O(g(n)
j. f (n) = (g(n)
2
More Asymptotics
a. The

EECS 336: Algorithms
1
Homework 8 Solutions
Secret Santa Cycles
We give an algorithm to solve this problem by reducing to bipartite matching. For each vertex in
V , construct a vertex vout on the left side and a vertex vin on the right. For every edge (u,

EECS 336: Algorithms
1
Homework 3 Solutions
Matroids
Assume (E, I) is not a matroid. Then either the subset property does not hold, or the subset
property does hold, but the augmentation property does not hold. We will argue these two cases
separately.
Su

EECS 336: Algorithms
1
Homework 9 Solutions
No Free Lunch
This problem is NP-Complete. We reduce from Set Cover.
Construction: An input to Set Cover is a list E = cfw_1, . . . , n of elements, a collection of subsets
of cfw_1, . . . , n, which we will cal

EECS 336: Algorithms
1
Homework 6 Solutions
Reading And/Or Weeping
Subproblem: Let OPT(i, j) be the total unhappiness you will experience from reading sections
j, . . . , n optimally over the course of days i, . . . , D.
Recurrence:
Notation:
P0
Let T (j

EECS 336: Algorithms
Problem X.1 Solution
Before we present the four-part DP solution, we need some notation. Note that you can represent a
planting solution as a 4 n matrix of 1s and 0s - 1 in entry (i, j) if there is a tree in that spot, 0 otherwise. Us

EECS 336: Algorithms
Problem X.2 Solution
We reduce from 3-SAT. Problem is often known as Vertex-Disjoint Paths.
Construction: We are given an input to 3-SAT with m clauses and n variables. Label the variables
(as opposed to literals) appearing in clause

EECS395/495 Solution #3
14.
The modification is simple, just replace
for (int i = 1; i < n; +)
with
for (int i = 1; i < n - l + 1; i+)
The proof of l-exclusion and l-starvation-free are similar as in the
textbook.
16.
a. Proof: By contradiction. Assume th

Hw#2 Solution
11.
a. It satisfies mutual exclusion
Proof: Suppose not. Let A and B be two threads concurrently in critical section.
Inspecting the execution sequence, we have
A: turn = A A: busy = false A: busy = true A: turn = A
B: turn = B B: busy = fal

Problem Set 2 Solutions
Prepared by Rachel Cummings
January 31, 2013
Note: For the following reductions, there are multiple algorithms that will also yield correct proofs. Full
credit was given on the homework for correct proofs, regardless of the algorit