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

Greedy Method

{ Knapsack Problem
The selection of some things, each with profit and weight values, to be
packed into one or more knapsacks with capacity is the fundamental idea
behind all families of knapsack problems. The knapsack problem had two
versions that are as follows:

 Fractional Knapsack Problem


 0 /1 Knapsack Problem

Knapsack Problem Using Greedy Method


Problem
In this method, the Knapsack's filling is done so that the maximum capacity of
the knapsack is utilized so that maximum profit can be earned from it. The
knapsack problem using the Greedy Method is referred to as:

Given a list of n objects, say {I1, I2,……, In) and a knapsack (or bag).

The capacity of the knapsack is M.

Each object Ij has a weight wj and a profit of pj

If a fraction xj (where x ∈ {0...., 1)) of an object Ij is placed into a knapsack,


then a profit of pjxj is earned.

The problem (or Objective) is to fill the knapsack (up to its maximum capacity
M), maximizing the total profit earned.
Note that the value of xj will be any value between 0 and 1 (inclusive).
If any object Ij is completely placed into a knapsack, its value is 1 (xj = 1).
If we do not pick (or select) that object to fill into a knapsack, its value is 0 ( x j = 0).
Otherwise, if we take a fraction of any object, then its value will be any value
between 0 and 1.
Knapsack Problem Using Greedy Method Pseudocode

A pseudo-code for solving knapsack problems using the greedy method is;

greedy fractional-knapsack (P[1...n], W[1...n], X[1..n]. M)

/*P[1...n] and W[1...n] contain the profit and weight of the n-objects ordered such that
X[1...n] is a solution set and M is the capacity of knapsack*/

{
For j ← 1 to n do
X[j]← 0

profit ← 0 // Total profit of item filled in the knapsack


weight ← 0 // Total weight of items packed in knapsacks

j←1
While (Weight < M) // M is the knapsack capacity
}
Job Sequencing Problem

{
Greedily choose the jobs with maximum profit first, by
sorting the jobs in decreasing order of their profit. This
would help to maximize the total profit as choosing the job
with maximum profit for every time slot will eventually
maximize the total profit
Follow the given steps to solve the problem:

 Sort all jobs in decreasing order of profit. 

 Iterate on jobs in decreasing order of profit.For each job , do the


following : 

 Find a time slot i, such that slot is empty and i < deadline and i
is greatest. Put the job in  this slot and mark this slot filled. 

 If no such i exists, then ignore the job. 


Input: Four Jobs with following deadlines and profits

Job id deadline profit

 a      4          20   
 b      1          10
 c      1          40  
 d      1          30

Output: Following is maximum profit sequence of jobs: c, a   


JobID   Deadline  Profit
  a            2          100
  b            1          19
  c            2          27
 d            1          25
 e            3          15

Output: Following is maximum profit sequence of jobs: c, a, e


Algorithm: Job-Sequencing-With-Deadline (D, J, n, k)

D(0) := J(0) := 0
k := 1
J(1) := 1 // means first job is selected
for i = 2 … n do
r := k
while D(J(r)) > D(i) and D(J(r)) ≠ r do
r := r – 1
if D(J(r)) ≤ D(i) and D(i) > r then
for l = k … r + 1 by -1 do
J(l + 1) := J(l)
J(r + 1) := i
k := k + 1

The complexity of this algorithm is O(n2).


Feature Greedy method Dynamic programming

In Dynamic Programming we make decision


In a greedy Algorithm, we make whatever choice seems
at each step considering current problem and
Feasibility  best at the moment in the hope that it will lead to global
solution to previously solved sub problem to
optimal solution.
calculate optimal solution .

It is guaranteed that Dynamic Programming


In Greedy Method, sometimes there is no such guarantee will generate an optimal solution as it
Optimality
of getting Optimal Solution. generally considers all possible cases and
then choose the best.

A Dynamic programming is an algorithmic


A greedy method follows the problem solving heuristic technique which is usually based on a
Recursion
of making the locally optimal choice at each stage. recurrent formula that uses some previously
calculated states.

It requires Dynamic Programming table for


Memoization                               It is more efficient in terms of memory as it never look
Memoization and it increases it’s memory
     back or revise previous choices
complexity.

Greedy methods are generally faster. For example,  Dynamic Programming is generally slower.
    Time        complexity              
Dijkstra’s shortest path algorithm takes O(ELogV + For example, Bellman Ford algorithm takes
   
VLogV) time. O(VE) time.

The greedy method computes its solution by making its Dynamic programming computes its solution
Fashion choices in a serial forward fashion, never looking back bottom up or top down by synthesizing them
or revising previous choices. from smaller optimal sub solutions.

Fractional knapsack . 
Example 0/1 knapsack problem
 

You might also like