You are on page 1of 5

Homework 6

Autumn Johnson
November 18, 2015

Page 313, Exercise 2

A.

li
hi

Week 1
2
1

Week 2
3
7

Week 3
1
50

The given algorithm finds no job for Week 1, a high-stress job for Week 2, and
a low-stress job for Week 3, resulting in a total value of 8. However, the correct
answer is choosing a low-stress job for Week 1, no job for Week 2, and a highstress job for Week 3, resulting in a total value of 52.
B.
Let Oj denote the optimal solution to the problem over 1, 2, ..., n weeks, and
let OPT(j) denote the value of this solution. Given a set of values l1 , l2 ,...,ln
and h1 , h2 ,...,hn corresponding to the value of a low-stress li or high-stress hi
job at the ith week, the following algorithm will find Oj :
Let J be a set of all possible jobs j with start time sj and finish time fj . Add
a j to J for each li such that sj = i - 1, fj = i, and value(j) = li . For each hi
over weeks 2, 3,...,n add a j to J such that value(j) = hi and sj = i - 2 and fj =
i. Finally, assuming n > 0, add a j to V such that value(j) = h1 , sk = 0, and fk
= 1. All j initialized with a high-stress value (except for h1 ) has an interval of
length 2 since a high-stress job at week i requires that no job be done on week
i - 1; thus the total time dedicated towards the value of a high-stress job is 2.
Sort these job value intervals in order of nondecreasing finish time: f1
f2 ... f2n . Define p(j), for a job j to be the largest index i < j such that
the intervals i and j are disjoint. In other words, fi sj . We define p(j) = 0 if
no job i < j is disjoint from j.
Thus, OPT(0) = 0 and OPT(j) = max(value(j) + OPT(p(j), OPT(j - 1)).
This means that j is only part of the optimal solution if value(j)+OP T (p(j))
1

OP T (j 1).
Therefore, to compute the optimal value over n weeks, the following algorithm FindOptimalValue(n) is called:
Algorithm 1
1: procedure FindOptValue(j)
2:
if j = 0 then return 0
3:
if M[j] is not empty then return M[j]
4:
else
5:
M[j] = max(value(j) + FindOptValue(p(j)), FindOptValue(j - 1))
return M[j]
In this algorithm, M with all the indices initially empty and it stores the
value of FindOptValue(j) as soon as it is determined. Since this value is saved,
this runs in polynomial time. This is because single call to FindOptValue() runs
in O(1), so the running time is bounded by a constant times the number of calls
ever issued to FindOptValue(). Namely, an upper bound for this is the number
of entires in M that are not empty. Initially this is 0, but each recurrence
issues 2 more calls and fills a new entry in M. Since M has n + 1 entries, there
are at most O(n) calls.

Page 317, Exercise 6

Let w1 ,...,wn be a set of n words where each wi has ci characters. Let L be


the maximum line length. The following into valid lines, so that the sum of the
squares of the slacks of all the lines (including the last line) is minimized.
Let OPT(j) be the sum of the squares of the slacks over the first j words.
Clearly, the OPT(0) = 0.
The slack(k,j) = L - (k - j) -

Pj

i=k ci .

Therefore, Opt(j) = min1<=k<=j,slack(k,j)>=0 (slack(k, j)2 + Opt(k 1))


This algorithm is O(n2 ) since the first loop iterates n times for the number
of words, and the inner for loop iterates i n times.

Algorithm 2
1: procedure FindMinSlackPartition
2:
for 1 i n do
3:
W[i] =
4:
slack = L - ci
5:
j=i-1
6:
while slack > 0 do
7:
W[i] = min(W[i], W[j] + slack 2 )
8:
slack = slack - 1 - cj
9:
j=j-1
return W[n]

Page 323, Exercise 11

Let MIN(i) = the minimum cost to ship for the first i weeks. Let OPT(j) = the
company chosen on the j th week and achieving MIN(i).
The optimal schedule can be obtained by either choosing A for the ith week
or choosing B for weeks i - 3 to i. Therefore, for i 4:
MIN(i) = min(MIN(i - 1) + r * si , MIN(i - 4) + 4 * c))
However, if i < 4, B is not relevant, and so:
MIN(0) = 0
MIN(i - 1) + r * si
Let M = m1 ,...,mn store the MIN(i), and Let 0 = s1 ,...,sn where each si is
a schedule, an array from 1 to storing the company chosen at a given week.
The runtime of the algorithm is O(n2 ) since the at each iteration of the
recurrance another index of O is filled up. Since O is an n by n matrix since
and each access it O(1) there is a total of O(n2 ) operations.

