Matrices and graph theory
🖨 Print a self-study sheet Why print?
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 is defined when the number of columns of equals the number of rows of ; each entry is a row of times a column of . Order matters: usually .
For , the determinant is and, if , A system of linear equations can be written , so . For systems the GDC finds 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.
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
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: 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}.
Find the inverse of C=\begin{pmatrix}4&2\\3&2\end{pmatrix}.
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}.
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: 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.
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: 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.
For T=\begin{pmatrix}0.9&0.3\\0.1&0.7\end{pmatrix}, find the steady-state vector.
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
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: 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.
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: 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.
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: 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.
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: 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\%.
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
The inverse formula divides by \det A.
Each column lists all the places one state can go next.
Each visit to a vertex uses one edge in and one out.
It gives a real route, which may not be the shortest.
Part II. True or false
| a) It is connected. | ||
| b) It has no cycles. | ||
| c) It has 5 edges. | ||
| d) It must have an Eulerian circuit. |
A tree has vertices of degree 1, so it has no Eulerian circuit.
Part III. Short answer
20-6=14.
\dfrac{12}{2}=6.
Show answers and explanations
- Part I, question 1: C. The inverse formula divides by \det A.
- Part I, question 2: A. Each column lists all the places one state can go next.
- Part I, question 3: B. Each visit to a vertex uses one edge in and one out.
- Part I, question 4: D. It gives a real route, which may not be the shortest.
- 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.
- Part III, question 1: 14. 20-6=14.
- Part III, question 2: 6. \dfrac{12}{2}=6.