0% found this document useful (0 votes)
26 views6 pages

Understanding Greedy Method in DAA

Uploaded by

anil shinde
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)
26 views6 pages

Understanding Greedy Method in DAA

Uploaded by

anil shinde
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

Page-1

Chapter-3
Greedy Method

What is Greedy Strategy?


Greedy algorithms are like dynamic programming algorithms that are often used to solve
optimal problems (find best solutions of the problem according to a particular criterion).

Greedy algorithms build a solution part by part, choosing the next part in such a way, that it
gives an immediate benefit. This approach never reconsiders the choices taken previously. This
approach is mainly used to solve optimization problems.

In many problems, it does not produce an optimal solution though it gives an approximate
(near optimal) solution in a reasonable time.

There are two critical components of greedy decisions:

1. Way of greedy selection: Select which solution is best at present and then solve the
subproblem arising from making the last selection. The selection of greedy algorithms
may depend on previous selections. But it cannot depend on any future selection or
depending on the solutions of subproblems.

The algorithm evolves in a way that makes selections in a loop, at the same time
shrinking the given problem to smaller subproblems.

2. Optimal substructure: You perform the optimal substructure for a problem if the
optimal solution of this problem contains optimal solutions to its subproblems.

Components of Greedy Algorithm

Greedy algorithms have the following five components −


 A candidate set − A solution is created from this set.
 A selection function − Used to choose the best candidate to be added to the solution.
 A feasibility function − Used to determine whether a candidate can be used to
contribute to the solution.
 An objective function − Used to assign a value to a solution or a partial solution.
 A solution function − Used to indicate whether a complete solution has been reached.
 Page-2

Areas of Application

Greedy approach is used to solve many problems, such as


 Finding the shortest path between two vertices using Dijkstra’s algorithm.
 Finding the minimal spanning tree in a graph using Prim’s /Kruskal’s algorithm, etc.

Fractional Knapsack
The Greedy algorithm could be understood very well with a well-known problem referred to as
Knapsack problem.
Although the same problem could be solved by employing other algorithmic
approaches(Dynamic programming), Greedy approach solves Fractional Knapsack problem
reasonably in a good time.

Knapsack Problem

 Given a set of items, each with a weight and a value


 Determine a subset of items to include in a collection so that the total weight is less
than or equal to a given limit and the total profit value is as large as possible.
 The knapsack problem is in combinatorial optimization problem.

Applications

 Finding the least wasteful way to cut raw materials


 Cutting stock problems

Problem Scenario
A thief is robbing a store and can carry a maximal weight of W into his knapsack. There are n
items available in the store and weight of ith item is wi and its profit is pi. What items should
the thief take?
The items should be selected in such a way that the thief will carry those items for which he
will gain maximum profit. Hence, the objective of the thief is to maximize the profit.
Based on the nature of the items, Knapsack problems are categorized as

 Fractional Knapsack(Greedy Method)


 0/1 Knapsack(Dynamic Programming)
 Page-3

Fractional Knapsack

 Items can be broken into smaller pieces


 Thief can select fractions of items.
According to the problem statement,
 There are n items in the store
 Weight of ith item wi>0
 Profit for ith item pi>0 and
 Capacity of the Knapsack is W
In this Knapsack problem, items can be broken into smaller pieces. So, the thief may take only
a fraction xi of ith item.
0⩽xi⩽1

The ith item contributes the weight xi, wi to the total weight in the knapsack and profit xi,pi to
the total profit.
Hence, the objective of this algorithm is to

It is clear that an optimal solution must fill the knapsack exactly; otherwise we could add a
fraction of one of the remaining items and increase the overall profit.
Page-4

Algorithm: Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)


for i = 1 to n
do x[i] = 0
weight = 0
for i = 1 to n
if weight + w[i] ≤ W then
x[i] = 1
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
break
return x

Analysis
If the provided items are already sorted into a decreasing order of pi/wi then the
whileloop takes a time in O(n); Therefore, the total time including the sort is
in O(n logn).
Page-5

Solution : Sorting all the items according to pi/wi.


First B is chosen as weight of B is less than the capacity of the knapsack. (Summation of Wi:10)
Next, item A is chosen, as the available capacity of the knapsack is greater than the weight
of A. (Summation of Wi: 10+40=50)
Now, C is chosen as the next item. (Summation of Wi: 10+40+20=70 which is exceeding the
bag capacity)
Therefore whole item cannot be chosen as the remaining capacity of the knapsack is less than
the weight of C. (Remaining capacity is 10)
Hence, fraction of C is chosen. The total weight of the selected items is 10 + 40 + 20 * (10/20)
= 60
Page-6

Now, the capacity of the Knapsack is equal to the selected items. Hence, no more items can be
selected.

And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440
This is the optimal solution. We cannot gain more profit selecting any different combination of
items.

Common questions

Powered by AI

The Greedy Method produces an accurate, albeit possibly not optimal, solution quickly due to its ability to make immediate, locally optimal decisions at each step without reconsidering prior choices. This simplicity and directness reduce computational overhead significantly, allowing problems to be solved in a much shorter time frame, especially when problems are large in scale or computational resources are limited . This efficiency is particularly beneficial in applications like sorting items by a heuristic value such as the value-to-weight ratio in the Fractional Knapsack problem, where the timely production of good solutions is valued over achieving absolute optimality .

