Вы находитесь на странице: 1из 2

GREEDY ALGORITHM

A greedy algorithm is a simple, intuitive algorithm that is used in optimization problems. The
algorithm makes the optimal choice at each step as it attempts to find the overall optimal way to solve
the entire problem or we can say that greedy algorithm always makes the choice that seems to be the
best at that moment. This means that it makes a locally-optimal choice in the hope that this choice
will lead to a globally-optimal solution.

Advantages of Greedy algorithms

 It is quite easy to come up with a greedy algorithm for a problem.


 Analyzing the run time for greedy algorithms will generally be much easier than for other
techniques.

Disadvantages of Greedy algorithms


 The difficult part is that for greedy algorithms you have to work much harder to understand
correctness issues. Even with the correct algorithm, it is hard to prove why it is correct.
Proving that a greedy algorithm is correct is more of an art than a science. It involves a lot of
creativity.

 Greedy algorithms fail to find the globally optimal solution because they do not consider all the
data. The choice made by a greedy algorithm may depend on choices it has made so far, but it is
not aware of future choices it could make.
Lets take a problem to get the feel of greedy algorithm:
Being a very busy person, you have exactly T time to do some interesting things and you want to do
maximum such things.
You are given an array A of integers, where each element indicates the time a thing takes for
completion. You want to calculate the maximum number of things that you can do in the limited time
that you have.
This is a simple Greedy-algorithm problem. In each iteration, you have to greedily select the things
which will take the minimum amount of time to complete while maintaining two variables
currentTime and numberOfThings. To complete the calculation, you must:
1. Sort the array A in a non-decreasing order.
2. Select each to-do item one-by-one.
3. Add the time that it will take to complete that to-do item into currentTime.
4. Add one to numberOfThings.
Easy, isn't it?
Lets take an example where greedy will not work :
The problem is to count to a desired value by choosing the least possible coins and the greedy approach
forces the algorithm to pick the largest possible coin. If we are provided coins of ₹ 1, 2, 5 and 10 and
we are asked to count ₹ 18 then the greedy procedure will be −

 1 − Select one ₹ 10 coin, the remaining count is 8

 2 − Then select one ₹ 5 coin, the remaining count is 3

 3 − Then select one ₹ 2 coin, the remaining count is 1

 4 − And finally, the selection of one ₹ 1 coins solves the problem


Though, it seems to be working fine, for this count we need to pick only 4 coins. But if we slightly
change the problem then the same approach may not be able to produce the same optimum result.
For the currency system, where we have coins of 1, 7, 10 value, counting coins for value 18 will be
absolutely optimum but for count like 15, it may use more coins than necessary. For example, the
greedy approach will use 10 + 1 + 1 + 1 + 1 + 1, total 6 coins. Whereas the same problem could be
solved by using only 3 coins (7 + 7 + 1)
Hence, we may conclude that the greedy approach picks an immediate optimized solution and may fail
where global optimization is a major concern.
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.

 Travelling Salesman Problem


 Graph - Map Coloring
 Graph - Vertex Cover
 Knapsack Problem
 Job Scheduling Problem
Conclusion
Greedy algorithms are usually easy to think of, easy to implement and run fast. Proving their
correctness may require rigorous mathematical proofs and is sometimes insidious hard. In addition,
greedy algorithms are infamous for being tricky. Missing even a very small detail can be fatal. But
when you have nothing else at your disposal, they may be the only salvation. With backtracking or
dynamic programming you are on a relatively safe ground. With greedy instead, it is more like walking
on a mined field. Everything looks fine on the surface, but the hidden part may backfire on you when
you least expect. While there are some standardized problems, most of the problems solvable by this
method call for heuristics. There is no general template on how to apply the greedy method to a given
problem, however the problem specification might give you a good insight.

Вам также может понравиться