Greedy RK
Greedy RK
Among all the algorithmic approaches, the simplest and straightforward approach is the Greedy
method. In this approach, the decision is taken on the basis of current available information
without worrying about the effect of the current decision in future.
General plan : - Greedy Approach suggest to construct a solution for a problem through a
sequence of steps, each step expanding a partially obtained solution until a complete solution is
reached
Greedy method is easy to implement and quite efficient in most of the cases.
In many problems, it does not produce an optimal solution though it gives an approximate (near
optimal) solution in a reasonable time.
(1) To construct the solution in an optimal way, this method derives two sets
a) Chosen items
b) Rejected items
A selection function − Used to choose the best candidate to be added to the solution.
A solution function − Used to indicate whether a complete solution has been reached.
In many problems, Greedy algorithm fails to find an optimal solution, moreover it may produce a
worst solution. Problems like Travelling Salesman and Knapsack cannot be solved using this
approach.
Examples
Most networking algorithms use the greedy approach. Here is a list of few of them −
• Travelling Salesman Problem
• Prim's Minimal Spanning Tree Algorithm
• Kruskal's Minimal Spanning Tree Algorithm
• Dijkstra's Minimal Spanning Tree Algorithm
• Graph - Map Coloring
• Knapsack Problem
• Job Scheduling Problem
Activity 1 2 3 4 5 6 7 8 9 10 11
Si 1 3 0 5 3 5 6 8 8 2 12
Fi 4 5 6 7 9 9 10 11 12 14 16
Example: Given 10 activities along with their start and end time as
S = (A1 A2 A3 A4 A5 A6 A7 A8 A9 A10)
Si = (1,2,3,4,7,8,9,9,11,12)
fi = (3,5,4,7,10,9,11,13,12,14)
Solution: The solution to the above Activity scheduling problem using a greedy strategy
is illustrated below:
Now, schedule A1
Skip A5 as it is interfering.
Skip A8 as it is interfering.
n jobs J1,J2,….Jn
Associated with
Deadlines d1,d2,….dn
Profits are p1,p2,p3…pn (If job completes before deadline profits is earned)
The objective is to earn maximum profits when only one job can
be scheduled or processed at any given time
Index 1 2 3 4 5
Jobs A1 A2 A3 A4 A5
Deadlines 2 1 3 2 1
Profits 60 100 20 40 20
Pseudo code
For i= 1 to n do
Set k = min(dmax,deadline(i))
While k>=1 do
If timeslot[k] is empty then
Time slot[k] = job[i]
Break
Endif
Set k= k-1
End while
Sort the jobs according to profit
Index 1 2 3 4 5
Jobs A2 A1 A4 A3 A5
Deadlines 1 2 2 3 1
Profits 100 60 40 20 20
Step 1:
dmax =3
n=5
look at job A2
deadline =1 timeslot(1)
step 2:
look at job A1
deadline =2 timeslot(2)
step3:
look at job A3
deadline 3 timeslot(3)