Professional Documents
Culture Documents
Greedy Method
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:
Given a list of n objects, say {I1, I2,……, In) and a knapsack (or bag).
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;
/*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
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:
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.
a 4 20
b 1 10
c 1 40
d 1 30
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
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