PrepStack · Markdown HTML

Exchange-argument greedy ordering

Greedy and scheduling · HTML

Formal shape

Compare adjacent contributions F(a,b) and F(b,a). If a transitive relation a≼b guarantees F(a,b)<=F(b,a) for minimization, eliminating every inversion yields a global optimum.

Recognition signals

False friends

SignalWhy it is different
A locally small choice feels rightWithout an exchange, cut, or stays-ahead proof, that feeling does not establish optimality.
A decision changes future availabilityIf swapping alters later feasibility, a pure ordering greedy is usually invalid.
A cyclic comparatorA preference cycle a≺b≺c≺a cannot define a consistent sort order.

Core invariant

The greedy prefix has no local inversion, and at least one global optimum agrees with that entire prefix.

State, transition, and processing order

ItemDefinition
StateObjects sorted by the derived key plus accumulated resource, time, or reward during the scan.
TransitionTake the next ordered object; when feasibility filtering remains, accept only in a way separately proved to preserve the prefix invariant.
Frontier / orderThe first unprocessed object in greedy order; in the proof, it is the next inversion removed from an arbitrary optimum.

Python skeleton

def greedy_order(items, key, initial_state, feasible, step):
    state = initial_state
    chosen = []
    for item in sorted(items, key=key):
        if feasible(state, item):
            state = step(state, item)
            chosen.append(item)
    return chosen, state

Correctness sketch

  1. Take an optimum and locate the first position that disagrees with the greedy order, producing a local inversion b,a.
  2. The exchange inequality permits replacing b,a by a,b without reducing value or feasibility.
  3. A finite number of exchanges yields an optimum in greedy order, so every greedy prefix extends to an optimum.

Complexity

TimeSpaceParameters
Typically O(n log n) sorting plus O(n) scanningO(n) or sorting-dependentn is object count; transitivity and feasibility-preserving exchange are prerequisites.

Edge cases

Error patterns

PatternWhy it fails / fix
Guessing a key before doing algebraWrite both adjacent-order costs first, then rearrange their difference into a key.
Proving only the local comparisonThe swap must also preserve all other feasibility and be repeatable globally.
Sorting by floating-point ratiosPrecision can reverse order; prefer cross products with overflow awareness.
Ignoring ties and the secondary objectiveWhen the main cost ties but stability or lexicographic order matters, specify a second key.

Selection and exclusion rules

RelationTemplateBoundary
fallbackFinite-state-machine DPA choice changes future state and the exchange no longer preserves feasibility.

Original micro-example

PartDetail
ResultOrder 1,2,5 is optimal, derived from the exchange difference rather than a slogan.
SetupJobs take 2,5,1 time units, and the sum of completion times is minimized.
TraceFor adjacent a>b, order ab costs a-b more than ba locally; swap every inversion.

Recall prompts

My notes