I. Orientation — Principle of Dynamic Programming
Dynamic programming (DP), developed systematically by Richard Bellman in the 1950s, is an algorithm-design method for problems whose optimal solutions can be constructed from optimal solutions to smaller, repeated subproblems. It replaces repeated computation with stored results and is especially useful in optimization, counting, and decision problems.
- Principle of optimality: An optimal solution contains optimal solutions to its relevant subproblems. If an optimal route from vertex to vertex passes through , then its portion from to must also be optimal under the same criteria.
- Optimal substructure: The value of a problem can be expressed using optimal values of smaller instances, as in .
- Overlapping subproblems: The same smaller instances recur. For example, a naive recursive Fibonacci computation evaluates multiple times.
- State: A state records all information needed to describe a subproblem; examples include an index , an interval , or a capacity .
- Decision: Each state considers one or more choices, such as a split position in matrix-chain multiplication.
- Recurrence: A mathematical relation connects each state to smaller states and specifies whether their values are minimized, maximized, or combined.
- Base cases: Smallest subproblems have directly known values, such as a single matrix requiring zero multiplication cost.
- Storage convention: Results are kept in a table or map so that each distinct state is solved once.
- Reconstruction: A second table may retain the decision made at each state, allowing the optimal solution—not merely its value—to be recovered.
- Complexity basis: DP time is generally the number of states multiplied by the work per state; space is determined by the number of stored states.
II. General Method — Designing a Dynamic Programming Solution
A. General method
The general method is to identify reusable subproblems, formulate a recurrence, compute every required state once, and reconstruct the resulting solution when necessary.
- Step 1—characterize the solution: Determine how an optimal solution can be decomposed. For an interval , a common characterization is that the final solution makes some decision and combines solutions to and .
- Step 2—define the state precisely: Assign one meaning to each table entry. If denotes the minimum cost for the first objects, this meaning must remain unchanged throughout the recurrence and implementation.
- Step 3—derive the recurrence: Express the state value using smaller states:
TextD[i] = min { D[j] + cost(j, i) : 0 ≤ j < i }
Here, is the optimal cost for the first items, is the previous boundary, and is the cost of choosing items through as the final component. - Step 4—specify base and impossible states: Set directly solvable states such as . A minimization state that is initially unreachable is commonly assigned , preventing it from being selected accidentally.
- Step 5—choose an evaluation strategy:
- Top-down memoization: Begin with the original problem, recursively request smaller states, and cache each result. It naturally avoids states that are never reached but uses recursion-stack space.
- Bottom-up tabulation: Evaluate states in dependency order, usually from smaller sizes to larger sizes. It avoids recursive overhead and makes time and space usage explicit.
- Step 6—record decisions: If the actual optimal arrangement is needed, store an argument such as
. Following these choices backward reconstructs the arrangement. - Bottom-up pattern: A generic minimization implementation is:
TextD[0] ← 0 for i ← 1 to n: D[i] ← +∞ for each valid predecessor j of i: candidate ← D[j] + cost(j, i) if candidate < D[i]: D[i] ← candidate choice[i] ← j return D[n]
Here, is the input size, is the value table, andchoicerecords optimal predecessors. - Correctness argument: Prove by induction over the evaluation order. The base states are correct directly; for state , assume every required smaller state is optimal, then show that the recurrence examines every valid final decision and selects the best one.
- Complexity analysis: If there are states and at most transitions per state, the running time is . Storing one value and one decision per state requires space.
- Applicability boundary: Dynamic programming is unsuitable when states omit information needed by later decisions or when optimal substructure fails. It may also be impractical when the state space is exponential, even though repeated recursion has been removed.
III. Chained Matrix Multiplication — Choosing an Optimal Parenthesization
A. Chained matrix multiplication
Chained matrix multiplication determines how to parenthesize a compatible sequence of matrices so that the number of scalar multiplications is minimized; it does not alter the matrices’ order.
- Dimension model: For matrices , let have dimensions . The product is compatible because the column count of equals the row count of .
- Cost rule: Multiplying an matrix by a matrix requires scalar multiplications under the standard algorithm.
- Why order matters: Matrix multiplication is associative, so , but the two parenthesizations can have different costs. Matrix multiplication is not commutative, so matrix order cannot be rearranged.
- State definition: Let be the minimum scalar-multiplication cost for . A single matrix needs no multiplication, giving .
- Recurrence: If the final multiplication splits after , the left and right products cost and , while combining them costs :
Textm[i,j] = min { m[i,k] + m[k+1,j] + p[i−1]·p[k]·p[j] : i ≤ k < j }
Here, and are chain endpoints, is the final split, and is the corresponding dimension value. - Evaluation order: Since depends on shorter intervals, compute chains by increasing length :
Textfor i ← 1 to n: m[i,i] ← 0 for L ← 2 to n: for i ← 1 to n−L+1: j ← i+L−1 m[i,j] ← +∞ for k ← i to j−1: q ← m[i,k] + m[k+1,j] + p[i−1]·p[k]·p[j] if q < m[i,j]: m[i,j] ← q s[i,j] ← k
The table stores the optimal split. - Worked example: Let be , be , and be .
- costs .
- costs .
- Therefore, saves scalar multiplications.
- Complexity: There are intervals and up to split positions per interval, producing time. Tables and each occupy space.
B. Applications and limitations
The algorithm is valuable whenever an associative sequence operation has parenthesization-dependent cost, but its classical cost model does not capture every practical concern.
- Applications: Database query optimization, compiler expression planning, tensor contraction, and scientific-computing pipelines use similar interval recurrences.
- Solution reconstruction: Recursively split at ; print when , otherwise reconstruct and .
- Model limitation: Scalar-operation count ignores memory hierarchy, parallel execution, sparsity, and numerical-library behavior; the theoretically cheapest parenthesization may not have the shortest wall-clock time.
IV. Optimal Storage on Tapes — Minimizing Mean Retrieval Time
A. Optimal storage on tapes
Optimal storage on tapes arranges files on sequential-access media so that expected retrieval time is minimized, because accessing a file requires scanning all files placed before it.
- Model: For files, let file have length and access probability , with . For an order , the retrieval time of the file at position is:
TextTπ(j) = Σ(k=1 to j) Lπ(k)
Here, identifies the file in position . - Objective: The mean retrieval time (MRT) is:
TextMRT(π) = Σ(j=1 to n) Pπ(j) · Tπ(j)
Each file’s completion position is weighted by its probability of retrieval. - Equal-probability rule: If every file is requested equally often, arrange files by nondecreasing length. A shorter file placed earlier reduces its own retrieval time and the retrieval times of all subsequent files.
- Unequal-probability rule: Arrange files by nondecreasing ratio , equivalently by nonincreasing . A file with is placed last because its ratio is treated as infinite.
- Exchange justification: Consider adjacent files and . Ordering contributes ; ordering contributes . The first is no worse precisely when:
TextL[a] / P[a] ≤ L[b] / P[b]
Thus, any ratio inversion can be exchanged without increasing MRT, proving the sorted order is optimal. - Worked example: Suppose files have values , , and . Their ratios are , , and , so the order is . Retrieval times are , , and , yielding:
TextMRT = 0.5(20) + 0.2(30) + 0.3(60) = 34 length-units - Algorithmic cost: Computing ratios takes ; comparison sorting takes ; evaluating MRT after sorting takes .
B. Applications and limitations
The tape-storage rule models ordered sequential access clearly, but it depends on stable lengths, known request probabilities, and a single linear storage sequence.
- Applications: The same objective appears in single-machine scheduling, where minimizing weighted completion time orders jobs by processing-time-to-weight ratio .
- Multiple tapes: With several tapes, files must be assigned to tapes as well as ordered, introducing a partitioning decision; dynamic programming may be used when tape capacities or assignment constraints are present.
- Limitations: Random-access storage, changing access frequencies, file replication, compression, and rewind costs require modified states and objective functions rather than the basic ratio rule.
Nearly through. Did unit 4 of ECAP538 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 4 of ECAP538 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill; the rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks.