0% found this document useful (0 votes)
9 views

Greedy RK

The document discusses the greedy algorithm approach, its characteristics, components and examples. The greedy approach makes locally optimal choices at each step to arrive at a global solution. It is simple and efficient for some problems but may not always produce an optimal solution.

Uploaded by

coolscools123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Greedy RK

The document discusses the greedy algorithm approach, its characteristics, components and examples. The greedy approach makes locally optimal choices at each step to arrive at a global solution. It is simple and efficient for some problems but may not always produce an optimal solution.

Uploaded by

coolscools123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Simplest and straight forward approach, this is technique

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

This approach is mainly used to solve optimization problems.

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.

Characteristics and features: -

(1) To construct the solution in an optimal way, this method derives two sets

a) Chosen items
b) Rejected items

(2) This technique make good local choices

a) Feasible Solution → Constraint

b) Optimal Solution → Min or Max Benefit

Components of Greedy Algorithm


Greedy algorithms have the following five components –

A candidate set − A solution is created from this set.

A selection function − Used to choose the best candidate to be added to the solution.

A feasibility function − Used to determine whether a candidate can be used to contribute to


the solution.

An objective function − Used to assign a value to a solution or a partial solution.

A solution function − Used to indicate whether a complete solution has been reached.

Where Greedy Approach Fails

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

Pseudo Code for Greedy Algorithm:

ALOG GREED Y(a,n)


{
Solution:=0;
for i = 1 to n do
{
x = select (a);
if feasible(solution, x) then
solution = union (solution, x)
}
return solution;
}
Activity Selection Problem

The activity selection problem is a mathematical optimization problem

GREEDY- ACTIVITY SELECTOR (s, f)


1. n ← length [s]
2. A ← {1}
3. j ← 1.
4. for i ← 2 to n
5. do if si ≥ fi
6. then A ← A ∪ {i}
7. j ← i
8. return A

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)

Compute a schedule where the greatest number of activities takes place.

Solution: The solution to the above Activity scheduling problem using a greedy strategy
is illustrated below:

Arranging the activities in increasing order of end time

Now, schedule A1

Next schedule A3 as A1 and A3 are non-interfering.


Next skip A2 as it is interfering.

Next, schedule A4 as A1 A3 and A4 are non-interfering, then next, schedule A6 as


A1 A3 A4 and A6 are non-interfering.

Skip A5 as it is interfering.

Next, schedule A7 as A1 A3 A4 A6 and A7 are non-interfering.

Next, schedule A9 as A1 A3 A4 A6 A7 and A9 are non-interfering.

Skip A8 as it is interfering.

Next, schedule A10 as A1 A3 A4 A6 A7 A9 and A10 are non-interfering.

Thus the final Activity schedule is:


For example Job Sequencing with deadline

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

Status Empty Empty Empty


Timeslot 1 2 3

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy