Search code examples
algorithmoptimizationgreedy

Optimization with a greedy algorithm


If an optimization problem can be solved using the greedy method, is it true that all its optimal solutions must always contain the first choice (i.e. the greedy choice)?


Solution

  • I am interpreting your question as "the set of all optimal solutions must always contain the first choice" otherwise it does not make sense for a solution to contain another solution.

    Naturally, the answer is trivially yes. If a greedy algorithm solves the problem, it produces an optimal solution, which by definition is in the set of optimal solutions.

    Perhaps you meant "if a greedy algorithm sometimes produces an optimal solution, ..." in that case again the answer is trivial.

    If you meant that "if a greedy algorithm sometimes produces an optimal solution, is it true that all guaranteed optimal algorithms will produce that solution too?" the answer depends upon whether the problem has a unique optimal solution or multiple ones.

    If a problem has multiple optimal solutions, the answer is clearly no.

    A good example to think about is to sort a list of numbers such that all single digit numbers come ahead of two digit numbers, two digit numbers come ahead of three digit numbers, and so forth. I. e. you don't care whether 99 comes before 11 or vice versa, you just want 9 to come before 25, and 33 before 872, and 555 before 1234.

    This example problem has multiple optimal solutions. A lazy but not greedy algorithm would not ensure that 11 comes before 99. An overenthusiastic algorithm would do so. Both would produce optimal solutions different from each other.

    If this doesn't help, nothing will ;-)