Algorithm HW 4
Algorithm HW 4
Alexander Holden
November 21, 2024
1 Problem 1
a) Let aj be the activity with the earliest starting time and fj = fn
In this case, the greedy choice would pick aj , but since the finishing time is the same as the nth, or
final, activity, it would return a maximum value of 1.
The greedy choice would first select a2 , because it has the least duration, however, since a2 over-
laps with the other two events, the other two would then be ignored.
2 Problem 2
Qn
Greedy Choice: Sort arrays a and b in increasing order. The max profit will be i=0 abi i
Proof: QLet S be the maximum profit.
n
If S = i=0 abi i where ai , bi is the i-th largest element in a,b, then the greedy choice is proven.
Qn Qn b Qn
If S ̸= i=0 abi i , this would imply j=0 ajj > i=0 abi i where aj , bj < ai , bi . However, this is a con-
tradiction because the exponent will cause the greatest increase in profit when paired with the largest
possible base.
maxProfit(a,b, n)
a ← QuickSort(a)
b ← Quicksort(b)
product ← 1
for i ← 1 to n do
product ← product * a[i]b[i]
return product
Runtime:
1
T(n) = 2O(nlogn) + Θ(n) which simplfies to
T(n) = O(nlogn)
3 Problem 3
Greedy Choice: Always pick the activity with the soonest deadline, therefore always picking the min-
imal ∆i each choice.
Greedy Choice Proof: Let S be an optimal set of activities and aj is the activity with the closest
deadline.
If aj ∈ S, the greedy choice is proven.
If aj ∈
/ S, let af be the activity in S with the closest deadline. Since aj is the closest deadline of all
activities, dj ≤ df . Constructing a new solution S’ using aj ,
S ′ = (S − {af }) ∪ {aj }
S’ is a feasible solution because since dj ≤ df , ∆i of aj < ∆i of af and |S| = |S ′ | because all that is
done is replacing one activity with another.
minDelay(t,d,n)
activities ← array of tuples (ti , di )
activities ← QuickSort(activities) //sorted in increasing order by deadline
maxDelay ← 0
time ← 0
order ← empty array of length n
for i ← 1 to n do
startTime ← time
endTime ← time + activities[i][0]
order[i] ← (startTime, endTime)
delay ← endTime - activities[i][1]
if delay > maxDelay
maxDelay ← delay
return order, maxDelay
4 Problem 4
a) The greedy choice can be disproven by a counter example.
Consider the following coin denominations c = (1,2,3,7,8,10) and n = 15.
According to the greedy choice, the first coin chosen would be 10, and we would solve for n = 5. In
total, this would return three (10,3,2), however, the more optimal choice would be choosing (7,8) which
is only two coins.
b) This greedy choice will work if the coin denominations have powers of 2 because any target value n
has a unique binary representation of the form
n = bk ∗ 2k−1 + bk−1 ∗ 2k−2 + ... + b1 ∗ 20 where bi ∈ 0, 1.
Suppose now there is a solution S with fewer coins than the greedy solution. This would imply that n
has another binary representation, which is impossible because the binary representation of a number
n is unique, therefore forming a contradiction.