Graph Basics: DAGs, Trees & BSTs

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

What this lesson covers

The lesson, slide by slide

1. What you will be able to do

Objectives

Graphs are the shape underneath almost every algorithm you will study this term. By the end of this lesson you can:

  1. Name the parts of a graph — vertices, edges, directed vs undirected — and read a small graph drawing.
  2. Compute the degree of a vertex, including in-degree and out-degree, and use the handshake lemma to check your count.
  1. Identify a path, a cycle, and a connected component in a graph.
  2. Build an adjacency list and an adjacency matrix for the same graph, and choose the right one based on space and time trade-offs.
  1. Explain what makes a directed graph acyclic, and recognize a directed acyclic graph (DAG).
  2. Define a tree as a special kind of graph, and use the binary search tree ordering property to search and insert correctly.

2. What survived from Huffman Coding?

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.

3. Toolkit check-in: name them before you look

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?

4. Break it if you can: Toolkit check-in: name them before you look

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.

5. A graph is dots and connections

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) \]

6. By analogy: A graph is dots and connections

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.

7. Two representations, two cost profiles

Picture it

Animation

Shows: Two representations, two cost profiles — a rendered Manim animation.

Rendered with Manim.

Takeaway: Traversal wants neighbours, so traversal wants lists.

8. Graphs are maps of relationships

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.

9. Teach it back: Graphs are maps of relationships

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.

10. Directed vs undirected edges

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.

11. Take the definitions apart: graph vs directed edge

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.

graph
A set of vertices together with a set of edges; where each edge connects a pair of vertices.; where V is the vertex set and E is the edge set.
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.
b1
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.
b2
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.

12. One-way street vs two-way street

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.

13. Picture it first: Reading a small undirected graph

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.

14. Reading a small undirected graph

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 \]

15. Decode the notation: Reading a small undirected graph

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\} \)

  • Every graph starts with its set of vertices — the dots. This graph has five of them.
  • Since the graph is undirected, each line is one edge and order inside the pair does not matter.
  • Count the drawn line segments: six lines. The edge set listed above also has six entries, so the reading is consistent.

16. Neighbors and adjacency

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).

17. What has to happen first: Listing neighbors from a graph

Ranking

Put in order

Put the moves of Listing neighbors from a graph into the order they have to happen.

  1. Scan the edge set for any edge that touches C
  2. Pull out the vertex on the other end of each edge
  3. Verify against the drawing

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.

18. Listing neighbors from a graph

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 \]

19. Listing neighbors from a graph — line by line

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.

20. Degree of a vertex

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 \,\}| \]

21. Plan first: Computing degree in an undirected graph

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:

  1. Count edges touching A
  2. Count edges touching B, C, D, E in the same way
  3. Verify the degrees sum to twice the edge count

22. Computing degree in an undirected graph

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.

VertexEdges touching itDegree
AAB, AC, AE3
BAB, BC2
CBC, CD, AC3
DCD, DE2
EDE, AE2

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 \]

23. Sparse and dense

Picture it

Animation

Shows: Sparse and dense — a rendered Manim animation.

Rendered with Manim.

Takeaway: The word sparse is what justifies preferring adjacency lists.

24. In-degree and out-degree

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 \]

25. Picture it first: Computing in-degree and out-degree

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.

26. Computing in-degree and out-degree

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.

VertexOut-degreeEdges out
P2P to Q, P to R
Q1Q to R
R1R to S
S1S 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.

VertexIn-degreeEdges in
P1S to P
Q1P to Q
R2Q to R, P to R
S1R 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 \]

27. Watch it run: Computing in-degree and out-degree

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?

  1. Step 1: Vertex is P
  2. Step 2: Vertex is Q
  3. Step 3: Vertex is R
  4. Step 4: Vertex is S

28. Something is wrong here: miscounting degree

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.

29. Trap: miscounting degree

Trap

The 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}) \]

The fix

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 \]

30. The handshake lemma

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| \]

31. See it: the handshake lemma

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.

32. The Graph Structure Claim skeleton

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.

33. Why every edge counts twice

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.

34. Decision point: why is the degree sum always even?

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.

35. Guess the shape of the answer: Verifying the handshake lemma

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.

36. Verifying the handshake lemma

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 \]

37. Verifying the handshake lemma — line by line

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.

38. Paths

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 \]

39. Simple paths vs walks

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.

40. Cycles

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} \]

41. A DAG has direction and no way back

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.

42. Connectivity: can you get there from here

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.

43. Connected graphs and connected components

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.

