04. Greedy algorithm
04. Greedy algorithm
04. Greedy algorithm
Greedy Algorithms
A greedy algorithm always makes the choice that looks best at this moment.
We hope that a locally optimal choice will lead to a globally optimal solution.
A Simple Example
Pick k numbers out of n numbers such that the sum of these k numbers is the largest.
Algorithm
FOR i = 1 to k
1. for i=1 to k do
2. pick out the largest number
3. delete this number from the input
4. end for
Optimization Problems
An optimization problem is the problem of finding the best solution from all feasible solutions
Practice problems:
PROBLEM 01. Fractional knapsack
The weights and values of n items are given. The items are such that you can take a whole item or some
fraction of it (divisible). You have a knapsack to carry those items, whose weight capacity is W. Due to
the capacity limit of the knapsack, it might not be possible to carry all the items at once. In that case,
pick items such that the profit (total values of the taken items) is maximized.
Write a program that takes the weights and values of n items, and the capacity W of the knapsack from
the user and then finds the items which would maximize the profit using a greedy algorithm.
● Pick the lightest item first, then pick the next lightest item and so on.
● Pick the costliest (per-unit value wise) item first, then pick the next costliest item and so on.
(optimal answer)
● Pick the costliest (total value wise) item first, then pick the next costliest item and so on.
Greedy Algorithm |Algo Lab | Fariha Tabassum Islam
There are n boxes of n different items in a warehouse. Each box has a label that says the name (m_i),
total weight (w_i) in kg and the total value (v_i) in taka of that item (i). All items are divisible. Suppose, k
thieves have come to steal from the warehouse, each with a knapsack of capacity W_i. Given each thief
wants to maximize his/her profit, how many thieves will be needed to empty the warehouse? Write a
code to solve this problem using a greedy algorithm.
You are attending your mid-term of the course “CS101”. The total marks of the exam is M and the total
time is T minutes. You have to answer N questions, where the i-th question carries m_i marks and takes
t_i minutes for you to answer. The marks you receive will be proportional to the percentage of your
answer compared to the full answer, e.g., if a question contains 100 marks and you complete 30% of it,
you will get 30 marks.
● Select the activity that starts first, next select the activity that starts first and does not conflict
with the already picked activities
● Select the activity that ends first (this one gives the optimal answer)
● Select the activity that has the shortest duration first
Consider the problem of making change for N cents using the fewest number of coins. Assume that each
coin’s value is an integer and there are an infinite number of coins for each coin type. Write a greedy
algorithm to make change consisting of coins .
Write a code to solve this problem using a greedy algorithm. Keep the time complexity of your code
O(n).
Huffman Decoding