Graph theory: practice problems with step-by-step solutions

Also called: Dijkstra’s algorithm · minimum spanning tree · Euler path

Free Graph theory practice problems with diagrams, hints and full worked solutions. New values every time, at three levels: degrees and edges, Euler paths and trees, shortest paths and spanning trees.

Free practice problemEasy

A tree has 12 vertices. How many edges |E| does it have?

|V| = 12

Exact answer: a whole number, no rounding.

Next problem →
Practise Graph theory for freePractise Graph theory now5 free problems every day. No card needed.

What you practise

  1. Easydegrees and edges
  2. MediumEuler paths and trees
  3. Hardshortest paths and spanning trees

How to solve Graph theory problems

  1. Write down the degree of every vertex; the degrees add up to twice the number of edges.
  2. For Euler questions, check that the graph is connected and count the vertices with odd degree.
  3. For trees, use n − 1 edges and the handshake lemma.
  4. For Dijkstra, keep a table of tentative distances and settle the smallest unsettled vertex each round.
  5. For a minimum spanning tree, sort the edges by weight and skip every edge that closes a cycle (Kruskal), or grow one tree from a start vertex (Prim).

Common mistakes

  • Forgetting to divide the degree sum by 2.
  • Calling a graph Eulerian without checking connectivity.
  • Settling a vertex in Dijkstra before its distance is the smallest.
  • Taking the lightest edges without checking for cycles.
  • Counting the Hamilton circuits of Kn as n! instead of (n − 1)!/2.

Key formulas

Handshake lemma
Σ deg(v) = 2|E|
Complete graphs
|E(Kn)| = n(n − 1)/2, |E(Km,n)| = m·n
Regular graph
|E| = n·k/2
Tree and forest
|E| = n − 1, |E| = n − c
Leaves of a tree
L = 2 + Σ (deg(v) − 2) over deg(v) ≥ 3
Euler
circuit: all degrees even; path: exactly 2 odd degrees
Hamilton circuits in Kn
(n − 1)!/2
Dijkstra update
d(v) = min(d(v), d(u) + w(u, v))
Walks of length L
(Mᴸ)ij

Learn Graph theory from scratch

Before this: Sets, relations & functions

1Vertices, edges and degrees

A graph consists of vertices (points) and edges (lines between two vertices). In a simple graph there are no loops and no double edges.

The degree of a vertex is the number of edges that meet it. Every edge has two ends, so it adds 1 to the degree of each end. That gives the handshake lemma:

Σ deg(v) = 2·|E|

Two consequences: the sum of the degrees is always even, and the number of vertices with odd degree is even too.

  • The complete graph Kn joins every pair: n(n − 1)/2 edges.
  • The complete bipartite graph Km,n joins every vertex on one side to every vertex on the other: m·n edges.
  • A k-regular graph on n vertices has n·k/2 edges.

The adjacency matrix has a 1 at (i, j) when i and j are joined. For an undirected graph it is symmetric, a row sum is a degree, and the entry (i, j) of Mᴸ counts the walks of length L from i to j.

Self-check A graph has degrees 3, 3, 2, 2, 2. How many edges does it have?
  1. 10
  2. 12
  3. 5
  4. 6

Answer: 6. The degrees add up to 12, and every edge is counted twice, so there are 12/2 = 6 edges. 5 is the number of vertices.

2Euler and Hamilton

An Euler circuit uses every edge exactly once and returns to its start. An Euler path uses every edge once but may end elsewhere. For a connected graph:

  • all degrees even: an Euler circuit exists;
  • exactly two odd degrees: an Euler path exists, from one odd vertex to the other;
  • more than two odd degrees: neither.

A Hamilton circuit visits every vertex exactly once. There is no simple degree test for it. In Kn there are (n − 1)!/2 different Hamilton circuits: fix the start, order the other n − 1 vertices, and divide by 2 for the two directions.

Self-check A connected graph has degrees 2, 3, 4, 3, 2. What does it have?
  1. An Euler circuit
  2. An Euler path, but no Euler circuit
  3. Both an Euler circuit and an Euler path that is not a circuit
  4. Neither

Answer: An Euler path, but no Euler circuit. Exactly two vertices have odd degree (the two 3s), so there is an Euler path between them, but no circuit.

3Trees

A tree is a connected graph without cycles. Every tree with n vertices has exactly n − 1 edges. Adding any edge creates a cycle; removing any edge disconnects it.

