Advanced Algorithm

Chapter 5
Dynamic Programming and State-Space Optimization

Post Graduate Program in AI and ML
Advanced Algorithm   •   Post Graduate

Dynamic Programming and State-Space Optimization

Introduction

Dynamic programming (DP) is a general method for solving optimization, counting, and decision problems whose solutions contain reusable subproblems. Rather than recomputing the same subproblem many times, DP records its result and reuses it. The method is especially valuable when a problem has optimal substructure and overlapping subproblems.

In advanced algorithm design, dynamic programming is not merely a collection of standard recipes such as knapsack or longest common subsequence. It is a disciplined way of modelling a problem: define a state, specify valid transitions, establish boundary conditions, determine an evaluation order, and analyze time and space complexity. For large AI and machine-learning systems, state-space optimization also determines whether a sequence model, planning system, alignment algorithm, or resource-allocation procedure is computationally feasible.

A DP formulation can usually be expressed as a recurrence over a directed acyclic state graph. Each vertex represents a subproblem, and each directed edge represents a transition. The algorithm computes the best, worst, total, or most probable value associated with each reachable state.

Learning Objectives

After completing this chapter, a learner should be able to:

  1. Recognize optimal substructure and overlapping subproblems.
  2. Formulate states, transitions, boundary conditions, and objective functions.
  3. Implement recursive memoization and iterative tabulation.
  4. Solve sequence, knapsack, and interval DP problems.
  5. Reconstruct an optimal solution rather than only its value.
  6. Optimize memory and reduce unnecessary state dimensions.
  7. Evaluate correctness, complexity, numerical stability, and practical limitations.

1. The Dynamic-Programming Model

1.1 Optimal substructure

A problem has optimal substructure when an optimal solution can be composed from optimal solutions to smaller subproblems. For example, if an optimal path from vertex A to vertex C passes through B, then its A-to-B prefix must itself be optimal; otherwise replacing it with a better prefix would improve the complete path.

Optimal substructure is not the same as saying that every locally best choice is globally best. Greedy algorithms commit to a locally attractive choice, whereas DP evaluates alternatives through a recurrence and preserves the information needed for later decisions.

1.2 Overlapping subproblems

Subproblems overlap when the same smaller problem is needed by multiple larger problems. Naive recursive Fibonacci computation illustrates this phenomenon. The recurrence is F(n) = F(n−1) + F(n−2), but the recursion repeatedly computes values such as F(n−2). Memoization changes the exponential recomputation pattern into linear computation.

1.3 State, transition, and value

A state is a sufficient description of the information needed to determine future choices. A state should be neither incomplete nor unnecessarily detailed. Let S denote the set of states and let V(s) be the optimal value from state s. If action a moves state s to s′ with immediate contribution r(s,a), a general maximization recurrence is:

V(s) = maxa ∈ A(s) { r(s,a) + V(T(s,a)) }

Here, A(s) is the set of legal actions, T(s,a) is the successor state, and terminal states have explicitly defined values. For minimization, replace max with min. For counting problems, alternatives are generally added rather than maximized; for probability models, alternatives may be summed or maximized depending on whether the objective is marginal probability or most likely explanation.

The recurrence is valid only when the state contains all information relevant to future decisions. This is the state sufficiency requirement. If two histories produce the same state, the algorithm must be able to treat their futures identically, except for any value already accumulated.

1.4 State-space graph

The recurrence can be viewed as a graph problem. Nodes are states, and edges are legal transitions. If transitions always move toward a smaller index, shorter prefix, smaller interval, or later time, the state graph is acyclic. Tabulation is then a topological evaluation of this graph. This perspective is useful when designing less conventional DP algorithms, including shortest paths in directed acyclic graphs, parsing, scheduling, and finite-horizon control.

2. Memoization and Tabulation

2.1 Memoization

Memoization is top-down dynamic programming. A recursive procedure computes a state only when requested and stores the result in a table.

Solve(s):
    if s is terminal: return Base(s)
    if memo[s] is already stored: return memo[s]
    best ← identity value
    for each legal action a from s:
        candidate ← reward(s,a) + Solve(T(s,a))
        best ← combine(best, candidate)
    memo[s] ← best
    return best

Advantages include direct correspondence with the mathematical recurrence and computation only of reachable states. Disadvantages include recursion depth, function-call overhead, and possible stack overflow. A memoized algorithm has time approximately proportional to the number of reachable states multiplied by the work per state:

Time = O(|R| × B)

