LEC 10 Greedy Approach
LEC 10 Greedy Approach
Greedy Approach
Greedy Approach
CS_GATE@ANKUSH_SAKLECHA 3
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Method
CS_GATE@ANKUSH_SAKLECHA 10
Greedy Approach
Algorithm Greedy (A,n)
{
// A[1….n] contains the n inputs solution
// initialize solution to empty
for i =1 to n do
x = SELECT(A);
if feasible(solution,x) then
solution = Union(solution,x);
end if
repeat
return (solution);
}
➢ The function SELECT() selects an input from A and assign its value to x.
➢ feasible() is a boolean valued function which determine if x can be included into
the solution vector.
➢ Union() combine x with solution and updates the objective function.
CS_GATE@ANKUSH_SAKLECHA 11
Greedy Approach
Problems where we can apply greedy approach :
1. Knapsack problem
2. Job sequencing with deadline
3. Optimal merge pattern
4. Huffman code
5. Minimum cost spanning tree
(a) Kruskal’s Algorithm
(b) Prim’s Algorithm
(c) Boruvka’s algorithm
6. Single source shortest path algorithm(Dijkstra's algorithm).etc
CS_GATE@ANKUSH_SAKLECHA 12
Greedy Approach
1. Knapsack Problem:
Total n objects are there and each object has some weight and profit corresponding to it
and one knapsack of size M.
Problem Statement: we need to choose objects and fill it into knapsack such that overall
weight should be less than or equal to size of knapsack and need to earn maximum
profit.
Hence,
The objective is maximize
𝒏
𝒙𝒊 ∗ 𝒑𝒊
𝒏=𝟏
subject to constraint,
𝒏
𝒙𝒊 ∗ 𝒘𝒊 ≤ 𝑴
𝒏=𝟏
and 0 <= xi <= 1
pi = profit of object i
wi = weight of object i
xi = portion of object i
13
CS_GATE@ANKUSH_SAKLEC
Greedy Approach
Find maximum profit. Size of knapsack is 60.
CS_GATE@ANKUSH_SAKLECHA 14
Greedy Approach
Find maximum profit. Size of knapsack is 60.
CS_GATE@ANKUSH_SAKLECHA 15
Greedy Approach
Algorithm Greedy(m,n)
{ // Assume all objects arrange according to highest profit/weight ratio first.
for(i=1 to n do 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];
}
CS_GATE@ANKUSH_SAKLECHA 16
Greedy Approach
Algorithm Greedy(m,n)
{ // Assume all objects arrange according to highest profit/weight ratio first.
for(i=1 to n do 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];
}
CS_GATE@ANKUSH_SAKLECHA 17
Greedy Approach
Time Complexity-
• The main time taking step is the sorting of all items in decreasing order of their value /
weight ratio.
• If the items are already arranged in the required order, then while loop takes O(n) time.
• The average time complexity of Quick Sort is O(nlogn).
• Therefore, total time taken including the sort is O(nlogn)
CS_GATE@ANKUSH_SAKLECHA 18
Greedy Approach
CS_GATE@ANKUSH_SAKLECHA 19
Greedy Approach
CS_GATE@ANKUSH_SAKLECHA 20
Greedy Approach
Job Sequencing with deadline :
The sequencing of jobs on a single processor with deadline constraints is called as Job Sequencing with Deadlines.
Here-
➢ You are given a set of jobs.
➢ Each job has a defined deadline and some profit associated with it.
➢ The profit of a job is given only when that job is completed within its deadline.
➢ Only one processor is available for processing all the jobs.
➢ Processor takes one unit of time to complete a job.
CS_GATE@ANKUSH_SAKLECHA 21
Greedy Approach
JOB J1 J2 J3 J4
PROFIT 100 40 50 25
DEADLINE 2 1 1 2
JOB J1 J2 J3 J4
PROFIT 100 40 50 25
DEADLINE 2 1 2 1
CS_GATE@ANKUSH_SAKLECHA 22
Greedy Approach
JOB J1 J2 J3 J4
PROFIT 100 40 50 25
DEADLINE 2 1 1 2
JOB J1 J2 J3 J4
PROFIT 100 40 50 25
DEADLINE 2 1 2 1
CS_GATE@ANKUSH_SAKLECHA 23
Greedy Approach
Given the jobs, their deadlines and associated profits as shown-
CS_GATE@ANKUSH_SAKLECHA 24
Greedy Approach
CS_GATE@ANKUSH_SAKLECHA 25
Greedy Approach
CS_GATE@ANKUSH_SAKLECHA 26
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach
Greedy Approach