liveBook · Manning

Optimal solution

As explained in Grokking Algorithms, Second Edition

An optimal solution is a feasible solution that achieves the best possible objective value for an optimization problem.

Imagine filling a 35-pound knapsack. An optimal solution is a feasible choice of items that produces the highest possible value. Choosing the most expensive item first may look sensible, but the stereo earns $3,000 while the laptop and guitar together earn $3,500. section 10.2

The difference is between a merely good or approximate result and the best possible result. Trying every possible set can find the optimum, but the number of sets doubles with each added item, making this approach impractical for large inputs. section 11.2

When a local choice is globally optimal

For classroom scheduling, repeatedly choosing the class that ends soonest produces three compatible classes. Here, each locally optimal choice leads to the globally optimal schedule. section 10.1

Use these questions to distinguish optimal solutions from attractive shortcuts:

  • Does the solution maximize the objective, or is it only close to the maximum? section 10.2 section 11.2
  • Must the container be filled completely? No: a valuable diamond can leave unused capacity and still belong to the optimal set. section 11.13

How Grokking Algorithms, Second Edition covers it

How deep it goes
Explains exhaustive enumeration, local-versus-global choices, and unused capacity, but stops short of detailing the complete exact algorithm.
Examples
Illustrates the idea with knapsack items, classroom schedules, a diamond, furniture packing, and trip planning.
What you can do after
Evaluate whether a result is globally optimal, merely approximate, or produced by a locally optimal choice.
How it connects
Connects optimal solutions to objective values, greedy algorithms, local optimality, global optimality, knapsack problems, and dynamic programming.

Also explained in

Compare how 3 books explain optimal solution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Algorithms, Second Edition. The section links point to the parts of the book it draws on.