where R is the set of reachable states and B is the average number of transitions examined per state.

2.2 Tabulation

Tabulation is bottom-up dynamic programming. The algorithm identifies an order in which all dependencies of a state are computed before that state. For a prefix DP indexed by i, this often means evaluating i from 0 through n. For interval DP, increasing interval length is usually appropriate.

Tabulation commonly offers lower overhead and predictable memory access. It can also make rolling-array optimization straightforward. However, it may compute states that are unreachable or irrelevant unless the state space is carefully restricted.

2.3 Correctness proof pattern

A rigorous DP proof normally contains three parts:

  1. State interpretation: define exactly what each table entry means.
  2. Recurrence justification: show that every feasible optimal solution is represented by one of the transitions, and that every transition produces a feasible solution.
  3. Induction and termination: prove that dependencies are correct and that the terminal state or final table entry represents the desired answer.

A common error is to state a recurrence without proving that the state stores sufficient information. Another is to omit impossible-state handling, allowing an invalid partial solution to be treated as optimal.

3. Sequence Dynamic Programming

Sequence problems operate on prefixes, suffixes, or pairs of prefixes. They occur in text comparison, bioinformatics, speech processing, time-series alignment, and structured prediction.

3.1 Longest Common Subsequence

Given sequences X of length m and Y of length n, the longest common subsequence (LCS) is the longest sequence appearing in both in the same relative order, not necessarily contiguously. Define L[i][j] as the LCS length of the first i elements of X and first j elements of Y.

