Professional Documents
Culture Documents
DAA Module 3
DAA Module 3
DAA Module 3
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
i1
i i
Subject to n
wx
i1
i i ≤M
= 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.
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