# Greedy Algorithms

A greedy algorithm is a basic, natural calculation that is utilized in streamlining issues. The calculation settles on the ideal decision at each progression as it endeavors to track down the generally ideal approach to tackle the whole issue. Ravenous calculations are very effective in certain issues, for example, Huffman encoding which is utilized to pack information, or Dijkstra’s calculation, which is utilized to track down the briefest way through a diagram.

Nonetheless, in numerous issues, a covetous technique doesn’t create an ideal arrangement. For instance, in the liveliness underneath, the voracious calculation tries to discover the way with the biggest whole. It does this by choosing the biggest accessible number at each progression. The avaricious calculation neglects to track down the biggest entirety, notwithstanding, in light of the fact that it settles on choices dependent on the data it has at any one stage, regardless of the general issue.

# Structure of a Greedy Algorithm:-

Greedy algorithms take the entirety of the information in a specific issue, and afterward set a standard for which components to add to the arrangement at each progression of the calculation. In the liveliness over, the arrangement of information is the entirety of the numbers in the diagram, and the standard was to choose the biggest number accessible at each level of the chart. The arrangement that the calculation fabricates is the amount of those decisions.

On the off chance that both of the properties underneath are valid, an eager calculation can be utilized to take care of the issue.

- Eager decision property: A worldwide (generally speaking) ideal arrangement can be reached by picking the ideal decision at each progression.
- Ideal foundation: An issue has an ideal base if an ideal answer for the whole issue contains the ideal answers for the sub-issues.

All in all, ravenous calculations work on issues for which the facts demonstrate that, at each progression, there is a decision that is ideal for the issue up to that progression, and after the last advance, the calculation creates the ideal arrangement of the total issue.

To make a covetous calculation, recognize an ideal foundation or subproblem in the issue. At that point, figure out what the arrangement will incorporate (for instance, the biggest whole, the most limited way, and so forth) Make a type of iterative approach through the entirety of the subproblems and fabricate an answer.

# Restrictions of Greedy Algorithms:-

Once in a while, greedy algorithms neglect to discover the internationally ideal arrangement since they don’t think about all the information. The decision made by an insatiable calculation may rely upon decisions it has made up until now, however, it doesn’t know about future decisions it could make.

# Applications:-

- Dijkstra’s Algorithm:-

is utilized to track down the briefest way between hubs in a chart. The calculation keeps a bunch of unvisited hubs and ascertains a provisional separation from an offered hub to another. In the event that the calculation tracks down a more limited approach to get to a given hub, the way is refreshed to mirror the more limited distance. This issue has an acceptable enhancement foundation since if An is associated with B, B is associated with C, and the way should go through An and B to get to objective C, at that point the most limited way from A to B and the briefest way from B to C should be a piece of the briefest way from A to C. So the ideal answers from the subproblems do add to the ideal response for the absolute issue. This is on the grounds that the calculation monitors the briefest way conceivable to some random hub.

2. Huffman Coding:-

Huffman Coding is another illustration of a calculation where a voracious methodology is fruitful. The Huffman calculation dissects a message and relying upon the frequencies of the characters utilized in the message, it allocates a variable-length encoding for every image. An all the more usually utilized image will have a more limited encoding while an uncommon image will have a more extended encoding. The Huffman coding calculation learns about the frequencies or probabilities of a specific image happening. It starts to fabricate the prefix tree from the base up, beginning with the two least plausible images in the rundown. It takes those images and structures a subtree containing them, and afterward eliminates the individual images from the rundown. The calculation entireties the probabilities of components in a subtree and adds the subtree and its likelihood to the rundown. Then, the calculation looks through the rundown and chooses the two images or subtrees with the littlest probabilities. It utilizes those to make another subtree, eliminates the first subtrees/images from the rundown, and afterward adds the new subtree and its consolidated likelihood to the rundown. This rehashes until there is one tree and all components have been added. At each subtree, the ideal encoding for every image is made and together creates the by and large ideal encoding.

# Where you shouldn’t use Greedy approach?

Knowing where not to utilize Voracious is truly significant since, in such a case that ideal base property is followed and the ravenous decision property isn’t pertinent, you will arrive at an off-base answer.

