Greedy Algorithm
Greedy Algorithm
complexity
GREEDY ALGORITHMS
Introduction
Let us start our discussion simple theory that will give us an understanding of the greedy
technique. In the game of Cwithhess, every time we make a decision 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 Strategy
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 assume 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: this property saya that the globally optimal solution can be
obtained by making a locally optimal solution(greedy). The choice made by a greedy
algorithm may depend on earlier choices but not on the future. It iteratively make one
greedy choice after another and reduces the given problem to a smaller one.
2. 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.
Does greegy always work?
Making locally optimal choices does not always work. Hence, greedy algorithms will not always
give the best solution. We will see particular examples.
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 make a decision, we do not have to spend time re-
examining the already computed values. Its main disadvantage is that for many problems there
is nogreedy 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
Greedy applications
Sorting: selection sort, Topological sort;
Pririty queues: Heap sort;
Huffman coding compression algorithm;
Prim’s and Kruskal’s algorithms;
Shortest path in weighted graph (Dijkstra’s)
Coin change problem;
Fractional Knapsak problem
Job scheduling algorithm
Greedy techniques can be used as an approximation algorithm for complex problems
Understanding greedy technique
For better understanding, let us go through an example
Understanding greedy technique
For better understanding, let us go through an example