Greedy Algorithm
Greedy Algorithm
Algorithm
General Structure
• Identify the problem as an optimization problem where we need to
find the best solution among a set of possible solutions.
• Determine the set of feasible solutions for the problem.
• Identify the optimal substructure of the problem, meaning that the
optimal solution to the problem can be constructed from the optimal
solutions of its subproblems.
• Develop a greedy strategy to construct a feasible solution step by
step, making the locally optimal choice at each step.
Prove the correctness of the algorithm by showing that the locally
optimal choices at each step lead to a globally optimal solution.
General Structure
• Among all the algorithmic approaches, the simplest and
straightforward approach is the Greedy method. In this approach, the
decision is taken on the basis of current available information without
worrying about the effect of the current decision in future.
• Greedy algorithms build a solution part by part, choosing the next
part in such a way, that it gives an immediate benefit. This approach
never reconsiders the choices taken previously. This approach is
mainly used to solve optimization problems.
Components of Greedy Algorithm
• A candidate set − A solution is created from this set.
• A selection function − Used to choose the best candidate to be added
to the solution.
• A feasibility function − Used to determine whether a candidate can be
used to contribute to the solution.
• An objective function − Used to assign a value to a solution or a
partial solution.
• A solution function − Used to indicate whether a complete solution
has been reached.
Pros and Cons
Advantages Disadvantages