44. State the rule before it runs: Finding a path and a cycle in a graph

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.

45. Finding a path and a cycle in a graph

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 \]

46. Finding a path and a cycle in a graph — line by line

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.

47. Plan first: Counting connected components

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:

  1. Group vertices that are chained together by edges
  2. Group the remaining edge the same way
  3. Check for leftover vertices with no edges
  4. Verify every vertex was placed in exactly one group

48. Counting connected components

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 \]

49. Counting connected components — line by line

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.

50. Adjacency list representation

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.

51. Term to definition: Graph Basics: DAGs, Trees & BSTs

Matching

Match the pairs

Match each term to the definition this lesson gave it — not the one you would guess from the word.

  • t1. graph
  • t2. directed edge
  • t3. degree
  • t4. in-degree / out-degree
  • t5. adjacency list
  • d1. 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.
  • d2. 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.
  • d3. 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.
  • d4. 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.
  • d5. 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.

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.

52. Guess the shape of the answer: Building an adjacency list

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.

53. Building an adjacency list

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.

VertexNeighbors
AB, C, E
BA, C
CA, B, D
DC, E
EA, 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 \]

54. Building an adjacency list — line by line

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.

55. Adjacency matrix representation

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.

56. What has to happen first: Building an adjacency matrix for the same graph

Ranking

Put in order

Put the moves of Building an adjacency matrix for the same graph into the order they have to happen.

  1. Set up a grid with one row and one column per vertex
  2. Mark a 1 wherever an edge connects the row vertex and the column vertex
  3. Verify the matrix is symmetric and matches the edge count

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.

57. Building an adjacency matrix for the same graph

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.

ABCDE
A01101
B10100
C11010
D00101
E10010

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 \]

58. Building an adjacency matrix for the same graph — line by line

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.

59. Plan first: Looking up an edge: list vs matrix side by side

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:

  1. Check the adjacency list
  2. Check the adjacency matrix
  3. Verify both methods agree

60. Looking up an edge: list vs matrix side by side

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 \]

61. Looking up an edge: list vs matrix side by side — line by line

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.

62. Sparse graphs waste space in a matrix

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.

63. Space trade-off: list vs matrix

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}) \]

64. Time trade-off: edge lookup vs neighbor iteration

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.

65. Guess the shape of the answer: Comparing real space cost on a sparse graph

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.

66. Comparing real space cost on a sparse graph

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 \]

67. Comparing real space cost on a sparse graph — line by line

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.

68. Something is wrong here: 'the adjacency matrix is always better'

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.

69. Trap: 'the adjacency matrix is always better'

Trap

The 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} \]

The fix

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.

70. Break it on purpose: 'the adjacency matrix is always better'

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.

71. What 'acyclic' means

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.

72. Directed acyclic graphs (DAGs)

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.

73. DAGs as dependency graphs

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.

74. Picture it first: Checking whether a directed graph is acyclic

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.

75. Checking whether a directed graph is acyclic

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 \]

76. Work backwards from the answer: Checking whether a directed graph is acyclic

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.

77. Picture it first: Finding a cycle that disqualifies 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.

78. Finding a cycle that disqualifies a DAG

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 \]

79. Draw the shape of it: Finding a cycle that disqualifies a DAG

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.

80. Picture it first: Trap: what a DAG does NOT require

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.

81. Trap: what a DAG does NOT require

Trap

The 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} \]

The fix

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 \]

82. Say it in words: Trap: what a DAG does NOT require

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.

83. A tree is a special kind of graph

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.

84. Exactly one path between any two vertices

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.

85. Tree vocabulary: root, parent, child, leaf

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'.

86. What feels wrong about this pair of facts?

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.

87. Tree properties: vertex and edge counts

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} \]

88. Decision point: prove the edge count

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.

89. Verifying a graph is a tree

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 \]

90. Draw the shape of it: Verifying a graph is a tree

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.

91. A tree is connectivity with nothing spare

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.

92. Complete the line: The move we just made, named

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.

93. The move we just made, named

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.

94. Plan first: Showing a graph that fails to be a tree

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:

  1. Count vertices and edges
  2. Trace the extra edge for a cycle
  3. Verify this disqualifies it as a tree

95. Showing a graph that fails to be a tree

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 \]

96. Showing a graph that fails to be a tree — line by line

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.

97. Something is wrong here: confusing a tree with a general graph

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.

98. Trap: confusing a tree with a general graph

Trap

The 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} \]

The fix

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 \]

99. Binary trees

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.

100. The binary search tree ordering property

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.

