Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

After this page, you should be able to:

Builds on: Contracts and invariants, recursion and sorting.

Establish a baseline before looking for a shortcut

Specify the required result, identify a straightforward correct search, then ask what structure makes some of its work unnecessary. An unproved fast-looking choice is not an improvement over a correct baseline.

StrategyWhat it usesMain question to justify
Brute forceEnumerate candidatesDoes the enumeration cover every valid answer?
Divide-and-conquerSmaller parts that can be recombinedWhy does the combination solve the whole problem?
GreedyA choice that can be fixed safelyWhy can some optimum include this choice?
Dynamic programmingRepeated subproblemsDoes the state contain everything the future needs?
BacktrackingA partial candidate and reversible decisionsDoes each prune exclude only impossible or irrelevant completions?

Define state by future relevance

State records the information needed to continue a computation. A shortest-path label describes a best route cost to a vertex; a coin-change subproblem can be the remaining amount. For choosing each item at most once, the remaining amount alone is insufficient: which positions remain available also matters.

Two histories can share a stored result only when their future possibilities and required result are equivalent under the state definition. Accidentally forgetting a constraint makes a small state fast but incorrect.

Reuse a solved problem through a justified reduction

Sometimes design means translating a new question into an existing operation. A dependency schedule becomes topological ordering; a minimum connection network becomes an MST problem; a repeated exact-key query suggests a map. Explain how solutions to the translated problem satisfy the original requirements, including any information lost by the representation.

Algorithms can combine strategies and structures. Dijkstra uses a heap but depends on a greedy settlement proof. DAG shortest paths can be viewed as dynamic programming over dependency-ordered states. Recursion is a way to express execution; it does not itself identify which design strategy is being used.

Compare problem contracts before comparing strategies

Example in this topicRequired resultConstraint that changes the design
Activity selectionA compatible set of maximum cardinalityHalf-open intervals; maximise count, not reward
Unbounded coin changeMinimum coin count, optionally with one reconstructionEach positive denomination can be reused
Subset enumerationEvery matching subset of input positionsEach position used at most once; equal values can be distinct items
Sorted two-sumOne pair of distinct indices, or NoneSorted input and consistent numeric operations
Prefix totals / fixed windowsRange totals / the best fixed-width totalSnapshot inputs / specified valid width

A result can be a boolean, a count, one witness or every witness. Those requests are not interchangeable. Before combining states or pruning candidates, state which distinctions the result must retain and which input assumptions justify eliminating work.

No strategy name has one universal time/space bound. Count candidate branches, distinct states and transitions, preprocessing, retained state and emitted output for the chosen implementation. A heap is a representation choice; greedy selection is a correctness policy; recursion is an execution mechanism. Several can appear in one algorithm.

Check your understanding

Predict the result or justify the choice before opening the explanation.

1. For choosing each item at most once, can remaining amount alone always identify a reusable state?

Show explanation

No. The available positions also matter. Two histories with the same remaining amount may have different usable items.

2. Does a heap prove that a local minimum choice is globally optimal?

Show explanation

No. It efficiently supplies a candidate. A settlement, exchange or other problem-specific argument must justify committing to it.

Next: Greedy design.