Chapter 3: Mathematical Foundations for DSA
3.06Beginner8 topics13 min

Graph and tree mathematics

The vocabulary and counting facts of graphs and trees: vertices and edges, degree, walks and paths, cycles, connectedness, rooted trees, the n - 1 edge count of a tree and the handshaking lemma.

Learning goals

  • Use graph terms precisely
  • Derive edge counts: n - 1 for a tree, n(n - 1)/2 for a complete graph
  • Apply the handshaking lemma

Prerequisites

Discrete mathematicsCounting fundamentals
3.06.1

Vertices and edges

A graph G = (V, E) has a set of vertices (nodes) V and a set of edges E, each joining two vertices. Edges may be directed (arrows) or undirected, and weighted or unweighted. A simple graph has no self-loops and no repeated edges.

A simple undirected graph on n vertices has at most n(n - 1)/2 edges (every pair), and a simple directed graph at most n(n - 1).

n verticesMax edges undirectedMax edges directed
4612
104590
1,000499,500999,000
3.06.2

Degree

The degree of a vertex is the number of edges touching it. In a directed graph it splits into in-degree and out-degree. A leaf has degree 1. Algorithms that visit all neighbours of every vertex take time proportional to the sum of the degrees.

In an adjacency list, the length of g.get(u) is the degree of u.

Java
1int n = 4;
2int[] degree = new int[n];
3int[][] edges = {{0, 1}, {0, 2}, {0, 3}, {1, 2}};
4for (int[] e : edges) { degree[e[0]]++; degree[e[1]]++; }
5// degree = [3, 2, 2, 1]; sum = 8 = 2 * 4 edges
3.06.3

Paths and walks

A walk is a sequence of vertices where consecutive ones are joined by an edge; vertices and edges may repeat. A trail repeats no edge; a path repeats no vertex. A path with k edges visits k + 1 vertices. Shortest-path algorithms minimise the number or total weight of edges.

In a graph with n vertices every simple path has at most n - 1 edges.

3.06.4

Cycles

A cycle is a path that returns to its starting vertex without repeating other vertices (at least 3 vertices in an undirected simple graph). A graph without cycles is acyclic. A directed acyclic graph (DAG) can be topologically sorted, and a graph with a cycle cannot.

Cycle detection uses depth-first search or union-find; this matters for dependency resolution and deadlock detection.

3.06.5

Connectedness

An undirected graph is connected if a path joins every pair of vertices; otherwise it splits into connected components. A connected graph on n vertices needs at least n - 1 edges. Directed graphs have strongly connected components, in which every vertex reaches every other.

Counting components with BFS, DFS or union-find costs O(V + E).

Java
1static int components(int n, List<List<Integer>> g) {
2 boolean[] seen = new boolean[n];
3 int count = 0;
4 for (int s = 0; s < n; s++) {
5 if (seen[s]) continue;
6 count++;
7 Deque<Integer> stack = new ArrayDeque<>(); stack.push(s); seen[s] = true;
8 while (!stack.isEmpty()) {
9 int u = stack.pop();
10 for (int v : g.get(u)) if (!seen[v]) { seen[v] = true; stack.push(v); }
11 }
12 }
13 return count;
14}
3.06.6

Rooted trees

A tree is a connected acyclic graph. Choosing a root gives each other node exactly one parent, and nodes with no children are leaves. Depth is the number of edges from the root; height is the largest depth. A binary tree allows at most two children per node.

A binary tree of height h has at most 2ʰ⁺¹ - 1 nodes, so n nodes need height at least ⌊log₂ n⌋.

TermMeaning
RootNode with no parent
LeafNode with no children
Depth of vEdges from root to v
Height of treeMaximum depth of any node
SubtreeA node and all its descendants
3.06.7

Tree edge count

A tree with n vertices has exactly n - 1 edges. Reason: starting with n isolated vertices (n components), each edge added without making a cycle merges two components, so n - 1 edges are needed to reach one component, and any further edge creates a cycle.

Consequently a connected graph is a tree exactly when it has n - 1 edges. This gives a quick test for a spanning tree and the O(V) bound on tree traversals.

3.06.8

Handshaking lemma

In any undirected graph the sum of all degrees is twice the number of edges, because each edge adds 1 to the degree of both its endpoints: Σ deg(v) = 2|E|. Therefore the number of vertices with odd degree is even.

Use it to check an adjacency list: if the lengths of all lists do not sum to an even number, an edge was recorded in only one direction.

Java
1// adjacency list of an undirected graph
2long total = 0;
3for (List<Integer> nbrs : g) total += nbrs.size();
4long edges = total / 2; // exact when every edge is stored in both directions

Practice ladder

Now try it yourself

Easy

  • How many edges does a complete graph on 7 vertices have?
  • Can a graph have degrees 3, 3, 3, 1?

Medium

  • Prove that a tree with at least 2 vertices has at least two leaves
  • How many edges does a forest of 10 vertices and 3 components have?

Advanced

  • Show that a connected graph with n vertices and n - 1 edges is acyclic

Key takeaways

  • A tree on n vertices has n - 1 edges; a simple undirected graph has at most n(n - 1)/2.
  • Σ deg(v) = 2|E|: the degrees always sum to an even number.
  • Paths never revisit a vertex, so they have at most n - 1 edges.