Introduction to Greedy Algorithm

# Introduction to Greedy Algorithm

Updated on Dec 14, 2022 20:01 IST

The greedy algorithm in data science is an approach to problem-solving that chooses the best choice based on the circumstances at hand. In this article, we will briefly discuss Greedy Algorithm.

A greedy algorithm is an algorithm for problem-solving that chooses the best choice at the time. It is unconcerned with whether the most excellent result at the moment will produce the final best result. Even if the option was the erroneous one, the algorithm never goes back and changes its mind. It functions in a top-down manner. In this article, we will talk about greedy algorithm approaches, their applications, and their limitations, along with various examples in different programming languages.

## Greedy Algorithm

The greedy algorithm in data science is an approach to problem-solving that chooses the best choice based on the circumstances at hand. This method disregards the possibility that the best result now obtained may not be the ultimate best outcome. The algorithm never goes back and corrects an inaccurate judgment.

This straightforward, understandable approach can be used to resolve any optimization issue that calls for the maximum or minimal ideal outcome. The simplicity of this algorithm makes it the best choice for implementation.

With a greedy solution, the runtime complexity is fairly manageable. However, you can only use a greedy solution if the issue statement satisfies the two following criteria:

Greedy Choice Property: Selecting the ideal option at every stage might result in an overall (global) optimal solution, which is known as the greedy choice property. Optimum Substructure: A problem has an ideal substructure if an ideal strategy for the whole problem includes optimal solutions to each of its subproblems.

## Steps to create Greedy Algorithm

You can create a solution for a greedy algorithm where the statement is given using the following steps below:

• Step 1: Find the ideal substructure or subproblem in the given problem
• Step 2: Choose the components of the solution (e.g., largest sum, shortest path).
• Step 3: Establish an iterative procedure for examining each subproblem and coming up with the best answer.

## Components of Greedy Algorithm

The following elements can be incorporated into the greedy algorithm:

• Candidate set: A candidate set is a solution that is derived from the set.
• Selection Function: Use this function to choose the candidates or subsets that can be included in the solution.
• Feasibility function: A function used to assess a candidate’s or subset’s potential to contribute to the solution is the feasibility function.
• Objective function: An objective function is used to give the solution or a portion of the solution a value.
• Solution Function: This function indicates whether or not the complete function has been achieved in the solution.

## Greedy Algorithm: Pseudo Code

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

The algorithm described above is greedy. The solution is first given a value of zero. The array and element count are passed to the greedy algorithm. We choose each piece individually inside the for loop and determine whether or not the solution is workable. We conduct the union if the solution is practical.

Let’s consider a real-world issue and come up with a greedy solution for this.

### ProblemStatement:

James is very busy, which is a problem. He has allotted T hours to complete some intriguing activities. He wants to accomplish as many things as he can in the time provided. In order to do that, he has made an array A containing timestamps for each item on his agenda.

Here, we need to calculate how many tasks Alex can finish in the given amount of time (T).

Solution: The presented issue is a simple greedy issue. With the current Time and number Of Things in mind, we must choose the items from array A that will complete a task in the shortest period of time for each iteration. We must complete the actions listed below in order to come up with a solution.

• The array A should be sorted ascending.
• Choose a timestamp one at a time.
• Add the timestamp amount to the current Time after obtaining the timestamp.
• Add one more thing to the number Of Things.
• Up until the current Time value reaches T, repeat steps 2 through 4.

## Example of Greedy Algorithm

From the origin to the destination, we must travel as cheaply as possible. Since the three possible solutions have cost pathways of 10, 20, and 5, respectively, 5, is the least expensive option, making it the best choice. This is the local optimum, and in order to compute the global optimal solution, we discover the local optimum at each stage in this manner.

## Limitations of Greedy Algorithm

Let us see below a few limitations of using the Greedy algorithm:

• The greedy algorithm does not offer the best solution for every problem since it bases its decisions on the information available at each repetition while taking the bigger picture into account.
• A greedy algorithm has trouble when it comes to evaluating its accuracy. It is challenging to show why the solution is accurate, even when it is the right one.
• A greedy method cannot solve optimization issues (Dijkstra’s Algorithm) with minus graph edges.

## Applications of Greedy Algorithm

Let us see below some applications of the Greedy algorithm:

• Prim’s and Kruskal’s algorithms, which are greedy algorithms, are employed in the construction of minimum spanning trees.
• A greedy technique is used to generate a Huffman tree, which compresses a given picture, spreadsheet, or video into a lossless compressed file, to implement Huffman encoding.
• Utilized to Address Optimization Issues: Classic optimization issues that can be resolved with the help of a greedy algorithmic paradigm include the  Graph – Vertex Cover, Activity Selection Problem, Knapsack Problem, Graph – Map Coloring, Job and Scheduling Problem

## Conclusion

In this article, we have discussed greedy algorithms in detail. The greedy algorithm in data science is an approach to problem-solving that chooses the best choice based on the circumstances at hand. This method disregards the possibility that the best result now obtained may not be the ultimate best outcome. We have also learned what a greedy programming paradigm is as well as the characteristics and procedures for creating a greedy solution. The essay also touches on applications and the greedy algorithm.