101. Searching a binary search tree in pseudocode

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 x

This 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.

102. Reading BST-SEARCH line by line

Notation

Every line of BST-SEARCH says one thing. Read the line, then read what it does — not the other way round.

Annotate

  • Two ways out: fall off the bottom of the tree, or land on the key. Falling off means the key is absent.
  • The key is smaller, so it can only be in the left subtree. Every key on the right is larger by the ordering property.
  • The mirror case, resting on the same guarantee.
  • One comparison per level. The number of steps is the tree's HEIGHT, not its size.
  • This is why a balanced tree matters: height log n gives log n search, but a tree degenerated into a chain gives linear search.

103. Step BST-SEARCH yourself

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.

  1. Line 1: start at the root
  2. Line 3: 13 is smaller than 15
  3. Line 4: everything right of 15 is gone
  4. Line 5: 13 is larger than 6
  5. Line 6: the left subtree of 6 is gone
  6. Line 5: 13 is larger than 7
  7. Line 6: found it
  8. Line 7: three comparisons; four nodes never looked at

104. One comparison discards a whole subtree

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.

105. BST ordering as a sorted filing system

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.

106. Teach it back: BST ordering as a sorted filing system

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.

107. In-order traversal of a BST is sorted

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.

108. Picture it first: Searching for a value in 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.

109. Searching for a value in a BST

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 \]

110. Decode the notation: Searching for a value in a BST

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 \)

  • 60 is greater than 50, and the ordering property says everything greater than a node lives in its right subtree, so go right.
  • 60 is less than 70, so by the same rule the answer must be in 70's left subtree.
  • 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.

111. What has to happen first: Inserting a value into a BST

Ranking

Put in order

Put the moves of Inserting a value into a BST into the order they have to happen.

  1. Start at the root and compare
  2. Compare against 70
  3. Compare against 60 and find an empty spot
  4. Verify the ordering property still holds everywhere

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.

112. Inserting a value into a BST

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 \]

113. Process: checking the BST property the fast way

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.

114. The BST ordering property

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.

115. Picture it first: Trap: 'any binary tree is a BST'

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.

116. Trap: 'any binary tree is a BST'

Trap

The 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} \]

The fix

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 \]

117. Which of these survive contact with Graph Basics: DAGs, Trees & BSTs?

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.

Holds up
You have named 14 reusable moves so far. Say as many as you can out loud, by number, from memory.; 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.; The degree of a vertex in an undirected graph is the number of edges touching it — equivalently, the number of neighbors it has.
Breaks
Double-count edge AC because A and C are both 'visited twice' while tracing the whole graph; Pick the matrix for this graph 'because lookups are faster'
sound
These are stated as this lesson states them — each one survives the edge cases Graph Basics: DAGs, Trees & BSTs puts it through.
flawed
Each of these is lifted from a trap in this deck: reasonable-sounding, and wrong in a way that only shows up once you rely on it.

118. BST performance and tree height

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.

119. A recipe for analyzing any graph or tree

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.

120. Where this shows up: Graph Basics: DAGs, Trees & BSTs

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.

121. Decision point: which facts today were actually new?

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.

122. By analogy: Decision point: which facts today were actually new?

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.

123. Rule out three: Check yourself: degree and the handshake lemma

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.

  • A. 6
  • B. 12
  • C. 5
  • D. 3

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.

124. Check yourself: degree and the handshake lemma

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?

  • A. 6 (correct)
  • B. 12
  • C. 5
  • D. 3

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.

Why B tempts people
This reports the raw degree sum (12) directly as the edge count, forgetting that the handshake lemma requires dividing that sum by 2 before it equals the number of edges.
Why C tempts people
This mistakes the number of vertices (5) for the number of edges, ignoring the degree values given entirely.
Why D tempts people
This comes from dividing the degree sum by 2 twice (12 divided by 2 divided by 2), an extra unjustified division beyond what the handshake lemma calls for.

125. Answer it before you see the options: Check yourself: choosing a…

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.

126. Check yourself: choosing a representation

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?

  • A. 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. (correct)
  • B. The adjacency matrix always uses less memory, since it has no per-vertex list overhead.
  • C. They use exactly the same amount of memory, since both represent the same underlying graph.
  • D. The adjacency list uses more memory, since it needs a full row for every vertex just like the matrix.

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.

