7 routes42 sections324 units

Unit 5.9 · about 4 h

127 / 324

Dynamic programming as shortest paths, formulations and limits

DP transfers to batching, caching and allocation. The skill is recognizing the state there, plus knowing when the state space is too large for exact methods.

  • explain
  • derive

You can now

  • Reformulate a scheduling, batching, caching or allocation narrative as DP or shortest path on a DAG.
  • State when exact optimization is infeasible and bound the heuristic or relaxation you use instead.
  • Reject tempting greedy or insufficient-state proposals with concrete counterexamples.

Covers

  • Dynamic programming as a DAG or shortest-path problem
  • Scheduling, caching, batching and allocation formulations
  • Exact, heuristic and approximate boundaries

Study, in this order

4 steps

  1. Coin change as the allocation family
    DSA Handbook - dynamic programming

    How minimum coins, count of ways and minimum cost share one recurrence: the allocation family with the reuse direction

    Depth · Reread for the allocation framing only; the loop mechanics belong to the knapsack unit.

  2. DP on an implicit DAG
    DSA Handbook - dynamic programming

    Strict-increase predicate: the implicit DAG and DFS-with-memo (LC 329); monotone moves make the cell graph acyclic

  3. Greed is stupid without a proof
    Algorithms - dynamic programming

    Section 3.5 (pp. 107-108): Warning: Greed is Stupid, with the shortest-prefix text-segmentation counterexample

  4. When exact scheduling is infeasible
    Erickson lecture note J - Approximation Algorithms

    Section J.1, Load Balancing: NP-hardness by reduction from Partition, GreedyLoadBalance, and Theorem J.1 with its factor-2 proof

Focus on

  • DP works on the cell graph because monotone moves or a strict predicate make it acyclic. Arbitrary four-directional movement is not DP.
  • Coin change is allocation: amount is the budget, denominations are unit costs, and the inner-loop direction is the reuse policy.
  • Partition makes exact load balancing NP-hard, so the greedy 2-approximation with its stated bound is the honest answer at scale.

Reproduce closed-book

In Python 3.13: longest-prefix-greedy Word Break returns False on \(s = \text{abcd}\) with dict [ab, abc, cd] while tabulated DP returns True. GreedyLoadBalance on jobs [5,5,4,4,3,3,3] with \(m = 2\) gives makespan 15 against optimal 14, inside the factor-2 bound. State both inputs and all three expected outputs.

Traps

  • Calling the greedy 2-approximation optimal. The bound is a worst-case factor, not an equality claim; 15 versus 14 on the demo inputs shows the gap.
  • Modeling allocation without stating the reuse policy. One loop direction means each unit once; the other means unlimited reuse. Name it.
  • Running memoized DFS on a cyclic move graph. With four-directional moves and no strict predicate, a call can reach a cell still on the stack, and the memo caches a value computed from an unfinished one. Prove the graph acyclic first, or switch to a shortest-path algorithm.

Practice

LeetCode 1235 - Maximum Profit in Job Scheduling

Solve tag-hidden. Example 1 returns 120 and example 2 returns 150. First show that earliest-finish greedy keeps 90 on example 1, then sort by end time and write \(\text{dp}\) over jobs with a binary search for the last compatible job.

Write the state and the take-or-skip recurrence before coding. After submitting, read the AlgoMaster Maximum Profit in Job Scheduling lesson (https://algomaster.io/learn/dsa/maximum-profit-in-job-scheduling).

LeetCode 139 - Word Break

Solve tag-hidden. Check (leetcode, [leet, code]) = True and (catsandog, [cats, dog, sand, and, cat]) = False, then submit.

Exhibit the abcd longest-prefix failure in your notes. After submitting, compare with the DSA Handbook decode-word-break page and the AlgoMaster Word Break lesson (https://algomaster.io/learn/dsa/word-break).

Self-test

  1. Word Break abcd with [ab, abc, cd]: why does longest-prefix greedy fail?

  2. Why is load balancing NP-hard, in one reduction?

  3. State the greedy load-balance bound with its two observations.

  4. A batching problem has \(10^6\) jobs and 64 machines. Exact DP or greedy?

Completion criterion

Stop when

Both tag-hidden solutions are accepted, and you can exhibit the earliest-finish and longest-prefix counterexamples plus the exact-versus-approximate boundary without notes.

Mark passed, then open 1.1 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • DSA Handbook - dynamic programming
    Handbook Academyprimarycourse
    Study Part 9 in order from state and recurrence through sequence, grid, tree and bitmask families. For every example, close the page and reconstruct the state, transition and evaluation order.
  • Algorithms - dynamic programming
    Jeff Ericksonrequiredtheory
    Use the dynamic-programming chapter to derive states and recurrences from first principles and to separate DP from greedy reasoning and shortest paths.
  • Erickson lecture note J - Approximation Algorithms
    Jeff Ericksonreferencetheory
    Read Section J.1 (Load Balancing) for the Partition reduction that makes exact scheduling NP-hard, the GreedyLoadBalance heuristic, and Theorem J.1 with its factor-2 proof.

Also in this section

  • AlgoMaster DSA - dynamic programming slice
    AlgoMasterrequiredpractice
    Attempt Coin Change, Longest Common Subsequence and House Robber III cold. Write the state, transition, base case and evaluation order first; compare against brute force on small inputs and reconstruct a witness when available.
  • Hypothesis - exhaustive and differential DP tests
    Hypothesissupportpractice
    Generate small valid instances, compare memoized, tabulated and exhaustive results, and retain shrunk failures for wrong state, base case, order and compression defects.
  • NeetCode - unseen mixed problems
    NeetCodeoptionalassessment
    Use two tag-hidden mixed problems as the final transfer test. One must require rejecting a tempting greedy or insufficient-state solution with a counterexample.
  • Python sys: recursion limit
    Python Software Foundationreferencereference
    Read sys.getrecursionlimit and sys.setrecursionlimit: the limit guards the C stack and its safe maximum is platform-dependent. Check the default with getrecursionlimit (1000 on CPython 3.13), raise it once for memoized DP, or switch to tabulation for deep inputs.
  • Erickson lecture note D - Advanced Dynamic Programming
    Jeff Ericksonreferencetheory
    Read Section D.1 (Saving Space: Divide and Conquer) for the Hirschberg Half-table split that recovers an optimal edit sequence in \(O(m + n)\) space.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels