Greedy Algorithms
Let us start our discussion with simple theory that will give us an understanding of
the Greedy technique. In the game of Chess, every time we decide about a move, we
have to also think about the future consequences. Whereas, in the game of Tennis (or
Volleyball), our action is based on the immediate situation. This means that in some
cases making a decision that looks right at that moment gives the best solution
(Greedy), but in other cases it doesn’t. The Greedy technique is best suited for looking
at the immediate situation. Greedy algorithms work in stages. In each stage, a decision
is made that is good at that point, without bothering about the future. This means that
some local best is chosen. It assumes that a local good selection makes for a global
optimal solution.
Elements of Greedy Algorithms
The two basic properties of optimal Greedy algorithms are:
1. Greedy choice property
2. Optimal substructure
• Greedy choice property
This property says that the globally optimal solution can be obtained by making a
locally optimal solution (Greedy). At each stage a local optimal solution is chosen with
the hope of achieving a global optimum. The choice made by a Greedy algorithm may
depend on earlier choices but not on the future. It iteratively makes one Greedy choice
after another and reduces the given problem to a smaller one.
• Optimal substructure
A problem exhibits optimal substructure if an optimal solution to the problem
contains optimal solutions to the subproblems. That means we can solve subproblems
and build up the solutions to solve larger problems.
Advantages and Disadvantages of Greedy Method
The main advantage of the Greedy method is that it is straightforward, easy to
understand and easy to code. In Greedy algorithms, once we decide, we do not have
to spend time re-examining the already computed values. Its main disadvantage is
that for many problems there is no greedy algorithm. That means, in many cases there
is no guarantee that making locally optimal improvements in a locally optimal
solution gives the optimal global solution.
Example of Greedy Algorithm
Problem Statement: Find the best route to reach the destination city from the given
starting point using a greedy method.
Greedy Solution: In order to tackle this problem, we need to maintain a graph
structure. And for that graph structure, we'll have to create a tree structure, which will
serve as the answer to this problem. The steps to generate this solution are given
below:
• Start from the source vertex.
• Pick one vertex at a time with a minimum edge weight (distance) from the
source vertex.
• Add the selected vertex to a tree structure if the connecting edge does not form
a cycle.
• Keep adding adjacent fringe vertices to the tree until you reach the destination
vertex.
The animation given below explains how paths will be picked up in order to reach the
destination city.
.