Greedy Sheet
Greedy Sheet
TABLE OF CONTENTS
TABLE OF CONTENTS .....................................................................................................1
FIRST: Solved Problems .................................................................................................2
Problem1: Task Scheduling .........................................................................................2
Problem2: Max Sequence Sum ...................................................................................3
Problem3: Highest Increase Pair .................................................................................4
Problem4: Burn Image DVDs ......................................................................................5
Problem5: Dividing Coins ...........................................................................................6
Problem6: Preparing a Present Basket .........................................................................7
SECOND: Unsolved Problems .........................................................................................9
Problem1: Office Organization ....................................................................................9
Problem2: Bread Transportation ............................................................................... 10
Problem3: Candy & Sugar Rush ................................................................................. 11
THIRD: Other Questions .............................................................................................. 12
QUESTION1: Huffman Code ...................................................................................... 12
QUESTION2: Knapsack Variation ............................................................................... 13
FIRST: Solved Problems
Problem1: Task Scheduling
Suppose you are given a set S = {a1, a2, ..., an} of tasks, where task ai requires pi units of
processing time to complete, once it has started. You have one computer on which to run
these tasks, and the computer can run only one task at a time. Each task must run non-
preemptively, that is, once task ai is started, it must run continuously for pi units of time.
Let ci be the completion time of task ai, that is, the time at which task ai completes
processing. Your goal is to minimize the average completion time, that is, to minimize
1 𝑛
∑ 𝑐 .
𝑛 𝑖=1 𝑖
For example, suppose there are two tasks, a1 and a2, with p1 = 3 and p2 = 5, and consider the
schedule in which a2 runs first, followed by a1. Then c2 = 5, c1 = 8, and the average
completion time is (5 + 8)/2 = 6.5.
Give an algorithm that schedules the tasks so as to minimize the average completion time.
State the running time of your algorithm?
SOLUTION:
Greedy-choice:
• Chose the process with minimum processing time and schedule it first.
𝟏 𝒏
• Since we want to minimize average completion time, ∑ 𝒄. Thus, we need to
𝒏 𝒊=𝟏 𝒊
Sub-problem:
Schedule the remaining (n – 1) processes in optimal way (i.e. repeat step (i) again)
pseudo-code
1. Sort processes according to their processingTime in ascending order
(Merge/Quick)
2. Keep executing the processes in their order until finishing them
curTime = 0
totalCompletionTime = 0
for j = 1 to N {
curTime = curTime + Processes[j].processingTime
Processes[j].completionTime = curTime
totalCompletionTime += curTime
}
Return totalCompletionTime / N
SOLUTION:
Greedy-choice:
• Start summing the elements from the beginning of the array
• If current sum is +ve, then continue (as there's possibility to increase this sum by adding
further elements)
Else if current sum is -ve, then reset the sum to 0 and restart from next element (as
starting from 0 is better (i.e. give us greater sum) than starting from –ve number)
Sub-problem:
• Continue the process of calculating current sum on the remaining elements.
• Keep track of the maxSoFar sum during your process.
maxSoFar = 0
curSum = 0
for i = 1 to n
{
curSum += x[i]
if curSum < 0 then
curSum = 0
Extra Requirement
• Modify the above code to return the start and end positions of the found sequence?
SOLUTION:
Greedy-choice:
Always select the min of the two values as the first value (xi), and then look for 2nd value (xj)
that leads to the max difference.
MaxSoFar = 0
i = 0
For (j = 1 to N)
If (A[i] > A[j]) Then
i = j
Else
Θ(N)
Diff A[j] – A[i]
If (Diff > MaxSoFar) Then
MaxSoFar = Diff
1stItem = i
2ndItem = j
End If
End For
Θ(N Log(N))
SOLUTION:
Greedy Solution
– Calculate H = Sum / 2
– Cast the problem: (make a choice ➔ one sub-problem to solve)
– Choice: select max coin value V[m] from the N coins
– Sub-problem: Find min num of coins from remaining N – 1 that are strictly
greater than H – V[m]
– Prove greedy choice is always SAFE:
– Since we need to exceed half the array sum with min num of coins
– Selecting max coin value is the shortest (i.e. min) way to reach this goal
– Ensure optimal substructure:
– Final optimal = optimal solution to sub-problem + greedy choice
Complexity
– Calculate half the sum ➔ Θ(N)
– Sort coins (descending) ➔ Θ(N log(N))
– Select min number of coins ➔ O(N)
SOLUTION:
1. 10 + 20 + 2x4 = 38 from items 1, 3, 6
2.
1) For each item: Calculate CostPerUnit[i] = Price[i] / Weight[i] [1 mark]
2) Sort all items using CostPerUnit [Descending order by MergeSort] [1 mark]
3) TotalBenefit = 0, i = 0
4) While (i < N AND W > 0) [1 mark]
If (Weight[i] ≤ W) [2 marks]
W = W - Weight[i]
TotalBenefit = TotalBenefit + Price[i]
i=i+1
else [2 marks]
RemainW = W
W=0
TotalBenefit = TotalBenefit + CostPerUnit[i] × RemainW
End if
End While
Return TotalBenefit
3. O(N log(N))
SECOND: Unsolved Problems
Problem1: Office Organization
Your office becomes full of documents. You currently have N units of documents on your
office, and your father demands that you have exactly M units of documents left by the end
of the day. The only hope for you now is to ask help from your brother and sister.
▪ Your sister offers that she can reduce your documents by half for $A (rounding
down when necessary – e.g. 25 ➔ 12).
▪ Your brother offers that he can reduce your entire documents by one unit for $B
Given N, M, A and B, your task is to find the minimum costs in MOST EFFICIENT WAY to
organize your office to meet your father needs.
Complexity
The complexity of your algorithm should be less than O(N)
Examples
1. N = 100, M = 5 A = 10 B = 1 Output = 37
2. N = 100, M = 5 A = 5, B = 2 Output = 22
Problem2: Bread Transportation
Problem3: Candy & Sugar Rush
THIRD: Other Questions
QUESTION1: Huffman Code
Consider the string “DATA-STRUCTURES-AND-ALGORITHMS”: which of the following
trees is/are considered optimal prefix-free code for this input string?
a) b)
c) d)
e) a) and b) f) a) and c)
SOLUTION:
Greedy & O(NLog(N)