Module 2 Greedy Method
Module 2 Greedy Method
Method
General Method
A greedy algorithm, as the name suggests, always makes the
choice that seems to be the best at that moment.
Start Then start picking up the item with the highest ratio until that item is completely taken up. After
that we will move to the next highest.
Algorithm
1. Calculate the ratio for all the n objects.
2. Sort the n objects from large to small based on the ratios
3. Initialize share array
4. Set weight = and i=1
5. While (i ≤ n and weight < )
a. If weight + ≤ then
b. else
c. weight = weight +
d. i++
6. Display share array.
Example
Question
Product A B C D
Profit 280 100 120 120
Weight 40 10 20 24
Capacity = m = 60
Solution
Product A B C D
Profit 280 100 120 120
Weight 40 10 20 24
Ratio 7 10 6 5
Example
Product B A C D
Profit 100 280 120 120
Weight 10 40 20 24
Ratio 10 7 6 5
Remaining Capacity 60-10=50 50-40=10 10-10=0 -
Share 1 1 -
Max Profit 0+100=100 100+280=380 380+120*0.5=440 440+0=440
Status Selected Selected Selected Rejected
Product A B C D
Profit 280 100 120 120
Weight 40 10 20 24
𝑥 [ 1 … 4 ] =[ 1 ,1 , 1 ,0 ] 𝑎𝑛𝑑 𝑃𝑟𝑜𝑓𝑖𝑡=280
Weight 10 20 30 30
Profit 60 100 120 90
Capacity = m = 60
Product A B C D
Weight 10 20 30 30
Profit 60 100 120 90
Ratio 6 5 4 3
Remaining Capacity 60-10=50 50-20=30 30-30=0 -
Share 1 1 -
Max Profit 0+60=60 60+100=160 160+120=280 280+0=280
Status Selected Selected Selected Rejected
Time Complexity
Time Complexity =
Time complexity of the sorting + Time complexity of the loop to maximize profit
Example - Question
A thief enters a house for robbing it. He can carry a maximal weight of
60 kg into his bag. There are 5 items in the house with the following
weights and values. What items should thief take if he can even take the
fraction of any item with him?
Product A B C D E
Weight 5 10 15 22 25
Profit 30 40 45 77 90
Capacity = m = 60
Example - Solution
Product A B E D C
Weight 5 10 25 22 15
Profit 30 40 90 77 45
Ratio 6 4 3.6 3.5 3
Remaining Capacity 60-5=55 55-10=45 45-25=20 20-20=0 -
Share 1 1 1 -
160+77*0.9090
Max Profit 0+30=30 30+40=70 70+90=160 230+0=230
=230
Status Selected Selected Selected Selected Rejected
Cost 72 97 60 75 200
Share 1 1 1 -
229+75*0.667=
Min Cost 0+72=72 72+97=169 169+60=229 279.025
279.025
Status Selected Selected Selected Selected Rejected
Question: 1
2
40
15
2
1
3 10 3
4 100 2
Question: 1
2
70
55
2
1
3 80 2
3
Start b d
2 3
2
a f
3
c e
Min-cost=13
Exercise
7
Start 1 2 3
4 9
2
4
0 8 4
8
7 1
6 2
5
Min-cost=37
Kruskal’s Algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If cycle is not formed, include this edge. Else, discard
it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Example
3
b d
2 3
2
a e
3
c e
Min-cost=13
Exercise
7
1 2 3
4 9
2
4
0 8 4
8
7 1
6 2
5
Min-cost=37
Greedy Method
Single Source Shortest Path
• Problem Statement:
Let G is the graph, and we want to find out the shortest path from source
vertex “s” to all other vertices. It can be directed or undirected graph.
Pseudocode
function dijekstras (source,cost,dist,n)
{
// Source is source vertex, cost is cost matrix of given graph G, dist is array to store shortest path from source to vertex, n is number of vertices in graph
for i=1 to n do
S[i] = false //to check if vertex i is visited or not
dist [i] = cost [ v, i ] //initial distance between source vertex v and vertex I from cost matrix
for num =2 to n do
choose a vertex u such that it has minimum path among all vertices which is not visited
S[u]= true
for w = 1 to n do
if dist [w] > dist [u] + cost [u] [w] then if dist [w] = dist [u] + cost [u] [w]
}
Example
1500 5 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
800 1200
2 3 4 250
300 ∞ ∞ ∞ ∞ ∞ ∞ ∞
1000 800 ∞ ∞ ∞ ∞ ∞ ∞
1000
300 1000 6 ∞ ∞ 1200 ∞ ∞ ∞ ∞ ∞
1400
900 ∞ ∞ ∞ 1500 250 ∞ ∞ ∞
1 1700 8 1000
7 ∞ ∞ ∞ ∞ ∞ ∞ 900 1400
∞ ∞ ∞ ∞ ∞ ∞ ∞ 1000
1700 ∞ ∞ ∞ ∞ ∞ ∞ ∞
Solution
Vertex
Iteration S 1 2 3 4 5 6 7 8
Selected
Initial {} ∞ ∞ ∞ 1500 0 250 ∞ ∞ 5
6 0 9 ∞ ∞ ∞ ∞ 18 ∞
∞ 9 0 11 ∞ 1 ∞ ∞ 2
∞ ∞ 11 0 14 10 ∞ ∞ ∞
∞ ∞ ∞ 14 0 12 ∞ ∞ ∞
∞ ∞ 1 10 12 0 9 ∞ ∞
∞ ∞ ∞ ∞ ∞ 9 0 4 7
8 18 ∞ ∞ ∞ ∞ 4 0 5
∞ ∞ 2 ∞ ∞ ∞ 7 5 0
Distance
Iteration S Vertex
Selected
1 2 3 4 5 6 7 8 9
Initial {} - ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Solutio
1 {1} 2 0 6 ∞ ∞ ∞ ∞ ∞ 8 ∞
2 {1,2} 8 0 6 15 ∞ ∞ ∞ ∞ 8 ∞
n
3 {1,2,8} 7 0 6 15 ∞ ∞ ∞ 12 8 13
4 {1,2,8,7} 9 0 6 15 ∞ ∞ 2 12 8 13
1
5 {1,2,8,7,9} 3 0 6 15 ∞ ∞ 2 12 8 13
1
6 {1,2,8,7,9,3} 6 0 6 15 26 ∞ 1 12 8 13
6
7 {1,2,8,7,9,3,6} 4 0 6 15 26 28 1 12 8 13
6
8 {1,2,8,7,9,3,6,4} 5 0 6 15 26 28 1 12 8 13
6
9 {1,2,8,7,9,3,6,4,5} - 0 6 15 26 28 1 12 8 13
Example
Today, we will learn a very common problem which can be solved using the greedy algorithm. If you
are not very familiar with a greedy algorithm, here is the gist: At every step of the algorithm, you take
the best available option and hope that everything turns optimal at the end which usually does. The
problem at hand is coin change problem, which goes like given coins of denominations 1,5,10,25,100;
find out a way to give a customer an amount with the fewest number of coins. For example, if I ask
you
Cointo returnproblem
change me change for 30.
: Algorithm
1. Sort n denomination coins in increasing order of
value.
2. Initialize set of coins as empty. S = {}
3. While amount is not zero:
3.1 Ck is largest coin such that amount > Ck
3.1.1 If there is no such coin return “no viable
solution”
3.1.2 Else include the coin in the solution S.
3.1.3 Decrease the remaining amount = amount –
Ck