Posts

Showing posts from May, 2022

GREEDY ALGORITHMS

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