A greedy algorithm is an algorithmic approach that follows the problem-solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. It iteratively makes one greedy choice after another, reducing the given problem into a smaller one. At each step, it makes a decision that seems to be the best at that moment. So, it works well when the locally optimal choice is also the globally optimal one.
Usually, a greedy algorithm consists of 2 steps
- Sorting the provided data according to a chosen heuristic, this heuristic is differnt for each problem.
- Create a solution using the sorted array and some extra heuristics.
It can be proven that they always find the optimal solution To prove the optimality of greedy algorithms, there are two methods:
Greedy Stays Ahead
Greedy stays ahead is a technique to prove that a solution generated by a greedy algorithm is optimal. A general step by step approach to prove an algorithm’s correctness can be summarized as such.
- Show that a greedy algorithm always makes locally optimal solutions.
- Prove that if we choose any other local solution, we cannot reach the globally optimal solution.
- Use mathematical induction to prove that greedy always stays ahead of any other solution
A General Skeleton
Let be the set of choices generated by our greedy solution and let be the optimal set of choices. Refer to each element in the solutions as and respectively. Let be a function that measures the optimality of a set of choices up . We will use induction to prove that (or the other way around, depends on whether we want to maximize or not) for any two arbitrary integers and such that .
- Base Case: Show that .
- Inductive Hypothesis: Assume that holds for any arbitrary and
- Inductive Step: Now prove that assuming that the inductive hypothesis holds. This part requires some math. Usually, we try to prove that the reverse() and derive a contradiction with the definition of the greedy algorithm from there.
Therefore we have proven that holds if the inductive hypothesis holds. This, combined with the base case forms the proof that greedy always stays ahead.
Exchange Argument
The exchange argument is a proof method used to prove the optimality of Greedy Algorithms. The proof is based on the idea that given an optimal solution with inversions, commpared to the the greedy solution, fixing those inversions brings the optimal solution closer to the greedy solution whilst preserving optimality.
WARNING
For two elements in the wrong order compared to the greedy solution to be considered an inversion, they need to be next to each other.
A basic skeleton
- Let the solution produced by our greedy algorithm be
- Let be the optimal solution with the least amount of inversions.
- Assume that greedy does not produce optimal solutions.
- Define what an inversion is, show that by definition cannot have any inversions.
- From here, we have two cases:
- does not have any inversions, this immediately contradicts with our statement at step 3.
- has at least one inversion. This impliest that there two points that are inverted. It is best to start this part with “Consider a solution…”
- Show that swapping the inverted elements preserves the optimality of the solution.
- Show that swapping the two points removes the inversion
- Show that swapping the two points does not introduce any new inversions.
- State that this creates a new solution that is still optimal and yet has less number of inversions than .
- This means is not the optimal solution with the least number of inversions, contradicting step 2.
- By reducing the number of inversions we brough closer to while preserving the optimality. This contradicts with step 3.
- Thus, the solution is optimal