Why B tempts people
This assumes the matrix is inherently more memory-efficient, but it ignores that the matrix's size grows with the SQUARE of the vertex count no matter how sparse the graph actually is — here that means 40,000 cells for only 300 real edges.
Why C tempts people
This ignores that the two representations have fundamentally different storage formulas; the same graph can occupy very different amounts of memory depending on which one is used.
Why D tempts people
This confuses the adjacency list with the matrix's structure. A list only stores a vertex's actual neighbors, not a fixed-size row covering every other vertex whether or not an edge exists.

127. Answer it before you see the options: Check yourself: what a DAG allows

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.

128. Check yourself: what a DAG allows

Check

Consider the definition of a directed acyclic graph.

Check your understanding

Which of the following is true about every DAG?

  • A. It has no directed cycles, but it can still be disconnected, and it can still have multiple distinct paths between the same two vertices. (correct)
  • B. It must be connected, since a graph implies every vertex can reach every other vertex.
  • C. It cannot have two different directed paths between the same pair of vertices, since that would form a cycle.
  • D. It cannot have any vertex with more than one incoming edge.

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.

Why B tempts people
This wrongly imports 'connected' into the DAG definition. A DAG made of two separate acyclic pieces is still perfectly acyclic and still a valid DAG.
Why C tempts people
This confuses two forward paths converging on the same destination with a backward loop. A cycle requires returning to the SAME starting vertex; two different one-way routes to a shared endpoint never do that.
Why D tempts people
This invents a restriction on in-degree that is not part of the DAG definition. A vertex can have several incoming edges (multiple prerequisites, for example) without creating any cycle.

129. Check yourself: BST vs binary tree

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?

  • A. No — the left subtree must hold only values smaller than the root, but 70 is greater than 50. (correct)
  • B. Yes — every node has at most two children, so it satisfies the definition of a BST.
  • C. Yes — since 30 and 70 are both children of the root, their exact ordering does not matter.
  • D. No — because the root does not have exactly two children on both sides matching in value.

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.

Why B tempts people
This confuses the structural definition of a binary tree (at most two children per node) with the BST ordering property, which is a separate and stronger requirement about the actual values stored.
Why C tempts people
This dismisses the ordering property entirely, treating any placement of values as acceptable as long as they are children of the root — but the ordering rule is exactly what defines a BST.
Why D tempts people
This invents a false requirement that BST nodes must always have exactly two children with matching values; neither having two children nor any value-matching is part of the BST definition.

130. Rule out three: Check yourself: counting connected components

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.

  • A. 3
  • B. 2
  • C. 6
  • D. 1

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.

131. Check yourself: counting connected components

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?

  • A. 3 (correct)
  • B. 2
  • C. 6
  • D. 1

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.

Why B tempts people
This groups K, L, M with N, O and forgets to count T as its own separate component, even though T has no edge connecting it to anything.
Why C tempts people
This treats every individual vertex as its own component, ignoring that K, L, M are actually connected to each other, as are N and O.
Why D tempts people
This assumes the whole graph must be connected just because it is called 'a graph', without actually checking whether a path exists between every pair of vertices.

132. Toolkit update

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.

133. Break it if you can: Toolkit update

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.

134. Connect it up: Graph Basics: DAGs, Trees & BSTs

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.

135. What you can do now

Recap

You now have the core vocabulary and structures the rest of CS3000's algorithms will build on.

StructureThe one check that defines it
TreeConnected AND acyclic
DAGDirected AND no directed cycle (nothing about connectivity)
BSTBinary tree AND left-smaller, right-larger at every node
Sparse graphAdjacency list beats adjacency matrix on memory

Sources

  1. Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms, 3rd ed., Ch. 12 (binary search trees) & Ch. 22 (elementary graph algorithms and representations) — MIT Press, 2009.
  2. Kleinberg & Tardos, Algorithm Design, Ch. 3 (graphs: basic definitions, degree, connectivity, DAGs and topological order) — Addison-Wesley, 2006.
  3. All degree/handshake counts, adjacency table entries, space comparisons, and BST search/insert traces re-derived and checked by hand. — Verified 2026-07-18.
  4. Northeastern University CS 3000, Algorithms and Data (Summer 2026) — course page and syllabus — course.ccs.neu.edu/cs3000su26. Sets Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms (3rd ed.) as the textbook; listings follow its conventions.
  5. CS 3000 course notes and midterm references circulated by students — github.com/vigneshsaravanakumar404/CS-3000-Algorithms-Data. Notes are typeset with the algpseudocode package, which is the style the listings in this deck follow.

Want this taught 1-on-1? Alexander tutors CS3000 Algorithms — $55/session, free consultation.

Book on Wyzant · Text (657) 465-8108