Learn on your own. Parents beside you. Teachers guide.

Lemonmath
Tiếng Việt

Matrices and graph theory

Lesson 3.1 · 55 min read · Updated

A delivery driver wants to visit every shop in town and get back to the depot by the shortest route. A council wants to connect five villages with cable using as little cable as possible. A weather forecaster knows that a sunny day is usually followed by another sunny day. These problems sound very different, but HL students of Maths AI solve all of them with two tools: matrices, which store many numbers at once, and graphs, which are just dots joined by lines.

Neither tool needs advanced theory. What makes them powerful is that a calculator can handle large matrices in a second, and a few clear algorithms turn messy route problems into step-by-step procedures. Our job is to set the problem up correctly and to read the answer in context.

Matrices

A product ABAB is defined when the number of columns of AA equals the number of rows of BB; each entry is a row of AA times a column of BB. Order matters: usually AB≠BAAB\ne BA.

For A=(abcd)A=\begin{pmatrix}a&b\\c&d\end{pmatrix}, the determinant is det⁡A=ad−bc\det A=ad-bc and, if det⁡A≠0\det A\ne0, A−1=1ad−bc(d−b−ca).A^{-1}=\frac{1}{ad-bc}\begin{pmatrix}d&-b\\-c&a\end{pmatrix}. A system of linear equations can be written AX=BAX=B, so X=A−1BX=A^{-1}B. For 3×33\times3 systems the GDC finds A−1A^{-1} or solves directly.

Transition matrices

When a system moves between states with fixed probabilities, a transition matrix T holds those probabilities, with each column adding to 1 (column j lists where state j goes next). If s_0 is the initial state vector, then after n steps s_n=T^ns_0. In the long run many systems settle to a steady state s with Ts=s.

Graphs

A graph has vertices joined by edges; a weighted graph has a number (distance, cost, time) on each edge. The degree of a vertex is the number of edges at it, and the sum of all degrees is twice the number of edges. The adjacency matrix M has entry m_{ij} equal to the number of edges from vertex i to vertex j; the entries of M^k count the walks of length k.

  • An Eulerian circuit (using every edge once and returning to the start) exists when every vertex has even degree; an Eulerian trail exists when exactly two vertices have odd degree, and it runs between them.
  • A Hamiltonian cycle visits every vertex exactly once and returns to the start.
  • A tree is a connected graph with no cycles; a minimum spanning tree connects all vertices with the least total weight.
42510386ABCDE
A weighted graph with five vertices

Kruskal's algorithm: list the edges in order of weight; add each one unless it makes a cycle; stop when every vertex is connected.

Route inspection (Chinese postman): to travel along every edge and return to the start, find the vertices of odd degree. If there are two, repeat the shortest path between them; the least route length is the total edge weight plus that path.

Travelling salesman: to visit every vertex and return, the nearest neighbour algorithm gives an upper bound; removing one vertex, finding a minimum spanning tree of the rest and adding the two shortest edges from the removed vertex gives a lower bound.

Similar exercises

Exercise 1

Find AB for A=\begin{pmatrix}1&2\\3&0\end{pmatrix} and B=\begin{pmatrix}4&1\\-1&2\end{pmatrix}.

What you need and how to start

What you need: entry (i,j) is row i of A times column j of B.

Show solution

The product is defined because A has 2 columns and B has 2 rows. Row 1 of A times the columns of B: 1(4)+2(-1)=2 and 1(1)+2(2)=5. Row 2: 3(4)+0(-1)=12 and 3(1)+0(2)=3.

So AB=\begin{pmatrix}2&5\\12&3\end{pmatrix}.

Answer. \begin{pmatrix}2&5\\12&3\end{pmatrix}.

Exercise 2

Find the inverse of C=\begin{pmatrix}4&2\\3&2\end{pmatrix}.

What you need and how to start

What you need: \det C=4(2)-2(3).

Show solution

