DAA Module 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Greedy Method

 Greedy Principal: are typically used to solve


optimization problem. Most of these problems
have n inputs and require us to obtain a subset that
satisfies some constraints. Any subset that
satisfies these constraints is called a feasible
solution. We are required to find a feasible
solution that either minimizes or maximizes a
given objective function. In the most common
situation we have:
Greedy Method
 Subset Paradigm : Devise an algorithm
that works in stages
 Consider the inputs in an order based on
some selection procedure
 Use some optimization measure for
selection procedure
 –At every stage, examine an input to see
whether it leads to an optimal solution
 If the inclusion of input into partial solution
yields an infeasible solution, discard the input;
otherwise, add it to thepartial solution

3 -4
Control Abstraction for Greedy
function greedy(C: set): set;
begin
S := Ø;
while (not solution(S) and C ≠ Ø ) do
begin
x := select(C);
C := C - {x};
if feasible(S{x})
then S := S {x};
end;
if solution(S) then return(S) else return(Ø);
end;
Control Abstraction Expalination
 C: A set of objects;
 S: The set of objects that have already
been used;
 feasible(): A function that checks if a set
is a feasible solution;
 solution(): A function that checks if a set
provides a solution;
 select(): A function for choosing most
next object.
 An objective function that we are trying
to optimize.
3 -6
Greedy Vs Divide and Conquer
Greedy Divide and Conquer
Used when need to find optimal No optimal solution, used when
solution problem have only one solution
Does not work parallel work parallel by dividing big
problem in smaller sub problem and
running then parallel
Example: Knapsack, Example: Sorting, Searching
Activity selection

3 -7
Knapsack problem using
Greedy Method

8
The Knapsack Problems
 The knapsack problem is a problem
of optimization: Given a set of items n,
each with a weight w and profit p,
determine the number of each item to
include in a kanpsack such that the
total weight is less than or equal to a
given knapsack limit M and the total
Profit is maximum.
The Knapsack Problems
 The Integer Knapsack Problem
n
Maximize
px
i1
i i

Subject to n

wx
i1
i i ≤M

 The 0-1 Knapsack Problem: same as integer knapsack


except that the values of xi's are restricted to 0 or 1.
 The Fractional Knapsack Problem : same as integer
knapsack except that the values of xi's are between 0 and 1.
The knapsack algorithm
 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, (p , p , p ) = (25, 24, 15)
1 2 3
(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 3 -11

= 31.5
Job sequencing using
Greedy Method

12
JOB SEQUENCING WITH DEADLINES
The problem is stated as below.
 There are n jobs to be processed on a machine.

 Each job i has a deadline d ≥ 0 and profit p ≥0 .


i i
 Pi is earned iff the job is completed by
its deadline.
 The job is completed if it is processed
on a machine for unit time.
 Only one machine is available for
processing jobs.
 Only one job is processed at a time on
the machine.

13
JOB SEQUENCING WITH
DEADLINES (Contd..)
 A feasible solution is a subset of jobs J
such that each job is completed by
its deadline.
 An optimal solution is a feasible solution
with maximum profit value.
Example : Let n = 4,
(p1,p2,p3,p4)= (100,10,15,27),
(d1,d2,d3,d4) = (2,1,2,1)
14
JOB SEQUENCING WITH
DEADLINES (Contd..)
Sr.No. Feasible Processing Profit value
Solution Sequence
(i) (1,2) (2,1) 110
(ii) (1,3) (1,3) or (3,1) 115
(iii) (1,4) (4,1) 127 is the optimal one
(iv) (2,3) (2,3) 25
(v) (3,4) (4,3) 42
(vi) (1) (1) 100
(vii) (2) (2) 10
(viii) (3) (3) 15
(ix) (4) (4) 27

15
GREEDY ALGORITHM TO OBTAIN AN
OPTIMAL SOLUTION for Job Scheduling
 Consider the jobs in the decreasing
order of profits subject to the
constraint that the resulting job
sequence J is a feasible solution.
 In the example considered before, the
decreasing profit vector is
(100 27 15 10) (2 1 2 1)
p1 p4 p3 p2 d1 d4 d3 d2

16
GREEDY ALGORITHM TO OBTAIN AN
OPTIMAL SOLUTION (Contd..)
J = { 1} is a feasible one
J = { 1, 4} is a feasible one with processing
sequence ( 4,1)
J = { 1, 3, 4} is not feasible
J = { 1, 2, 4} is not feasible
J = { 1, 4} is optimal

17
Activity Selection Using
Greedy Method

18
The activity selection problem
 Problem: n activities, S = {1, 2, …, n},
each activity i has a start time si and a finish
time fi, si  fi.
 Activity i occupies time interval [si, fi].
 i and j are compatible if si  fj or sj  fi.
 The problem is to select a maximum-size set
of mutually compatible activities

3 -19
Example:
i 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 8 9 10 11 12 13 14

The solution set = {1, 4, 8, 11}


Algorithm:
Step 1: Sort fi into
nondecreasing order. After
sorting, f1
 f2  f3  …  fn.
Step 2: Add the next activity i to the solution set if i is
compatible with each in the solution set.
Step 3: Stop if all activities are examined. Otherwise, go
to step 2.
3 -20

Time complexity: O(nlogn)

You might also like