Greedy Method
Greedy Method
Introduction
Greedy Method
Greedy algorithms are a class of algorithms that make locally
optimal choices at each step with the hope of finding a global
optimum solution. In these algorithms, decisions are made based on the
information available at the current moment without considering the
consequences of these decisions in the future. The key idea is to select the best
possible choice at each step, leading to a solution that may not always be the
most optimal but is often good enough for many problems. It works for cases
where minimization or maximization leads to the required solution. It is
called “greedy” because it tries to find the best solution by making the best
choice at each step, without considering future steps or the consequences of
the current decision.
Optimization
Optimization refers to the process of finding the best possible solution from all
feasible solutions. An optimal problem, in this context, is one that seeks to find
the optimal solution, which could involve maximizing or minimizing an
objective function while satisfying certain constraints.
If both of the properties below are true, a greedy algorithm can be used to solve
the problem.
2. At each step, an item is added to the solution set until a solution is reached.
Example: suppose we want to find the longest path in the graph below from root to leaf.
Let's use the greedy algorithm here.
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.
Finaly 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.
tree that can be split into two copies, such that the maximum edge weight in the