All chapters[•]Data StructuresChapter 01
Data Structures · Chapter 1

Complexity and Proof MethodsData Structures · Chapter 1

Two programs can give the same correct answer and still differ by a factor of a million in how long they take. This chapter gives you the tools to tell which is which before you run anything: count steps, describe their growth with Big-O, prove bounds with witnesses, average a rare expensive step over many cheap ones, and prove that a loop does what you claim with induction and invariants.

Instructors: Boxi Yu · Yintong Huo · Python and Java versions (switch at the top) · Every program here has been run, and every output shown is the real output. · All chapters

Two programs, one input size

Two bars, "Program A" (n(n−1)/2 steps) and "Program B" (n steps) growing as you slide n from 10 to 1,000,000. The ratio is printed.

Why this matters

about 90–120 minutes (4 videos of 8–12 minutes plus the exercises).

Choose by growth, not by stopwatch

Seconds depend on the machine, the language and what else is running; the number of steps as a function of n does not.

Every structure in this course is a trade

Arrays, linked lists, trees, hash tables and heaps are fast at some operations and slow at others; Big-O is the language in which we state the trade.

Claims need proofs, including the ones an AI makes

Witnesses, induction and loop invariants are how you check that "this is O(n)" is true.

5 parts

Four videos with their notes, then the summary and practice. Read them in order.

01

Counting operations and Big-O

Programs are compared by the number of steps they take as a function of the input size n, not by seconds. We count the steps of two correct programs for "does this list contain a duplicate?", see how the counts grow when n doubles, and keep only the dominant term: Big-O.

step countingT(n)growth when n doublesdominant termBig-O
Open →
02

Proving asymptotic bounds

"Drop constants and smaller terms" is a rule of thumb; this video turns it into a definition you can prove. f(n) = O(g(n)) means there are constants c and n₀ with f(n) ≤ c·g(n) for every n ≥ n₀. We find such witnesses, meet Ω and Θ, and learn how to show that a bound does not hold.

O, Ω, Θwitnesses c and n₀upper, lower, tightnon-examples
Open →
03

Amortized analysis

Some operations are usually cheap and occasionally expensive. A dynamic array's append copies the whole array when it is full, yet n appends cost fewer than 3n steps. We prove that two ways: the aggregate method (add it all up) and the accounting method (charge every append a little extra and save it for the rare expensive step).

dynamic arraydoublingaggregate methodaccounting methodamortized O(1)
Open →
04

Induction and loop invariants

How do you know a loop or a formula is right for every n? Induction proves a statement for all n from a base case and a step. A loop invariant is induction applied to a loop: a fact true before the loop, kept true by every iteration, which gives the answer when the loop stops.

inductionbase caseinductive steploop invariantinitializationmaintenancetermination
Open →
05

Summary and practice

A one-page map of the chapter, five self-check questions, ten exercises with solutions, and a way to test claims an AI makes about complexity.

self-checkexercises
Open →

How to use these notes

Watch · Predict · Step through · Practise

STEP 1

Watch

Each part starts with its video, 8–10 minutes, in Python or Java.

STEP 2

Predict

Answer the amber Predict box before you open the answer.

STEP 3

Step through

The interactive demos are being added; each one is described where it will appear.

STEP 4

Practise

Finish with the self-check and the exercises, with templates in your language.