L[i][j] = { L[i−1][j−1] + 1, if X[i] = Y[j]
{ max(L[i−1][j], L[i][j−1]), if X[i] ≠ Y[j]

The boundary conditions are L[i][0] = 0 and L[0][j] = 0. The table requires O(mn) time and O(mn) space. If only the length is required, two rows reduce space to O(n), assuming n is the shorter dimension. If the actual subsequence must be reconstructed, predecessor information or a suitable divide-and-conquer method is required.

For X = ABCBDAB and Y = BDCABA, one LCS is BCBA, with length 4. A reconstruction starts at L[m][n]: move diagonally when characters match; otherwise move toward the larger neighboring value.

3.2 Edit distance

The Levenshtein edit distance is the minimum number of insertions, deletions, and substitutions needed to transform one sequence into another. Let D[i][j] be the distance between prefixes of lengths i and j. With unit cost for each operation:

D[i][j] = min { D[i−1][j] + 1,
D[i][j−1] + 1,
D[i−1][j−1] + cost(X[i],Y[j]) }

where cost is 0 when the symbols match and 1 otherwise. The boundaries are D[i][0] = i and D[0][j] = j. Weighted versions use operation-specific costs, provided the costs are defined consistently and the objective remains additive.

3.3 General sequence-design lessons

Sequence DP often uses one or two indices because a prefix boundary summarizes the past. For more complex alignment, additional dimensions may represent a mode, such as match, insertion, or deletion. The extra dimension improves modelling accuracy but increases complexity. A state should be expanded only when omitting that information would make the recurrence incorrect.

4. Knapsack and Resource-Allocation DP

In the 0/1 knapsack problem, item i has value vᵢ and weight wᵢ, each item may be selected at most once, and the capacity is W. Define K[i][c] as the maximum value obtainable using the first i items with capacity c.

K[i][c] = max(K[i−1][c], K[i−1][c−wᵢ] + vᵢ) if wᵢ ≤ c
K[i][c] = K[i−1][c] if wᵢ > c

The first alternative excludes item i; the second includes it. Boundary values K[0][c] = 0 and K[i][0] = 0 apply when values are nonnegative and selecting no items is allowed. Complexity is O(nW) time and O(nW) space, where n is the number of items. This is pseudo-polynomial, not polynomial in the binary input length, because W may be exponentially larger than log W.

A one-dimensional implementation uses K[c]. To prevent an item from being used more than once, capacities must be processed in descending order:

for each item (w, v):
    for c from W down to w:
        K[c] ← max(K[c], K[c−w] + v)

For unbounded knapsack, where an item may be used repeatedly, capacities are usually processed in ascending order. The iteration direction is therefore a correctness condition, not merely a performance choice.

Worked example

Suppose capacity W = 7 and items are (weight, value): (2, 6), (3, 8), and (4, 9). Feasible combinations include item 1 + item 2, with weight 5 and value 14; item 1 + item 3, with weight 6 and value 15; and item 2 + item 3, with weight 7 and value 17. Thus the optimum is 17, obtained by selecting items 2 and 3. The DP table reaches this result without enumerating combinations explicitly.

Knapsack formulations generalize to budgeting, model selection under memory limits, portfolio-like discrete allocation, and selecting features or experiments under a resource constraint. Real applications may require multiple resources, in which case a state such as K[i][c₁][c₂] can become expensive. Approximation, dominance pruning, or integer programming may then be preferable.

5. Interval Dynamic Programming

Interval DP solves problems in which the relevant subproblem is a contiguous interval [i,j]. Typical examples include matrix-chain multiplication, optimal binary search trees, polygon triangulation, and context-free parsing.

5.1 Matrix-chain multiplication

Suppose matrices A₁ through Aₙ have compatible dimensions. Matrix multiplication is associative, but different parenthesizations may have different scalar multiplication costs. Let M[i][j] be the minimum cost of multiplying Aᵢ…Aⱼ. If Aₖ is the final split, then:

M[i][j] = mini ≤ k < j { M[i][k] + M[k+1][j] + pᵢ₋₁ pₖ pⱼ }

Here Aᵢ has dimensions pᵢ₋₁ × pᵢ, and the cost counts scalar multiplications. Base cases are M[i][i] = 0. Evaluate intervals by increasing length. There are O(n²) states and O(n) candidate splits per state, giving O(n³) time and O(n²) space.

5.2 Interval-DP design pattern

  1. Define the interval meaning.
  2. Identify the final operation or split.
  3. Enumerate all valid split points.
  4. Combine the two smaller intervals and the split cost.
  5. Evaluate by increasing interval length.

An interval recurrence can fail if the problem contains cross-boundary interactions not captured by the two subintervals. State augmentation or a different formulation may be necessary.

6. State-Space and Memory Optimization

6.1 Dependency analysis

Memory can be reduced when a table row depends only on a small window of previous rows. LCS and edit distance require only the previous row to compute the next row, reducing O(mn) space to O(n). Fibonacci requires only two previous values, reducing O(n) storage to O(1).

This optimization is safe only if overwritten values will never be needed later. Before replacing a two-dimensional table, inspect every dependency in the recurrence. In-place updates can also require a particular direction, as in 0/1 and unbounded knapsack.

6.2 Rolling arrays and reconstruction trade-offs

Memory optimization may remove the historical information needed to reconstruct a solution. Three responses are common:

  • retain predecessor decisions while storing compact values;
  • recompute selected portions during backtracking;
  • use divide-and-conquer reconstruction, as in Hirschberg’s algorithm for LCS-like problems.

Thus, “optimal memory” depends on whether the output is a value, a count, a path, or a complete sequence of decisions.

6.3 State compression and dominance

A state can sometimes be compressed by removing a dimension that is derivable from other variables. In shortest-path or resource problems, dominated states can be discarded: state s₁ dominates s₂ if it is no worse in every relevant resource and strictly better in at least one. Any continuation feasible from s₂ is then no better than a continuation from s₁.

Compression must preserve equivalence. Two states that currently have equal value are not necessarily interchangeable if their future legal actions differ.

6.4 Sparse state spaces

If only a small fraction of theoretically possible states is reachable, a hash map or dictionary can replace a dense array. Sparse storage is particularly useful for large capacities, irregular constraints, and memoized search. Its disadvantages include hashing overhead, less predictable memory access, and potentially high per-entry storage.

7. A Systematic DP Design Procedure

A reliable workflow is:

  1. Clarify the objective. Is the task maximization, minimization, counting, feasibility, or probability estimation?
  2. Identify decision boundaries. Examples include a prefix index, time step, capacity, interval endpoints, or automaton state.
  3. Define the state in one sentence. If the sentence is ambiguous, the state is not ready.
  4. List legal transitions. Include exclusions, insertions, splits, and mode changes.
  5. Set boundary and impossible values. For maximization use an appropriate negative sentinel; for minimization use positive infinity, ensuring arithmetic cannot overflow.
  6. Choose evaluation strategy. Memoization is useful for irregular reachability; tabulation is useful for dense, ordered state spaces.
  7. Prove correctness. Use state interpretation, transition completeness, and induction.
  8. Analyze complexity. Count states and transitions separately.
  9. Test edge cases. Test empty input, zero capacity, equal symbols, infeasible states, duplicate values, and the smallest legal instance.
  10. Decide whether reconstruction is required. This affects memory and data structures.

8. Applied Case: Resource-Constrained Model Selection

The following is an illustrative case.

Background

An AI engineering team has candidate preprocessing and inference components for an edge-deployed classifier. Each component consumes memory and contributes an empirically estimated accuracy benefit. The device has a strict memory budget, and each component can be selected at most once.

Problem

The team must maximize total benefit without exceeding the memory limit. Components may include quantization support, a compact embedding layer, a feature cache, and an optimized kernel. The benefit estimates are additive for this simplified planning model.

Analysis and approach

The problem is a 0/1 knapsack instance. Let K[i][c] denote the best estimated benefit using the first i components within c megabytes. The recurrence distinguishes excluding and including component i. A production implementation should store component identifiers or predecessor decisions if the selected configuration must be reported.

The additive-benefit assumption must be checked. If two components interact—for example, a kernel benefit is available only when quantization is enabled—the state must include the relevant compatibility mode, or the components must be represented as grouped alternatives. If memory consumption varies by input batch or hardware backend, a single scalar capacity may be inadequate.

Outcome

The DP returns a feasible configuration with the highest modelled benefit. The team should then validate the selected configuration using actual latency, energy, accuracy, and robustness measurements. DP optimizes the stated model; it does not guarantee that the model is an accurate representation of deployment reality.

Learning points

  1. State design encodes assumptions about independence and future choices.
  2. A mathematically optimal configuration can be operationally poor if estimates are biased.
  3. Multiple constraints may require multidimensional DP or a different optimizer.
  4. Governance requires documenting objective definitions, measurement procedures, and rejected alternatives.

9. Limitations, Reliability, and Responsible Use

Dynamic programming can suffer from state explosion. If d dimensions each have many possible values, the product of their ranges may be infeasible. Approximation schemes, pruning, relaxations, beam search, integer programming, or learned heuristics may be more suitable.

Numerical issues also matter. Probabilistic sequence DP can underflow when multiplying many small probabilities. Log-space computation replaces products with sums; when adding probabilities in log-space, a numerically stable log-sum-exp operation is required. Costs and sentinels must use compatible numeric types, and overflow in expressions such as sentinel + transition cost must be prevented.

In AI applications, the objective function may encode consequential decisions. A DP that maximizes a proxy metric can systematically neglect fairness, safety, privacy, or uncertainty. Practitioners should document the objective, constraints, data provenance, sensitivity to parameter changes, and conditions under which the solution is invalid. Where DP is used for allocation or automated decisions, human review and auditability may be necessary.

10. Contemporary Directions

Modern systems combine DP with other methods. Differentiable dynamic programming approximates discrete operators so that parameters can be learned through gradient-based optimization. Neural sequence models may provide heuristic costs or transition scores, while exact DP enforces structural constraints. In planning, learned value estimates can guide search, although replacing exact values with approximations changes optimality guarantees.

Other active directions include adaptive pruning, GPU parallelization for wavefront-style recurrences, compressed representations for long sequences, and hybrid algorithms that switch between dense and sparse state storage. The central design principle remains unchanged: preserve the information needed for valid future decisions while avoiding unnecessary state detail.

Chapter Summary

Dynamic programming solves structured problems by exploiting optimal substructure and overlapping subproblems. A sound formulation defines a sufficient state, legal transitions, boundary conditions, and an objective-specific recurrence. Memoization evaluates reachable states recursively, whereas tabulation evaluates states in dependency order. Sequence DP supports LCS and edit distance; knapsack DP handles discrete resource allocation; interval DP handles split-based problems such as matrix-chain multiplication. Complexity is determined by the number of states and transitions, and memory can often be reduced through rolling arrays, state compression, sparse storage, and dominance pruning. Such optimizations must preserve dependencies and may trade away solution reconstruction. In professional AI and ML systems, correctness also depends on realistic objectives, numerical stability, validation, and responsible governance.

Key Terms

Dynamic programming: A method that stores and reuses solutions to overlapping subproblems.

State: A sufficient representation of information needed to evaluate future decisions.

Transition: A legal movement from one state to another.

Optimal substructure: The property that an optimal solution contains optimal solutions to relevant subproblems.

Overlapping subproblems: Repeated occurrence of the same smaller subproblem.

Memoization: Top-down DP that caches results of recursive calls.

Tabulation: Bottom-up DP that fills a table in dependency order.

Boundary condition: A value specified for terminal or smallest states.

State-space graph: A graph whose vertices are states and edges are transitions.

Sequence DP: Dynamic programming over prefixes, suffixes, or sequence pairs.

Longest Common Subsequence: The longest order-preserving sequence shared by two sequences.

Edit distance: Minimum cost of transforming one sequence into another using permitted operations.

Knapsack problem: Selection of items under one or more resource constraints.

Pseudo-polynomial: Polynomial in a numeric value such as capacity, but not necessarily in its encoded input length.

Interval DP: DP whose subproblems correspond to contiguous ranges.

State compression: Removal or aggregation of state information while preserving correctness.

Dominance pruning: Discarding states that can never outperform another state.

Reconstruction: Recovering the decisions that produced an optimal value.

Optimal substructure proof: A correctness argument showing that the recurrence captures all optimal solutions.

Review Questions

5 Short Answer Questions

  1. What are the two principal properties that make a problem suitable for dynamic programming?
  2. Distinguish memoization from tabulation.
  3. Why must 0/1 knapsack capacities be updated in descending order in a one-dimensional implementation?
  4. What does a DP state-sufficiency requirement mean?
  5. Why is the usual knapsack algorithm called pseudo-polynomial?

5 Long Answer Questions

  1. Explain how to formulate a DP recurrence, including state definition, transitions, boundary conditions, and correctness proof.
  2. Derive the LCS recurrence and explain how the actual subsequence can be reconstructed.
  3. Compare dense tables, rolling arrays, and sparse dictionaries for state-space storage.
  4. Explain interval DP using matrix-chain multiplication and justify its evaluation order and complexity.
  5. Discuss the limitations of applying DP to AI model-selection and planning problems.

3 Analytical / Application Questions

  1. Design a DP formulation for selecting machine-learning pipeline components under memory and latency constraints. State the assumptions that make your recurrence valid.
  2. A sequence algorithm requires three modes—match, insertion, and deletion. Propose a state representation and explain how adding the mode changes complexity.
  3. Given a DP with many dominated resource states, formulate a dominance rule and explain how pruning affects correctness and practical performance.

Multiple Choice Questions with Answers

Question 1

Which property ensures that an optimal solution can be assembled from optimal solutions to smaller subproblems?

A. Optimal substructure

B. Randomization

C. Hash consistency

D. Loop unrolling

Correct Answer: A. Optimal substructure

Question 2

For 0/1 knapsack with a one-dimensional table, why are capacities processed from W down to wᵢ?

A. To sort items by value

B. To prevent the current item from being used more than once

C. To guarantee negative values are excluded

D. To reduce the number of items

Correct Answer: B. To prevent the current item from being used more than once

Question 3

What is the time complexity of the standard LCS table for sequences of lengths m and n?

A. O(m + n)

B. O(log(mn))

C. O(mn)

D. O(m² + n²) only

Correct Answer: C. O(mn)

Question 4

In matrix-chain multiplication, what does the split index k represent?

A. The number of matrices discarded

B. The final multiplication boundary between two subchains

C. The memory capacity

D. The matrix with the largest dimension

Correct Answer: B. The final multiplication boundary between two subchains

Question 5

Which technique evaluates only states reached by recursive requests and stores their results?

A. Tabulation

B. Memoization

C. Greedy selection

D. Random sampling

Correct Answer: B. Memoization

Question 6

For items (weight, value) (2,6), (3,8), and (4,9) with capacity 7, what is the maximum value?

A. 14

B. 15

C. 16

D. 17

Correct Answer: D. 17

Question 7

What is the main risk of removing a state dimension without proving equivalence?

A. The program always uses more memory

B. Future legal choices may no longer be represented correctly

C. The input becomes sorted

D. All transitions become greedy

Correct Answer: B. Future legal choices may no longer be represented correctly

Question 8

Which interval-DP evaluation order ensures shorter intervals are available before longer intervals?

A. Decreasing interval length

B. Random interval order

C. Increasing interval length

D. Alphabetical endpoint order

Correct Answer: C. Increasing interval length

Question 9

Why is standard knapsack DP pseudo-polynomial?

A. It ignores item values

B. Its complexity depends on the numeric capacity W

C. It uses recursion exclusively

D. It requires floating-point arithmetic

Correct Answer: B. Its complexity depends on the numeric capacity W

Question 10

If a DP state is dominated by another state that is no worse in every relevant resource and better in one, what can generally be done?

A. Discard the dominated state

B. Duplicate the dominated state

C. Make it a terminal state

D. Reverse every transition

Correct Answer: A. Discard the dominated state

Advanced Algorithm
Chapter 5 of 15