A forest of c trees on n vertices has n − c edges. A connected graph with m edges keeps a spanning tree after removing m − n + 1 edges.

The handshake lemma counts leaves: Σ deg = 2(n − 1). A short form is L = 2 + Σ (d − 2) over the vertices with degree d ≥ 3. A tree with two vertices of degree 3 and the rest leaves has 2 + 1 + 1 = 4 leaves.

4Dijkstra and minimum spanning trees

Dijkstra’s algorithm finds shortest distances from a start vertex in a graph with nonnegative weights.

  1. Give the start distance 0 and every other vertex ∞.
  2. Settle the unsettled vertex with the smallest distance; its distance is now final.
  3. For each neighbour v, update d(v) = min(d(v), d(u) + w(u, v)).
  4. Repeat until the target is settled.

A minimum spanning tree connects all vertices with the smallest total weight. Kruskal sorts the edges from light to heavy and takes each one unless it closes a cycle. Prim grows one tree from a start vertex, always adding the lightest edge that leaves it. Both stop after n − 1 edges.

Worked example

Find the shortest distance from A to E in the graph below.

1

Start

412581ABCDE

d(A) = 0. Its neighbours get d(B) = 4 and d(C) = 1.

2

Settle C (1)

Through C: d(B) = min(4, 1 + 2) = 3 and d(E) = 1 + 8 = 9.

3

Settle B (3)

Through B: d(D) = 3 + 5 = 8.

4

Settle D (8)

Through D: d(E) = min(9, 8 + 1) = 9. No improvement, a tie.

5

Settle E (9)

E is settled: its distance is final.

Answer

The shortest distance is d(E) = 9, along A → C → E. The route A → C → B → D → E has the same length, 1 + 2 + 5 + 1 = 9.

Self-check Kruskal’s algorithm has taken the edges of weight 1, 2 and 3. The next lightest edge, weight 4, joins two vertices that are already connected. What happens?
  1. It is skipped because it would close a cycle
  2. It is taken because it is the lightest left
  3. The algorithm stops
  4. The edge of weight 3 is removed instead

Answer: It is skipped because it would close a cycle. Kruskal never closes a cycle: an edge between two vertices of the same component is skipped, and the algorithm moves on to the next edge.

5Common mistakes

  • Forgetting to divide the degree sum by 2 to get the number of edges.
  • Testing for an Euler circuit without checking that the graph is connected.
  • Settling a vertex in Dijkstra before it has the smallest tentative distance.
  • Taking the n − 1 lightest edges without checking for cycles.
  • Counting Hamilton circuits of Kn as n! instead of (n − 1)!/2.

6How exam questions look

  • Easy: degrees and edges. You use the handshake lemma, count the edges of Kn, Km,n, regular graphs and trees, read an adjacency matrix, or click the vertex with a given degree.
  • Medium: Euler paths and trees. You decide whether a graph has an Euler circuit or path, count leaves of a tree, count edges to remove for a spanning tree, or count Hamilton circuits in Kn.
  • Hard: shortest paths and spanning trees. You run Dijkstra’s algorithm, find the vertex settled in a given round, find the weight of a minimum spanning tree, click the edge Kruskal or Prim adds next, or count walks with the adjacency matrix.

Learn it step by step in the appLearn it step by step in the app

Worked examples

Example 1Easy
AABBCCDDEE0010000001100010000101110

The adjacency matrix of a simple undirected graph is shown. How many edges |E| does the graph have?

Find |E|

Show hints
  1. Entry (i, j) is 1 when vertices i and j are joined by an edge.
  2. Each edge {i, j} gives two 1s: one at (i, j) and one at (j, i).
  3. The matrix contains 8 ones.
Show the answer and the step-by-step solution

Degrees: the degree of a vertex is the number of edges that meet it. Every edge has two ends, so the degrees add up to twice the number of edges: Σ deg(v) = 2|E| (the handshake lemma). The number of vertices with odd degree is therefore even. Kn has n(n − 1)/2 edges, a k-regular graph on n vertices has nk/2, and a tree on n vertices has n − 1 edges.

Count the ones

row sums: 1 + 1 + 2 + 1 + 3 = 8
|E| = 8/2 = 4

Answer

|E| = 4
Example 2Medium
ABCDEFGEuler circuitEuler path, no circuitneither

