Professional Documents
Culture Documents
Introduction
• Greedy Method find out the best method/option out of many present ways
• Greedy algorithm is optimization technique, it tries to find an optimal solution from the set of
feasible solutions.
• Greedy algorithm derive solution step by step, by looking at information available at current
moment. It does not look at future prospects.
• Decisions are completely locally optimal.
- The choice made under greedy solution procedure are irrevocable, means once we have selected
solution the local best solution it cannot be backtracked.
Algorithm GREEDY_APPROACH(L, n)
//Description: Solve the given problem using greedy approach.
//Input: L: List of possble choices, n: size of solution
//Output: Set Solution containing solution of given problem
Solution ∅
for i 1 to n do
Choice Select(L)
if (feasible(Choice ∪ Solution)) then
Solution Choice ∪ Solution
end
end
return Solution
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 1
Analysis of Algorithm Greedy method Approach
Dijkstra's Algorithm
• It is a greedy algorithm that solves the single-source shortest path problem.
• Single-Source means that only one source is given, and we have to find the shortest path from the
source to all the nodes.
• Dijkstra's Algorithm maintains a set S of vertices whose final shortest - path weights from the
source s have already been determined.
Example:
Let's understand the working of Dijkstra's algorithm. Consider the below graph.
First, we have to consider any vertex as a source vertex. Suppose we consider vertex 0 as a source
vertex.
Here we assume that 0 as a source vertex, and distance to all the other vertices is infinity. Initially,
we do not know the distances. First, we will find out the vertices which are directly connected to the vertex
0. As we can observe in the above graph that two vertices are directly connected to vertex 0.
Let's assume that the vertex 0 is represented by 'x' and the vertex 1 is represented by 'y'. The distance
between the vertices can be calculated by using the below formula:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 2
Analysis of Algorithm Greedy method Approach
Therefore, we come to the conclusion that the formula for calculating the distance between the
vertices:
Therefore, the value of d(y) is 8. We replace the infinity value of vertices 1 and 4 with the values
4 and 8 respectively. Now, we have found the shortest path from the vertex 0 to 1 and 0 to 4. Therefore,
vertex 0 is selected. Now, we will compare all the vertices except the vertex 0. Since vertex 1 has the
lowest value, i.e., 4; therefore, vertex 1 is selected.
Since vertex 1 is selected, so we consider the path from 1 to 2, and 1 to 4. We will not consider the
path from 1 to 0 as the vertex 0 is already selected.
First, we calculate the distance between the vertex 1 and 2. Consider the vertex 1 as 'x', and the
vertex 2 as 'y'.
Since 15 is not less than 8, we will not update the value d(4) from 8 to 12.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 3
Analysis of Algorithm Greedy method Approach
Till now, two nodes have been selected, i.e., 0 and 1. Now we have to compare the nodes except
the node 0 and 1. The node 4 has the minimum distance, i.e., 8. Therefore, vertex 4 is selected.
Since vertex 4 is selected, so we will consider all the direct paths from the vertex 4. The direct paths
from vertex 4 are 4 to 0, 4 to 1, 4 to 8, and 4 to 5. Since the vertices 0 and 1 have already been selected so
we will not consider the vertices 0 and 1. We will consider only two vertices, i.e., 8 and 5.
First, we consider the vertex 8. First, we calculate the distance between the vertex 4 and 8. Consider
the vertex 4 as 'x', and the vertex 8 as 'y'.
Since 15 is less than the infinity so we update d(8) from infinity to 15.
Now, we consider the vertex 5. First, we calculate the distance between the vertex 4 and 5. Consider
the vertex 4 as 'x', and the vertex 5 as 'y'.
Till now, three nodes have been selected, i.e., 0, 1, and 4. Now we have to compare the nodes
except the nodes 0, 1 and 4. The node 5 has the minimum value, i.e., 9. Therefore, vertex 5 is selected.
Since the vertex 5 is selected, so we will consider all the direct paths from vertex 5. The direct paths
from vertex 5 are 5 to 8, and 5 to 6.
First, we consider the vertex 8. First, we calculate the distance between the vertex 5 and 8. Consider
the vertex 5 as 'x', and the vertex 8 as 'y'.
Since 24 is not less than 15 so we will not update the value d(8) from 15 to 24.
Now, we consider the vertex 6. First, we calculate the distance between the vertex 5 and 6. Consider
the vertex 5 as 'x', and the vertex 6 as 'y'.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 4
Analysis of Algorithm Greedy method Approach
Till now, nodes 0, 1, 4 and 5 have been selected. We will compare the nodes except the selected
nodes. The node 6 has the lowest value as compared to other nodes. Therefore, vertex 6 is selected.
Since vertex 6 is selected, we consider all the direct paths from vertex 6. The direct paths from
vertex 6 are 6 to 2, 6 to 3, and 6 to 7.
First, we consider the vertex 2. Consider the vertex 6 as 'x', and the vertex 2 as 'y'.
Since 15 is not less than 12, we will not update d(2) from 12 to 15
Now we consider the vertex 3. Consider the vertex 6 as 'x', and the vertex 3 as 'y'.
Now we consider the vertex 7. Consider the vertex 6 as 'x', and the vertex 7 as 'y'.
Till now, nodes 0, 1, 4, 5, and 6 have been selected. Now we have to compare all the unvisited
nodes, i.e., 2, 3, 7, and 8. Since node 2 has the minimum value, i.e., 12 among all the other unvisited nodes.
Therefore, node 2 is selected.
Since node 2 is selected, so we consider all the direct paths from node 2. The direct paths from
node 2 are 2 to 8, 2 to 6, and 2 to 3.
First, we consider the vertex 8. Consider the vertex 2 as 'x' and 8 as 'y'.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 5
Analysis of Algorithm Greedy method Approach
Now, we consider the vertex 6. Consider the vertex 2 as 'x' and 6 as 'y'.
Since 16 is not less than 11 so we will not update d(6) from 11 to 16.
Now, we consider the vertex 3. Consider the vertex 2 as 'x' and 3 as 'y'.
Till now, nodes 0, 1, 2, 4, 5, and 6 have been selected. We compare all the unvisited nodes, i.e., 3,
7, and 8. Among nodes 3, 7, and 8, node 8 has the minimum value. The nodes which are directly connected
to node 8 are 2, 4, and 5. Since all the directly connected nodes are selected so we will not consider any
node for the updation.
The unvisited nodes are 3 and 7. Among the nodes 3 and 7, node 3 has the minimum value, i.e., 19.
Therefore, the node 3 is selected. The nodes which are directly connected to the node 3 are 2, 6, and 7.
Since the nodes 2 and 6 have been selected so we will consider these two nodes.
Now, we consider the vertex 7. Consider the vertex 3 as 'x' and 7 as 'y'.
Since 28 is not less than 21, so we will not update d(7) from 28 to 21.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 6
Analysis of Algorithm Greedy method Approach
Vertex T Y X Z
S 10 5 00 00
Vertex T Y X Z
S-> y 8 - 14 7
Vertex T Y X Z
S-> y->t - - 9 7
Vertex T Y X Z
S-> y->t->z - - 9 -
- Weight from s to y is 5
- Weight from s to z is 7
- Weight from s to t is 8
- Weight from s to x is 9
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 7
Analysis of Algorithm Greedy method Approach
Knapsack Problem
• The Greedy algorithm could be understood very well with a well-known problem referred to as
Knapsack problem.
• Let M is the capacity of knapsack i.e. knapsack can not hold item having collective weight more
than M.
• Knapsack problem is problem of filling knapsack using item in x such that it maximizes the profit
such that total weight item should not cross the knapsack capacity.
• 0/1 knapsack
• Fractional Knapsack
Example:
Solve following using 0/1 knapsack method.
Solution:
If items are selected according to decreasing order of profit to weight ratio using fractional
knapsack strategy, then algorithm always finds the optimal solution. First arrange items in decreasing order
of profit density. Items are labeled as X = (3, 1, 2, 4, 5), have profit V = (27, 18, 25, 10, 15) an weight W
= (4, 3, 5, 3, 6).
We shall select one by one item from above table. If the inclusion of an item does not cross the
knapsack capacity, then add it. Otherwise, break the current item and select only the portion of item
equivalent to remaining knapsack capacity. Select the profit accordingly. We should stop when knapsack
is full or all items are scanned.
Initialize, Weight of selected items, SW = 0, Profit of selected items, SP=0,
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 8
Analysis of Algorithm Greedy method Approach
- Knapsack is full, no more items can be added to knapsack. Selected items are (3, 1, 2), which gives
the profit of 70.
Job Sequencing
- Schedule jobs out of set of N jobs on a single processor which maximizes profit as much as possible.
- Each job is taking unit time for execution and having some profit and deadline associated with it.
- Greedy approach produces an optimal result in fairly less time as each job takes same amount of time
- No Preemption :- if we start one job we should complete it without assigning any other
Job J1 J2 J3 J4
Profit 50 15 10 25
Deadline 2 1 2 1
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 9
Analysis of Algorithm Greedy method Approach
SP← 0 //Sum is the profit earned
end
end
Example:
Let n = 4, (P1, P2, P3, P4)= (100, 10, 15, 27) and (d1, d2, d3, d4) = (2, 1, 2, 1). Find feasible solutions,
using job sequencing with deadlines.
Soln.:
Sort all jobs in descending order of profit.
So, P=(100, 27, 15, 10), J = (J1, J4, J3, J2) and D = (2, 1, 2, 1). We shall select one by one job from
the list of sorted jobs, and check if it satisfies the deadline. If so, schedule the job in the latest free slot. If
no such slot is found, skip the current job and process the next one.
Initially,
S
0 1 2 3 4
Profit of scheduled jobs, SP = 0
Iteration 1
Deadline for job J1 is 2. Slot 2(t = 1 to t = 2) is free, so schedule it in slot 2.
Solution set S = {J1}, and Profit SP = {100}
S J1
0 1 2 3 4
Iteration 2
Deadline for job J4 is 1. Slot 1(t = 0 to t = 1) is free, so schedule it in slot 1.
Solution set S = {J1, J4}, and Profit SP = {100, 27}
S J4 J1
0 1 2 3 4
Iteration 3
Job J3 is not feasible because first two slots are already occupied and if we schedule J3 any time
later t = 2, it cannot be finished before its deadline 2. So job J3 is discarded.
Solution set S = (J1, J4), and Profit SP = {100, 27}
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 10
Analysis of Algorithm Greedy method Approach
Iteration 4
Job J2 is not feasible because first two slots are already occupied and if we schedule J2 any time
later t = 2, it cannot be finished before its deadline 1. So job J2 is discarded,
Solution set S = {J1, J4), and Profit SP= (100, 27) With the greedy approach, we will be able to
schedule two jobs (J1, J4), which gives a profit of 100 + 27 = 127 units..
Prims algorithm
Refer class notes
Kruskal algorithm
Refer class notes
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 11