Unit 2 of 8 · about 4 hours · derive

Aggregate, accounting and potential amortized analysis

Appends, queue transfers and table resizes all hide rare expensive steps inside cheap sequences. Without amortization you either over-reject a fine structure or ship a latency spike.

You'll be able to

  • Distinguish worst-case-per-operation from amortized cost over a sequence, and name which one a latency-critical loop needs.
  • Derive the per-push bound for geometric growth from the series, and compute copy totals for and .
  • Prove binary-counter and dynamic-array bounds by the summation, taxation and potential methods, including the derivation.

Covers

Aggregate, accounting and amortized reasoningPotential-function amortized proofs
1

Read

In this order, only the parts listed

0 of 3 done

  1. Amortised analysis (inside Computational complexity and Big-O)DSA Handbook - Foundations and linear patterns

    Amortised analysis: the aggregate, banker and potential methods on the dynamic-array example, plus the hard-real-time caveat

    Read only this section; the rest of the chapter belongs to the previous unit.

  2. Dynamic array internalsDSA Handbook - Foundations and linear patterns

    The proof in one paragraph (geometric series to ); How fast capacity actually grows (the 1000-push copy counts for and ); Five libraries (the CPython 1.125 row with the listobject.c formula and the python-dev history); What happens on the way down (shrink hysteresis); the Python-doubles and front-insertion warnings

  3. Lecture 9: Amortized Analysis, Sections 9.1 to 9.3Algorithms lecture notes - amortized analysis and hashing

    9.1 Incrementing a Binary Counter; 9.2 Counting from 0 to with 9.2.1 Summation, 9.2.2 Taxation, 9.2.3 Charging and 9.2.4 Potential (amortized cost 2 per increment); 9.3 Incrementing and Decrementing (the double-binary counter); 9.5 Generalities and Warnings

    Skip 9.4 Gray Codes; it is depth material for later. 9.5 is one page on why one structure can have several valid sets of amortized bounds.

2

Focus on

  • Every method answers one question: total cost of the sequence divided by its length. Summation counts flips, taxation banks credit, potential tracks .
  • The proof needs geometric growth: with factor the copies sum to at most ; arithmetic growth collapses to per push.
  • CPython grows lists by about 1.125x after the small-list steps 0, 4, 8, 16: about nine moves per push, the highest constant in the table, chosen to bound peak RAM.
  • Shrinking needs hysteresis (shrink at a quarter full, not a half) or alternating push and pop replays the resize forever.
Common mistakes · 3
  • Amortized does not bound the single slowest push; the resize push copies everything.
  • Python lists do not double. The formula is (newsize + (newsize >> 3) + 6) & ~3 after the 0, 4, 8 start.
  • Halving on pop breaks the bound; only the quarter rule (or no shrinking) keeps it.
3

Try it yourself

Closed-book, before checking anything

Without notes, derive the amortized bound both ways: the copy series summing to at most , and the potential with the resize case . Then append 200 ints to a list in Python 3.13.14 and confirm with sys.getsizeof that capacity steps land at lengths 1, 5, 9, 17, 25 and 33.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Derive the amortized cost per push for growth factor 2.Answer

    Resizes copy elements; plus slot writes the sequence costs , so at most 2 per push. The potential method with gives the looser constant 3; both are .

  2. 2.In the potential proof, why is the resize push amortized 3 and not ?Answer

    Actual cost plus the change. Before the push the table is full so ; after, and so . The change is and the amortized cost is .

  3. 3.How many bit flips do binary-counter increments from zero perform, exactly?Answer

    minus the number of 1 bits in . Bit flips every iterations, so it flips times.

  4. 4.Push then pop alternately at the half-full boundary with a halving shrink policy. What breaks?Answer

    Every operation triggers a full resize, so operations cost . Shrink only at a quarter full.

5

Practice

  • LeetCode 232: Implement Queue using Stacks

    Implement the two-stack lazy transfer. In your solution note, prove each element moves at most twice, so the amortized cost per operation is . Judge with the Example sequence push 1, push 2, peek, pop, empty.

    After submitting, read the handbook queues-deques section Why the patterns chapters teach the rest for the lazy-transfer argument.

You're done when

You derive the resize-push bound from without notes, and your queue solution states its amortized argument next to the transfer code.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 6 more in the section
  • DSA Handbook - Foundations and linear patternsHandbook Academy · primary

    Study Part 0, Part 1, Part 3 and Part 4 in order, plus the Part 5 Linked-list fundamentals chapter. Use the routing-rule pages for arrays versus lists, map choice, stack versus queue, two pointers, sliding windows and prefix methods before seeing solved code.

  • Algorithms lecture notes - amortized analysis and hashingJeff Erickson · support

    Use note 9 Amortized Analysis for the summation, taxation, charging and potential-function proofs, and note 5 Hashing for chaining, universal hashing and open-addressing analysis, when the handbook treatment is too brief for a proof.

Also in this section

  • AlgoMaster DSA - linear structures sliceAlgoMaster · required

    Attempt Design HashMap, Longest Substring Without Repeating Characters and Sliding Window Maximum cold. Hide tags, state the invariant first, and add randomized operation histories to Design HashMap.

  • MIT 6.042J - asymptotics and invariantsMIT OpenCourseWare · support

    Use the asymptotics, state-machine invariants and proof-method readings when a cost or correctness argument is weak. Solve only the matching problems.

  • Algorithmica - memory and data layoutSergey Slotin · support

    Read the memory hierarchy, cache and complexity-model chapters, then measure one contiguous-versus-linked and one hash-versus-ordered workload crossover.

  • NeetCode - unseen linear problemsNeetCode · optional

    Use only for two tag-hidden transfer problems after the AlgoMaster slice. Do not start another full roadmap or watch a solution before the attempt is complete.

  • Hypothesis documentation - Quickstart and stateful testingHypothesis · support

    Follow Quickstart for @given strategies and the pytest combination, Configuring test settings for max_examples and derandomize, then Rule-based state machines (rule, precondition, invariant, run_state_machine_as_test) for randomized operation histories against dict and deque oracles.

  • CPython source - list and dict objectsPython Software Foundation · reference

    Read list_resize in Objects/listobject.c for the growth formula and pattern comment, and the USABLE_FRACTION definition, perturbation-probing comment and dummy-entry handling in Objects/dictobject.c. Read the cited functions and comments only.