Unit 9 of 9 · about 4 hours · explain, derive

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.

You'll be able to

  • 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 problemScheduling, caching, batching and allocation formulationsExact, heuristic and approximate boundaries
1

Read

In this order, only the parts listed

0 of 4 done

  1. Coin change as the allocation familyDSA Handbook - dynamic programming

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

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

  2. DP on an implicit DAGDSA 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 proofAlgorithms - dynamic programming

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

  4. When exact scheduling is infeasibleErickson 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

2

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.
Common mistakes · 3
  • 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.
3

Try it yourself

Closed-book, before checking anything

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

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

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

    It takes abc first and strands d. The valid split ab + cd needs the shorter first piece; only the OR-over-splits recurrence compares both.

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

    Partition reduces to two-machine makespan: the jobs split evenly iff some assignment reaches . Exact optimization at scale is infeasible, which is why the greedy bound matters.

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

    Makespan at most twice optimal. covers the longest job and the average load; the last job on the bottleneck machine adds at most one to a below- base.

  4. 4.A batching problem has jobs and 64 machines. Exact DP or greedy?Answer

    Greedy with the stated bound, then measure. Load balancing is NP-hard, so exact search over assignments is infeasible at this size; GreedyLoadBalance is within a factor of 2, and sorting the jobs longest-first tightens the bound.

5

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 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).

You're done 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.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 5 more in the section
  • DSA Handbook - dynamic programmingHandbook Academy · primary

    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 programmingJeff Erickson · required

    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 AlgorithmsJeff Erickson · reference

    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 sliceAlgoMaster · required

    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 testsHypothesis · support

    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 problemsNeetCode · optional

    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 limitPython Software Foundation · reference

    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 ProgrammingJeff Erickson · reference

    Read Section D.1 (Saving Space: Divide and Conquer) for the Hirschberg Half-table split that recovers an optimal edit sequence in space.