SlideShare a Scribd company logo
1 of 28
Greedy Method
• The Greedy method is most straightforward design
technique which can be applied to a wide variety of
problems. This algorithm works in steps. In each step it
selects the best available options until all option finish
• Most of the problems have n inputs and require us to
obtain a subset that satisfies some constraints. Any
subset that satisfies these constraints is called as a
feasible solution.
• A feasible solution that either minimizes or maximizes
a given objective function is called as Optimal
Solution.
• The Greedy method suggest that one can devise an
algorithm that work in stages, considering one input at a
time. At each stage, a decision is made regarding whether
a particular input is an optimal solution or not. This is
done by considering the inputs in an order determined by
some selection procedure. If the inclusion of the next
input into the partially constructed optimal solution will
result in an infeasible solution, then this input is not
added to the partial solution. Otherwise, it is added. The
selection procedure itself is based on some optimization
measures. This measure may be the objective function.
Several different optimization measures may be possible
for a given problem. However, will result in algorithm
that generate suboptimal solutions is called subset
paradigm.
Greedy method control abstraction for the subset
paradigm can be written as –
Algorithm Greedy(a,n) //a[1:n] contains the n inputs
{
Solution:=0;
for i:=1 to n do
{ x:=Select(a);
if Feasible(Solution, x) then
Solution:=Union(Solution, x);
}
return Solution;
}
The above Greedy function describes the essential way that a greedy algorithm
will look, once a particular problem is chosen and the function Select, Feasible
and Union are properly implemented. The function Select selects an input
from a[] and removes it. The selected input’s value is assigned to x. Feasible is
a Boolean-valued function that determines whether x can be included into the
solution vector or not. The function Union combines x with the solution and
updates the objective function
Example 1 [Change Making]
• A child buys candy valued at less than Rs. 1 and gives an Rs 1 bill to the
cashier. The cashier whishes to return change using the fewest
number of coins. Assume that an unlimited supply of 50 paisa, 25 paisa,
20 paisa, 10 paisa and 5 paisa coins are available. The cashier constructs
the change in stages. In each stage a coin is added to the change. This
coin is selected using the greedy criterion: At each stage increase the
total amount of change constructed by as much as possible. To assure
feasibility (i.e. the change given exactly equals the desired amount) of
the solution, the selected coin should not cause the total amount of
change given so far to exceed the final desired amount.
• Suppose that 65 paisa in change is due to child. The first coin selected is
50 paisa, a 25 paisa or 20 paisa coin cannot be selected as a second coin
because such a selection results in an infeasible selection of coins
(change exceeds 65 paisa). The second coin selected is a 10 paisa coin
and then a 5 paisa coin is added to the change.
• Here there were number of other possible sets of coins which formulate
65 paisa but the greedy method implies that with minimum number of
inputs or values it is expected to obtain the optimum solution.
Example 2 [Machine Scheduling]
• You are given n tasks and an infinite supply of machines
on which these tasks can be performed. Each task has a
start time Si and a finish time Fi, Si<Fi. [Si, Fi] is the
processing interval for task i. Two tasks i and j overlap
iff their processing interval overlap at a point other than
the interval start or end. A feasible task-to-machine
assignment is an assignment in which no machine is
assigned two overlapping tasks. Therefore in a feasible
assignment each machine works on the most one task at
any time. An optimal assignment is a feasible
assignment that utilizes the fewest number of machines
to complete the task.
• Suppose we have n=7 tasks labeled from a through g and their start and finish
times are as shown bellow.
• The above task-to-machine assignment is a feasible assignment that uses seven
machines for seven tasks. E.g. task a assign to machine M1, task b to machine
m2 etc. This assignment is not an optimal assignment because other assignment
method uses fewer machines as shown in following chart.
M3 _____________c___________
________d_________
M2 ________________f________________ _________g________
M1 ______a__ ____________b___________________ _________e________________
0 1 2 3 4 5 6 7 8 9 10 11
• Above chart shows you that, we can assign tasks a, b, and e to some machine,
task f and g for another machine and task c and d for other machine, reducing the
number of utilized machine to three.
Task A b c d e F G
Start 0 3 4 9 7 1 6
Finish 2 7 7 11 10 5 8
• A greedy way to obtain an optimal task assignment is to assign
the tasks in stages, one task per stage and in ascending order of
task start times. Call a machine old if at least one task has been
assigned to it. If machine is not old, it is New.
• For machine selection, use the greedy criterion: If an old machine
becomes available by the start time of the task to be assigned,
assign the task to this machine; if not, assign it to a new machine.
• Here, first stage has no old machines, so task a is assigned to new
machine (Say M1). This machine is now busy from time 0 to 2. In
stage 2 task f is considered. Since the only old machine is busy
when task f is to start, it is assigned to a new machine (Say M2).
When task b is considered in stage 3, we find that the old machine
M1 is free at time 3, so b is assigned to M1. In stage 4 task c is
considered. Since neither of the old machine is available at time
4, task c is assigned to new machine (Say M3). Task g is
considered in stage 5 and assigned to machine M2, which is the
first to become available. Task is assigned in stage 6 to machine
M1, and finally, in stage 7, task d assigned to machine M2 or M3.
Example 3: Container Loading
• A large ship is to be loaded with cargo.
• The cargo is containerized, and all
containers are the same size.
• Different containers may have different
weights.
• We wish to load the ship with maximum
number of containers.
This problem can be formulated as an optimization
problem in the following way:
• Let Wi be the weight of the ith
container, 1 ≤
i ≤ n.
• The cargo capacity of the ship is C.
• Let Xi be a variable whose value can be
either 0 or 1. If we set Xi to 0, then
container I is not to be loaded. If Xi is 1,
then the container is to be loaded in ship.
• We wish to assign the values to the Xi that satisfy
the constraints
≤ C and Xi {0, 1}, 1 ≤ i ≤ n. The∈
optimization function is
• Every set of Xi’s that satisfies the constraints is a
feasible solution and every feasible solution that
maximizes is an optimal solution.
• The ship may be loaded in stages; one container per stage. At each
stage we need to select container load. For this decision we may
use the greedy criterion: From the remaining containers, select the
one with least weight. This order of selection will keep the total
weight of the selected container to minimum and hence leave
Greedy algorithm implies that,
• We first select the container that has least
weight, then the one with the next smaller
weight, and so on until either all containers
have been loaded or there isn’t enough
capacity for the next one.
Pseudo-code for Greedy container-loading method
can be written as
Algorithm ContainerLoading( C, Capacity, NumberOfContainers, X)
{ Sort(C, NumberOfContainers); //Sort into increasing order of weight.
N:=NumberOfContainsers;
For i:=1 to N do
X[i]:=0; //Initialixe all X to zero (Ship is vacant)
i:=1 // Select containers in ascending order of weight
while(i ≤ N && C[i].weight ≤ Capacity) //enough capacity for container C[i].id
{ X[C[i].id]:=1; // Load container to ship
Capacity -=C[i].weight; // Calculate remaining load capacity of ship
i++; // take next container.
}
}
Suppose we want to load 8 containers of different weights
(in tones) [100, 200, 50, 90, 150, 50, 20, 80] in a ship
having capacity of 400 tones. Specify How many and
which containers can be loaded in to ship.
Here, n=8, C=400 Let weights of container are -
W1, W2, W3, W4, W5, W6, W7, W8 Total Total
100, 200, 50, 90, 150, 50, 20, 80 Weight Containers
X1= 1 1 1 0 0 1 0 0 = 400 4
X2= 1 0 1 0 1 0 1 1 = 400 5
X3= 0 1 1 0 1 0 0 0 = 400 3
X4= 1 0 1 1 0 1 1 1 = 390 6
:
:
• When a greedy algorithm is used, the
containers are considered for loading in the
order 7, 3, 6, 8, 4, 1, 5, 2. Containers 7, 3, 6,
8, 4, and 1 together weight 390 tones and
are loaded. The available capacity is now
only 10 tones, which is inadequate for any
of the remaining containers.
• In the greedy solution we have [X1, X2,…,
X8]=[ 1, 0, 1, 1, 0, 1, 1, 1] and =
6. i.e. we can load maximum 6 containers
into the ship with identification count 1, 3,
4, 6, 7 and 8.
Example 4: Knapsack problem
• Let, we are given n objects and a Knapsack or
Bag.
• Object i has weight Wi and
• the Knapsack has a capacity M.
• if a fraction Xi of object i is placed into
Knapsack, then a profit of PiXi is earned.
• The objective is to obtain a filling of Knapsack
that maximizes the total profit earned.
• Maximize (A)
• Subject to (B)
•
• And 0 ≤ Xi ≤ 1, 1 ≤i ≤n (C)
• The profit and weights are the positive numbers.
• Here, A feasible solution is any set (X1, X2, …,
Xn) satisfying above rules (B) and (C).
• And an optimal solution is feasible solution for
which rule (A) is maximized.
• Example: Suppose, you have provided a three
objects of different weights as (18, 15 and 10)
with their corresponding profit values (25, 24
and 15). Also a knapsack having capacity of 20
is provided. Find out the different feasible
solution and optimal solution that yield
maximum profit.
• Solution
– Here, N=3,
– M=20,
– (P1, P2, P3)=(25, 24, 15) and
– (W1, W2, W3)=(18, 15, 10)
Here, N=3, M=20, (P1, P2, P3)=(25, 24, 15) and (W1, W2, W3)=(18, 15, 10)
Different feasible solutions are:
(X1, X2, X3)
1. (1/2, 1/3, ¼) 16.5 24.25
2. (1, 2/15, 0) 20 28.2
3. (0, 2/3, 1) 20 31
4. (0, 1, 1/2) 20 31.5
5. (1/2, 2/3, 1/ 10) 20 30
6. (1, 0, 2/10) 20 28
• Of these Six feasible solutions, solution 4 yields the maximum profit.
Therefore solution 4 is optimal for the given problem instance.
• Consideration 1 - In case the sum of all the weights is ≤ M, then Xi=1, 1 ≤ i ≤n
is an optimal solution.
• Consideration 2 - All optimal solutions will fill the knapsack exactly.
Algorithm GreedyKnapsack( M, n) //P[1:n] and W[1:n] contains Profit & Weights
{ respectively of the n objects ordered such that P[i]/W[i] ≥ P[i+1]/ W[i+1].
for i:=1 to n do M is the knapsack size and X[1:n] is the feasible solution vector.
X[i]:=0.0;
U:=M;
for i:=1 to n do
{
if(W[i]>U then
break;
X[i]:=1.0;
U:=U-W[i];
}
if (i≤n) then
X[i]:=U/W[i];
}
Job Sequencing with Deadlines
• We are given a set of n jobs.
• Di is a deadline given to complete ith
job for profit Pi where Pi>0 &
Di ≥ 0.
• For any job i profit Pi is earned iff the job is completed within its
deadline.
• To complete a job, one has to process the job on a machine for one
unit of time.
• Only one machine is available for processing jobs.
• A feasible solution for this problem is a subset J of jobs such that
each job in this subset can be completed by its deadline.
• The value of a feasible solution J is the sum of the profits of the job
in J.
• An optimal solution is a feasible solution with maximum value.
Example –Suppose on a single machine four jobs
with profit values (100, 10, 15 and 27) and their
respective deadline unit values (2, 1, 2, 1) are
given. Calculate the different feasible solutions to
complete the jobs with optimal solution.
Solution -Here,
Number of Jobs (n)= 4
Profit values of four jobs (P1, P2, P3, P4)=(100, 10, 15, 27)
Process deadlines for respective job are (D1, D2, D3, D4)
=(2, 1, 2, 1)
(P1, P2, P3, P4)=(100, 10, 15, 27) (D1, D2, D3, D4) =(2, 1, 2, 1)
The feasible solutions and their values are –
Feasible sol. subset Processing Sequence Values
(1, 2) (2, 1) 110
(1, 3) (1, 3 or 3, 1) 115
(1, 4) (4, 1) 127
(3, 2) (2, 3) 25
(3, 4) (4, 3) 42
(1) (1) 100
(2) (2) 10
(3) (3) 15
(4) (4) 27
Here, Solution 3 is optimal solution. In this solution only job 1 and 4
are processed and the profit value is 127. These jobs must be
processed in the order Job 4 followed by job 1. Thus the processing
of job 4 begins at time zero and that of job 1 is completed at time 2.
Single-Source Shortest Paths
• Path is the way between source and destination.
• The source, destination and the path were generally represented with
the help of graph.
• Vertices were used to represent Source or Destination and
• the lines used to connect sources and destinations are called Edges.
• The edges can then be assigned weights which may be either the
distance between two vertices or average time to drive between two
cities etc.
• The graphs were generally used to answer the following questions –
– Is there a path between source and destination?
– If there are more than one paths from source to destination, which is the
shortest path?
• The length of path is the sum of weights of the edges on the path.
• The starting vertex of the path is referred as Source, and the last
vertex the destination.
• Consider the directed graph , the number
on the edges are the weights.
• The shortest path from node 1 to other
destination node are
Path Length
1-4 10
1-5 25
1-2 (45,50)= 45
1-3 (45,50,60) = 45
1-6 No path
If node 1 is the source vertex, then the shortest path from 1 to 2 is 1-
4-5-2. The length of this path is 10 + 15 + 20 =45. Even though
there are three edges on this path, it is shorter than the path 1-2
which is of length 50.
• The systematic way to generate the shortest paths from
vertex V0 to the remaining vertices is to generate these
paths in ascending order of path length.
• First, a shortest path to the nearest vertex is generated.
• Then a shortest path to the second nearest vertex is
generated, and so on.
– For the above graph the nearest vertex to V0=1 is 4
(Cost[1,4]=10). The path 1-4 is the first path generated.
– The second nearest vertex to node 1 is 5 and the distance
between 1to5 is 25. Then path 1-4-5 is the next path generated.
– The third nearest vertex to node 1 is 2 and the distance
between 1to2 is 45. Then path 1-4-5-2 is the next path
generated.
– The forth nearest vertex to node 1 is 3 and the distance
between 1to3 is 45. Then path 1-3 is the next path generated.
The Cost adjacency matrix for above problem is as shown bellow
Destination Nodes
1 2 3 4 5 6
1 0 50 45 10
2 0 10 15
3 0 30
4 20 0 15
5 20 35 0
6 3 0
Dijkstra’s algorithm is used to determine the length of the
shortest path from vertex V0 to all other vertices in graph
G.
• Suppose you have 7 containers whose weights are
50, 10, 30, 20, 70, 60, and 5 and a ship whose
capacity is 110. Find an optimal solution to this
instance of the container loading problem.
W1, W2, W3, W4, W5, W6, W7, W8 Total Total
100, 200, 50, 90, 150, 50, 20, 80 Weight Containers
X1= 1 1 1 0 0 1 0 0 = 400 4
Find an optimal solution to the knapsack instance
n=7, m=15,
(P1, P2, P3,… P7)=(10, 5, 15, 7, 6, 18, 3), and
(W1, W2, W3,…,W7)=(2, 3, 5, 7, 1, 4, 1).
Sets Total weight Profit earned
1.(0,1,0,1,1,1,0) 15 36
2.(1,0,0,1,1,1,1) 15 44
3.(0,1,1,1,0,0,0) 15 27
4.(1,1,1,0,0,1,1) 15 51
5.(1,1,1,0,1,1,0) 15 54
6.(1,2/3,1,0,1,1,1) 15 55.33

More Related Content

What's hot

sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using BacktrackingAbhishek Singh
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languagesSOMNATHMORE2
 
Counter propagation Network
Counter propagation NetworkCounter propagation Network
Counter propagation NetworkAkshay Dhole
 
AI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction ProblemAI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction ProblemMohammad Imam Hossain
 
Conceptual dependency
Conceptual dependencyConceptual dependency
Conceptual dependencyJismy .K.Jose
 
Presentation on cyclic redundancy check (crc)
Presentation on cyclic redundancy check (crc)Presentation on cyclic redundancy check (crc)
Presentation on cyclic redundancy check (crc)Sudhanshu Srivastava
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMRajendran
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and SearchHitesh Mohapatra
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Hasanain Alshadoodee
 
Architecture of Mobile Computing
Architecture of Mobile ComputingArchitecture of Mobile Computing
Architecture of Mobile ComputingJAINIK PATEL
 
Congestion avoidance in TCP
Congestion avoidance in TCPCongestion avoidance in TCP
Congestion avoidance in TCPselvakumar_b1985
 
Routing algorithm
Routing algorithmRouting algorithm
Routing algorithmBushra M
 

What's hot (20)

sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
 
Counter propagation Network
Counter propagation NetworkCounter propagation Network
Counter propagation Network
 
AI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction ProblemAI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction Problem
 
Conceptual dependency
Conceptual dependencyConceptual dependency
Conceptual dependency
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Lower bound
Lower boundLower bound
Lower bound
 
Presentation on cyclic redundancy check (crc)
Presentation on cyclic redundancy check (crc)Presentation on cyclic redundancy check (crc)
Presentation on cyclic redundancy check (crc)
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEM
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Dempster shafer theory
Dempster shafer theoryDempster shafer theory
Dempster shafer theory
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and Search
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
 
Architecture of Mobile Computing
Architecture of Mobile ComputingArchitecture of Mobile Computing
Architecture of Mobile Computing
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Linker and Loader
Linker and Loader Linker and Loader
Linker and Loader
 
Congestion avoidance in TCP
Congestion avoidance in TCPCongestion avoidance in TCP
Congestion avoidance in TCP
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Routing algorithm
Routing algorithmRouting algorithm
Routing algorithm
 

Viewers also liked

Pedagogy of science
Pedagogy of sciencePedagogy of science
Pedagogy of scienceAbu Bashar
 
Simplex method
Simplex methodSimplex method
Simplex methodAbu Bashar
 
Sequencing problems in Operations Research
Sequencing problems in Operations ResearchSequencing problems in Operations Research
Sequencing problems in Operations ResearchAbu Bashar
 
Duality in lpp
Duality in lppDuality in lpp
Duality in lppAbu Bashar
 
Inventory Management
Inventory ManagementInventory Management
Inventory ManagementAbu Bashar
 
Simulation theory
Simulation theorySimulation theory
Simulation theoryAbu Bashar
 
Linear programming in market application
Linear programming in market applicationLinear programming in market application
Linear programming in market applicationAhmad Raza Bhatti
 
Role and importance of language in the curriculum
Role and importance of language in the curriculumRole and importance of language in the curriculum
Role and importance of language in the curriculumAbu Bashar
 
Payback model of risk management by Dr. B. J. Mohite
Payback model of risk management by Dr. B. J. MohitePayback model of risk management by Dr. B. J. Mohite
Payback model of risk management by Dr. B. J. MohiteZeal Education Society, Pune
 
What is research??
What is research??What is research??
What is research??Abu Bashar
 
Project management cpm-pert
Project management   cpm-pertProject management   cpm-pert
Project management cpm-pertAbu Bashar
 
Plant location and layout
Plant location and layoutPlant location and layout
Plant location and layoutAbu Bashar
 
Design and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. MohiteDesign and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. MohiteZeal Education Society, Pune
 
Sales forecasting techniques
Sales forecasting techniquesSales forecasting techniques
Sales forecasting techniquesAbu Bashar
 
Enterprise Resource Management by Dr. B. J. Mohite
Enterprise Resource Management by Dr. B. J. MohiteEnterprise Resource Management by Dr. B. J. Mohite
Enterprise Resource Management by Dr. B. J. MohiteZeal Education Society, Pune
 

Viewers also liked (20)

Pedagogy of science
Pedagogy of sciencePedagogy of science
Pedagogy of science
 
Productivity
ProductivityProductivity
Productivity
 
Simplex method
Simplex methodSimplex method
Simplex method
 
Sequencing problems in Operations Research
Sequencing problems in Operations ResearchSequencing problems in Operations Research
Sequencing problems in Operations Research
 
Duality in lpp
Duality in lppDuality in lpp
Duality in lpp
 
Managerial Decision Making by Dr. B. J. Mohite
Managerial Decision Making by Dr. B. J. MohiteManagerial Decision Making by Dr. B. J. Mohite
Managerial Decision Making by Dr. B. J. Mohite
 
Inventory Management
Inventory ManagementInventory Management
Inventory Management
 
Simulation theory
Simulation theorySimulation theory
Simulation theory
 
Linear programming in market application
Linear programming in market applicationLinear programming in market application
Linear programming in market application
 
Function Point Analysis (FPA) by Dr. B. J. Mohite
Function Point Analysis (FPA) by Dr. B. J. MohiteFunction Point Analysis (FPA) by Dr. B. J. Mohite
Function Point Analysis (FPA) by Dr. B. J. Mohite
 
Replacement Theory. by Dr. Babasaheb. J. Mohite
Replacement Theory. by  Dr. Babasaheb. J. MohiteReplacement Theory. by  Dr. Babasaheb. J. Mohite
Replacement Theory. by Dr. Babasaheb. J. Mohite
 
Software metrics by Dr. B. J. Mohite
Software metrics by Dr. B. J. MohiteSoftware metrics by Dr. B. J. Mohite
Software metrics by Dr. B. J. Mohite
 
Role and importance of language in the curriculum
Role and importance of language in the curriculumRole and importance of language in the curriculum
Role and importance of language in the curriculum
 
Payback model of risk management by Dr. B. J. Mohite
Payback model of risk management by Dr. B. J. MohitePayback model of risk management by Dr. B. J. Mohite
Payback model of risk management by Dr. B. J. Mohite
 
What is research??
What is research??What is research??
What is research??
 
Project management cpm-pert
Project management   cpm-pertProject management   cpm-pert
Project management cpm-pert
 
Plant location and layout
Plant location and layoutPlant location and layout
Plant location and layout
 
Design and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. MohiteDesign and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. Mohite
 
Sales forecasting techniques
Sales forecasting techniquesSales forecasting techniques
Sales forecasting techniques
 
Enterprise Resource Management by Dr. B. J. Mohite
Enterprise Resource Management by Dr. B. J. MohiteEnterprise Resource Management by Dr. B. J. Mohite
Enterprise Resource Management by Dr. B. J. Mohite
 

Similar to Greedy method by Dr. B. J. Mohite

data structures and algorithms Unit 4
data structures and algorithms Unit 4data structures and algorithms Unit 4
data structures and algorithms Unit 4infanciaj
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptRuchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptRuchika Sinha
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesNirmalavenkatachalam
 
Greedy method1
Greedy method1Greedy method1
Greedy method1Rajendran
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"22bcs058
 
Data structure notes
Data structure notesData structure notes
Data structure notesanujab5
 
04-Unit Four - OR.pptx
04-Unit Four - OR.pptx04-Unit Four - OR.pptx
04-Unit Four - OR.pptxAbdiMuceeTube
 
Introduction to Optimization revised.ppt
Introduction to Optimization revised.pptIntroduction to Optimization revised.ppt
Introduction to Optimization revised.pptJahnaviGautam
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...2022cspaawan12556
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfyashodamb
 
LINEAR PROGRAMMING
LINEAR PROGRAMMINGLINEAR PROGRAMMING
LINEAR PROGRAMMINGrashi9
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 

Similar to Greedy method by Dr. B. J. Mohite (20)

data structures and algorithms Unit 4
data structures and algorithms Unit 4data structures and algorithms Unit 4
data structures and algorithms Unit 4
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
 
Greedy
GreedyGreedy
Greedy
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
Lec30
Lec30Lec30
Lec30
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
 
Data structure notes
Data structure notesData structure notes
Data structure notes
 
04-Unit Four - OR.pptx
04-Unit Four - OR.pptx04-Unit Four - OR.pptx
04-Unit Four - OR.pptx
 
Introduction to Optimization revised.ppt
Introduction to Optimization revised.pptIntroduction to Optimization revised.ppt
Introduction to Optimization revised.ppt
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
 
LINEAR PROGRAMMING
LINEAR PROGRAMMINGLINEAR PROGRAMMING
LINEAR PROGRAMMING
 
Unit V.pdf
Unit V.pdfUnit V.pdf
Unit V.pdf
 
greedy method.pdf
greedy method.pdfgreedy method.pdf
greedy method.pdf
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 

More from Zeal Education Society, Pune

More from Zeal Education Society, Pune (8)

Knowledge Management System By Dr. B. J. Mohite
Knowledge Management System By Dr. B. J. MohiteKnowledge Management System By Dr. B. J. Mohite
Knowledge Management System By Dr. B. J. Mohite
 
Functional Modules of ERP By Dr. B. J. Mohite
Functional Modules of ERP By Dr. B. J. MohiteFunctional Modules of ERP By Dr. B. J. Mohite
Functional Modules of ERP By Dr. B. J. Mohite
 
Ms-Project by Dr. B. J. Mohite
Ms-Project by Dr. B. J. MohiteMs-Project by Dr. B. J. Mohite
Ms-Project by Dr. B. J. Mohite
 
COCOMO Model By Dr. B. J. Mohite
COCOMO Model By Dr. B. J. MohiteCOCOMO Model By Dr. B. J. Mohite
COCOMO Model By Dr. B. J. Mohite
 
Software Project Management by Dr. B. J. Mohite
Software Project Management by Dr. B. J. MohiteSoftware Project Management by Dr. B. J. Mohite
Software Project Management by Dr. B. J. Mohite
 
Fundamentals of Organizational Behavior by Dr. B. J. Mohite
Fundamentals of Organizational Behavior by Dr. B. J. MohiteFundamentals of Organizational Behavior by Dr. B. J. Mohite
Fundamentals of Organizational Behavior by Dr. B. J. Mohite
 
Principles and Practices of Management By Dr. B. J. Mohite
Principles and Practices of Management By Dr. B. J. MohitePrinciples and Practices of Management By Dr. B. J. Mohite
Principles and Practices of Management By Dr. B. J. Mohite
 
Queuing Theory by Dr. B. J. Mohite
Queuing Theory by Dr. B. J. MohiteQueuing Theory by Dr. B. J. Mohite
Queuing Theory by Dr. B. J. Mohite
 

Recently uploaded

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Greedy method by Dr. B. J. Mohite

  • 1. Greedy Method • The Greedy method is most straightforward design technique which can be applied to a wide variety of problems. This algorithm works in steps. In each step it selects the best available options until all option finish • Most of the problems have n inputs and require us to obtain a subset that satisfies some constraints. Any subset that satisfies these constraints is called as a feasible solution. • A feasible solution that either minimizes or maximizes a given objective function is called as Optimal Solution.
  • 2. • The Greedy method suggest that one can devise an algorithm that work in stages, considering one input at a time. At each stage, a decision is made regarding whether a particular input is an optimal solution or not. This is done by considering the inputs in an order determined by some selection procedure. If the inclusion of the next input into the partially constructed optimal solution will result in an infeasible solution, then this input is not added to the partial solution. Otherwise, it is added. The selection procedure itself is based on some optimization measures. This measure may be the objective function. Several different optimization measures may be possible for a given problem. However, will result in algorithm that generate suboptimal solutions is called subset paradigm.
  • 3. Greedy method control abstraction for the subset paradigm can be written as – Algorithm Greedy(a,n) //a[1:n] contains the n inputs { Solution:=0; for i:=1 to n do { x:=Select(a); if Feasible(Solution, x) then Solution:=Union(Solution, x); } return Solution; } The above Greedy function describes the essential way that a greedy algorithm will look, once a particular problem is chosen and the function Select, Feasible and Union are properly implemented. The function Select selects an input from a[] and removes it. The selected input’s value is assigned to x. Feasible is a Boolean-valued function that determines whether x can be included into the solution vector or not. The function Union combines x with the solution and updates the objective function
  • 4. Example 1 [Change Making] • A child buys candy valued at less than Rs. 1 and gives an Rs 1 bill to the cashier. The cashier whishes to return change using the fewest number of coins. Assume that an unlimited supply of 50 paisa, 25 paisa, 20 paisa, 10 paisa and 5 paisa coins are available. The cashier constructs the change in stages. In each stage a coin is added to the change. This coin is selected using the greedy criterion: At each stage increase the total amount of change constructed by as much as possible. To assure feasibility (i.e. the change given exactly equals the desired amount) of the solution, the selected coin should not cause the total amount of change given so far to exceed the final desired amount. • Suppose that 65 paisa in change is due to child. The first coin selected is 50 paisa, a 25 paisa or 20 paisa coin cannot be selected as a second coin because such a selection results in an infeasible selection of coins (change exceeds 65 paisa). The second coin selected is a 10 paisa coin and then a 5 paisa coin is added to the change. • Here there were number of other possible sets of coins which formulate 65 paisa but the greedy method implies that with minimum number of inputs or values it is expected to obtain the optimum solution.
  • 5. Example 2 [Machine Scheduling] • You are given n tasks and an infinite supply of machines on which these tasks can be performed. Each task has a start time Si and a finish time Fi, Si<Fi. [Si, Fi] is the processing interval for task i. Two tasks i and j overlap iff their processing interval overlap at a point other than the interval start or end. A feasible task-to-machine assignment is an assignment in which no machine is assigned two overlapping tasks. Therefore in a feasible assignment each machine works on the most one task at any time. An optimal assignment is a feasible assignment that utilizes the fewest number of machines to complete the task.
  • 6. • Suppose we have n=7 tasks labeled from a through g and their start and finish times are as shown bellow. • The above task-to-machine assignment is a feasible assignment that uses seven machines for seven tasks. E.g. task a assign to machine M1, task b to machine m2 etc. This assignment is not an optimal assignment because other assignment method uses fewer machines as shown in following chart. M3 _____________c___________ ________d_________ M2 ________________f________________ _________g________ M1 ______a__ ____________b___________________ _________e________________ 0 1 2 3 4 5 6 7 8 9 10 11 • Above chart shows you that, we can assign tasks a, b, and e to some machine, task f and g for another machine and task c and d for other machine, reducing the number of utilized machine to three. Task A b c d e F G Start 0 3 4 9 7 1 6 Finish 2 7 7 11 10 5 8
  • 7. • A greedy way to obtain an optimal task assignment is to assign the tasks in stages, one task per stage and in ascending order of task start times. Call a machine old if at least one task has been assigned to it. If machine is not old, it is New. • For machine selection, use the greedy criterion: If an old machine becomes available by the start time of the task to be assigned, assign the task to this machine; if not, assign it to a new machine. • Here, first stage has no old machines, so task a is assigned to new machine (Say M1). This machine is now busy from time 0 to 2. In stage 2 task f is considered. Since the only old machine is busy when task f is to start, it is assigned to a new machine (Say M2). When task b is considered in stage 3, we find that the old machine M1 is free at time 3, so b is assigned to M1. In stage 4 task c is considered. Since neither of the old machine is available at time 4, task c is assigned to new machine (Say M3). Task g is considered in stage 5 and assigned to machine M2, which is the first to become available. Task is assigned in stage 6 to machine M1, and finally, in stage 7, task d assigned to machine M2 or M3.
  • 8. Example 3: Container Loading • A large ship is to be loaded with cargo. • The cargo is containerized, and all containers are the same size. • Different containers may have different weights. • We wish to load the ship with maximum number of containers.
  • 9. This problem can be formulated as an optimization problem in the following way: • Let Wi be the weight of the ith container, 1 ≤ i ≤ n. • The cargo capacity of the ship is C. • Let Xi be a variable whose value can be either 0 or 1. If we set Xi to 0, then container I is not to be loaded. If Xi is 1, then the container is to be loaded in ship.
  • 10. • We wish to assign the values to the Xi that satisfy the constraints ≤ C and Xi {0, 1}, 1 ≤ i ≤ n. The∈ optimization function is • Every set of Xi’s that satisfies the constraints is a feasible solution and every feasible solution that maximizes is an optimal solution. • The ship may be loaded in stages; one container per stage. At each stage we need to select container load. For this decision we may use the greedy criterion: From the remaining containers, select the one with least weight. This order of selection will keep the total weight of the selected container to minimum and hence leave
  • 11. Greedy algorithm implies that, • We first select the container that has least weight, then the one with the next smaller weight, and so on until either all containers have been loaded or there isn’t enough capacity for the next one.
  • 12. Pseudo-code for Greedy container-loading method can be written as Algorithm ContainerLoading( C, Capacity, NumberOfContainers, X) { Sort(C, NumberOfContainers); //Sort into increasing order of weight. N:=NumberOfContainsers; For i:=1 to N do X[i]:=0; //Initialixe all X to zero (Ship is vacant) i:=1 // Select containers in ascending order of weight while(i ≤ N && C[i].weight ≤ Capacity) //enough capacity for container C[i].id { X[C[i].id]:=1; // Load container to ship Capacity -=C[i].weight; // Calculate remaining load capacity of ship i++; // take next container. } }
  • 13. Suppose we want to load 8 containers of different weights (in tones) [100, 200, 50, 90, 150, 50, 20, 80] in a ship having capacity of 400 tones. Specify How many and which containers can be loaded in to ship. Here, n=8, C=400 Let weights of container are - W1, W2, W3, W4, W5, W6, W7, W8 Total Total 100, 200, 50, 90, 150, 50, 20, 80 Weight Containers X1= 1 1 1 0 0 1 0 0 = 400 4 X2= 1 0 1 0 1 0 1 1 = 400 5 X3= 0 1 1 0 1 0 0 0 = 400 3 X4= 1 0 1 1 0 1 1 1 = 390 6 : :
  • 14. • When a greedy algorithm is used, the containers are considered for loading in the order 7, 3, 6, 8, 4, 1, 5, 2. Containers 7, 3, 6, 8, 4, and 1 together weight 390 tones and are loaded. The available capacity is now only 10 tones, which is inadequate for any of the remaining containers. • In the greedy solution we have [X1, X2,…, X8]=[ 1, 0, 1, 1, 0, 1, 1, 1] and = 6. i.e. we can load maximum 6 containers into the ship with identification count 1, 3, 4, 6, 7 and 8.
  • 15. Example 4: Knapsack problem • Let, we are given n objects and a Knapsack or Bag. • Object i has weight Wi and • the Knapsack has a capacity M. • if a fraction Xi of object i is placed into Knapsack, then a profit of PiXi is earned. • The objective is to obtain a filling of Knapsack that maximizes the total profit earned.
  • 16. • Maximize (A) • Subject to (B) • • And 0 ≤ Xi ≤ 1, 1 ≤i ≤n (C) • The profit and weights are the positive numbers. • Here, A feasible solution is any set (X1, X2, …, Xn) satisfying above rules (B) and (C). • And an optimal solution is feasible solution for which rule (A) is maximized.
  • 17. • Example: Suppose, you have provided a three objects of different weights as (18, 15 and 10) with their corresponding profit values (25, 24 and 15). Also a knapsack having capacity of 20 is provided. Find out the different feasible solution and optimal solution that yield maximum profit. • Solution – Here, N=3, – M=20, – (P1, P2, P3)=(25, 24, 15) and – (W1, W2, W3)=(18, 15, 10)
  • 18. Here, N=3, M=20, (P1, P2, P3)=(25, 24, 15) and (W1, W2, W3)=(18, 15, 10) Different feasible solutions are: (X1, X2, X3) 1. (1/2, 1/3, ¼) 16.5 24.25 2. (1, 2/15, 0) 20 28.2 3. (0, 2/3, 1) 20 31 4. (0, 1, 1/2) 20 31.5 5. (1/2, 2/3, 1/ 10) 20 30 6. (1, 0, 2/10) 20 28 • Of these Six feasible solutions, solution 4 yields the maximum profit. Therefore solution 4 is optimal for the given problem instance. • Consideration 1 - In case the sum of all the weights is ≤ M, then Xi=1, 1 ≤ i ≤n is an optimal solution. • Consideration 2 - All optimal solutions will fill the knapsack exactly.
  • 19. Algorithm GreedyKnapsack( M, n) //P[1:n] and W[1:n] contains Profit & Weights { respectively of the n objects ordered such that P[i]/W[i] ≥ P[i+1]/ W[i+1]. for i:=1 to n do M is the knapsack size and X[1:n] is the feasible solution vector. X[i]:=0.0; U:=M; for i:=1 to n do { if(W[i]>U then break; X[i]:=1.0; U:=U-W[i]; } if (i≤n) then X[i]:=U/W[i]; }
  • 20. Job Sequencing with Deadlines • We are given a set of n jobs. • Di is a deadline given to complete ith job for profit Pi where Pi>0 & Di ≥ 0. • For any job i profit Pi is earned iff the job is completed within its deadline. • To complete a job, one has to process the job on a machine for one unit of time. • Only one machine is available for processing jobs. • A feasible solution for this problem is a subset J of jobs such that each job in this subset can be completed by its deadline. • The value of a feasible solution J is the sum of the profits of the job in J. • An optimal solution is a feasible solution with maximum value.
  • 21. Example –Suppose on a single machine four jobs with profit values (100, 10, 15 and 27) and their respective deadline unit values (2, 1, 2, 1) are given. Calculate the different feasible solutions to complete the jobs with optimal solution. Solution -Here, Number of Jobs (n)= 4 Profit values of four jobs (P1, P2, P3, P4)=(100, 10, 15, 27) Process deadlines for respective job are (D1, D2, D3, D4) =(2, 1, 2, 1)
  • 22. (P1, P2, P3, P4)=(100, 10, 15, 27) (D1, D2, D3, D4) =(2, 1, 2, 1) The feasible solutions and their values are – Feasible sol. subset Processing Sequence Values (1, 2) (2, 1) 110 (1, 3) (1, 3 or 3, 1) 115 (1, 4) (4, 1) 127 (3, 2) (2, 3) 25 (3, 4) (4, 3) 42 (1) (1) 100 (2) (2) 10 (3) (3) 15 (4) (4) 27 Here, Solution 3 is optimal solution. In this solution only job 1 and 4 are processed and the profit value is 127. These jobs must be processed in the order Job 4 followed by job 1. Thus the processing of job 4 begins at time zero and that of job 1 is completed at time 2.
  • 23. Single-Source Shortest Paths • Path is the way between source and destination. • The source, destination and the path were generally represented with the help of graph. • Vertices were used to represent Source or Destination and • the lines used to connect sources and destinations are called Edges. • The edges can then be assigned weights which may be either the distance between two vertices or average time to drive between two cities etc. • The graphs were generally used to answer the following questions – – Is there a path between source and destination? – If there are more than one paths from source to destination, which is the shortest path? • The length of path is the sum of weights of the edges on the path. • The starting vertex of the path is referred as Source, and the last vertex the destination.
  • 24. • Consider the directed graph , the number on the edges are the weights. • The shortest path from node 1 to other destination node are Path Length 1-4 10 1-5 25 1-2 (45,50)= 45 1-3 (45,50,60) = 45 1-6 No path If node 1 is the source vertex, then the shortest path from 1 to 2 is 1- 4-5-2. The length of this path is 10 + 15 + 20 =45. Even though there are three edges on this path, it is shorter than the path 1-2 which is of length 50.
  • 25. • The systematic way to generate the shortest paths from vertex V0 to the remaining vertices is to generate these paths in ascending order of path length. • First, a shortest path to the nearest vertex is generated. • Then a shortest path to the second nearest vertex is generated, and so on. – For the above graph the nearest vertex to V0=1 is 4 (Cost[1,4]=10). The path 1-4 is the first path generated. – The second nearest vertex to node 1 is 5 and the distance between 1to5 is 25. Then path 1-4-5 is the next path generated. – The third nearest vertex to node 1 is 2 and the distance between 1to2 is 45. Then path 1-4-5-2 is the next path generated. – The forth nearest vertex to node 1 is 3 and the distance between 1to3 is 45. Then path 1-3 is the next path generated.
  • 26. The Cost adjacency matrix for above problem is as shown bellow Destination Nodes 1 2 3 4 5 6 1 0 50 45 10 2 0 10 15 3 0 30 4 20 0 15 5 20 35 0 6 3 0 Dijkstra’s algorithm is used to determine the length of the shortest path from vertex V0 to all other vertices in graph G.
  • 27. • Suppose you have 7 containers whose weights are 50, 10, 30, 20, 70, 60, and 5 and a ship whose capacity is 110. Find an optimal solution to this instance of the container loading problem. W1, W2, W3, W4, W5, W6, W7, W8 Total Total 100, 200, 50, 90, 150, 50, 20, 80 Weight Containers X1= 1 1 1 0 0 1 0 0 = 400 4
  • 28. Find an optimal solution to the knapsack instance n=7, m=15, (P1, P2, P3,… P7)=(10, 5, 15, 7, 6, 18, 3), and (W1, W2, W3,…,W7)=(2, 3, 5, 7, 1, 4, 1). Sets Total weight Profit earned 1.(0,1,0,1,1,1,0) 15 36 2.(1,0,0,1,1,1,1) 15 44 3.(0,1,1,1,0,0,0) 15 27 4.(1,1,1,0,0,1,1) 15 51 5.(1,1,1,0,1,1,0) 15 54 6.(1,2/3,1,0,1,1,1) 15 55.33