University of Auckland · FACULTY OF COMPUTER SCIENCE

COMPSCI320 Chap.8 Dynamic Programming Foundations

- one subject, every graph, every model, every mark
6 Chapters5-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 8 of 13 · COMPSCI320

Dynamic Programming Foundations

Dynamic programming stores the answers to overlapping subproblems so each is solved once.

This chapter follows the tutorial recipe of state definition, transition and base cases, fills and traces back a table for the maximum-weight independent set on a path, builds a transition over all earlier states for the longest rising subsequence, and solves weighted interval scheduling with a predecessor index.

Each worked problem states the meaning of a table entry before its equation, fills the table and traces back the decisions.

The practice cards cover a one-dimensional run and weighted interval scheduling, and the closing note connects the circular version of the path problem from the fourth programming assignment.

In this chapter

What this chapter covers

  • 01

    State, transition and base cases

  • 02

    Memoised recursion and bottom-up tables

  • 03

    Independent sets on a path

  • 04

    Longest rising subsequence

  • 05

    Maximum subarray and sub-matrix sums

  • 06

    Weighted interval scheduling

Worked example · free

Find a maximum-weight independent set on a path

Q [4 marks]. A path has vertex weights 5, 8, 6, 3, 7 and 4 in order. Use S[i] = max(S[i − 1], S[i − 2] + w_i) to find the best total and a set achieving it. The marks are an AskSia practice weighting, not a university marking scheme.
Maximum-weight independent set on a path with weights 5, 8, 6, 3, 7 and 4.Maximum-weight independent set on a path with weights 5, 8, 6, 3, 7 and 4. The orange row holds S[i], the best total using the first i vertices; green vertices are the chosen set with total 18.5588611311718418
Maximum-weight independent set on a path with weights 5, 8, 6, 3, 7 and 4. The orange row holds S[i], the best total using the first i vertices; green vertices are the chosen set with total 18.
  • 1Start with S[0] = 0 and S[1] = 5.
  • 1Fill S[2] to S[6] as 8, 11, 11, 18, 18.
  • 1Trace back: S[5] = S[3] + 7, S[3] = S[1] + 6, and S[1] takes vertex 1.
  • 1The set of vertices 1, 3 and 5 has weight 5 + 6 + 7 = 18.
This four-mark allocation is an AskSia practice weighting and not an official university scheme. The best total is 18 from vertices 1, 3 and 5, found in linear time.
Sia tip — Write what S[i] means in words before the recurrence; the traceback compares each entry with its two options.
Glossary

Key terms

Dynamic Programming
A technique that solves a problem through a table of overlapping subproblems, each solved once.
State Definition
A precise statement of what one table entry represents in a dynamic program.
Memoisation
Caching the result of each recursive call so that a repeated subproblem is answered from the cache.
Independent Set
A set of vertices in which no two are joined by an edge.
Transition Equation
The formula that expresses one table entry of a dynamic program through smaller entries.
Traceback
The backward walk through a filled table that recovers the decisions behind an optimal value.
FAQ

Dynamic Programming Foundations FAQ

What should come first in a dynamic programming answer?

The state definition, written in words. Say exactly what one entry means, for example the best weight using the first i vertices, and check that the original problem is one of the entries. The transition, base cases and running time all follow from that sentence, and a wrong or vague state is the most common reason a recurrence fails.

How do I get the running time of a dynamic program?

Multiply the number of table entries by the work needed to fill one entry. The path problem has n entries and constant work each, so linear time. The longest rising subsequence has n entries but each scans all earlier days, so quadratic time. Add the cost of any sorting or preprocessing, such as computing predecessor indices.

Is memoisation or a bottom-up loop better?

They do the same work when every subproblem is needed. Memoisation follows the recurrence directly and computes only the entries actually reached; a bottom-up loop avoids recursion overhead and depth limits on very long inputs. For a by-hand exam answer either is fine, provided you state the order in which entries are filled.

How do I recover the actual solution, not just its value?

Store the table, then walk backwards from the answer entry, asking which option of the transition produced each value. For the path problem, an entry equal to the previous one means the current vertex was skipped; otherwise it was taken and the walk jumps back two places. Recording the choice in a second array makes this automatic.

Study strategy

Exam move

For every problem in this chapter write the three recipe parts on one card: the state in words, the transition with every option, and the base cases. Solve each card on a fresh instance, then trace back the decisions. Finally state each running time as entries times work per entry.

Before the exam, solve one new problem from scratch with the recipe, timing yourself, and check the answer against a brute-force enumeration on a tiny input. Keep a list of transitions you have seen, noting whether each looks back one step, two steps or over all earlier states.

Working through Dynamic Programming Foundations in COMPSCI320? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI320 Dynamic Programming Foundations question and get a clear, step-by-step explanation grounded in how COMPSCI320 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 34 of your University of Auckland subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMPSCI320 tutor, unlimited, worked the way the exam marks it
The full 5-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works