Greedy Method(Unit-II) ada

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Greedy Method

• A greedy method is simple and straight


forward method, It always makes the choice
that seems to be the best at that moment.
This means that it makes a locally-optimal
choice in the hope that this choice will lead to
a globally-optimal solution.
1 3
Cont..
• A Greedy algorithm makes greedy choices at
each step to ensure that the objective
function is optimized. The Greedy algorithm
has only one shot to compute the optimal
solution so that it never goes back and
reverses the decision.
• Objective Function:- It is a function that needs
to be optimized (either maximized or
minimized) at a given point.

1 4
Greedy Algorithm
• Greedy Algorithm is used to solve a
optimization problem. An optimization
problem is one in which, we are given a set of
input values which are required to be either
maximize or minimize the objective function .
• In this method we have to find out two types
of solution.
1. Feasible Solution.
2. Optimal Solution.

1 5
Solutions
• Feasible Solution: All those solutions which
satisfies the problem constraints is known as
feasible solution.
• Optimal Solution: The solution from the list of
feasible solution that will maximize or
minimize the objective function is known as
the optimal solution.

1 6
Example of Greedy Algorithm
• Travelling Salesman Problem
• Prim's Minimal Spanning Tree Algorithm
• Kruskal's Minimal Spanning Tree Algorithm
• Dijkstra's Minimal Spanning Tree Algorithm
• Knapsack Problem
• Job Scheduling Problem

1 7
Knapsack Problem
• Given a set of items, each with a weight and a
value, determine a subset of items to include
in a collection so that the total weight is less
than or equal to a given limit and the total
value is as large as possible.
• A knapsack (kind of shoulder bag) with limited
weight capacity.

1 8
Cont…
• In this problem we have n objects from (i=1,2,3--n) Each
object i has some positive weight Wi and some profit value
is associated with each object which is denoted as Pi. And
some capacity constraint M.
• This Knapsack carry at most W KG weight.
• Our Goal :-
a) Choose only those objects that gives maximum
profit .
b) The total weight of selected objects should be
less than to M.
c) Our objective is to earn the maximum profit by filling of
objects in the knapsack.

1 9
Cont…

• The problem states-Which items should be


placed into the knapsack such that-
 The value or profit obtained by putting the
items into the knapsack is maximum.

 And the weight limit of the knapsack does not


exceed.

1 10
Knapsack

1 11
Cont..
• According to the problem statement,
• There are n items in the store
• Weight of ith item wi>0
• Profit for ith item pi>0 and
• Capacity of the Knapsack is W
• In this version of Knapsack problem, items can be broken into smaller pieces. So,
the fraction xi of ith item.
Hence, the objective of this algorithm is to
maximize∑n=1n(xi.pi)
• subject to constraint,
0⩽xi⩽1
• The ith item contributes the weight xi.wi to the total weight in the knapsack and
profit xi.pi to the total profit.
n
∑ (xi.wi)⩽W
n=1

1 12
Cont..
• Fraction of weights Xi(0 < x <1) of object i is
placed into the knapsack, then a profit of PiXi is
earned.
• Objective Function should be maximize

= ∑ PiXi
(1<i<n)

1 13
Example
• Q.1) Consider the following instances of the
knapsack problem: n=3, m=20, (P1,P2,P3)
=(25,24,15) and (W1,W2,W3)=(18,15,10) obtain
the solution for this knapsack.
• Solution: We have given

W: W1 W2 W3

P: P1 P2 P3

1 14
Solution :1
W: 18 15 10
P: 25 24 15
18
i) m=20,
X: 1
m=20-18=2; 2/15
X: 1 2/15
m=2- 2 =0; 18
X: 1 2/15 0

1 15
Solution 1
• Solution 1:
Total Profit= P i * X i
P : 25 24 15
X: 1 2/15 0
P i * X i = (1*25) +(2/15*24)+(0*15)
= 25+3.2+0
P1 = 28.2
It is not a optimal solution but it is a feasible
solution.
1 16
Solution :2
W: 18 15 10
P: 25 24 15
15
i) m=20,
X: 1
m=20-15=5; 5/10
X: 1 5/10
m=5- 5 =0; 18
X: 0 1 5/10

1 17
Cont..
• Solution 2:
Total Profit= P i * X i
P: 25 24 15
X: 0 1 5/10
P i * X i = (0*25) +(1*24)+(5/10*15)
= 0+24+7.5
P2 = 31.1
it is also feasible solution.

1 18
Optimal Solution
• Optimal Solution : for obtaining the optimal solution
we have to calculate profit per unit weight.
Wi: 18 15 10
P i : 25 24 15
P i /W i : 1.3 1.6 1.5
X: 0 1 5/10
m=20-15=5-5;
P i * X i = (0*25) +(1*24)+(5/10*15)
= 0+24+7.5
P2= 31.1
it is also Optimal solution.
1 19
Algorithm for Knapsack
• Algorithm if(w[i]>cu) break;
knapsack(w,p,m,n,x) else
{ {
for(i=1 to n) x[i]=1.0;
{ cu=cu-w[i];
x[i]=0.0; }
} }
cu=m; if(i<=n) then
for(i=1 to n) x[i]=cu/w[i];
{ }

1 20
Thank You

1 21

You might also like