This deck covers the vocabulary and structures that every graph algorithm builds on: vertices and edges, directed against undirected graphs, degree and the handshake lemma, paths, cycles, and connectivity, the trade-offs between an adjacency list and an adjacency matrix, and DAGs, trees, and binary search trees. It targets the beliefs that a matrix is always the better representation, that any tree or any binary tree behaves like a BST, and that a DAG must be connected or cannot have two paths between the same nodes, along with sloppy degree counting.
Subject: CS3000 Algorithms · 135 slides · symbolic lesson
Open the interactive version of this deck · Homework for this lesson
Objectives
Graphs are the shape underneath almost every algorithm you will study this term. By the end of this lesson you can:
Warm-up
Discussion prompt
Before we open Graph Basics: DAGs, Trees & BSTs: without looking back, what was the main idea of Huffman Coding, and what could you do by the end of it that you could not do before?
Hint: One sentence for the idea, one for the skill. If the second one is blank, that is the part to revisit.
Answer:
That deck shows how Huffman coding builds an optimal variable-length, prefix-free code from symbol frequencies. It traces the merge-the-two-smallest algorithm by hand on a six-symbol alphabet, encodes and decodes a message, and sketches the exchange argument for why the greedy strategy is provably optimal. It targets codes that are not prefix-free, merging the wrong nodes or forgetting to reinsert the merged node, the reversal that would give frequent symbols longer codes, and the false belief that fixed-length coding is never worse than Huffman.
Concept
Before any new material: cover the screen.
You have named 14 reusable moves so far. Say as many as you can out loud, by number, from memory.
Do not advance until you have actually tried. Getting four of eight is information; skipping the exercise is not.
Here they are. Score yourself.
Today adds no new moves. Every proof in this lesson is built out of the list above. That is the whole point of the list.
The question that starts every proof from here on is not how do I begin. It is which of these applies here?
Counterexample
Discussion prompt
You have named 14 reusable moves so far. Say as many as you can out loud, by number, from memory.
That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.
Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.
Answer:
Do not advance until you have actually tried. Getting four of eight is information; skipping the exercise is not.
Concept
A graph is a way to model things and the relationships between them. Each thing is a vertex (also called a node); each relationship is an edge connecting two vertices.
graph — A set of vertices together with a set of edges, where each edge connects a pair of vertices. Written as G equals V and E, where V is the vertex set and E is the edge set.
\[ G = (V, E) \]
Analogy
Discussion prompt
Explain A graph is dots and connections by analogy to something with no CS3000 Algorithms in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.
Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.
Answer:
A graph is a way to model things and the relationships between them. Each thing is a vertex (also called a node); each relationship is an edge connecting two vertices.
Picture it
Animation
Shows: Two representations, two cost profiles — a rendered Manim animation.
Rendered with Manim.
Takeaway: Traversal wants neighbours, so traversal wants lists.
Intuition
A road map is a graph: towns are vertices, roads are edges. So is a friendship network — people are vertices, friendships are edges. So is a course catalog — courses are vertices, prerequisites are edges.
Anywhere you can ask is this thing connected to that thing, you can draw a graph. The whole point of learning graphs is that the same tools answer that question no matter what the dots represent.
Explain it
Discussion prompt
Explain Graphs are maps of relationships to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.
Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.
Answer:
Anywhere you can ask is this thing connected to that thing, you can draw a graph. The whole point of learning graphs is that the same tools answer that question no matter what the dots represent.
Concept
In an undirected graph, an edge is a two-way connection — if A connects to B, B also connects to A. In a directed graph, an edge has a direction: it points from one vertex to another, and that direction matters.
directed edge — An edge with a direction, usually drawn as an arrow from a source vertex to a target vertex. An edge from A to B does not imply an edge from B to A.
Definition probe
Sort into buckets
Every line below is part of the definition of graph or of directed edge — one or the other, never both. Put each where it belongs.
Intuition
An undirected edge is a two-way street: traffic (or influence, or connection) flows both ways equally. A directed edge is a one-way street: you can drive from A to B, but there may be no road back from B to A.
A friendship graph is naturally undirected — if you are my friend, I am yours. A follows relationship on social media, or a prerequisite relationship between courses, is naturally directed — following someone does not mean they follow you back.
Picture it
Figure (svg): An undirected graph with five vertices A, B, C, D, E. Edges connect A-B, B-C, C-D, D-E, A-C, and A-E, forming a triangle A-B-C and a larger loop through D and E.
Discussion prompt
Read the picture before the words. What is this showing, and what is the one thing it is built to make obvious? Commit to an answer, then read on.
Hint: Name the parts, then say what changes between them — and if nothing changes, say what is being held still.
Answer:
Here is a small undirected graph on five vertices. Read off its vertices and edges before doing anything else with it.
Worked example
Here is a small undirected graph on five vertices. Read off its vertices and edges before doing anything else with it.
Figure (svg): An undirected graph with five vertices A, B, C, D, E. Edges connect A-B, B-C, C-D, D-E, A-C, and A-E, forming a triangle A-B-C and a larger loop through D and E.
List the vertices
Why: Every graph starts with its set of vertices — the dots. This graph has five of them.
\[ V = \{A, B, C, D, E\} \]
List the edges by reading each connecting line once
Why: Since the graph is undirected, each line is one edge and order inside the pair does not matter.
\[ E = \{\,AB,\ BC,\ CD,\ DE,\ AC,\ AE\,\} \]
Verify the count matches the picture
Why: Count the drawn line segments: six lines. The edge set listed above also has six entries, so the reading is consistent.
\[ |E| = 6\ \checkmark \]
Notation
Annotate
From Reading a small undirected graph — read this one piece at a time. What is each part doing?
On: \( V = \{A, B, C, D, E\} \)
Concept
Two vertices are adjacent if an edge connects them directly. A vertex's neighbors are exactly the vertices adjacent to it — the ones it shares an edge with.
neighbor — For a vertex v, any vertex u such that an edge connects u and v. In a directed graph, it is common to separate this into in-neighbors (edges pointing in) and out-neighbors (edges pointing out).
Ranking
Put in order
Put the moves of Listing neighbors from a graph into the order they have to happen.
Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. A vertex's neighbors come only from edges that mention it — every other edge is irrelevant to C's neighbor list.
Worked example
Using the same five-vertex graph, list every neighbor of vertex C.
\[ E = \{AB, BC, CD, DE, AC, AE\} \]
Scan the edge set for any edge that touches C
Why: A vertex's neighbors come only from edges that mention it — every other edge is irrelevant to C's neighbor list.
\[ BC,\quad CD,\quad AC \quad (\text{all mention } C) \]
Pull out the vertex on the other end of each edge
Why: Each matching edge gives exactly one neighbor: the vertex that is not C.
\[ \text{neighbors}(C) = \{A, B, D\} \]
Verify against the drawing
Why: In the figure, C has lines running to B, D, and A, and no others. That matches the three neighbors found by scanning the edge list.
\[ |\text{neighbors}(C)| = 3\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Listing neighbors from a graph", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: In the figure, C has lines running to B, D, and A, and no others. That matches the three neighbors found by scanning the edge list.
Concept
The degree of a vertex in an undirected graph is the number of edges touching it — equivalently, the number of neighbors it has.
degree — For a vertex v in an undirected graph, the count of edges incident to v, written deg(v). A vertex with no edges has degree zero.
\[ \deg(v) = |\{\, e \in E : e \text{ touches } v \,\}| \]
Step zero
Discussion prompt
Computing degree in an undirected graph — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.
Hint: It starts with: Count edges touching A
Answer:
Worked example
Find the degree of every vertex in the five-vertex graph.
\[ E = \{AB, BC, CD, DE, AC, AE\} \]
Count edges touching A
Why: A appears in AB, AC, and AE — three edges.
\[ \deg(A) = 3 \]
Count edges touching B, C, D, E in the same way
Why: Go through the edge list once per vertex, tallying every edge that mentions it.
| Vertex | Edges touching it | Degree |
|---|---|---|
| A | AB, AC, AE | 3 |
| B | AB, BC | 2 |
| C | BC, CD, AC | 3 |
| D | CD, DE | 2 |
| E | DE, AE | 2 |
Verify the degrees sum to twice the edge count
Why: Adding all five degrees gives 3 + 2 + 3 + 2 + 2 = 12, and the graph has 6 edges, so 12 equals 2 times 6 — the totals agree.
\[ 3+2+3+2+2 = 12 = 2(6)\ \checkmark \]
Picture it
Animation
Shows: Sparse and dense — a rendered Manim animation.
Rendered with Manim.
Takeaway: The word sparse is what justifies preferring adjacency lists.
Concept
In a directed graph, one degree number is not enough, because direction matters. A vertex has an in-degree — how many edges point into it — and an out-degree — how many edges point out of it.
in-degree / out-degree — For a vertex v, the in-degree counts edges arriving at v; the out-degree counts edges leaving v. A vertex can have very different in- and out-degrees.
\[ \text{in-deg}(v) + \text{out-deg}(v) = \text{total edges touching } v \]
Picture it
Figure (svg): A directed graph with vertices P, Q, R, S. Arrows go P to Q, Q to R, R to S, S to P, and P to R.
Discussion prompt
Read the picture before the words. What is this showing, and what is the one thing it is built to make obvious? Commit to an answer, then read on.
Hint: Name the parts, then say what changes between them — and if nothing changes, say what is being held still.
Answer:
This directed graph has four vertices. Find the in-degree and out-degree of each one.
Worked example
This directed graph has four vertices. Find the in-degree and out-degree of each one.
Figure (svg): A directed graph with vertices P, Q, R, S. Arrows go P to Q, Q to R, R to S, S to P, and P to R.
List the directed edges
Why: Each arrow is one directed edge, written source then target.
\[ E = \{\, P{\to}Q,\ Q{\to}R,\ R{\to}S,\ S{\to}P,\ P{\to}R \,\} \]
Count arrows leaving each vertex for out-degree
Why: P has two outgoing arrows (to Q and to R); Q, R, and S each have exactly one outgoing arrow.
| Vertex | Out-degree | Edges out |
|---|---|---|
| P | 2 | P to Q, P to R |
| Q | 1 | Q to R |
| R | 1 | R to S |
| S | 1 | S to P |
Count arrows arriving at each vertex for in-degree
Why: R receives two arrows (from Q and from P); P, Q, and S each receive exactly one.
| Vertex | In-degree | Edges in |
|---|---|---|
| P | 1 | S to P |
| Q | 1 | P to Q |
| R | 2 | Q to R, P to R |
| S | 1 | R to S |
Verify both totals equal the number of edges
Why: Out-degrees sum to 2+1+1+1 = 5 and in-degrees sum to 1+1+2+1 = 5. Both match the graph's 5 edges, as they must: every edge contributes exactly one outgoing count and one incoming count.
\[ \textstyle\sum \text{out-deg} = \sum \text{in-deg} = |E| = 5\ \checkmark \]
Pattern
Step through it
Step through Computing in-degree and out-degree one row at a time. What is driving the change, and what would the row after the last one be?
Anomaly
Predict first
A student writes this, and it looks reasonable:
A student counts vertex A's degree in the five-vertex undirected graph by walking the edge list and stopping the moment they hit 3, or counts an edge twice because A appears on both ends of a loop-like path in their scribbled notes.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: Tracing every path through the graph and tallying every time a vertex is passed conflates degree (edges touching a vertex) with how many times it shows up while walking around the whole picture.
Degree counts edges, not visits. Scan the edge list exactly once and tally every edge that mentions the vertex — no more, no less.
Why: Tracing every path through the graph and tallying every time a vertex is passed conflates degree (edges touching a vertex) with how many times it shows up while walking around the whole picture. This inflates the count to 4 instead of 3.
Trap
A student counts vertex A's degree in the five-vertex undirected graph by walking the edge list and stopping the moment they hit 3, or counts an edge twice because A appears on both ends of a loop-like path in their scribbled notes.
\[ E = \{AB, BC, CD, DE, AC, AE\} \]
Double-count edge AC because A and C are both 'visited twice' while tracing the whole graph
Why: Tracing every path through the graph and tallying every time a vertex is passed conflates degree (edges touching a vertex) with how many times it shows up while walking around the whole picture. This inflates the count to 4 instead of 3.
\[ \text{wrong: } \deg(A) = 4 \]
In a directed graph, report a single 'degree' by counting only outgoing arrows
Why: For P in the four-vertex directed graph, counting only the two outgoing arrows and ignoring the one incoming arrow from S gives an incomplete picture — direction was ignored on purpose to get a quick number.
\[ \text{wrong: 'degree'}(P) = 2 \ (\text{out-degree only, in-degree dropped}) \]
Degree counts edges, not visits. Scan the edge list exactly once and tally every edge that mentions the vertex — no more, no less.
\[ E = \{AB, BC, CD, DE, AC, AE\} \]
Count each edge touching A exactly once
Why: A appears in AB, AC, and AE — exactly three edges, each counted a single time regardless of how many times a drawing might be traced through A.
\[ \deg(A) = 3 \]
In a directed graph, always report in-degree and out-degree separately
Why: P's out-degree is 2 (to Q and R) and its in-degree is 1 (from S). Neither number alone is 'the' degree — direction changes which count you need, and dropping one loses real information.
\[ \text{in-deg}(P) = 1, \quad \text{out-deg}(P) = 2 \]
Concept
The handshake lemma says that in any undirected graph, the sum of all vertex degrees is always exactly twice the number of edges. It is a free check you can run on any degree count you compute.
\[ \sum_{v \in V} \deg(v) = 2|E| \]
Picture it
Animation
Shows: The handshake lemma — a rendered Manim animation.
Rendered with Manim.
Takeaway: A counting fact that shows up in surprisingly many proofs.
Concept
Every proof of this kind has the same five or six moves in the same order. The order is not something you rediscover each time.
It is on the right. It will stay on the right through the worked examples that follow.
Why this matters: the structure is now handled. You are not spending working memory on what comes next — you are spending all of it on the one hard step.
Step 3 is the choice that decides the proof. Peel a leaf and a tree argument falls out in two lines; peel an arbitrary vertex and the remainder may not even be connected.
Intuition
Picture handshakes at a party: every handshake involves exactly two people, and it adds one to each of their personal handshake counts. If you add up everyone's personal count, each handshake got counted twice — once for each participant.
An edge is exactly a handshake between two vertices. Adding one edge always raises the total degree by two — one for each endpoint — so the running total of all degrees is forced to be twice the edge count, no matter what the graph looks like.
Intuition
What move should we make next?
The claim, for every undirected graph:
\[ \sum_{v \in V} \deg(v) = 2 |E| \]
It holds for every graph you have ever drawn. That is not a proof.
Do not check more examples. Peel something off instead. What do you peel, and what does the hypothesis then give you?
_Look at your toolkit. Say a move number out loud before this slide advances._ A wrong guess is useful. A silent guess is not.
Estimation
Predict first
Use the five-vertex graph's degrees to check the handshake lemma holds.
Commit before you compute: what does Verifying the handshake lemma come out to? A rough magnitude and the right form is enough — the point is to have something concrete to be wrong about.
Correct: Verify the two numbers match
Why: A prediction you can defend turns the computation into a check rather than a leap of faith — and an answer that contradicts it is caught on the spot. The degree sum (12) equals twice the edge count (12), confirming the handshake lemma on this graph and confirming the degree counts from earlier were done correctly.
Worked example
Use the five-vertex graph's degrees to check the handshake lemma holds.
\[ \deg(A)=3,\ \deg(B)=2,\ \deg(C)=3,\ \deg(D)=2,\ \deg(E)=2 \]
Add up every vertex's degree
Why: The handshake lemma is about the total across all vertices, so sum the whole degree list.
\[ 3+2+3+2+2 = 12 \]
Double the edge count and compare
Why: The graph has 6 edges, so twice the edge count is 12.
\[ 2|E| = 2(6) = 12 \]
Verify the two numbers match
Why: The degree sum (12) equals twice the edge count (12), confirming the handshake lemma on this graph and confirming the degree counts from earlier were done correctly.
\[ 12 = 12\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Verifying the handshake lemma", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: The degree sum (12) equals twice the edge count (12), confirming the handshake lemma on this graph and confirming the degree counts from earlier were done correctly.
Concept
A path is a sequence of vertices where each consecutive pair is connected by an edge. A simple path never repeats a vertex.
\[ v_1, v_2, \ldots, v_k \ \text{where each } (v_i, v_{i+1}) \in E \]
Concept
A walk is any way of moving along edges, and it is allowed to revisit vertices or edges. A simple path is a stricter walk: no vertex is visited more than once.
Unless a problem specifically says walk, assume path means simple path — the version that never doubles back through a vertex it already used.
Concept
A cycle is a path that starts and ends at the same vertex, using at least one edge, and otherwise repeats no vertex along the way.
\[ v_1, v_2, \ldots, v_k, v_1 \ \text{with } v_1, \ldots, v_k \text{ all distinct} \]
Picture it
Animation
Shows: A DAG has direction and no way back — a rendered Manim animation.
Rendered with Manim.
Takeaway: Every path moves forward. Nothing ever returns to where it started.
Intuition
Ask this question about any two vertices: starting at one, can you walk along edges and eventually arrive at the other? If the answer is yes for every pair of vertices in the graph, the graph is connected.
If some pair of vertices has no route between them at all, the graph splits into separate pieces — it is disconnected, and each piece is its own self-contained island.
Concept
A graph is connected if there is a path between every pair of vertices. If it is not connected, it breaks into pieces called connected components — maximal groups of vertices that can all reach each other.
connected component — A maximal set of vertices such that every pair in the set is joined by some path, and no vertex outside the set can reach into it. Every vertex belongs to exactly one connected component.
Hypothesis
Predict first
Finding a path and a cycle in a graph is about to be worked. State your hypothesis first: which rule or definition decides this one, and what is the first move it forces? Then watch whether the example agrees with you.
Correct: Chain together edges that share a vertex, without repeating a vertex
Why: A to B is an edge, B to C is an edge, and C to D is an edge, so following them in order visits A, B, C, D with no repeats.
A hypothesis you wrote down is falsifiable; a vague sense of how it will go is not. If the example opens somewhere else, that gap is the thing worth chasing.
Worked example
Using the five-vertex graph again, find one simple path and one cycle.
\[ E = \{AB, BC, CD, DE, AC, AE\} \]
Chain together edges that share a vertex, without repeating a vertex
Why: A to B is an edge, B to C is an edge, and C to D is an edge, so following them in order visits A, B, C, D with no repeats.
\[ \text{path: } A \to B \to C \to D \]
Find a chain of edges that returns to its start
Why: A to B, B to C, and C back to A (using edge AC) forms a triangle — it starts and ends at A and repeats no other vertex.
\[ \text{cycle: } A \to B \to C \to A \]
Verify each edge used actually exists in the graph
Why: AB, BC, CD are all in the edge set for the path, and AB, BC, AC are all in the edge set for the cycle — nothing was invented.
\[ AB, BC, CD, AC \in E\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Finding a path and a cycle in a graph", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: AB, BC, CD are all in the edge set for the path, and AB, BC, AC are all in the edge set for the cycle — nothing was invented.
Step zero
Discussion prompt
Counting connected components — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.
Hint: It starts with: Group vertices that are chained together by edges
Answer:
Worked example
This graph has six vertices but far fewer edges than the earlier examples. Count its connected components.
\[ V = \{K,L,M,N,O,T\}, \quad E = \{KL, LM, NO\} \]
Group vertices that are chained together by edges
Why: K to L to M forms one connected chain, since KL and LM both exist.
\[ \{K, L, M\} \ \text{— all mutually reachable} \]
Group the remaining edge the same way
Why: N and O are connected by the edge NO, and neither one has any other edge, so they form their own separate pair.
\[ \{N, O\} \ \text{— a second group} \]
Check for leftover vertices with no edges
Why: T does not appear in any edge, so it is its own island, disconnected from everything.
\[ \{T\} \ \text{— a third group, all alone} \]
Verify every vertex was placed in exactly one group
Why: K, L, M, N, O, T are all six vertices, and they appear across the three groups exactly once each, so the component count is complete.
\[ 3 \text{ connected components}\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Counting connected components", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: T does not appear in any edge, so it is its own island, disconnected from everything.
Concept
An adjacency list stores, for every vertex, a list of its neighbors. It is the most common way to represent a graph in code, and it stores exactly the edges that exist — nothing more.
adjacency list — A collection of lists, one per vertex, where the list for vertex v contains every vertex adjacent to v. An undirected edge appears once in each endpoint's list.
Matching
Match the pairs
Match each term to the definition this lesson gave it — not the one you would guess from the word.
Why: These are the working definitions of graph, directed edge, degree, in-degree / out-degree, adjacency list as Graph Basics: DAGs, Trees & BSTs uses them. Pairing them correctly is the test of whether you could state each one with the slide switched off.
Estimation
Predict first
Build the adjacency list for the five-vertex graph.
Commit before you compute: what does Building an adjacency list come out to? A rough magnitude and the right form is enough — the point is to have something concrete to be wrong about.
Correct: Verify each neighbor entry has a matching return entry
Why: A prediction you can defend turns the computation into a check rather than a leap of faith — and an answer that contradicts it is caught on the spot. Since the graph is undirected, B appears in A's list and A appears in B's list — every entry should have a mirror image.
Worked example
Build the adjacency list for the five-vertex graph.
\[ E = \{AB, BC, CD, DE, AC, AE\} \]
Give every vertex its own row
Why: An adjacency list needs one entry per vertex, even if that vertex turns out to have few or no neighbors.
Fill each row with the vertex's neighbors from the edge list
Why: Read the edge set once per vertex, the same way degree was counted, but record the actual neighbor names instead of just a count.
| Vertex | Neighbors |
|---|---|
| A | B, C, E |
| B | A, C |
| C | A, B, D |
| D | C, E |
| E | A, D |
Verify each neighbor entry has a matching return entry
Why: Since the graph is undirected, B appears in A's list and A appears in B's list — every entry should have a mirror image. Spot-checking A/B, A/C, and A/E all confirms this.
\[ B \in \text{list}(A) \ \text{and} \ A \in \text{list}(B)\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Building an adjacency list", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: Since the graph is undirected, B appears in A's list and A appears in B's list — every entry should have a mirror image. Spot-checking A/B, A/C, and A/E all confirms this.
Concept
An adjacency matrix is a grid with one row and one column per vertex. The cell in row u, column v holds a 1 if an edge connects u and v, and a 0 otherwise.
adjacency matrix — A square grid of size the number of vertices by the number of vertices, where entry (u, v) is 1 exactly when u and v are adjacent. For an undirected graph the matrix is symmetric.
Ranking
Put in order
Put the moves of Building an adjacency matrix for the same graph into the order they have to happen.
Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. Every vertex needs both a row and a column, so a five-vertex graph produces a five by five grid.
Worked example
Build the adjacency matrix for the same five-vertex graph used for the adjacency list.
\[ E = \{AB, BC, CD, DE, AC, AE\} \]
Set up a grid with one row and one column per vertex
Why: Every vertex needs both a row and a column, so a five-vertex graph produces a five by five grid.
Mark a 1 wherever an edge connects the row vertex and the column vertex
Why: Read the edge list once and place a 1 in both the (row, column) and (column, row) cells, since the graph is undirected.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 1 | 1 | 0 | 1 |
| B | 1 | 0 | 1 | 0 | 0 |
| C | 1 | 1 | 0 | 1 | 0 |
| D | 0 | 0 | 1 | 0 | 1 |
| E | 1 | 0 | 0 | 1 | 0 |
Verify the matrix is symmetric and matches the edge count
Why: Cell (A,B) and cell (B,A) are both 1, as they must be for an undirected graph. Counting the 1s above the diagonal gives 6, matching the graph's 6 edges.
\[ 6 \text{ ones above the diagonal} = |E|\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Building an adjacency matrix for the same graph", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: Cell (A,B) and cell (B,A) are both 1, as they must be for an undirected graph. Counting the 1s above the diagonal gives 6, matching the graph's 6 edges.
Step zero
Discussion prompt
Looking up an edge: list vs matrix side by side — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.
Hint: It starts with: Check the adjacency list
Answer:
Worked example
Check whether an edge exists between C and E, once using the adjacency list and once using the adjacency matrix.
Check the adjacency list
Why: C's neighbor list is A, B, D. That list must be scanned entry by entry to see whether E appears — it does not, after checking all three entries.
\[ \text{list}(C) = \{A, B, D\},\quad E \notin \text{list}(C) \]
Check the adjacency matrix
Why: Go directly to cell (C, E) in the matrix and read its value — no scanning required, the answer is sitting in one cell.
\[ \text{matrix}[C][E] = 0 \]
Verify both methods agree
Why: The list scan and the direct matrix lookup both report that C and E are not adjacent, matching the original edge set which never lists CE.
\[ \text{no edge } CE \ \text{in either representation}\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Looking up an edge: list vs matrix side by side", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: C's neighbor list is A, B, D. That list must be scanned entry by entry to see whether E appears — it does not, after checking all three entries.
Intuition
An adjacency matrix always reserves a cell for every possible pair of vertices, whether or not an edge actually exists there. In a sparse graph — one with far fewer edges than the maximum possible — almost every cell in that grid sits at zero, doing nothing.
An adjacency list only ever stores real neighbors. It never pays for a relationship that does not exist, so it naturally shrinks or grows with the actual number of edges instead of the square of the vertex count.
Concept
The adjacency list's total size grows with the number of vertices plus the number of edges. The adjacency matrix's size grows with the number of vertices squared, no matter how many edges actually exist.
\[ \text{list: } O(|V| + |E|) \qquad \text{matrix: } O(|V|^{2}) \]
Concept
A matrix answers is there an edge between u and v instantly, by reading one cell. A list must scan a vertex's neighbor entries to answer the same question, which can take longer if that vertex has many neighbors.
The trade reverses for a different task: visit every neighbor of v. A list hands you exactly those neighbors and nothing else. A matrix forces you to scan an entire row, even past the many zero cells, to find the few real neighbors.
Estimation
Predict first
A graph has 200 vertices and only 300 edges — clearly sparse, since the maximum possible number of edges on 200 vertices is far larger. Compare the two representations' storage.
Commit before you compute: what does Comparing real space cost on a sparse graph come out to? A rough magnitude and the right form is enough — the point is to have something concrete to be wrong about.
Correct: Verify the list is dramatically smaller here
Why: A prediction you can defend turns the computation into a check rather than a leap of faith — and an answer that contradicts it is caught on the spot. 800 entries versus 40,000 cells is a fifty-fold difference, confirming that for this sparse graph the list is by far the more space-efficient choice.
Worked example
A graph has 200 vertices and only 300 edges — clearly sparse, since the maximum possible number of edges on 200 vertices is far larger. Compare the two representations' storage.
\[ |V| = 200, \quad |E| = 300 \]
Compute the matrix size
Why: The matrix always allocates a full grid of size vertices by vertices, regardless of how many edges actually exist.
\[ |V|^{2} = 200^{2} = 40{,}000 \ \text{cells} \]
Compute the list size
Why: The list needs one entry point per vertex (200) plus one neighbor entry for each endpoint of each edge — since the graph is undirected, each edge contributes two neighbor entries total.
\[ 200 + 2(300) = 200 + 600 = 800 \ \text{entries} \]
Verify the list is dramatically smaller here
Why: 800 entries versus 40,000 cells is a fifty-fold difference, confirming that for this sparse graph the list is by far the more space-efficient choice.
\[ 40{,}000 \div 800 = 50\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Comparing real space cost on a sparse graph", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: 800 entries versus 40,000 cells is a fifty-fold difference, confirming that for this sparse graph the list is by far the more space-efficient choice.
Anomaly
Predict first
A student writes this, and it looks reasonable:
A student reasons that since a matrix gives instant edge lookups, it must be the better representation to reach for by default, regardless of the graph's size or how many edges it actually has.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: This ignores that the matrix still needs 40,000 cells for a graph that only has 300 real edges — almost all of that storage is wasted zeros, and it will only get worse as the vertex count grows, since matrix size grows…
Choose the representation based on how dense the graph actually is, not on a fixed habit.
Why: This ignores that the matrix still needs 40,000 cells for a graph that only has 300 real edges — almost all of that storage is wasted zeros, and it will only get worse as the vertex count grows, since matrix size grows with the square of the vertex count.
Trap
A student reasons that since a matrix gives instant edge lookups, it must be the better representation to reach for by default, regardless of the graph's size or how many edges it actually has.
\[ |V| = 200, \quad |E| = 300 \]
Pick the matrix for this graph 'because lookups are faster'
Why: This ignores that the matrix still needs 40,000 cells for a graph that only has 300 real edges — almost all of that storage is wasted zeros, and it will only get worse as the vertex count grows, since matrix size grows with the square of the vertex count.
\[ 40{,}000 \ \text{cells stored}, \ \text{only } 600 \ \text{of them are 1s} \]
Choose the representation based on how dense the graph actually is, not on a fixed habit.
\[ |V| = 200, \quad |E| = 300 \]
Recognize this graph is sparse and pick the list
Why: 300 edges out of a possible 19,900 pairs on 200 vertices is a tiny fraction. The list's 800 total entries do the same job as the matrix's 40,000 cells with far less memory.
\[ \text{list: } 800 \ \text{entries} \ \ll \ \text{matrix: } 40{,}000 \ \text{cells} \]
Save the matrix for genuinely dense graphs, or when O(1) edge checks are worth the memory
Why: If a graph has close to the maximum possible number of edges, or the algorithm needs constant-time edge lookups far more than it needs to save memory, the matrix's fixed cost stops being wasteful and becomes worth paying.
Break the constraint
Discussion prompt
The rule this trap just fixed:
Choose the representation based on how dense the graph actually is, not on a fixed habit.
Now break it on purpose. Build a case that violates it and follow the consequences until something visibly fails. Where does the failure first show up — and would you have noticed it if you had not been looking?
Hint: The dangerous rules are the ones whose violation still produces an answer. If yours fails loudly, try to find one that fails quietly.
Answer:
This ignores that the matrix still needs 40,000 cells for a graph that only has 300 real edges — almost all of that storage is wasted zeros, and it will only get worse as the vertex count grows, since matrix size grows with the square of the vertex count.
Concept
A graph is acyclic if it contains no cycles at all — there is no way to start at a vertex, follow a sequence of edges, and return to that same vertex without reusing an edge or vertex along the way.
Acyclic is purely about the absence of a round trip back to a starting point. It says nothing about how many vertices there are, whether the graph is one connected piece, or how many different routes exist between two vertices.
Concept
A directed acyclic graph, or DAG, is a directed graph with no directed cycles. You can never follow the arrows and return to where you started.
DAG — A directed graph in which there is no sequence of directed edges that starts and ends at the same vertex. Equivalently, the vertices can be arranged in an order where every edge points forward.
Intuition
Think of course prerequisites: an edge from course X to course Y means X must be completed before Y. If that graph ever had a cycle, some course would require itself as a prerequisite through a chain of other courses — an impossible schedule.
That is exactly why dependency structures — build systems, spreadsheet formulas, task schedules — are modeled as DAGs. Acyclic guarantees there is always a valid order to do things in, one dependency at a time.
Picture it
Figure (svg): A directed diamond-shaped graph with vertices 1, 2, 3, 4. Arrows go from 1 to 2, 1 to 3, 2 to 4, and 3 to 4.
Discussion prompt
Read the picture before the words. What is this showing, and what is the one thing it is built to make obvious? Commit to an answer, then read on.
Hint: Name the parts, then say what changes between them — and if nothing changes, say what is being held still.
Answer:
Check whether this directed graph on four vertices is a DAG.
Worked example
Check whether this directed graph on four vertices is a DAG.
Figure (svg): A directed diamond-shaped graph with vertices 1, 2, 3, 4. Arrows go from 1 to 2, 1 to 3, 2 to 4, and 3 to 4.
List the directed edges
Why: Read the arrows off the drawing exactly as they point.
\[ E = \{\, 1{\to}2,\ 1{\to}3,\ 2{\to}4,\ 3{\to}4 \,\} \]
Try to trace a path back to any starting vertex
Why: Every edge moves from a lower-numbered vertex toward vertex 4; there is no edge pointing back from 4 to 1, 2, or 3, and no edge pointing back into 1 at all.
\[ \text{no edge ends at } 1; \ \text{no edge leaves } 4 \]
Verify no cycle exists anywhere in the graph
Why: Since every edge moves strictly forward in the order 1, 2, 3, 4 and nothing points backward, no sequence of edges can ever return to a vertex already visited. The graph is acyclic — it is a DAG.
\[ \text{DAG confirmed}\ \checkmark \]
Reverse engineer
Discussion prompt
Work backwards. The example finished here:
Verify no cycle exists anywhere in the graph
What was it asked to do, and what must it have been given? Reconstruct the problem from its answer.
Hint: Every quantity in the result had to enter somewhere. Account for each one.
Answer:
Check whether this directed graph on four vertices is a DAG.
Picture it
Figure (svg): A directed triangle graph with vertices X, Y, Z. Arrows go X to Y, Y to Z, and Z back to X, forming a directed cycle.
Discussion prompt
Read the picture before the words. What is this showing, and what is the one thing it is built to make obvious? Commit to an answer, then read on.
Hint: Name the parts, then say what changes between them — and if nothing changes, say what is being held still.
Answer:
Check whether this three-vertex directed graph is acyclic.
Worked example
Check whether this three-vertex directed graph is acyclic.
Figure (svg): A directed triangle graph with vertices X, Y, Z. Arrows go X to Y, Y to Z, and Z back to X, forming a directed cycle.
List the directed edges
Why: Read the three arrows off the drawing.
\[ E = \{\, X{\to}Y,\ Y{\to}Z,\ Z{\to}X \,\} \]
Follow the arrows starting from X
Why: From X, follow X to Y, then Y to Z, then Z to X — each step uses an edge that really exists.
\[ X \to Y \to Z \to X \]
Check whether this path returns to its starting vertex
Why: The path began at X and, after three edges, arrived back at X without repeating any edge — that is exactly the definition of a cycle.
\[ \text{starts and ends at } X \Rightarrow \text{cycle found} \]
Verify this graph is not a DAG
Why: A DAG cannot contain any cycle, and X to Y to Z to X is a genuine directed cycle. So despite being directed, this graph fails the acyclic requirement and is not a DAG.
\[ \text{cycle present} \Rightarrow \text{not a DAG}\ \checkmark \]
Blank canvas
Draw it
Draw what Finding a cycle that disqualifies a DAG just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.
Picture it
Figure (svg): A directed graph with two separate pieces: vertices 1, 2, 3, 4 forming a diamond with arrows 1 to 2, 1 to 3, 2 to 4, 3 to 4, and a separate pair of vertices 5 and 6 with an arrow from 5 to 6, unconnected to the diamond.
Discussion prompt
Read the picture before the words. What is this showing, and what is the one thing it is built to make obvious? Commit to an answer, then read on.
Hint: Name the parts, then say what changes between them — and if nothing changes, say what is being held still.
Answer:
A student sees a directed acyclic graph made of two separate pieces and assumes it cannot really be a DAG, because 'a graph' should be all one connected structure. A second student sees two different arrow-paths between the same two vertices and assumes that must be a hidden cycle.
Trap
A student sees a directed acyclic graph made of two separate pieces and assumes it cannot really be a DAG, because 'a graph' should be all one connected structure. A second student sees two different arrow-paths between the same two vertices and assumes that must be a hidden cycle.
Figure (svg): A directed graph with two separate pieces: vertices 1, 2, 3, 4 forming a diamond with arrows 1 to 2, 1 to 3, 2 to 4, 3 to 4, and a separate pair of vertices 5 and 6 with an arrow from 5 to 6, unconnected to the diamond.
Declare the graph disqualified because vertices 5 and 6 are not reachable from 1, 2, 3, or 4
Why: This treats connectedness as part of the DAG definition, but acyclic only ever talks about cycles. Two separate acyclic pieces are still, together, an acyclic graph — nothing requires them to touch.
\[ \text{wrong belief: disconnected} \Rightarrow \text{not a DAG} \]
Flag the two routes from 1 to 4 (via 2, and via 3) as a hidden cycle
Why: A cycle requires returning to the SAME vertex you started from. Two different one-way paths that both end at 4 never loop back to 1, so nothing here revisits a vertex — there is no cycle, just two valid routes to the same destination.
\[ \text{wrong belief: } 1{\to}2{\to}4 \text{ and } 1{\to}3{\to}4 \Rightarrow \text{cycle} \]
Acyclic means only one thing: no directed path loops back to its own starting vertex. It says nothing about connectivity and nothing about how many different paths exist between two vertices.
\[ \text{DAG requirement: no directed cycle — that's it} \]
Confirm the disconnected graph is still a DAG
Why: Check each piece separately for cycles: the diamond on 1, 2, 3, 4 has none, and the single edge 5 to 6 obviously has none either. With no cycle anywhere, the whole graph is acyclic, connected or not.
\[ \text{no cycle in either piece} \Rightarrow \text{DAG}\ \checkmark \]
Confirm two paths between the same pair is perfectly fine
Why: 1 to 2 to 4 and 1 to 3 to 4 both move strictly forward and never return to 1, 2, or 3. Multiple forward paths to the same vertex is normal in a DAG — a task can depend on several prerequisites without creating a cycle.
\[ 1{\to}2{\to}4 \ \text{and} \ 1{\to}3{\to}4 \ \text{— no repeated start}\ \checkmark \]
Translation
\( \text{wrong belief: disconnected} \Rightarrow \text{not a DAG} \)
Draw it
Translate both ways. First write the expression above as a sentence with no symbols in it at all. Then cover it, and write your sentence back as notation. If the two versions disagree, the disagreement is the thing to fix.
Concept
A tree is an undirected graph that is both connected and acyclic. That is the entire definition — every property trees are famous for follows from just those two conditions.
tree — A connected, acyclic, undirected graph. A tree is a graph — every tree satisfies the graph definition — but most graphs are not trees, since most graphs are either disconnected or contain a cycle somewhere.
Intuition
Being connected guarantees at least one path between any two vertices. Being acyclic rules out a second, different path — because if two distinct paths existed between the same two vertices, splicing them together would trace out a cycle.
So a tree is the leanest possible way to keep every vertex reachable: exactly one route between any two vertices, with no spare edges and no redundancy at all.
Concept
A tree is often drawn with one vertex singled out as the root, and every other vertex hanging below it. A vertex directly below another is its child; the vertex above is its parent. A vertex with no children is a leaf.
Choosing a root does not change the tree itself — it is the same connected, acyclic graph either way. The root just gives you a starting point for describing direction, like 'up toward the root' or 'down toward the leaves'.
Intuition
What feels wrong about this?
A tree on n vertices has exactly n minus 1 edges. A connected graph on n vertices needs at least n minus 1 edges.
\[ |E| = n - 1 \quad \text{and} \quad |E| \ge n - 1 \]
_Plain English only. No notation, no algebra. Just say what bothers you._
The feeling: a tree is sitting exactly on the boundary — it is connected using the absolute minimum number of edges, with none to spare.
That feeling is the proof. It is not a substitute for the proof — it is the thing the proof writes down.
That is why a tree breaks if you remove any edge and gains a cycle if you add any. Being exactly at the boundary is the definition doing its work, and it is the fact the whole MST lesson will rest on.
Concept
Every tree with n vertices has exactly n minus 1 edges — never more, never fewer. Any fewer and it could not stay connected; any more and a cycle would be forced somewhere.
\[ \text{tree on } n \text{ vertices} \ \Rightarrow \ n - 1 \text{ edges} \]
Intuition
What move should we make next?
The claim: every tree on n vertices has exactly n minus 1 edges.
\[ n = 1 \;\Rightarrow\; 0 \text{ edges}, \qquad n = 2 \;\Rightarrow\; 1 \text{ edge}, \qquad n = 3 \;\Rightarrow\; 2 \text{ edges} \]
Two moves, in order. Name both — and be specific about which vertex you peel, because the obvious choice does not work.
_Look at your toolkit. Say a move number out loud before this slide advances._ A wrong guess is useful. A silent guess is not.
Worked example
Check whether this graph is a tree.
Figure (svg): A rooted tree with root F, children G and H, and H has two children I and J.
Count vertices and edges
Why: There are five vertices — F, G, H, I, J — and four drawn edges: F-G, F-H, H-I, H-J.
\[ n = 5, \quad |E| = 4 = n - 1 \]
Check connectivity
Why: Starting from F, you can reach G directly, and reach H, I, J by following F to H, then H to I or H to J. Every vertex is reachable, so the graph is connected.
\[ \text{connected: every vertex reachable from } F \]
Verify there is no cycle
Why: With exactly n minus 1 edges and full connectivity already confirmed, there is no room for an extra edge to create a cycle — and inspecting the picture confirms no vertex has two different routes back to another. This graph is connected and acyclic, so it is a tree.
\[ \text{connected} + \text{acyclic} \Rightarrow \text{tree}\ \checkmark \]
Blank canvas
Draw it
Draw what Verifying a graph is a tree just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.
Picture it
Animation
Shows: A tree is connectivity with nothing spare — a rendered Manim animation.
Rendered with Manim.
Takeaway: Three equivalent definitions of the same object.
Fill the middle
Fill in the blanks
From The move we just made, named — finish the line. Write what belongs on the right of the equals sign before you look.
(n - 2) + 1 = n - 1
Why: Producing the right-hand side unprompted is the difference between recognising this line and being able to use it. Remove a random vertex and the remainder can shatter into pieces, so the hypothesis does not apply.
Concept
The move: #5 (Peel one off), then #6 (Substitute the hypothesis).
Peel off a leaf, not an arbitrary vertex
Why: Remove a random vertex and the remainder can shatter into pieces, so the hypothesis does not apply. Remove a leaf and what is left is still a tree, on n minus 1 vertices. Choosing which piece to peel is the entire proof.
Substitute the hypothesis
Why: The smaller tree has n minus 2 edges by assumption.
Put the leaf back
Why: It brings exactly one edge with it, giving n minus 1. Done.
\[ (n - 2) + 1 = n - 1 \]
Every tree has at least one leaf, which is the fact that makes the peel legal — and it is itself worth proving by contradiction as an exercise.
Step zero
Discussion prompt
Showing a graph that fails to be a tree — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.
Hint: It starts with: Count vertices and edges
Answer:
Worked example
Take the same five vertices, but add one extra edge: G to I.
\[ V = \{F,G,H,I,J\}, \quad E = \{FG, FH, HI, HJ, GI\} \]
Count vertices and edges
Why: Still five vertices, but now five edges instead of four — already one more than the n minus 1 a tree requires.
\[ n = 5, \quad |E| = 5 \neq n - 1 \]
Trace the extra edge for a cycle
Why: Following F to G, then the new edge G to I, then I back to H, then H back to F traces a closed loop: F, G, I, H, F.
\[ F \to G \to I \to H \to F \ \text{— a cycle} \]
Verify this disqualifies it as a tree
Why: A tree must be acyclic, and a genuine cycle was just found. The edge count check (5 instead of 4) already hinted at this, and tracing the cycle confirms it directly — this graph is connected but not a tree.
\[ \text{cycle found} \Rightarrow \text{not a tree}\ \checkmark \]
Picture it
Animation
Shows: Each line of the worked example "Showing a graph that fails to be a tree", appearing one at a time.
The same working the example does, in the order a tutor would write it.
Takeaway: A tree must be acyclic, and a genuine cycle was just found. The edge count check (5 instead of 4) already hinted at this, and tracing the cycle confirms it directly — this graph is connected but not a tree.
Anomaly
Predict first
A student writes this, and it looks reasonable:
A student sees any drawing with branches — vertices spreading out from a central point — and calls it a tree, without checking for cycles or disconnected pieces.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: This graph has 5 vertices but only 3 edges — one short of the 4 a tree needs — because F, G, H form one piece and I, J form a completely separate piece.
A tree is defined by two properties you must actually check: connected, and acyclic. Appearance is not a substitute for checking them.
Why: This graph has 5 vertices but only 3 edges — one short of the 4 a tree needs — because F, G, H form one piece and I, J form a completely separate piece. Looking spread-out is not the same as being connected.
Trap
A student sees any drawing with branches — vertices spreading out from a central point — and calls it a tree, without checking for cycles or disconnected pieces.
\[ V = \{F,G,H,I,J\}, \quad E = \{FG, FH, IJ\} \]
Call this a tree because it 'looks branch-like'
Why: This graph has 5 vertices but only 3 edges — one short of the 4 a tree needs — because F, G, H form one piece and I, J form a completely separate piece. Looking spread-out is not the same as being connected.
\[ |E| = 3 \neq 5 - 1; \quad \text{2 components, not 1} \]
A tree is defined by two properties you must actually check: connected, and acyclic. Appearance is not a substitute for checking them.
\[ V = \{F,G,H,I,J\}, \quad E = \{FG, FH, IJ\} \]
Check connectivity first
Why: Is there a path from F to J? Following F's edges only reaches G and H — I and J are never reached. The graph is disconnected.
\[ \text{no path from } F \text{ to } J \]
Conclude it is not a tree
Why: Since the graph fails the connected requirement, it cannot be a tree — no need to even check for cycles once connectivity has already failed. It is simply a general graph, one with two separate components.
\[ \text{disconnected} \Rightarrow \text{not a tree, just a graph}\ \checkmark \]
Concept
A binary tree is a rooted tree where every vertex has at most two children, conventionally distinguished as a left child and a right child.
binary tree — A rooted tree in which every node has zero, one, or two children, with each existing child labeled left or right. Nothing in this definition says anything about what VALUES are stored where.
Concept
A binary search tree, or BST, is a binary tree with one extra rule: for every node, every value in its left subtree is smaller than the node's value, and every value in its right subtree is larger.
BST ordering property — For every node v in the tree: all values in v's left subtree are less than v's value, and all values in v's right subtree are greater than v's value. This must hold at every single node, not only at the root.
Concept
The ordering property is not decoration — it is what lets this procedure throw away an entire subtree at every step without looking inside it.
BST-SEARCH(x, key)
while x is not NIL and key != x.key
if key < x.key
x = x.left
else
x = x.right
return xThis is binary search on a tree instead of an array. Line 4 discards the whole right subtree, unexamined, because the ordering property guarantees every key in it is too big. That guarantee is the only reason the discard is safe.
Notation
Every line of BST-SEARCH says one thing. Read the line, then read what it does — not the other way round.
Annotate
Invariant
If the key is in the tree at all, it is inside the subtree rooted at x. Every step preserves that, which is what makes returning NIL a trustworthy 'not present'.
Step through it
At each node, say which whole subtree you just discarded and why it cannot hold the key.
Picture it
Animation
Shows: BST-SEARCH executing: the current line of pseudocode is highlighted while the data it touches changes.
Rendered with Manim.
Takeaway: Each comparison discards an entire subtree, so the cost is the tree's height — log n when balanced, n when it is a chain.
Intuition
Think of the ordering property as a filing rule you follow at every drawer: smaller files go left, bigger files go right, and that same rule repeats inside every sub-drawer, all the way down.
Because that rule is consistent everywhere in the tree, you never need to search both directions at once. At each node you compare once, and that single comparison tells you which whole side of the tree can be ignored.
Explain it
Discussion prompt
Explain BST ordering as a sorted filing system to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.
Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.
Answer:
Think of the ordering property as a filing rule you follow at every drawer: smaller files go left, bigger files go right, and that same rule repeats inside every sub-drawer, all the way down.
Picture it
Animation
Shows: In-order traversal of a BST is sorted — a rendered Manim animation.
Rendered with Manim.
Takeaway: Which is the fastest way to check whether a tree really is a BST.
Picture it
Figure (svg): A binary search tree with root 50, left child 30 and right child 70. 30 has children 20 and 40. 70 has children 60 and 80.
Discussion prompt
Read the picture before the words. What is this showing, and what is the one thing it is built to make obvious? Commit to an answer, then read on.
Hint: Name the parts, then say what changes between them — and if nothing changes, say what is being held still.
Answer:
Search for the value 60 in this binary search tree.
Worked example
Search for the value 60 in this binary search tree.
Figure (svg): A binary search tree with root 50, left child 30 and right child 70. 30 has children 20 and 40. 70 has children 60 and 80.
Compare the target to the root
Why: 60 is greater than 50, and the ordering property says everything greater than a node lives in its right subtree, so go right.
\[ 60 > 50 \ \Rightarrow \ \text{go right to } 70 \]
Compare the target to the current node, 70
Why: 60 is less than 70, so by the same rule the answer must be in 70's left subtree.
\[ 60 < 70 \ \Rightarrow \ \text{go left to } 60 \]
Verify the target is found
Why: The current node's value is exactly 60, matching the search target. The path taken was 50, then 70, then 60 — two comparisons located it, because each comparison eliminated one whole subtree.
\[ \text{found } 60 \ \text{after path } 50 \to 70 \to 60\ \checkmark \]
Notation
Annotate
From Searching for a value in a BST — read this one piece at a time. What is each part doing?
On: \( \text{found } 60 \ \text{after path } 50 \to 70 \to 60\ \checkmark \)
Ranking
Put in order
Put the moves of Inserting a value into a BST into the order they have to happen.
Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. 65 is greater than 50, so by the ordering property the new value belongs somewhere in the right subtree.
Worked example
Insert the value 65 into the same binary search tree.
\[ \text{tree so far: root } 50, \ \text{right subtree rooted at } 70 \ (\text{children } 60, 80) \]
Start at the root and compare
Why: 65 is greater than 50, so by the ordering property the new value belongs somewhere in the right subtree.
\[ 65 > 50 \ \Rightarrow \ \text{go right to } 70 \]
Compare against 70
Why: 65 is less than 70, so the new value belongs in 70's left subtree, which is currently just the single node 60.
\[ 65 < 70 \ \Rightarrow \ \text{go left to } 60 \]
Compare against 60 and find an empty spot
Why: 65 is greater than 60, so it belongs to 60's right. 60 currently has no right child, so that empty spot is exactly where 65 gets attached.
\[ 65 > 60 \ \Rightarrow \ \text{attach 65 as the right child of 60} \]
Verify the ordering property still holds everywhere
Why: 65 sits in the right subtree of 50 (correct, 65 > 50), in the left subtree of 70 (correct, 65 < 70), and as the right child of 60 (correct, 65 > 60). Every ancestor relationship still checks out after the insert.
\[ 50 < 65 < 70 \ \text{and} \ 60 < 65\ \checkmark \]
Intuition
Watch me not know the answer. This is what the first two minutes actually look like.
Given a binary tree, decide whether it satisfies the binary search tree ordering property.
Try the local check: at every node, left child smaller and right child larger
Why: It is exactly what the definition says at each node, it is one line of code, and it passes on every small example anyone draws by hand.
It accepts trees that are not BSTs
Why: Root 10, left child 5, and 5 has a right child of 12. Every local check passes: 5 is less than 10, and 12 is greater than 5. But 12 sits in the left subtree of 10, so an in-order walk produces 5, 12, 10 — not sorted.
Dead end. Not a mistake — a move that was worth trying and did not pay off. This happens in most proofs.
Back up. The property is about subtrees, not children
Why: Every value in the left subtree must be less than the node, not just the immediate child. Carry a permitted range down the recursion instead of comparing pairs.
The dead end came from reading the definition one node at a time when it quantifies over a whole subtree. That misreading is worth more to you than the fix.
The expert does not see the whole path in advance. The expert tries something, reads the result, and adjusts. That is the skill.
Picture it
Animation
Shows: The BST ordering property — a rendered Manim animation.
Rendered with Manim.
Takeaway: Everything left of a node is smaller; everything right is larger.
Picture it
Figure (svg): A small binary tree with root 50, left child 70, and right child 30. The left child is larger than the root, violating BST ordering.
Discussion prompt
Read the picture before the words. What is this showing, and what is the one thing it is built to make obvious? Commit to an answer, then read on.
Hint: Name the parts, then say what changes between them — and if nothing changes, say what is being held still.
Answer:
A student sees a binary tree — root with a left and a right child — and assumes the usual go-left-if-smaller search will work, without checking whether the ordering property actually holds.
Trap
A student sees a binary tree — root with a left and a right child — and assumes the usual go-left-if-smaller search will work, without checking whether the ordering property actually holds.
Figure (svg): A small binary tree with root 50, left child 70, and right child 30. The left child is larger than the root, violating BST ordering.
Assume 'it's a binary tree, so BST search rules apply'
Why: This treats being a binary tree — a purely structural fact about having at most two children per node — as if it guaranteed the ordering property, which is a completely separate condition on the VALUES stored.
\[ \text{structure: root has left \& right child} \ \neq \ \text{ordering property holds} \]
Search for 30 by comparing to the root and going left because '30 should be on the smaller side'
Why: But the left child here is 70, which is larger than the root 50 — the ordering property is already broken at the very first level. Blindly going left based on the general BST habit walks toward 70, away from 30.
\[ \text{left child} = 70 > 50 \ \text{— property violated} \]
Before trusting BST search logic on a binary tree, check the ordering property explicitly, at every node — not just structurally counting children.
\[ \text{check: left subtree} < \text{node} < \text{right subtree, at every node} \]
Check the root's children against the ordering rule
Why: The root is 50. Its left child, 70, should be smaller than 50 — but 70 is greater than 50. The ordering property fails right here.
\[ 70 > 50 \ \Rightarrow \ \text{ordering property violated} \]
Conclude this binary tree is not a valid BST
Why: Since the required left-smaller, right-larger relationship does not hold at the root, BST search cannot be trusted on this tree at all — you would have to search it like a general binary tree, checking every node.
\[ \text{not a BST} \Rightarrow \text{no shortcut search allowed}\ \checkmark \]
Two truths and a lie
Sort into buckets
Some of these hold up and some are the exact mistakes this lesson is built to prevent. Sort them.
Concept
Both search and insert in a BST work the same way: compare, then move left or right, repeating once per level of the tree. So both operations cost time proportional to the tree's height — the number of levels from root to the deepest node.
A well-balanced BST on n values has height around the logarithm of n, making search and insert fast. But a poorly shaped BST — say, one built by inserting already-sorted values in order — can degrade into a single long chain, with height close to n itself.
Pattern
1. Identify the vertices and edges, and note directed or undirected
Why: Everything else depends on getting the basic shape right first — you cannot compute degree or check acyclicity without knowing exactly what the edge set is.
2. Compute degrees, and check them against the handshake lemma
Why: For undirected graphs, summing all degrees and comparing to twice the edge count catches counting mistakes immediately. For directed graphs, check in-degree and out-degree separately.
3. Test connectivity and search for cycles
Why: This single check tells you whether you have a general graph, a connected graph with cycles, or — if it passes both connected and acyclic — a tree.
4. Choose a representation based on density, not habit
Why: Compare the edge count to the maximum possible. A sparse graph favors an adjacency list; a graph close to fully connected, or one needing constant-time edge checks, can justify an adjacency matrix.
5. For a rooted binary tree, verify the ordering property before assuming it is a BST
Why: Having at most two children per node is a structural fact. Being a BST is a separate, stronger claim about the values — check left-smaller, right-larger at every node before trusting BST search shortcuts.
Real world
Discussion prompt
Outside this lesson: where does Graph Basics: DAGs, Trees & BSTs actually turn up? Name one concrete situation — a job, a piece of software someone ships, a decision somebody has to make — and say which part of A recipe for analyzing any graph or tree is doing the work in it.
Hint: Vague is the failure mode here. "Engineering" is not a situation; "deciding whether this build is fast enough to ship" is.
Answer:
That deck covers the vocabulary and structures that every graph algorithm builds on: vertices and edges, directed against undirected graphs, degree and the handshake lemma, paths, cycles, and connectivity, the trade-offs between an adjacency list and an adjacency matrix, and DAGs, trees, and binary search trees. It targets the beliefs that a matrix is always the better representation, that any tree or any binary tree behaves like a BST, and that a DAG must be connected or cannot have two paths between the same nodes, along with sloppy degree counting.
Intuition
What move should we make next?
Handshake lemma, tree edge counts, why a DAG has no cycle, why a BST search is a path.
Four structural facts, all proved.
For each of the four, name the move that proved it. If any of them needed a move that is not on your list, say which — and be honest about it.
_Look at your toolkit. Say a move number out loud before this slide advances._ A wrong guess is useful. A silent guess is not.
Analogy
Discussion prompt
Explain Decision point: which facts today were actually new? by analogy to something with no CS3000 Algorithms in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.
Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.
Answer:
Handshake lemma, tree edge counts, why a DAG has no cycle, why a BST search is a path.
Elimination
Eliminate the wrong options
Using the handshake lemma, how many edges does this graph have?
3 of these 4 are wrong. Strike them one at a time, and say what rules each one out before you strike the next. The survivor is the answer.
Survives elimination: A
Why: The handshake lemma says the sum of all degrees equals twice the number of edges. The degrees sum to 3+3+2+2+2 = 12, and edges equal that sum divided by 2, which is 6.
Check
A graph has five vertices with degree sequence 3, 3, 2, 2, 2.
\[ \deg\text{-sequence} = 3, 3, 2, 2, 2 \]
Check your understanding
Using the handshake lemma, how many edges does this graph have?
Answer: A
Why: The handshake lemma says the sum of all degrees equals twice the number of edges. The degrees sum to 3+3+2+2+2 = 12, and edges equal that sum divided by 2, which is 6.
Prediction
Predict first
Which statement correctly compares the two representations' memory use for this graph?
Answer it in your own words, now, with nothing to choose from. The options are on the next slide — and picking the right one off a list is an easier skill than producing it.
Correct: The adjacency list uses far less memory here, since its size scales with vertices plus edges, while the matrix always needs vertices squared cells regardless of how many edges exist.
Why: The matrix needs 200 squared, or 40,000, cells no matter what. The list needs roughly 200 vertex entries plus 2 times 300 neighbor entries, about 800 total — far smaller, because a sparse graph has very few real edges to store.
Check
A graph has 200 vertices and only 300 edges.
\[ |V| = 200, \quad |E| = 300 \]
Check your understanding
Which statement correctly compares the two representations' memory use for this graph?
Answer: A
Why: The matrix needs 200 squared, or 40,000, cells no matter what. The list needs roughly 200 vertex entries plus 2 times 300 neighbor entries, about 800 total — far smaller, because a sparse graph has very few real edges to store.
Prediction
Predict first
Which of the following is true about every DAG?
Answer it in your own words, now, with nothing to choose from. The options are on the next slide — and picking the right one off a list is an easier skill than producing it.
Correct: It has no directed cycles, but it can still be disconnected, and it can still have multiple distinct paths between the same two vertices.
Why: Acyclic only forbids a directed path that loops back to its own starting vertex. A DAG can consist of separate pieces, and it can have several different forward paths between the same two vertices, as long as none of those paths ever returns to a vertex already visited.
Check
Consider the definition of a directed acyclic graph.
Check your understanding
Which of the following is true about every DAG?
Answer: A
Why: Acyclic only forbids a directed path that loops back to its own starting vertex. A DAG can consist of separate pieces, and it can have several different forward paths between the same two vertices, as long as none of those paths ever returns to a vertex already visited.
Check
A binary tree has root 50, with left child 70 and right child 30.
\[ \text{root } 50, \ \text{left child } 70, \ \text{right child } 30 \]
Check your understanding
Is this tree a valid binary search tree?
Answer: A
Why: A BST requires every node's left subtree to hold only smaller values and its right subtree to hold only larger values. Here the left child, 70, is greater than the root, 50, which directly violates that ordering property, regardless of the tree's shape.
Elimination
Eliminate the wrong options
How many connected components does this graph have?
3 of these 4 are wrong. Strike them one at a time, and say what rules each one out before you strike the next. The survivor is the answer.
Survives elimination: A
Why: K, L, and M are chained together by edges KL and LM, forming one component. N and O form a second component through edge NO. T has no edges at all, so it forms its own third component, all alone.
Check
A graph has vertices K, L, M, N, O, T with edges K-L, L-M, and N-O only.
\[ V = \{K,L,M,N,O,T\}, \quad E = \{KL, LM, NO\} \]
Check your understanding
How many connected components does this graph have?
Answer: A
Why: K, L, and M are chained together by edges KL and LM, forming one component. N and O form a second component through edge NO. T has no edges at all, so it forms its own third component, all alone.
Concept
Moves added today: none.
That is a result, not a gap. Everything in this lesson was proved with moves you already owned.
Moves you reused today:
Graphs look like a new world and are not. Every structural fact today was proved by peeling one piece off and substituting the hypothesis — the same two moves as lesson 3.
Full toolkit so far: #1 through #14.
Next session opens with you naming every one of these from memory, before any new material.
Counterexample
Discussion prompt
Graphs look like a new world and are not. Every structural fact today was proved by peeling one piece off and substituting the hypothesis — the same two moves as lesson 3.
That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.
Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.
Answer:
Next session opens with you naming every one of these from memory, before any new material.
Connect it up
Draw it
One page, no notation unless you need it: draw how these connect — A recipe for analyzing any graph or tree · Toolkit check-in: name them before you look · A graph is dots and connections · Graphs are maps of relationships · Directed vs undirected edges. Put an arrow wherever one of them is what makes another possible, and label the arrow with why.
Recap
You now have the core vocabulary and structures the rest of CS3000's algorithms will build on.
| Structure | The one check that defines it |
|---|---|
| Tree | Connected AND acyclic |
| DAG | Directed AND no directed cycle (nothing about connectivity) |
| BST | Binary tree AND left-smaller, right-larger at every node |
| Sparse graph | Adjacency list beats adjacency matrix on memory |
Want this taught 1-on-1? Alexander tutors CS3000 Algorithms — $55/session, free consultation.