The Greedy Method, specifically used in the Fractional Knapsack problem, allows items to be broken into smaller pieces, enabling the solution to consider fractions of items for maximizing profit. It makes local, immediate decisions to optimize the ratio of profit to weight for each item, leading to an approximate solution in a reasonable time . In contrast, Dynamic Programming is used in the 0/1 Knapsack problem where items cannot be divided and are either entirely included or excluded. Dynamic Programming evaluates the complete solution space and builds the solution by considering all possible combinations of items, ensuring an optimal solution by solving subproblems and combining their solutions efficiently .

The Fractional Knapsack problem is optimally solved using the Greedy Method in scenarios where items can be broken into smaller, fractional parts. This allows for maximizing theoretical profit by selecting portions of items based on their value-to-weight ratio. The Greedy Method efficiently handles such scenarios by sorting items in decreasing order of this ratio and then adding them sequentially to the knapsack until the capacity is exhausted or only a fractional part of the last item can be fitted. This method ensures that the profit is maximized up to the capacity constraint . It is important that items can actually be divided; otherwise, the problem turns into a 0/1 Knapsack, which requires different approaches like Dynamic Programming .

While the Greedy approach is efficient, particularly in terms of computational speed and simplicity, its major limitation lies in its inability to guarantee globally optimal solutions for problems that do not inherently possess the optimal substructure and greedy choice properties. It operates on the principle of making the best choice at every step, leading to suboptimal results in problems where future choices depend on current decisions or when the problem context requires a more holistic view of potential solutions. Furthermore, it cannot backtrack to revise choices when faced with better alternatives discovered retrospectively, and it often disregards interactions between components which might collectively yield better outcomes, a critical consideration in complex problem spaces .

Sorting items by their value-to-weight ratio in decreasing order is crucial for effectively solving the Fractional Knapsack problem with the Greedy Method because it ensures that the algorithm considers adding the most valuable items first relative to their weight. This prioritization is essential for maximizing total profit, as each subsequent selection is locally optimal in terms of value per unit weight. Once sorted, the algorithm adds each item in sequence, filling the knapsack until capacity is reached, at which point only a fractional part of the least valuable remaining item (by weight) may be added. This sorting step, combined with the greedy selection process, ensures the method's overall efficiency and profitability .

The solution function in a greedy algorithm determines when a complete and acceptable solution has been reached. It checks whether the constructed sequence of choices satisfies the criteria for completeness within the context of the specific problem—such as filling the knapsack up to its capacity without exceeding it in the Knapsack problem . This function essentially signals the algorithm's termination, ensuring that no further candidates are added once a solution that meets all constraints and optimizes the objective function is achieved. This ensures efficiency by preventing unnecessary operations once a valid solution has been established .

The Greedy Strategy is prone to failure in producing optimal solutions because it relies on making local, immediate decisions that seem best at each step, often without considering the broader implications of those decisions on the overall solution. This approach does not reconsider past decisions nor does it account for future possibilities, which can result in missing out on a globally optimal configuration. Greedy algorithms are designed to find an approximate solution quickly, which is useful in situations where finding the exact optimal solution is less critical than speed or where computational resources are limited. In particular, it is most effective when the problem exhibits optimal substructure and greedy choice properties .

The feasibility function in a greedy algorithm guarantees the correctness of solutions by evaluating whether the current candidate can be included in the solution without violating any constraints. Specifically, it checks that the addition remains within permissible bounds, such as not exceeding the knapsack’s weight limit in the Knapsack problem. This function applPublishes constraints that ensure the solution remains viable and adheres to problem-specific laws or conditions. By rigorously applying these constraints at each step, the algorithm avoids making invalid choices that would undermine the integrity and correctness of the overall solution, thus ensuring that the final result is coherent and logically sound within the given framework .

The notion of 'optimal substructure' is integral to the design of greedy algorithms as it posits that an optimal solution to the entire problem can be constructed efficiently from optimal solutions of its subproblems. This property allows greedy algorithms to build a solution incrementally by solving and combining solutions to smaller subproblems, each of which independently adheres to optimal criteria . It ensures that making the optimal choice at each step leads to an overall effective resolution, as opposed to needing to explore every possible combination of choices. Problems that exhibit this property are naturally suited for a greedy approach, as they allow for simplification through decomposition and synthesis, aligning well with greedy algorithm strategies .

A greedy algorithm consists of five primary components: a candidate set, a selection function, a feasibility function, an objective function, and a solution function. The candidate set is the pool from which potential elements of the solution are chosen. The selection function identifies the most promising candidate to be added to the existing solution based on an immediate benefit criterion. The feasibility function ensures that adding a candidate maintains the solution’s viability. The objective function assigns a value to each partial or complete solution, guiding the greedy choices toward maximizing this value. Finally, the solution function determines when a full solution has been reached, at which point the algorithm terminates . These components collectively enable greedy algorithms to quickly converge on a sufficiently good, albeit not necessarily optimal, solution by continuously making local, myopically optimal choices .

You might also like