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:

Greedy Algorithm

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

  1. It is quite easy to come up with a greedy algorithm (or even multiple greedy algorithms) for a problem.
  2. 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.
  3. 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
  4. This algorithm can perform better than other algorithms (but, not in all cases).
  5. 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:

  1. It has optimal substructures. The optimal solution for the problem contains optimal solutions to the sub-problems.

  2. 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

Algorithm Greedy (a, n) { 
Solution : = 0; 
for i = 0 to n do { 
x: = select(a); 
if feasible(solution, x) 
{ 
Solution: = union(solution , x)
 } return solution;
 } }

Greedy approach


1. Let's start with the root node 20. The weight of the right child is 3 and the weight of the left child is 2. Our problem is to find the largest path. And, the optimal solution at the moment is 3. So, the greedy algorithm will choose 
3. Finally the weight of an only child of 3 is 1. This gives us our final result 20 + 3 + 1 = 24. 
However, it is not the optimal solution. There is another path that carries more weight (20 + 2 + 10 = 32) as shown in the image below
Longest path

Steps for using Greedy Algorithm

  1. To begin with, the solution set (containing answers) is empty.
  2. At each step, an item is added to the solution set until a solution is reached.
  3. If the solution set is feasible, the current item is kept.
  4. 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

  1. Set the first element as minimum.
  2. Compare minimum with the second element. If the second element is smaller than minimum, assign the second element as minimum.

    Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.
  3. After each iteration, minimum is placed in the front of the unsorted list
  4. 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

Top of Form

  • 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

  • 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.

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