0% found this document useful (0 votes)
6 views

Greedy Approach

The document explains the greedy algorithm, which solves optimization problems by making the best immediate choice without considering the overall outcome. It outlines the characteristics of greedy algorithms, provides examples such as the knapsack problem, and describes the process of selecting items based on their value-to-weight ratio. Additionally, it discusses the fractional knapsack problem and demonstrates how to achieve optimal solutions using the greedy approach.

Uploaded by

Sneha Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Greedy Approach

The document explains the greedy algorithm, which solves optimization problems by making the best immediate choice without considering the overall outcome. It outlines the characteristics of greedy algorithms, provides examples such as the knapsack problem, and describes the process of selecting items based on their value-to-weight ratio. Additionally, it discusses the fractional knapsack problem and demonstrates how to achieve optimal solutions using the greedy approach.

Uploaded by

Sneha Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Greedy Approach

A greedy algorithm is an approach for solving a problem by selecting the best


option available at the moment. It doesn't worry whether the current best result
will bring the overall optimal result. The algorithm never reverses the earlier
decision even if the choice is wrong. It works in a top-down approach. This
algorithm may not produce the best result for all the problems. It's because it
always goes for the local best choice to produce the global best result.

Greedy Algorithm is defined as a method for solving optimization problems by


taking decisions that result in the most evident and immediate benefit irrespective
of the final outcome. It works for cases where minimization or maximization
leads to the required solution.
Why choose Greedy Approach?
The greedy approach has a few trade offs, which may make it suitable for
optimization. One prominent reason is to achieve the most feasible solution
immediately. In the activity selection problem, if more activities can be done
before finishing the current activity, these activities can be performed within the
same time. Another reason is to divide a problem recursively based on a
condition, with no need to combine all the solutions. In the activity selection
problem, the “recursive division” step is achieved by scanning a list of items only
once and considering certain activities.

Characteristics of Greedy algorithm


For a problem to be solved using the Greedy approach, it must follow a few
major characteristics:
 There is an ordered list of resources(profit, cost, value, etc.)
 Maximum of all the resources(max profit, max value, etc.) are taken.
 For example, in the fractional knapsack problem, the maximum
value/weight is taken first according to available capacity.

Greedy Algorithm
To begin with, the solution set (containing answers) is empty.
1. At each step, an item is added to the solution set until a solution is
reached.
2. If the solution set is feasible, the current item is kept.
3. Else, the item is rejected and never considered again.
Problem: You have to make a change of an amount using the smallest possible number of
coins.
Amount: $18
Available coins are
$5 coin
$2 coin
$1 coin

Solution:
1. Create an empty solution-set = { }. Available coins are {5, 2, 1}.
2. We are supposed to find the sum = 18. Let's start with sum = 0.
3. Always select the coin with the largest value (i.e. 5) until the sum > 18. (When we
select the largest value at each step, we hope to reach the destination faster. This
concept is called greedy choice property.)
4. In the first iteration, solution-set = {5} and sum = 5.
5. In the second iteration, solution-set = {5, 5} and sum = 10.
6. In the third iteration, solution-set = {5, 5, 5} and sum = 15.
7. In the fourth iteration, solution-set = {5, 5, 5, 2} and sum = 17. (We cannot select 5
here because if we do so, sum = 20 which is greater than 18. So, we select the 2nd
largest item which is 2.)
8. Similarly, in the fifth iteration, select 1. Now sum = 18 and solution-set = {5, 5, 5, 2,
1}.

Different Types of Greedy Algorithm


 Knapsack Problem
 Minimum Spanning Tree
 Single-Source Shortest Path Problem
 Job Scheduling Problem
 Prim's Minimal Spanning Tree Algorithm
 Kruskal's Minimal Spanning Tree Algorithm
 Dijkstra's Minimal Spanning Tree Algorithm
 Huffman Coding
Knapsack Problem Variants-
Knapsack problem has the following two variants-
1. Fractional Knapsack Problem
2. 0/1 Knapsack Problem

Fractional Knapsack Problem


The fractional knapsack problem is also one of the techniques which are used to
solve the knapsack problem. In fractional knapsack, the items are broken in order
to maximize the profit. The problem in which we break the item is known as a
Fractional knapsack problem.
There are basically three approaches to solve the problem:
o The first approach is to select the item based on the maximum profit.
o The second approach is to select the item based on the minimum weight.
o The third approach is to calculate the ratio of profit/weig ht.

What is Knapsack Problem Using Greedy Method?

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.

Problem-
For the given set of items and knapsack capacity = 60 kg, find the optimal solution
for the fractional knapsack problem making use of greedy approach.

Item Weight Value


1 5 30
2 10 40
3 15 45
4 22 77
5 25 90

OR
Find the optimal solution for the fractional knapsack problem making use of greedy
approach. Consider-
n=5
w = 60 kg
(w1, w2, w3, w4, w5) = (5, 10, 15, 22, 25)
(b1, b2, b3, b4, b5) = (30, 40, 45, 77, 90)
Solution-
Step-01:
Compute the value / weight ratio for each item-
Step-02:

Sort all the items in decreasing order of their value / weight ratio-

I1 I2 I5 I4 I3
(6) (4) (3.6) (3.5) (3)
Step-03:
Start filling the knapsack by putting the items into it one by one.

Knapsack Items Cost


Weight in Knapsack
60 Ø 0
55 I1 30
45 I1, I2 70
20 I1, I2, I5 160

Now,
 Knapsack weight left to be filled is 20 kg but item-4 has a weight of 22 kg.
 Since in fractional knapsack problem, even the fraction of any item can be
taken.
 So, knapsack will contain the following items-
< I1 , I2 , I5 , (20/22) I4 >

Total cost of the knapsack


= 160 + (20/22) x 77
= 160 + 70
= 230 units
0
Problem-
Consider the knapsack instances, the capacity of the knapsack is 15, and 7
object has profit and weight as follows:
Object X1 X2 X3 X4 X5 X6 X7
Weight 2 3 5 7 1 4 1
Profit 10 5 15 7 6 18 3
Which object is partially placed in the knapsack using the fractional knapsack
problem?
1. X4
2. X2
3. X1
4. X3
Answer: Option B
Solution: Let us first find each object's profit by weight ratio.
object X1 X2 X3 X4 X5 X6 X7
Profit/weight 5 1.66 3 1 6 4.5 3
Now we will arrange the profit by weight ratio in descending order and pick the
objects accordingly.
X5 X1 X6 X7 X3 X2 X4
6 5 4.5 3 3 1.6 1
The total capacity of the knapsack = 15
After selecting X5 capacity = 15-1=14
After selecting X1 capacity = 14-2=12
After selecting X6 capacity = 12-4=8
After selecting X7 capacity = 8-1=7
After selecting X3 capacity = 7-5=2
After selecting X2 capacity = 2-(2/3)*3=0
Thus we are taking only 2/3rd weight of the object X2
We take fractional parts from X2.
Therefore, option B is correct.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy