You are on page 1of 11

Analysis of Algorithm Greedy method Approach

Chapter-3. Greedy Method Approach

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.

➢ Steps for achieving a Greedy Algorithm are


• Feasible:
Choice should satisfy problem constraint.
• Local Optimal Choice:
Best Solution from all feasible solution at current stage should be selected.
• Unalterable:

- Once the choice is made it cannot be altered at any subsequence stages.

- 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

1. d(x, y) = d(x) + c(x, y) < d(y)


2. = (0 + 4) < ∞
3. = 4 < ∞

Since 4<∞ so we will update d(v) from ∞ to 4.

Therefore, we come to the conclusion that the formula for calculating the distance between the
vertices:

1. {if( d(u) + c(u, v) < d(v))


2. d(v) = d(u) +c(u, v) }

Now we consider vertex 0 same as 'x' and vertex 4 as 'y'.

1. d(x, y) = d(x) + c(x, y) < d(y)


2. = (0 + 8) < ∞
3. = 8 < ∞

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'.

d(x, y) = d(x) + c(x, y) < d(y)


= (4 + 8) < ∞
= 12 < ∞
Since 12<∞ so we will update d(2) from ∞ to 12.
Now, we calculate the distance between the vertex 1 and vertex 4. Consider the vertex 1 as 'x' and
the vertex 4 as 'y'.

1. d(x, y) = d(x) + c(x, y) < d(y)


2. = (4 + 11) < 8
3. = 15 < 8

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'.

d(x, y) = d(x) + c(x, y) < d(y)


= (8 + 7) < ∞
= 15 < ∞

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'.

1. d(x, y) = d(x) + c(x, y) < d(y)


2. = (8 + 1) < ∞
3. = 9 < ∞

Since 5 is less than the infinity, we update d(5) from infinity to 9.

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'.

d(x, y) = d(x) + c(x, y) < d(y)


= (9 + 15) < 15
= 24 < 15

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'.

1. d(x, y) = d(x) + c(x, y) < d(y)


2. = (9 + 2) < ∞</p>
3. = 11 < ∞

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

Since 11 is less than infinity, we update d(6) from infinity to 11.

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'.

d(x, y) = d(x) + c(x, y) < d(y)


= (11 + 4) < 12
= 15 < 12

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'.

1. d(x, y) = d(x) + c(x, y) < d(y)


2. = (11 + 14) < ∞
3. = 25 < ∞

Since 25 is less than ∞, so we will update d(3) from ∞ to 25.

Now we consider the vertex 7. Consider the vertex 6 as 'x', and the vertex 7 as 'y'.

d(x, y) = d(x) + c(x, y) < d(y)


= (11 + 10) < ∞
= 22 < ∞

Since 22 is less than ∞ so, we will update d(7) from ∞ to 22.

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'.

1. d(x, y) = d(x) + c(x, y) < d(y)


2. = (12 + 2) < 15
3. = 14 < 15

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

Since 14 is less than 15, we will update d(8) from 15 to 14.

Now, we consider the vertex 6. Consider the vertex 2 as 'x' and 6 as 'y'.

d(x, y) = d(x) + c(x, y) < d(y)


= (12 + 4) < 11
= 16 < 11

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'.

1. d(x, y) = d(x) + c(x, y) < d(y)


2. = (12 + 7) < 25
3. = 19 < 25

Since 19 is less than 25, we will update d(3) from 25 to 19.

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'.

d(x, y) = d(x) + c(x, y) < d(y)


= (19 + 9) < 21
= 28 < 21

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

Shortest path from s

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 -

• Thus we get all shortest path vertex as

- 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.

Item (I) Values (vi) Weight (wi)


1 18 3
2 25 5
3 27 4
4 10 3
5 15 6

Knapsack capacity M = 12.

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).

Item Values (vi) Weight (wi) Pi = ci / wi


3 27 4 6.75
1 18 3 6
2 25 5 5
4 10 3 3.33
5 15 6 2.5

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

Set of selected items, S = { }.


Here, Knapsack capacity M = 12.
Iteration 1: SW= (SW+w3) = 0 + 4 = 4
SW ≤ M, so select item 3
S = {3}, SW = 4, SP = 0 + 27 = 27
Iteration 2: SW = (SW + w1) = 4 + 3 = 7
SW ≤ M, so select item 1
S = {3, 1}, SW = 7, SP = 27 + 18 = 45
Iteration 3: SW (SW + w2) = 7 + 5 = 12
SW ≤ M, so select item 2
S = (3, 1, 2), SW = 12, SP = 45 + 25 = 70

- 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.

- The job is feasible only if it can be finished on or before its deadline.

- Greedy approach produces an optimal result in fairly less time as each job takes same amount of time

- Uniprocessor System: - One Processor is going to work on it.

- No Preemption :- if we start one job we should complete it without assigning any other

- 1 Unit of Time: 1 hr, 1Day, 1Month ….

Job J1 J2 J3 J4

Profit 50 15 10 25

Deadline 2 1 2 1

Algorithm JOB SCHEDULING(J, D, P)


//Description: Schedule the jobs using greedy approach which maximizes the profit
//Input: J: Array of N jobs
D: Array of deadline for each job
P: Array of profit associated with each job
Sort all jobs in J in decreasing order of profit If job does not miss its
deadline

S← ∅ //S is set of scheduled jobs, initially it is empty

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

Add job to solution set


for i ← 1 to N do
if Job J[i] is feasible then
Schedule the job in latest possible free slot meeting its deadline.
S ← S ∪ J[i]
Add respective profit
SP ← SP + P[i]

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..

Minimum spanning tree


• A connected Subgraph ‘s’ of graph G(V, E) is said to be spanning if
- ‘s’ should contain all vertices of ‘G’
- ‘s’ should contain (|v|-1) Edges.
• Spanning tree with minimum cost is called as minimal spanning tree.
• Application of MST
- MST is used in network design
It is used to implement efficient routing algorithm
- It is used to solve traveling salesman Problem
• Prims and Kruskal algorithm.

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

You might also like