The determinant is \det C=8-6=2\ne0, so the inverse exists.

Swapping the diagonal entries and changing the signs of the others, then dividing by 2, gives C^{-1}=\dfrac12\begin{pmatrix}2&-2\\-3&4\end{pmatrix}=\begin{pmatrix}1&-1\\-1.5&2\end{pmatrix}.

So C^{-1}=\begin{pmatrix}1&-1\\-1.5&2\end{pmatrix}.

Answer. \begin{pmatrix}1&-1\\-1.5&2\end{pmatrix}.

Exercise 3

A graph has five vertices with degrees 3,\ 3,\ 2,\ 2,\ 2. How many edges does it have, and does it have an Eulerian circuit or an Eulerian trail?

What you need and how to start

What you need: the sum of the degrees is twice the number of edges.

Show solution

The sum of the degrees is 3+3+2+2+2=12, so the graph has \dfrac{12}{2}=6 edges.

Exactly two vertices have odd degree, so there is no Eulerian circuit, but there is an Eulerian trail starting at one odd vertex and ending at the other.

So the graph has 6 edges and an Eulerian trail, but no Eulerian circuit.

Answer. 6 edges; an Eulerian trail only.

Exercise 4

For the graph with edges AB, AC, BC and CD, write the adjacency matrix and find the number of walks of length 2 from A to D.

What you need and how to start

What you need: a walk of length 2 from A to D goes A\to X\to D.

Show solution

With vertices in the order A,B,C,D, the adjacency matrix is M=\begin{pmatrix}0&1&1&0\\1&0&1&0\\1&1&0&1\\0&0&1&0\end{pmatrix}.

A walk of length 2 from A to D must go through a vertex joined to both; only C is, so there is one walk, A\to C\to D. This is the entry in row A, column D of M^2.

So there is 1 walk of length 2 from A to D.

Answer. 1.

Exercise 5

For T=\begin{pmatrix}0.9&0.3\\0.1&0.7\end{pmatrix}, find the steady-state vector.

What you need and how to start

What you need: solve 0.9p+0.3(1-p)=p.

Show solution

At the steady state, multiplying by T leaves the vector unchanged. Let the steady state be \begin{pmatrix}p\\1-p\end{pmatrix}. The first row of Ts=s gives 0.9p+0.3(1-p)=p, so 0.3=0.4p and p=0.75.

So the steady-state vector is \begin{pmatrix}0.75\\0.25\end{pmatrix}.

Answer. \begin{pmatrix}0.75\\0.25\end{pmatrix}.

Applied exercises

Exercise 6

A museum sold 100 tickets for 880 dollars. Adult tickets cost 12 dollars, child tickets 6 dollars and senior tickets 8 dollars, and twice as many child tickets as senior tickets were sold. How many of each were sold?

What you need and how to start

What you need: three equations in a, c, s; solve with matrices on the GDC.

Show solution

Let a, c and s be the numbers of adult, child and senior tickets. Then a+c+s=100, 12a+6c+8s=880 and c-2s=0.

In matrix form \begin{pmatrix}1&1&1\\12&6&8\\0&1&-2\end{pmatrix}\begin{pmatrix}a\\c\\s\end{pmatrix}=\begin{pmatrix}100\\880\\0\end{pmatrix}, and the GDC gives a=40, c=40, s=20. Check: 480+240+160=880.

So the museum sold 40 adult, 40 child and 20 senior tickets.

Answer. 40 adult, 40 child, 20 senior.

Exercise 7

For the weighted graph in the lesson, find the length of the shortest route that travels along every edge at least once and returns to its start.

What you need and how to start

What you need: the odd vertices are B and D.

Show solution

The degrees are A:2, B:3, C:4, D:3, E:2, so the odd vertices are B and D. The total weight of all edges is 4+2+5+10+3+8+6=38.

