GREEDY ALGORITHMS
A greedy algorithm, as the name suggests, 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.
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.
How do you decide which choice is optimal?
Assume that you have an objective function that needs to be optimized (either maximized or minimized) at a given point. A Greedy algorithm makes greedy choices at each step to ensure that the objective function is optimized. The Greedy algorithm has only one shot to compute the optimal solution so that it never goes back and reverses the decision.
Consider the graph which is given below:

We have to travel from the source to the destination at the minimum cost. Since we have three feasible solutions having cost paths as 10, 20, and 5. 5 is the minimum cost path so it is the optimal solution. This is the local optimum, and in this way, we find the local optimum at each stage in order to calculate the global optimal solution.
Structure of a Greedy Algorithm
Greedy algorithms take all of the data in a particular problem, and then set a rule for which elements to add to the solution at each step of the algorithm. In the animation above, the set of data is all of the numbers in the graph, and the rule was to select the largest number available at each level of the graph. The solution that the algorithm builds is the sum of all of those choices.
Advantages and Disadvantages
- It is quite easy to come up with a greedy algorithm (or even multiple greedy algorithms) for a problem.
- Analyzing the run time for greedy algorithms will generally be much easier than for other techniques (like Divide and conquer). For the Divide and conquer technique, it is not clear whether the technique is fast or slow. This is because at each level of recursion the size of gets smaller and the number of sub-problems increases.
- 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
- This algorithm can perform better than other algorithms (but, not in all cases).
- The greedy algorithm doesn't always produce the optimal solution.
Where to use Greedy algorithms?
A problem must comprise these two components for a greedy algorithm to work:
It has optimal substructures. The optimal solution for the problem contains optimal solutions to the sub-problems.
It has a greedy property (hard to prove its correctness!). If you make a choice that seems the best at the moment and solve the remaining sub-problems later, you still reach an optimal solution. You will never have to reconsider your earlier choices
Pseudo code of Greedy Algorithm
Greedy approach
Steps for using Greedy Algorithm
- To begin with, the solution set (containing answers) is empty.
- At each step, an item is added to the solution set until a solution is reached.
- If the solution set is feasible, the current item is kept.
- Else, the item is rejected and never considered again.
Different Types of Greedy Algorithm
- Selection sort-Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list.
Working of Selection Sort
- Set the first element as
minimum. - Compare
minimumwith the second element. If the second element is smaller thanminimum, assign the second element asminimum.
Compareminimumwith the third element. Again, if the third element is smaller, then assignminimumto the third element otherwise do nothing. The process goes on until the last element. - After each iteration,
minimumis placed in the front of the unsorted list - For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated until all the elements are placed at their correct position
- Knapsack Problem- knapsack problem is a problem in combinatorial
optimization: Given a set of items, each with a weight and a value, determine
the number of each item to include in a collection so that the total
weight is less than or equal to a given limit and the total value is as
large as possible.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?
· In this context, 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
- Knapsack
- Minimum Spanning Tree-A minimum spanning tree is a spanning tree in
which the sum of the weight of the edges is as minimum as possible.
- Single-Source Shortest Path Problem-problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
- Huffman Coding-technique of compressing data to reduce its
size without losing any of the details. It was first developed by David
Huffman.
Huffman Coding is generally useful to compress the
data in which there are frequently occurring characters.
- Ford-Fulkerson Algorithm-a greedy approach for calculating the
maximum possible flow in a network or a graph.
APPLICATIONS OF GREEDY ALGORITHMS
- Greedy algorithms typically (but not always)
fail to find the globally optimal solution because they usually do not
operate exhaustively on all the data. They can make commitments to certain
choices too early, preventing them from finding the best overall solution
later. For example, all known greedy coloring algorithms
for the graph coloring
problem and all other NP-complete problems do not
consistently find optimum solutions. Nevertheless, they are useful because
they are quick to think up and often give good approximations to the
optimum.
- If a greedy algorithm can be proven to yield
the global optimum for a given problem class, it typically becomes the
method of choice because it is faster than other optimization methods
like dynamic programming.
Examples of such greedy algorithms are Kruskal's algorithm and Prim's algorithm for
finding minimum spanning
trees and the algorithm for finding optimum Huffman trees.
- Greedy algorithms appear in the network routing as well. Using greedy routing,
a message is forwarded to the neighbouring node which is
"closest" to the destination. The notion of a node's location
(and hence "closeness") may be determined by its physical
location, as in geographic routing used
by ad hoc networks.
Location may also be an entirely artificial construct as in small world routing and distributed hash
table

Comments
Post a Comment