Algorithm 3
1: procedure FindMinCostSchedule
2:
M[0] = 0
3:
for 1 min(n, 3) do
4:
M[i] = M[i - 1] + r * si
5:
for 1 k i 1 do
6:
O[i][k] = O[i - 1][k]
7:
O[i][k] = A
8:
for 4 i n do
9:
M[i] =min(M[i - 1] + r * si , M[i - 4] + 4 * c))
10:
if M [i 1] + r si M [i 4] + 4 c then
11:
for 1 k i 1 do
12:
O[i][k] = O[i - 1][k]
13:
O[i][i] = A
14:
else
15:
for 1 k i 4 do
16:
O[i][k] = O[i - 4][k]
17:
for 0 j 3 do
18:
O[i][i - j] = B
return O[n]

Page 331, Exercise 24

Let ai and bi be the number of voters for A and B respectively in the ith precinct,
an element of 1, 2,...,n. For each i, calculate the difference fi = ai - bi .
Now, to determine if gerrymandering is possible, we must be able to divide
the ps into two districts d1 and d2 , each of size n/2, such that sum of the ps
in each side is positive.
Before beginning the algorithm, initialize the following values:
Let Array M = [p, |d1 |, value(d1 ), value(d2 )] where p represents the count of
the precincts assigned thus far, |d1 | represents the count of the precincts that
have been assigned to district 1, and val(di ) represents the delta of votes away
from the majority in district i. Each M index holds a true or false value: true
if a majority is in both districts and false otherwise.
Let maxValue = sum of all positive fi and minValue = sum of all negative fi
maxValue and minValue are the highest and lowest possible value respectively that a given district can hold. Therefore, initialize M for all 0 p n,
0 |d1 | p, and minV alue val(di ) maxV alue.

Set M[p, |d1 |, val(d1 ), val(d2 )] = true for all p and |d1 | = 0, and val(d1 ), val(d2 )
0.
Algorithm 4 Returns true if gerrymandering is possible and false otherwise
1: procedure GerrymanderingIsPossible(M)
2:
for each precinct 1 p n do
3:
for each value minV alue val(d1 ) maxV alue do
4:
for minV alue value(d2 ) maxV alue do
5:
for 0 |d1 | i do
6:
bool addToD1 = |d1 | 1 and M[p-1, |d1 |-1, val(d1 )+fp ,
val(d2 )]
7:
bool addD2 = p 1 and M[p-1, |d1 |, val(d1 ), val(d2 )+fp ]
8:
M[p, |d1 |, val(d1 ), val(d2 )] = addD1 or addD2
return M[n, n/2, 0, 0]
The runtime of this algorithm is O(n2 * m2 ) since the the p loop iterates
n times and the |d1 | loop iterates <= i times, resulting in a O(n2 ). Moreover,
the loops with val(d1 ) and value(d2 ) iterate each up to a maximum of m times
since that is the greatest distance between the max and min possible values a
district can hold.
This algorithm works because the true/false value at M[n, n/2, 0, 0] reflects
having assigned n precincts such that n/2 are assigned to d1 and val(d1 ) and
val(d2 ) are positive, reflecting a majority in both districts.

You might also like