GREEDY APPROACH
Shashikant V. Athawale
Assistant Professor ,Computer Engineering Department
AISSMS College of Engineering,
Kennedy Road, Pune , MS, India - 411001
1
GREEDY METHOD
 A greedy algorithm always makes the
choice that looks best at the moment
The hope: a locally optimal choice will lead to a
globally optimal solution
For some problems, it works
 greedy algorithms tend to be easier to
code
2
KNAPSACK PROBLEM
 Given a knapsack with maximum capacity W,
and a set S consisting of n items
 Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?
3
KNAPSACK PROBLEM
 The greedy algorithm:
Step 1: Sort pi
/wi
into nonincreasing order.
Step 2: Put the objects into the knapsack
according
to the sorted sequence as possible as we
can.
 e. g.
n = 3, M = 20, (p1
, p2
, p3
) = (25, 24, 15)
(w1
, w2
, w3
) = (18, 15, 10)
Sol: p1
/w1
= 25/18 = 1.39
p2
/w2
= 24/15 = 1.6
p3
/w3
= 15/10 = 1.5
Optimal solution: x1
= 0, x2
= 1, x3
= 1/2
total profit = 24 + 7.5 = 31.5
4
ACTIVITY SELECTION
 Problem: Schedule an exclusive resource in
competition with other entities. For example,
scheduling the use of a room (only one entity can
use it at a time) when several groups want to use
it. Or, renting out some piece of equipment to
different people.
 Definition: Set S={1,2, … n} of activities. Each
activity has a start time si and a finish time fj,
where si <fj. Activities i and j are compatible if
they do not overlap. The activity selection
problem is to select a maximum-size set of
mutually compatible activities.
5
ACTIVITY SELECTION
 Just march through each activity by finish time
and schedule it if possible:
6
JOB SEQUENCING
 Problem: n jobs, S={1, 2, …, n}, each job i has a
deadline di ≥ 0 and a profit pi ≥ 0. We need one
unit of time to process each job and we can do at
most one job each time. We can earn the profit pi
if job i is completed by its deadline.
The optimal solution = {1, 2, 4}.
The total profit = 20 + 15 + 5 = 40. 7
JOB SEQUENCING
Algorithm:
Step 1: Sort pi into nonincreasing order. After
sorting p1 ≥ p2 ≥ p3 ≥ … ≥ pn.
Step 2: Add the next job i to the solution set if i can
be completed by its deadline. Assign i to time slot
[r-1, r], where r is the largest integer such that 1
≤ r ≤ di and [r-1, r] is free.
Step 3: Stop if all jobs are examined. Otherwise, go
to step 2.
Time complexity: O(n2
) 8
Thank You
9
Thank You
9

Greedy method

  • 1.
    GREEDY APPROACH Shashikant V.Athawale Assistant Professor ,Computer Engineering Department AISSMS College of Engineering, Kennedy Road, Pune , MS, India - 411001 1
  • 2.
    GREEDY METHOD  Agreedy algorithm always makes the choice that looks best at the moment The hope: a locally optimal choice will lead to a globally optimal solution For some problems, it works  greedy algorithms tend to be easier to code 2
  • 3.
    KNAPSACK PROBLEM  Givena knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight wi and benefit value bi (all wi and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items? 3
  • 4.
    KNAPSACK PROBLEM  Thegreedy algorithm: Step 1: Sort pi /wi into nonincreasing order. Step 2: Put the objects into the knapsack according to the sorted sequence as possible as we can.  e. g. n = 3, M = 20, (p1 , p2 , p3 ) = (25, 24, 15) (w1 , w2 , w3 ) = (18, 15, 10) Sol: p1 /w1 = 25/18 = 1.39 p2 /w2 = 24/15 = 1.6 p3 /w3 = 15/10 = 1.5 Optimal solution: x1 = 0, x2 = 1, x3 = 1/2 total profit = 24 + 7.5 = 31.5 4
  • 5.
    ACTIVITY SELECTION  Problem:Schedule an exclusive resource in competition with other entities. For example, scheduling the use of a room (only one entity can use it at a time) when several groups want to use it. Or, renting out some piece of equipment to different people.  Definition: Set S={1,2, … n} of activities. Each activity has a start time si and a finish time fj, where si <fj. Activities i and j are compatible if they do not overlap. The activity selection problem is to select a maximum-size set of mutually compatible activities. 5
  • 6.
    ACTIVITY SELECTION  Justmarch through each activity by finish time and schedule it if possible: 6
  • 7.
    JOB SEQUENCING  Problem:n jobs, S={1, 2, …, n}, each job i has a deadline di ≥ 0 and a profit pi ≥ 0. We need one unit of time to process each job and we can do at most one job each time. We can earn the profit pi if job i is completed by its deadline. The optimal solution = {1, 2, 4}. The total profit = 20 + 15 + 5 = 40. 7
  • 8.
    JOB SEQUENCING Algorithm: Step 1:Sort pi into nonincreasing order. After sorting p1 ≥ p2 ≥ p3 ≥ … ≥ pn. Step 2: Add the next job i to the solution set if i can be completed by its deadline. Assign i to time slot [r-1, r], where r is the largest integer such that 1 ≤ r ≤ di and [r-1, r] is free. Step 3: Stop if all jobs are examined. Otherwise, go to step 2. Time complexity: O(n2 ) 8
  • 9.
  • 10.