The shortest path from B to D is B\to C\to D with length 5+3=8 (shorter than the direct edge 10, or B\to A\to C\to D with length 9).

So the shortest route has length 38+8=46.

Answer. 46.

Exercise 8

Four towns have the following distances (km): AB=5, AC=8, AD=6, BC=4, BD=7, CD=3. Find an upper bound for the travelling salesman problem with the nearest neighbour algorithm from A, and a lower bound by deleting A. What can you conclude?

What you need and how to start

What you need: the minimum spanning tree of B,C,D uses BC and CD.

Show solution

Nearest neighbour from A: the nearest town is B (5), then C (4), then D (3), then back to A (6), giving 5+4+3+6=18 km. This is an upper bound.

Deleting A, the minimum spanning tree of B, C, D uses BC and CD, weight 7. Adding the two shortest edges from A, 5 and 6, gives a lower bound of 7+11=18 km.

So the upper and lower bounds are both 18 km, and the route A\to B\to C\to D\to A is optimal.

Answer. Both bounds are 18 km; the route ABCDA is optimal.

Exercise 9 Challenge

Two phone companies share a market. Each year, 70\% of company X's customers stay and 30\% move to Y, while 80\% of Y's customers stay and 20\% move to X. Each company has half the market now. Find the market shares after 2 years and in the long run.

What you need and how to start

What you need: T=\begin{pmatrix}0.7&0.2\\0.3&0.8\end{pmatrix}, s_0=\begin{pmatrix}0.5\\0.5\end{pmatrix}.

Show solution

After one year, s_1=Ts_0=\begin{pmatrix}0.35+0.10\\0.15+0.40\end{pmatrix}=\begin{pmatrix}0.45\\0.55\end{pmatrix}, and after two years s_2=Ts_1=\begin{pmatrix}0.315+0.11\\0.135+0.44\end{pmatrix}=\begin{pmatrix}0.425\\0.575\end{pmatrix}.

For the steady state, 0.7p+0.2(1-p)=p gives 0.2=0.5p, so p=0.4.

So after two years X has 42.5\% and Y 57.5\% of the market, and in the long run X has 40\% and Y 60\%.

Answer. 42.5\% and 57.5\%; long run 40\% and 60\%.

Mini test

Marked like the Vietnamese graduation exam: Part I 0.25 points per question; Part II up to 1 point per question (1, 2, 3, 4 correct statements give 0.1, 0.25, 0.5, 1 point); Part III 0.5 points per question.

Part I. Multiple choice

Question 1. If \det A=0, then
Question 2. In a transition matrix as used here, each column adds to
Question 3. A graph has an Eulerian circuit when
Question 4. The nearest neighbour algorithm for the travelling salesman problem gives

Part II. True or false

Question 1. A graph has 6 vertices and is a tree. Decide whether each statement is true or false.
a) It is connected.
b) It has no cycles.
c) It has 5 edges.
d) It must have an Eulerian circuit.

Part III. Short answer

Question 1. Find \det\begin{pmatrix}5&2\\3&4\end{pmatrix}.

Question 2. A graph has degrees 4,\ 3,\ 3,\ 2. How many edges does it have?

Show answers and explanations
  1. Part I, question 1: C. The inverse formula divides by \det A.
  2. Part I, question 2: A. Each column lists all the places one state can go next.
  3. Part I, question 3: B. Each visit to a vertex uses one edge in and one out.
  4. Part I, question 4: D. It gives a real route, which may not be the shortest.
  5. Part II, question 1: a) True; b) True; c) True; d) False. A tree has vertices of degree 1, so it has no Eulerian circuit.
  6. Part III, question 1: 14. 20-6=14.
  7. Part III, question 2: 6. \dfrac{12}{2}=6.
📈 My progress

Finding Lemonmath useful?

Send this page to a friend, a parent or a teacher who could use it. Studying on your own is easier with company, and Lemonmath is free for everyone.

Facebook ✉ Email