Menu
Sign up Sign in

AI HL · AHL 1

Graph Theory

AHL 1.2

Adjacency matrices and degree

AHL 1.2

Adjacency matrices and degree

Target: I can write the adjacency matrix of an undirected graph, including a loop and multiple edges. (+4 more)

By the end of this section

Key points

The adjacency matrix

  • List the vertices in a fixed order. Use the same order for the rows and the columns.
  • The entry in row i, column j is the number of edges between vertex i and vertex j.
  • An undirected graph gives a symmetric matrix. A loop counts as 2 on the diagonal: it adds 2 to the degree of its vertex. (To count walks, in a later lesson, that 2 is changed to 1.)
  • The degree of a vertex is the sum of its row. All the entries add up to twice the number of edges.

Reading a matrix

  • The vertices adjacent to vertex i have a non-zero entry in row i. An entry of 2 means two edges, so add the entries; do not count them.
  • To draw the graph, use the entries above the diagonal for the edges between different vertices. A 2 on the diagonal is one loop.

Directed graphs

  • In a directed graph the rows are the vertices the edges come from and the columns are the vertices they go to.
  • The matrix is not symmetric in general. A loop counts as 1 on the diagonal.
  • Out-degree is the row sum. In-degree is the column sum.
  • The out-degrees add up to the number of edges, and so do the in-degrees.

On the calculator

Entering an adjacency matrix

  • Use the matrix template. Add rows and columns until the matrix is n by n.
  • Type the entries row by row, in the order of the vertices.
  • For an undirected graph, check that the matrix is symmetric.

Screenshot and video to come

Explore: build a graph

Press a vertex, then another, to add an edge. Press one vertex twice for a loop. Press an edge to remove it. Drag a vertex to move it.

Adjacency matrix. A loop counts 2.
Simple
Connected
Complete
Tree

Press a vertex, then another, to add an edge.

  1. Start from the ring. Add an edge and watch the sum of the degrees. By how much does it change, and why?
  2. Remove edges from the ring one at a time. When does the graph become a tree? What happens if you remove one more?
  3. Press Join every pair. How many edges are there? Add a vertex and do it again. Can you explain the number?
  4. Switch to Directed. Which vertex has the greatest out-degree? Add an edge and say which row sum and which column sum change. Why is the matrix not symmetric?

Worked examples

Problems like the ones in class. Pick one, try it, then open the working.
A matrix with a loop and multiple edges

The graph has two edges between A and B. There is also a loop at C. Write down its adjacency matrix, with the vertices in the order A, B, C, D.

ABCD
Working
  1. Set up

    4 vertices: a 4 by 4 matrix

    Rows and columns are in the order A, B, C, D. Entry in row i, column j is the number of edges between i and j.

  2. Fill in each row

    Row A: [0, 2, 0, 0], row B: [2, 0, 1, 1], row C: [0, 1, 2, 1], row D: [0, 1, 1, 0]

    A and B have two edges, so the entry is 2. The loop at C gives 2 on the diagonal.

  3. The matrix

    \(\begin{pmatrix}0 & 2 & 0 & 0 \\ 2 & 0 & 1 & 1 \\ 0 & 1 & 2 & 1 \\ 0 & 1 & 1 & 0\end{pmatrix}\)

    An undirected graph gives a symmetric matrix: the entry for AB equals the entry for BA.

Video walkthrough coming
Drawing a graph from a matrix

A graph has adjacency matrix \(\begin{pmatrix}0 & 1 & 0 & 2 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 2 & 1 \\ 2 & 0 & 1 & 0\end{pmatrix}\), with rows and columns in the order A, B, C, D. Draw the graph. How many edges does it have?

Working
  1. Read above the diagonal

    AB: 1, AD: 2, BC: 1, CD: 1

    Each entry above the diagonal says how many edges join that pair. Entries below repeat them.

  2. The diagonal

    C: 2, so one loop at C

    A 2 on the diagonal is one loop. A, B and D have 0.

  3. ABCD
    Draw

    6 edges: AB, AD (2 edges), BC, a loop at C and CD

    Two edges join A and D, drawn as two curves. The loop at C is drawn as a small circle on C.

Video walkthrough coming
Neighbours and degree from a matrix

The adjacency matrix of a graph, with rows and columns in the order A, B, C, D, E, is \(\begin{pmatrix}0 & 1 & 0 & 2 & 1 \\ 1 & 0 & 1 & 0 & 0 \\ 0 & 1 & 0 & 1 & 1 \\ 2 & 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 1 & 0\end{pmatrix}\). List the vertices adjacent to B, and find the degree of A.

Working
  1. Row B

    Row B: [1, 0, 1, 0, 0]

    Adjacent vertices have a non-zero entry in the row.

  2. Adjacent to B

    A and C

    The entries in columns A and C are non-zero.

  3. Degree of A

    0 + 1 + 0 + 2 + 1 = 4

    The degree of a vertex is its row sum, because each entry counts edges.

Video walkthrough coming
Out-degree and in-degree

The directed graph has adjacency matrix \(\begin{pmatrix}0 & 1 & 1 & 0 \\ 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 0\end{pmatrix}\). Rows are the vertex an edge is from and columns the vertex it goes to, in the order A, B, C, D. Find the out-degree and the in-degree of B.

ABCD
Working
  1. Out-degree: the row

    Row B: [1, 0, 1, 0], sum 2

    Row B lists the edges leaving B.

  2. In-degree: the column

    Column B: [1, 0, 0, 0], sum 1

    Column B lists the edges arriving at B.

  3. Check

    Out-degrees add to 6, in-degrees add to 6

    Both totals equal the number of edges. The matrix is not symmetric.

Video walkthrough coming
Drawing a directed graph

A directed graph has adjacency matrix \(\begin{pmatrix}0 & 1 & 1 & 0 \\ 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 0\end{pmatrix}\), with rows and columns in the order P, Q, R, S. Rows are the vertex an edge leaves. Draw the graph. How many edges does it have?

Working
  1. Row by row

    P: to Q and R; Q: to P and R; R: to S; S: to P

    A 1 in row P, column Q is an arrow from P to Q.

  2. PQRS
    Draw

    6 edges

    P to Q and Q to P are two separate edges, drawn as curves either side. The matrix is not symmetric.

Video walkthrough coming