Does the connected graph have an Euler circuit, an Euler path that is not a circuit, or neither? Click your answer.

|V| = 7|E| = 10
  1. 1Euler circuit
  2. 2Euler path, no circuit
  3. 3neither
Show hints
  1. Euler’s theorem only needs the degrees of the vertices (the graph is connected).
  2. All degrees even: Euler circuit. Exactly two odd degrees: Euler path between them. More: neither.
  3. Count the vertices with odd degree.
Show the answer and explanation

Euler, Hamilton and trees: a connected graph has an Euler circuit (every edge once, back to the start) when every vertex has even degree, and an Euler path that is not a circuit when exactly two vertices have odd degree; the path then runs from one odd vertex to the other. A Hamilton circuit visits every vertex once; Kn has (n − 1)!/2 of them. A tree on n vertices has n − 1 edges, and a connected graph with m edges keeps a spanning tree after removing m − n + 1 edges.

Degrees

deg(A) = 1, deg(B) = 4, deg(C) = 2, deg(D) = 4, deg(E) = 4, deg(F) = 2, deg(G) = 3

Odd vertices

A, G → 2
Euler path, no circuit

Answer

Euler path, no circuit

The Euler path starts at one odd vertex and ends at the other.

Example 3Hard
ABCDEF

How many closed walks of length 3 start and end at E? A walk may repeat vertices and edges. Call the number N.

M = adjacency matrixL = 3

Find N

Show hints
  1. With the adjacency matrix M, the number of walks of length L from i to j is the entry (i, j) of Mᴸ.
  2. Count walks of length 2 from E to every vertex first, then take one more step.
  3. Walks of length 2 from E: A: 2, B: 1, C: 0, D: 2, E: 4, F: 1.
Show the answer and the step-by-step solution

Degrees: the degree of a vertex is the number of edges that meet it. Every edge has two ends, so the degrees add up to twice the number of edges: Σ deg(v) = 2|E| (the handshake lemma). The number of vertices with odd degree is therefore even. Kn has n(n − 1)/2 edges, a k-regular graph on n vertices has nk/2, and a tree on n vertices has n − 1 edges.

Adjacency matrix

A: 0 1 1 0 0 0
B: 1 0 0 1 1 0
C: 1 0 0 0 1 0
D: 0 1 0 0 1 1
E: 0 1 1 1 0 1
F: 0 0 0 1 1 0

One more step

(M³)EE = 2·0 + 1·1 + 0·1 + 2·1 + 4·0 + 1·1 = 4

Answer

N = 4

Questions and answers

What is Graph theory?

A graph consists of vertices (points) and edges (lines between two vertices). In a simple graph there are no loops and no double edges.

The degree of a vertex is the number of edges that meet it. Every edge has two ends, so it adds 1 to the degree of each end. That gives the handshake lemma:

How do I solve Graph theory problems step by step?
  1. Write down the degree of every vertex; the degrees add up to twice the number of edges.
  2. For Euler questions, check that the graph is connected and count the vertices with odd degree.
  3. For trees, use n − 1 edges and the handshake lemma.
  4. For Dijkstra, keep a table of tentative distances and settle the smallest unsettled vertex each round.
  5. For a minimum spanning tree, sort the edges by weight and skip every edge that closes a cycle (Kruskal), or grow one tree from a start vertex (Prim).
What are common mistakes in Graph theory?
  • Forgetting to divide the degree sum by 2.
  • Calling a graph Eulerian without checking connectivity.
  • Settling a vertex in Dijkstra before its distance is the smallest.
  • Taking the lightest edges without checking for cycles.
  • Counting the Hamilton circuits of Kn as n! instead of (n − 1)!/2.
What do the Graph theory exercises cover?
  • Easy: degrees and edges
  • Medium: Euler paths and trees
  • Hard: shortest paths and spanning trees

Every problem has new values, so you learn the method instead of an answer. Stuck? Open a small hint first, and the full explanation only when you need it.

How practising works

  • Every problem has new values, so you learn the method instead of an answer.
  • Stuck? Open a small hint first, and the full explanation only when you need it.
  • Your progress, mistakes and saved problems are kept, so you can review before the exam.

Topics before and after this one

Learn first

Courses with this topic

More topics in Mathematics & statistics

Printable worksheet

Practise Graph theory for freePractise Graph theory now5 free problems every day. No card needed.