TDesign and Analysis 4
TDesign and Analysis 4
Chapter 5
Greedy Algorithms
The greedy algorithm consists of four functions.
i. Solution Function:- A function that checks whether chosen set of items provides a solution.
ii. Feasible Function:- A function that checks the feasibility of a set.
iii. Selection Function:- The selection function tells which of the candidates is the most
promising.
iv. Objective Function:- An objective function, which does not appear explicitly, but gives the
value of a solution.
Knapsack Problem
Knapsack Problem
Fractional Knapsack Problem
Introduction
We are given objects and a knapsack.
Object has a positive weight and a positive value for
The knapsack can carry a weight not exceeding .
Our aim is to fill the knapsack in a way that maximizes the value of the included
objects, while respecting the capacity constraint.
In a fractional knapsack problem, we assume that the objects can be broken into
smaller pieces.
So we may decide to carry only a fraction of object , where
In this case, object contribute to the total weight in the knapsack, and to the value of
the load.
Symbolic Representation of the problem can be given as follows:
maximize subject to
Where, and for .
Fractional Knapsack Problem - Example
We are given objects and the weight carrying capacity of
knapsack is .
For each object, weight and value are given in the following table.
Object
Fill the knapsack with given objects such that the total value of
knapsack is maximized.
Fractional Knapsack Problem - Greedy Solution
Three Selection Functions can be defined as,
1. Sort the items in descending order of their values and
select the items till weight criteria is satisfied.
2. Sort the items in ascending order of their weight and
select the items till weight criteria is satisfied.
3. To calculate the ratio value/weight for each item and sort
the item on basis of this ratio. Then take the item with the
highest ratio and add it.
Fractional Knapsack Problem - Greedy Solution
Object
Solution:
Step Sort
1: the jobs in decreasing order of their profit.
Job
Profit
Deadline
Job Scheduling with Deadlines - Example
Job
Profit
Deadline
P 1 2 3
Job selected J2 0 J1
P 1 2 3
Job selected J2 J4 J1