Overview

Many real problems are about points and the connections between them: computers joined by cables, cities joined by flight routes, rooms joined by hallways. A graph is the mathematical object that models all of these situations.

A graph is a collection of vertices (also called nodes) and edges. An edge is a connection between two vertices. We usually draw a graph with dots for vertices and lines for edges, but the graph itself is defined by its sets, not by any particular picture. Two drawings that look completely different can represent exactly the same graph.

Key Concepts:

  • A graph is fully described by its set of vertices and its set of edges, e.g. V = {A, B, C, D, E, F, G, H} and E = {AB, AD, BD, CF, FG, GH, GE, HE}.
  • Edges may be undirected (AB is the same as BA) or directed (AB goes from A to B only).
  • Questions usually ask you to count paths or cycles, decide whether a graph is a tree, or read a graph from its adjacency matrix.

Key Concepts

Terminology

Take the undirected graph with vertices {A, B, C, D, E, F, G, H} and edges {AB, AD, BD, CF, FG, GH, GE, HE} as a running example.

ABD CFG HE

Directed Graphs

In a directed graph every edge has a direction. The edge XY can be used to travel from X to Y but not from Y to X unless YX is also an edge. Arrows on both ends of a line mean both directions are present, which behaves like an undirected edge. A directed graph with no cycles is called a DAG (directed acyclic graph).

For example, with vertices {A, B, C, D, E, F, G, H} and directed edges {AB, AD, DA, DB, EG, GE, HG, HE, GF, CF, FC}, there is exactly one directed path from G to C (G → F → C), but no directed path from C back to G.

Trees and Forests

Adjacency Matrices

A graph with N vertices can be stored as an N × N grid. Label the rows and columns with the vertices in the same order. Put a 1 in row X, column Y if there is an edge from X to Y, and 0 otherwise. For an undirected graph the matrix is symmetric; for a directed graph it usually is not.

Counting paths with matrix powers

  • M itself counts paths of length 1 (the edges).
  • M2 counts paths of length 2: cell (i, j) of M2 is the number of two-edge routes from i to j.
  • In general, Mp(i, j) is the number of paths of length p from vertex i to vertex j.
  • To compute an entry of M2 by hand, multiply row i of M by column j of M and add: the sum counts every middle vertex k with edges i → k and k → j.

Examples

Example 1: Counting cycles

How many different cycles are in the directed graph with vertices {A, B, C, D, E} and edges {AB, BA, BC, CD, DC, DB, DE}?

List the edges leaving each vertex, then look for loops that return to their start without repeating a vertex in between.

  • A → B, B → A: the cycle ABA.
  • C → D, D → C: the cycle CDC.
  • B → C → D → B: the cycle BCDB.
  • E has no outgoing edges, so nothing that reaches E can come back.

There are 3 cycles.

Example 2: Building an adjacency matrix and squaring it

Directed graph with vertices {A, B, C, D} and edges {AB, AD, BC, BD, CB, DC}.

Matrix M (row = from, column = to):

ABCD
A0101
B0011
C0100
D0010

To find the number of paths of length 2 from A to C, multiply row A by column C: (0)(0) + (1)(1) + (0)(0) + (1)(1) = 2. The two paths are A → B → C and A → D → C.

Similarly, row A times column D gives (0)(1) + (1)(1) + (0)(0) + (1)(0) = 1, the single path A → B → D. Row B times column B gives (0)(1) + (0)(0) + (1)(1) + (1)(0) = 1, the path B → C → B.

Example 3: Reading a graph from its matrix
ABCD
A0101
B1001
C1000
D0110

The matrix is 4 × 4, so there are 4 vertices. Every 1 is an edge, read as row → column: the seven edges are {AB, AD, BA, BD, CA, DB, DC}. The graph is directed because the matrix is not symmetric (AB is present but CA has no matching AC).

Practice Problems

Problem 1 Junior

An undirected graph has vertices {A, B, C, D, E, F, G, H} and edges {AB, BC, DE, EF, FD, GH}. How many connected components does it have?

Problem 2 Junior

How many different cycles are there in the directed graph with vertices {A, B, C, D, E} and edges {AB, BC, CA, CD, DE, ED}?

Problem 3 Intermediate

A directed graph has vertices {A, B, C, D} and edges {AB, AC, BC, BD, CD, DA}. How many different paths of length 2 are there from A to D?

Problem 4 Intermediate

The adjacency matrix below represents a directed graph with vertices A, B, C, D (rows are the starting vertex, columns the ending vertex). How many different paths of length 3 are there from D back to D?

ABCD
A0110
B0011
C0001
D1000

Problem 5 Senior

How many different cycles are there in the directed graph with vertices {A, B, C, D, E} and edges {AB, AC, BC, BD, CD, DE, EA}?