If you form the path by choosing the maximum value child at each level(The Greedy Choice!), you will end up with the path **5->7->2** But we can see that clearly **5->3->17** is the path with the maximum sum of values. We assumed the greedy choice property applies in this problem and ended up with the wrong answer! We need to traverse every path and check the sum once we reach the leaf and compare the path sum with the maximum global sum we reached until then.

# Greedy vs Divide & Conquer vs Dynamic Programming

**Greedy:-**

- Optimises by making the best choice at the moment
- Doesn’t generally track down the ideal arrangement, yet is quick
- Requires basically no memory

**Divide & Conquer:-**

- Upgrades by separating a subproblem into less difficult adaptations of itself and utilizing multi-stringing and recursion to address
- Continuously tracks down the ideal arrangement, however is more slow than Greedy
- Requires some memory to recollect recursive calls

**Dynamic Programming:-**

- Same as Divide and Conquer, yet advances by reserving the responses to each subproblem as not to rehash the computation twice.
- Continuously tracks down the ideal arrangement, however could be inconsequential on little datasets.
- Requires a ton of memory for remembrance/classification

# Minimum Spanning Trees Using Prim’s Algorithm:-

Tidy’s calculation is an insatiable calculation that tracks down a base spreading over tree for a weighted undirected diagram. It tracks down the ideal course from each hub to each and every hub in the tree.

With a little change to Dijkstra’s calculation, we can fabricate another algorithm — Prim’s calculation!

We casually portray the calculation as:

- Make another tree with a solitary vertex (picked haphazardly)
- Of the multitude of edges not yet in the new tree, track down the base weighted edge and move it to the new tree.
- Rehash stage 2 until all vertices are in the tree

We have this chart.

Our following stage is to pick a self-assertive hub.

We pick the hub A. We at that point look at all the edges associating A to other vertices. Demure’s calculation is avaricious. That implies it picks the most limited edge that interfaces with an unvisited vertex.

In our model, it picks B.

We currently take a gander at all hubs reachable from A **and **B. This is the differentiation among Dijkstra’s and Prim’s. With Dijkstra’s, we’re searching for a way from 1 hub to a specific other hub (hubs that have not been visited). With Prim’s, we need the base crossing tree.

We have 3 edges with equivalent loads of 3. We pick 1 haphazardly.

It is useful to feature our chart as we come, since it makes it simpler to make the base spreading over tree.

Presently we see all edges of A, B, and C. The briefest edge is C > E with a load of 1.

Furthermore, we rehash:

The edge B > E with a load of 3 is the littlest edge. Be that as it may, both vertices are consistently in our **VISITED **list. Which means we don’t pick this edge. We rather pick C > F, as we have not visited

The solitary hub left is G, so we should visit it.

Note that if the edge loads are particular, the base traversing tree is interesting.

We can add the edge loads to get the base crossing tree’s complete edge weight:

2 + 3 + 3 + 1 + 6 + 9 = 242+3+3+1+6+9=24

# Is Greedy Optimal? Does Greedy Always Work?

It is ideal locally, however some of the time it isn’t ideal worldwide. In the change giving calculation, we can constrain a point where it isn’t ideal all around the world.

The algorithm for doing this is:

- Pick 3 divisions of coins. 1p, x, and under 2x yet more than x. We’ll pick 1, 15, 25.
- Ask for change of 2 * second denomination (15)

We’ll request change of 30. Presently, how about we see what our Greedy algorithm does.

`[5, 0, 1]`

It choses 1x 25p, and 5x 1p. The ideal arrangement is 2x 15p.

Our Insatiable calculation fizzled on the grounds that it didn’t take a gander at 15p. It took a gander at 25p and thought “yes, that fits. How about we take it.”

It at that point took a gander at 15p and figured “that doesn’t fit, we should proceed onward”.

This is an illustration of where Insatiable Calculations fall flat.

To get around this, you would either need to make money where this doesn’t work or to beast power the arrangement. Or then again utilize Dynamic Programming.

# Conclusion :-

Greedy algorithms are quick, however may not give the ideal arrangement. They are additionally simpler to code than their partners.