0% found this document useful (0 votes)
2 views

Algorithm HW 4

The document presents an algorithms assignment that discusses various greedy choice problems, including activity selection, maximizing profit through sorted arrays, minimizing delays based on deadlines, and coin denomination challenges. Each problem is analyzed with examples and proofs to demonstrate the effectiveness or limitations of greedy algorithms. The assignment also includes runtime analysis for the proposed solutions.

Uploaded by

alexholden645
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm HW 4

The document presents an algorithms assignment that discusses various greedy choice problems, including activity selection, maximizing profit through sorted arrays, minimizing delays based on deadlines, and coin denomination challenges. Each problem is analyzed with examples and proofs to demonstrate the effectiveness or limitations of greedy algorithms. The assignment also includes runtime analysis for the proposed solutions.

Uploaded by

alexholden645
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithms Assignment 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.

b) Consider the following three activities:


a1 = (7, 10)
a2 = (1, 8)
a3 = (9, 12)

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.

c) Consider the following eight activities:


a1 = (1, 3)
a2 = (4, 6)
a3 = (7, 10)
a4 = (1, 3)
a5 = (4, 6)
a6 = (1, 6)
a7 = (1, 3)
a8 = (4, 6)
Following the greedy choice, the solution would be a3 because it has zero overlaps. Afterward it add
a6 because it only has two overlaps. The algorithm stops there because the following activities can not
fit in the current solution. However, this solution is incorrect since the optimal solution is (a1 , a2 , a3 ).

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.

You might also like