PrepStack · Markdown HTML

Finite-state-machine DP

Dynamic programming · HTML

Formal shape

dp[i][s] is the best value after time i in mode s. Update dp[i][s']=Agg_s(dp[i-1][s]+cost(s→s',i)) over edges allowed by the state graph.

Recognition signals

False friends

SignalWhy it is different
Exponentially many modesThis is not a finite-state model; consider a bitmask or another structured state.
Independent per-step rewardsIf history imposes no constraint, aggregate locally without DP.
A provably regret-free actionWhen one greedy rule is valid, state-machine DP is unnecessary machinery.

Core invariant

After time i, each state value is the optimum among every legal history ending in that mode, and no illegal history enters a finite state.

State, transition, and processing order

ItemDefinition
StateTime i and finite mode s, with unreachable modes initialized to positive or negative infinity as appropriate.
TransitionFor every legal state-graph edge, extend the prior source state with the current action contribution into the target state.
Frontier / orderTime advances forward; each round derives new from old so multiple edges cannot be traversed accidentally at one instant.

Python skeleton

def max_gain(prices):
    # States: flat or holding one unit; any number of transactions.
    flat, hold = 0, float('-inf')
    for price in prices:
        old_flat, old_hold = flat, hold
        flat = max(old_flat, old_hold + price)
        hold = max(old_hold, old_flat - price)
    return flat

Correctness sketch

  1. Initially flat=0 and hold is unreachable, exactly describing zero processed times.
  2. Every history ending in a target state has a final action corresponding to one incoming state edge, all of which are enumerated.
  3. Source states are optimal by induction, so aggregation makes each target optimal; choose among allowed terminal states.

Complexity

TimeSpaceParameters
O(n·|E_s|), which is O(n) for a fixed graphO(|S|)n is time count and S/E_s are modes/legal transitions.

Edge cases

Error patterns

PatternWhy it fails / fix
In-place updates using current-round valuesIf chained actions are forbidden, derive every new state from an old snapshot.
States overlap or omit needed historyOverlap double-counts histories; insufficiency merges histories with different futures.
Initializing illegal modes to zeroThat creates value from nowhere; an unreachable maximizing state needs negative infinity.
Drawing nodes without action labelsEdge conditions and gains define the recurrence; names alone do not.

Selection and exclusion rules

RelationTemplateBoundary
generalizationLinear dynamic programmingEach position has several mutually exclusive modes.
analogyTree dynamic programmingTree nodes have mutually exclusive modes and edges impose compatible transitions.

Original micro-example

PartDetail
ResultThe terminal flat value is 3, corresponding to buy at 1 and sell at 4.
SetupPrices are [3,1,4], with at most one item held.
TraceAt price 1, hold=max(-3,-1)=-1; at price 4, flat=max(0,-1+4)=3.

Recall prompts

My notes