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

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 $S$
- Let $\mathscr{O}$ 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 $S$ cannot have any inversions.
- From here, we have two cases:
- $\mathscr{O}$ does not have any inversions, this immediately contradicts with our statement at step 3.
- $\mathscr{O}$ has at least one inversion. This impliest that there two points $(o_i, o_{i+1})$ 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 $\mathscr{O}’$ that is still optimal and yet has less number of inversions than $\mathscr{O}$.
- This means $\mathscr{O}$ is not the optimal solution with the least number of inversions, contradicting step 2.
- By reducing the number of inversions we brough $\mathscr{O}’$ closer to $S$ while preserving the optimality. This contradicts with step 3.
- Thus, the solution $S$ is optimal