0% found this document useful (0 votes)
148 views13 pages

Greedy Sheet

Here is a greedy algorithm to solve this problem: 1. Sort the fruits in descending order of price to weight ratio (prices[i]/weights[i]) 2. Initialize total weight = 0 3. Iterate through fruits: - Add the current fruit to the basket if total weight + weight of current fruit <= W - Update total weight 4. Return total price of fruits in basket This runs in O(NlogN) time for sorting and O(N) time for iteration. The greedy choice is to pick the fruit with the highest price to weight ratio at each step. This ensures we maximize the total price for the given weight constraint. Picking fruits in this order provides the optimal solution

Uploaded by

youssef amr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views13 pages

Greedy Sheet

Here is a greedy algorithm to solve this problem: 1. Sort the fruits in descending order of price to weight ratio (prices[i]/weights[i]) 2. Initialize total weight = 0 3. Iterate through fruits: - Add the current fruit to the basket if total weight + weight of current fruit <= W - Update total weight 4. Return total price of fruits in basket This runs in O(NlogN) time for sorting and O(N) time for iteration. The greedy choice is to pick the fruit with the highest price to weight ratio at each step. This ensures we maximize the total price for the given weight constraint. Picking fruits in this order provides the optimal solution

Uploaded by

youssef amr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ALG: Greedy Problems

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
𝒏 𝒊=𝟏 𝒊

minimize completion time of each process ci.


• Since 𝑐𝑖 = 𝑐𝑖−1 + 𝑝𝑖 , where, 𝑐𝑖−1 is completion time of previous process (i – 1)
and 𝑝𝑖 is processing time of current process i.
To minimize 𝑐𝑖 , we need to minimize both 𝑐𝑖−1 and 𝑝𝑖 .
But 𝑐𝑖−1 = 𝑐𝑖−2 + 𝑝𝑖−1 , to minimize it, we need to minimize both 𝑐𝑖−2 and 𝑝𝑖−1 .
And 𝑐𝑖−2 = 𝑐𝑖−3 + 𝑝𝑖−2 , to minimize it, we need to minimize both 𝑐𝑖−3 and 𝑝𝑖−2 .

And so on. Ending with 𝑐1 = 𝑐0 + 𝑝1 = 0 + 𝑝1
• This means to minimize 𝑐1 , choose, among all processes (n), the process with
minimum processing time (𝑝1 ).
Then to minimize 𝑐2 , choose, among remaining processes (n-1), the process with
minimum processing time (𝑝2 ).
And so on.

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

Deduce the complexity


1. Sort processes according to their processingTime in ascending order
➔ Θ(N log(N))
2. Keep executing the processes in their order until finishing them
➔ Θ(N)

Total = ➔ Θ(N log(N))

Problem2: Max Sequence Sum


Given the array of real numbers x[n], compute the maximum sum found in any contiguous
sub-array.
Example: If the input array is
31 -41 59 26 -53 58 97 -93 -23 84

then the program returns the sum of x[2..6] = 187


Give an efficient algorithm to solve this problem? State the running time of your algorithm?

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.

Write the pseudo-code

maxSoFar = 0
curSum = 0
for i = 1 to n
{
curSum += x[i]
if curSum < 0 then
curSum = 0

maxSoFar = max(maxSoFar, curSum)


}

Deduce the complexity


 Θ(N)

Extra Requirement
• Modify the above code to return the start and end positions of the found sequence?

Problem3: Highest Increase Pair


Suppose that we need to find in a given a sequence of numbers x1,…, xn the highest
increase, i.e., some pair with i < j, xi < xj and maximum difference xj - xi. A naïve algorithm
can do that in O(n2) time. Give a more clever algorithm that needs only O(n) time. Clearly
explain the time bound.

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.

1. Let xi = 1st item in the array


2. Check it with the next item
3. If xi < next item, then calculate their difference and update the MaxSoFar value
4. Else, set xi to the next item
Sub-problem:
• Look for max difference in remaining items (each time: the number of items is reduced
by 1)

Write the pseudo-code

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

Deduce the complexity


 Θ(N)

Problem4: Burn Image DVDs


Suppose there are a set of N bitmap images, all of the same size S. Each image has one of
possible R ranks (1: min rank, R: max rank). There are K blank DVDs, each with a capacity C.
It’s desired to burn each DVD with the most valuable images (i.e. with max total rank).
Following is an example:
N = 9 Images, S = 2 GB, K = 2 DVDs, C = 4.5 GB
Input Image 1 2 3 4 5 6 7 8 9
rank r[i] 3 2 5 1 2 2 2 4 3
DVD#1: total rank = 9, Images are: 3, 8
Output
DVD#2: total rank = 6, Images are: 1, 9
Given:
• N images, each of size S and its own rank r[i],
• K DVDs each with capacity C
Required:
• Calculate max total rank for each of the K DVDs, in efficient way?
1. What’s the Greedy choice for this problem?
2. Write the pseudo-code of an efficient solution to solve?
3. What’s the complexity of this efficient solution?
SOLUTION:
For each DVD, select the image with max rank

1. Sort the images according to their rank (descending)


2. Total[1…K] = 0
3. x=1
4. NumPerDVD = floor(C / S)
5. For d = 1 to K
6. For i = 1 to NumPerDVD
a. Total[d] += r[x]
b. x++
7. Return Total

Θ(N Log(N))

Problem5: Dividing Coins

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)

Problem6: Preparing a Present Basket


A loving daughter decided to prepare a precious gift for her parents. She bought one basket
and decided to fill it with the most expensive fruits from the supermarket. Basket can be
filled with a maximum weight W.
In the supermarket; there're 'N' fruits. For each one, the supermarket displays the full
remaining weights 'weights[i]' with their total prices 'prices[i]'. It’s required to make the
daughter fill her basket with the most expensive collection of fruits. Following are two
examples:
CASE#1 (Solved) CASE#2 (Required?)
W = 40 W=7
Item Weight Price Item Weight Price
1 30 10$ 1 1 10$
2 10 50$ 2 25 50$
3 20 20$ 3 4 20$
4 40 80$ 4 50 15$
5 25 200$ 5 40 80$
6 50 150$ 6 25 100$
Max Profit = 265$ (from items 2,5,6) Max Profit = ??
1. Based on the problem specifications, find the solution of the second case (BOTH profit &
item(s))?
2. Write down the pseudo-code of an efficient Greedy solution for this problem?
3. What’s the time complexity of this efficient solution?

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

Note that work can never be reduced to less than 0.

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)

Frequency of each char:


Cha A T S R - D U C E N L G O I H M
r
TOTAL
Fre 4 4 3 3 3 2 2 1 1 1 1 1 1 1 1 1
q
a) 3 3 4 3 3 4 4 5 5 4 5 5 5 5 5 5 40+32+42=114
b) 3 3 3 3 3 4 4 6 6 5 5 5 5 5 5 5 12+35+16+51=
114
c) 3 3 4 3 4 4 4 4 4 4 5 5 5 5 5 5 30+52+33=115
d) 3 3 4 3 3 5 4 5 5 5 5 5 5 5 4 4 45+28+42=115

Correct choice: (e)


QUESTION2: Knapsack Variation
If you apply the 0-1 Knapsack algorithm to a set of equally weighted items but with
different costs. What’s the suitable design paradigm that can be used to solve this
problem? What’s the complexity of such solution?

(N: number of items, W: Knapsack weight)

SOLUTION:
Greedy & O(NLog(N)

You might also like