Mathematics for Computer Science 201-N11 • CEGEP, Montreal

Corrected exercises: introduction to graphs and matrices (201-N11)

This is the corrected exercise set on graphs and matrices for Mathematics for Computer Science, 201-N11, the discrete mathematics course taken at CEGEP by students in computer science programs (Computer Science Technology, and Computer Science and Mathematics). Part A covers the basics: graph vocabulary with the handshaking lemma, the adjacency matrix of a directed graph and how to read in-degrees and out-degrees from it, the sum, product and transpose of matrices together with noncommutativity, powers of the adjacency matrix and counting paths, then connectivity and the reachability matrix. Part B rises to exam level: plane transformations written as matrices and their effect on areas, the determinant and the inverse matrix, a weighted road network in which the shortest route is not the one with the fewest links, and composing transformations in homogeneous coordinates.

Exercise 0.1 opens the set with ten matrix computations to carry out directly, like the drill lists in textbooks: the linear combination 2A−B2A-B, the transpose of a 2×32\times 3 matrix, the product of two 2×22\times 2 matrices, of a 2×32\times 3 by a 3×23\times 2 and of a 3×33\times 3 by a column vector, the square and the cube of an adjacency matrix, the 2×22\times 2 and 3×33\times 3 determinants and the inverse of a 2×22\times 2 matrix, each with an entry or a value to find and checked on the spot.

The thread of the set: the graph is drawn, the matrix is computed, and both say the same thing. The bridge between them fits in one sentence: entry (i,j)(i,j) of MkM^{k} counts the paths of length kk from ii to jj. That theorem is what turns a question about a drawing into a multiplication.

The pitfalls named explicitly in the solutions: confusing in-degree and out-degree by reading a row for a column, believing that a path cannot pass through a vertex twice, forgetting that the rightmost factor of a matrix product acts first, and thinking that a matrix with determinant zero is invertible.

11 corrected exercises • 110 points • 150 minutes

This chapter is part of 201-N11, every chapter with its set and sheet Before you start Revision sheet: the traps and the method for this chapter

Self-checking set Type your answers and check them question by question: the page tells you right or wrong before you open the solution. With an account, every answer right on the first try earns points, and your right answers stay filled in when you come back.

Course recap

  • • Handshaking lemma: ∑vdeg⁡(v)=2∣E∣\sum_{v} \deg(v) = 2\lvert E \rvert. The number of vertices of odd degree is therefore even.
  • • Adjacency matrix: Mij=1M_{ij}=1 if there is an arc from ii to jj. Row ii gives the out-degree, column jj the in-degree.
  • • MM is symmetric if and only if the graph is undirected. A nonzero diagonal entry signals a loop.
  • • (Mk)ij(M^{k})_{ij} is the number of paths of length exactly kk from ii to jj, repeated vertices being allowed.
  • • Reachability matrix: Boolean union of M,M2,…,Mn−1M, M^{2}, \dots, M^{n-1}. A strongly connected graph gives no zero off the diagonal; a return from a vertex to itself may need up to MnM^{n}.
  • • A spanning tree of a connected graph with nn vertices has n−1n-1 edges.
  • • The matrix product is associative but NOT commutative. (PQ)T=QTPT(PQ)^{T} = Q^{T}P^{T} and (PQ)−1=Q−1P−1(PQ)^{-1} = Q^{-1}P^{-1}.
  • • (abcd)−1=1ad−bc(d−b−ca)\begin{pmatrix} a & b \\ c & d \end{pmatrix}^{-1} = \dfrac{1}{ad-bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}, and the matrix is invertible if and only if ad−bc≠0ad-bc \neq 0.
  • • The columns of a transformation matrix are the images of the basis vectors. Area is multiplied by ∣det⁡∣\lvert \det \rvert.
  • • Homogeneous coordinates: a point becomes (x,y,1)(x,y,1) and transformations become 3×33 \times 3 matrices, which makes it possible to write a translation as a product.

Calculations: matrix computations

Exercise 0.1: Computing with matrices: ten direct calculations

For each line, carry out the requested computation, then give the indicated entry or entries of the result. Entry (i,j)(i,j) is the one in row ii and column jj.

A single rule carries the whole chapter: entry (i,j)(i,j) of a product PQPQ is obtained by running row ii of PP against column jj of QQ, term by term, then adding. Before computing, check the sizes: an m×nm\times n times an n×pn\times p gives an m×pm\times p. For a 2×22\times 2 determinant, it is ad−bcad-bc; for a 3×33\times 3, expand along the first row with the signs ++, −-, ++.

  • a) A=(3−125)A=\begin{pmatrix} 3 & -1 \\ 2 & 5 \end{pmatrix} and B=(14−20)B=\begin{pmatrix} 1 & 4 \\ -2 & 0 \end{pmatrix}: compute 2A−B2A-B, then give its entries (1,2)(1,2) and (2,1)(2,1).
  • b) C=(20−3145)C=\begin{pmatrix} 2 & 0 & -3 \\ 1 & 4 & 5 \end{pmatrix}: write the transpose CTC^{T}, then give its entries (3,1)(3,1) and (1,2)(1,2).
  • c) P=(2−314)P=\begin{pmatrix} 2 & -3 \\ 1 & 4 \end{pmatrix} and Q=(51−23)Q=\begin{pmatrix} 5 & 1 \\ -2 & 3 \end{pmatrix}: compute PQPQ, then give its entries (1,2)(1,2) and (2,1)(2,1).
  • d) D=(120−132)D=\begin{pmatrix} 1 & 2 & 0 \\ -1 & 3 & 2 \end{pmatrix} and E=(210−143)E=\begin{pmatrix} 2 & 1 \\ 0 & -1 \\ 4 & 3 \end{pmatrix}: compute DEDE, then give its entries (2,1)(2,1) and (2,2)(2,2).
  • e) F=(1023−1102−2)F=\begin{pmatrix} 1 & 0 & 2 \\ 3 & -1 & 1 \\ 0 & 2 & -2 \end{pmatrix} and v⃗=(3−12)\vec{v}=\begin{pmatrix} 3 \\ -1 \\ 2 \end{pmatrix}: compute Fv⃗F\vec{v}, then give its second and third components.
  • f) M=(011101010)M=\begin{pmatrix} 0 & 1 & 1 \\ 1 & 0 & 1 \\ 0 & 1 & 0 \end{pmatrix} is the adjacency matrix of a directed graph: compute M2M^{2}, then give its entries (2,2)(2,2) and (2,3)(2,3).
  • g) With the same matrix MM: compute M3M^{3}, then give its entries (1,3)(1,3) and (3,1)(3,1).
  • h) Compute det⁡(−453−6)\det\begin{pmatrix} -4 & 5 \\ 3 & -6 \end{pmatrix}.
  • i) G=(4322)G=\begin{pmatrix} 4 & 3 \\ 2 & 2 \end{pmatrix}: compute G−1G^{-1}, then give its entries (1,2)(1,2) and (2,1)(2,1).
  • j) Compute det⁡(2−13104−211)\det\begin{pmatrix} 2 & -1 & 3 \\ 1 & 0 & 4 \\ -2 & 1 & 1 \end{pmatrix} by cofactor expansion along the first row.

Type your answers, the page tells you right or wrong 0/18

a)
b)
c)
d)
e)
f)
g)
h)
i)
j)
Show the solution

Answers

  • a) 2A−B=(5−6610)2A-B=\begin{pmatrix} 5 & -6 \\ 6 & 10 \end{pmatrix}; −6-6 and 66
  • b) CT=(2104−35)C^{T}=\begin{pmatrix} 2 & 1 \\ 0 & 4 \\ -3 & 5 \end{pmatrix}; −3-3 and 11
  • c) PQ=(16−7−313)PQ=\begin{pmatrix} 16 & -7 \\ -3 & 13 \end{pmatrix}; −7-7 and −3-3
  • d) DE=(2−162)DE=\begin{pmatrix} 2 & -1 \\ 6 & 2 \end{pmatrix}; 66 and 22
  • e) Fv⃗=(712−6)F\vec{v}=\begin{pmatrix} 7 \\ 12 \\ -6 \end{pmatrix}; 1212 and −6-6
  • f) M2=(111021101)M^{2}=\begin{pmatrix} 1 & 1 & 1 \\ 0 & 2 & 1 \\ 1 & 0 & 1 \end{pmatrix}; 22 and 11
  • g) M3=(122212021)M^{3}=\begin{pmatrix} 1 & 2 & 2 \\ 2 & 1 & 2 \\ 0 & 2 & 1 \end{pmatrix}; 22 and 00
  • h) (−4)(−6)−(5)(3)=9(-4)(-6)-(5)(3)=9
  • i) G−1=(1−32−12)G^{-1}=\begin{pmatrix} 1 & -\frac{3}{2} \\ -1 & 2 \end{pmatrix}; −32-\frac{3}{2} and −1-1
  • j) 2(−4)+1(9)+3(1)=42(-4)+1(9)+3(1)=4

The move that decides everything: write down the size of the result BEFORE computing, then aim at the requested entry by its row and its column. A product is not computed by multiplying term by term; you run a row against a column.

a)

Term by term: 2A=(6−2410)2A=\begin{pmatrix} 6 & -2 \\ 4 & 10 \end{pmatrix}, so 2A−B=(6−1−2−44−(−2)10−0)=(5−6610)2A-B=\begin{pmatrix} 6-1 & -2-4 \\ 4-(-2) & 10-0 \end{pmatrix}=\begin{pmatrix} 5 & -6 \\ 6 & 10 \end{pmatrix}. Entries: (1,2)=−6(1,2)=-6 and (2,1)=6(2,1)=6. Pitfall: 4−(−2)=64-(-2)=6, the double minus sign.

b)

The rows of CC become the columns: CT=(2104−35)C^{T}=\begin{pmatrix} 2 & 1 \\ 0 & 4 \\ -3 & 5 \end{pmatrix}, a 3×23\times 2 matrix. Entries: (CT)31=C13=−3(C^{T})_{31}=C_{13}=-3 and (CT)12=C21=1(C^{T})_{12}=C_{21}=1. Pitfall: keeping the size 2×32\times 3 and reading (1,2)(1,2) in CC, which gives 00.

c)

PQ=(2(5)+(−3)(−2)2(1)+(−3)(3)1(5)+4(−2)1(1)+4(3))=(16−7−313)PQ=\begin{pmatrix} 2(5)+(-3)(-2) & 2(1)+(-3)(3) \\ 1(5)+4(-2) & 1(1)+4(3) \end{pmatrix}=\begin{pmatrix} 16 & -7 \\ -3 & 13 \end{pmatrix}. Entries: (1,2)=−7(1,2)=-7 and (2,1)=−3(2,1)=-3. Pitfall: multiplying term by term, which would give (1,2)=−3(1,2)=-3.

d)

A 2×32\times 3 times a 3×23\times 2 gives a 2×22\times 2. Row 2 of DD, (−1,3,2)(-1,3,2), against column 1 of EE, (2,0,4)(2,0,4): −2+0+8=6-2+0+8=6; against column 2, (1,−1,3)(1,-1,3): −1−3+6=2-1-3+6=2. In full, DE=(2−162)DE=\begin{pmatrix} 2 & -1 \\ 6 & 2 \end{pmatrix}. Pitfall: believing the result is 3×33\times 3, which is the size of EDED.

e)

Each row of FF against v⃗\vec{v}: 1(3)+0(−1)+2(2)=71(3)+0(-1)+2(2)=7, 3(3)+(−1)(−1)+1(2)=123(3)+(-1)(-1)+1(2)=12, 0(3)+2(−1)+(−2)(2)=−60(3)+2(-1)+(-2)(2)=-6. So Fv⃗=(712−6)F\vec{v}=\begin{pmatrix} 7 \\ 12 \\ -6 \end{pmatrix}, second component 1212, third −6-6. Pitfall: (−1)(−1)=+1(-1)(-1)=+1 in the second row.

f)

Each row of M2M^{2} is the sum of the rows of MM picked out by the 11s of the starting row. Row 1: rows 2 and 3, (1,1,1)(1,1,1). Row 2: rows 1 and 3, (0,2,1)(0,2,1). Row 3: row 2, (1,0,1)(1,0,1). So M2=(111021101)M^{2}=\begin{pmatrix} 1 & 1 & 1 \\ 0 & 2 & 1 \\ 1 & 0 & 1 \end{pmatrix}, entries (2,2)=2(2,2)=2 and (2,3)=1(2,3)=1. Pitfall: squaring each entry, which gives back MM.

g)

M3=M2MM^{3}=M^{2}M: row 1 of M2M^{2}, (1,1,1)(1,1,1), adds up the three rows of MM, giving (1,2,2)(1,2,2); row 3, (1,0,1)(1,0,1), adds up rows 1 and 3, giving (0,2,1)(0,2,1). So M3=(122212021)M^{3}=\begin{pmatrix} 1 & 2 & 2 \\ 2 & 1 & 2 \\ 0 & 2 & 1 \end{pmatrix}, entries (1,3)=2(1,3)=2 and (3,1)=0(3,1)=0. The 00 says that no path of length 3 leads from vertex 3 to vertex 1.

h)

ad−bc=(−4)(−6)−(5)(3)=24−15=9ad-bc=(-4)(-6)-(5)(3)=24-15=9. Pitfall: −24−15=−39-24-15=-39, losing the sign of the product of two negatives.

i)

det⁡G=4(2)−3(2)=2\det G=4(2)-3(2)=2. Swap the diagonal entries, change the sign of the off-diagonal entries, divide by 22: G−1=12(2−3−24)=(1−32−12)G^{-1}=\frac{1}{2}\begin{pmatrix} 2 & -3 \\ -2 & 4 \end{pmatrix}=\begin{pmatrix} 1 & -\frac{3}{2} \\ -1 & 2 \end{pmatrix}. Entries: (1,2)=−32(1,2)=-\frac{3}{2} and (2,1)=−1(2,1)=-1. Pitfall: forgetting to divide by the determinant.

j)

2∣0411∣−(−1)∣14−21∣+3∣10−21∣=2(0−4)+1(1+8)+3(1−0)=−8+9+3=42\begin{vmatrix} 0 & 4 \\ 1 & 1 \end{vmatrix}-(-1)\begin{vmatrix} 1 & 4 \\ -2 & 1 \end{vmatrix}+3\begin{vmatrix} 1 & 0 \\ -2 & 1 \end{vmatrix}=2(0-4)+1(1+8)+3(1-0)=-8+9+3=4. Pitfall: the minus sign in front of the second term, which turns −(−1)-(-1) into +1+1; forgetting it gives −14-14.

Check: a product is cross-checked by recomputing another entry the other way, row against column. For the inverse, GG−1GG^{-1} must give back the identity: row 1 of GG against column 2 of G−1G^{-1} gives 4(−32)+3(2)=04\left(-\frac{3}{2}\right)+3(2)=0. For a 3×33\times 3 determinant, redo the computation along another row or with the rule of Sarrus.

Tick the exercises you have done or want to review: a free account, no password, keeps your ticks and your right answers from one visit to the next, tells you which chapter to tackle next and lets you ask for the exercise you are missing. Create your space, an email is enough.

Part A: the basics (/50)

Exercise 1: Graph vocabulary: vertices, edges, degrees

An undirected graph GG consists of a set V(G)V(G) of vertices and a set E(G)E(G) of edges, each edge being a pair of vertices. The degree of a vertex is the number of edges that end at it. The graph below has five vertices; note that its vertex EE is not the edge set E(G)E(G).

ABCDE
  • a) List the elements of V(G)V(G) and E(G)E(G). How many edges are there?
  • b) Give the degree of each vertex.
  • c) Check the handshaking lemma: the sum of the degrees equals twice the number of edges.
  • d) Explain why this lemma holds for every graph, and deduce that the number of vertices of odd degree is even.
  • e) What is the maximum number of edges of a simple graph with 5 vertices? And with nn vertices?
  • f) Is there a path from AA to EE? Is the graph connected?

Type your answers, the page tells you right or wrong 0/15

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) V(G)={A,B,C,D,E}V(G) = \{A,B,C,D,E\} and E(G)={AB,AC,BC,BD,CD,DE}E(G) = \{AB, AC, BC, BD, CD, DE\}, that is, 6 edges
  • b) 2,3,3,3,12, 3, 3, 3, 1
  • c) 2+3+3+3+1=12=2×62+3+3+3+1 = 12 = 2 \times 6
  • d) Each edge counts 2 in the sum, which is therefore even: the vertices of odd degree come in an even number, four here
  • e) (52)=10\dbinom{5}{2} = 10, and n(n−1)2\dfrac{n(n-1)}{2} in general
  • f) Yes, A→B→D→EA \to B \to D \to E: the graph is connected
a)

V(G)={A,B,C,D,E}V(G) = \{A, B, C, D, E\} and E(G)={AB,AC,BC,BD,CD,DE}E(G) = \{AB, AC, BC, BD, CD, DE\}, that is, 6 edges. Read them off by sweeping the drawing in a fixed order, for example alphabetically on the first vertex, so as to forget none and count none twice.

b)

deg⁡(A)=2\deg(A) = 2 (to BB and CC), deg⁡(B)=3\deg(B) = 3 (to AA, CC, DD), deg⁡(C)=3\deg(C) = 3 (to AA, BB, DD), deg⁡(D)=3\deg(D) = 3 (to BB, CC, EE), deg⁡(E)=1\deg(E) = 1 (to DD).

c)

Sum of the degrees: 2+3+3+3+1=122+3+3+3+1 = 12. Number of edges: 6. We do have 12=2×612 = 2 \times 6. This check is the first one to make after reading off a graph: if it fails, an edge has been forgotten or counted twice.

d)

Each edge has two endpoints, so it contributes 1 to the degree of each of them, that is, 2 to the total of the degrees. Summing over all the edges gives ∑vdeg⁡(v)=2∣E(G)∣\sum_{v} \deg(v) = 2\lvert E(G) \rvert. This sum is even; the vertices of even degree contribute an even amount to it, so the contribution of the vertices of odd degree must also be even, which is only possible if there is an even number of them. Here, the vertices of odd degree are BB, CC, DD and EE: there are indeed four of them.

e)

In a simple graph, there is at most one edge per pair of vertices and no loop: the maximum is the number of pairs, (52)=5×42=10\dbinom{5}{2} = \dfrac{5 \times 4}{2} = 10 edges, and n(n−1)2\dfrac{n(n-1)}{2} in general. Such a graph is called complete. Our graph has 6 of the 10 possible edges.

f)

Yes: A→B→D→EA \to B \to D \to E is a path, and there are others, such as A→C→D→EA \to C \to D \to E. The graph is connected, since step by step every vertex is reached from AA: BB and CC directly, DD through BB, EE through DD. A connected graph with nn vertices has at least n−1n-1 edges; here 6≥46 \ge 4, which is consistent, without being a proof on its own.

Exercise 2: Adjacency matrix of a directed graph

The adjacency matrix MM of a graph with nn vertices is the n×nn \times n matrix whose entry MijM_{ij} is 1 if there is an arc from vertex ii to vertex jj, and 0 otherwise. It holds exactly the same information as the drawing, but it can be computed with. The directed graph below has four vertices numbered 1 to 4.

1234
  • a) Read off the six arcs, then write the adjacency matrix MM.
  • b) What does the sum of the entries of a row represent? And of a column?
  • c) Give the out-degree and the in-degree of each vertex, then check that the two sums are equal.
  • d) The adjacency matrix of an undirected graph has a particular property: which one, and why?
  • e) What would a nonzero entry on the diagonal mean?

Type your answers, the page tells you right or wrong 0/14

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) Six arcs, 1→21\to2, 1→31\to3, 2→32\to3, 2→42\to4, 3→13\to1, 4→34\to3, and 6 entries equal to 1
  • b) Row: OUT-degree. Column: IN-degree
  • c) Out-degrees 2,2,1,12,2,1,1 and in-degrees 1,1,3,11,1,3,1: both sums equal 6, the number of arcs
  • d) It is SYMMETRIC. Ours is not: M12=1M_{12}=1 and M21=0M_{21}=0
  • e) A LOOP. Our diagonal is zero
a)

The arcs are 1→21 \to 2, 1→31 \to 3, 2→32 \to 3, 2→42 \to 4, 3→13 \to 1 and 4→34 \to 3. Listing the vertices in the order 1, 2, 3, 4, row ii describes the arcs THAT LEAVE ii: M=(0110001110000010)M = \begin{pmatrix} 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \end{pmatrix}. Check: the matrix has 6 entries equal to 1, as many as the graph has arcs.

b)

The sum of row ii is the number of arcs leaving ii, that is, its OUT-degree. The sum of column jj is the number of arcs arriving at jj, its IN-degree. Confusing the two is the mistake that skews the whole rest of the chapter, because it amounts to transposing the graph.

c)

Out-degrees: 2,2,1,12, 2, 1, 1, with sum 6. In-degrees: vertex 1 receives from 3, so 1; vertex 2 receives from 1, so 1; vertex 3 receives from 1, 2 and 4, so 3; vertex 4 receives from 2, so 1. Sum: 1+1+3+1=61+1+3+1 = 6. Both sums equal 6, the number of arcs: each arc has exactly one tail and one head. This is the directed version of the handshaking lemma, without the factor 2.

d)

It is SYMMETRIC, that is, equal to its transpose. An undirected edge between ii and jj can be read in both directions, so Mij=MjiM_{ij} = M_{ji}. Conversely, a symmetric adjacency matrix describes a graph that can be regarded as undirected. Our matrix is not symmetric: M12=1M_{12} = 1 but M21=0M_{21} = 0, which confirms that the graph is indeed directed.

e)

An entry Mii=1M_{ii} = 1 would signal a LOOP, that is, an arc from a vertex to itself. Our graph has none, its diagonal is zero. Loops are forbidden in a so-called simple graph, but they appear naturally as soon as you model an automaton or a web page that links to itself.

Exercise 3: Matrix operations and noncommutativity

Let A=(123−1)A = \begin{pmatrix} 1 & 2 \\ 3 & -1 \end{pmatrix} and B=(0421)B = \begin{pmatrix} 0 & 4 \\ 2 & 1 \end{pmatrix}. Entry (i,j)(i,j) of the product ABAB is obtained by running row ii of AA against column jj of BB: it is the only rule of the chapter, and on its own it explains why the product is not commutative.

  • a) Compute A+BA + B and 2A−3B2A - 3B.
  • b) Compute ABAB and BABA. What do you observe?
  • c) Give ATA^{T} and BTB^{T}.
  • d) Check on this example that (AB)T=BTAT(AB)^{T} = B^{T}A^{T}.
  • e) Under what condition on the sizes does the product PQPQ exist?
  • f) Compute A2A^{2}. Is it the matrix of the squares of the entries of AA?

Type your answers, the page tells you right or wrong 0/18

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) A+B=(1650)A+B = \begin{pmatrix} 1 & 6 \\ 5 & 0 \end{pmatrix} and 2A−3B=(2−80−5)2A-3B = \begin{pmatrix} 2 & -8 \\ 0 & -5 \end{pmatrix}
  • b) AB=(46−211)AB = \begin{pmatrix} 4 & 6 \\ -2 & 11 \end{pmatrix} and BA=(12−453)BA = \begin{pmatrix} 12 & -4 \\ 5 & 3 \end{pmatrix}: the product is not commutative
  • c) AT=(132−1)A^{T} = \begin{pmatrix} 1 & 3 \\ 2 & -1 \end{pmatrix} and BT=(0241)B^{T} = \begin{pmatrix} 0 & 2 \\ 4 & 1 \end{pmatrix}
  • d) Both equal (4−2611)\begin{pmatrix} 4 & -2 \\ 6 & 11 \end{pmatrix}: the order is REVERSED
  • e) Columns of PP = rows of QQ, and the product is of size m×pm \times p
  • f) A2=7IA^{2} = 7I, not the matrix of the squares: AA is invertible, with inverse 17A\dfrac{1}{7}A
a)

A+B=(1650)A+B = \begin{pmatrix} 1 & 6 \\ 5 & 0 \end{pmatrix}, term by term. For the combination: 2A=(246−2)2A = \begin{pmatrix} 2 & 4 \\ 6 & -2 \end{pmatrix} and 3B=(01263)3B = \begin{pmatrix} 0 & 12 \\ 6 & 3 \end{pmatrix}, so 2A−3B=(2−80−5)2A - 3B = \begin{pmatrix} 2 & -8 \\ 0 & -5 \end{pmatrix}.

b)

AB=(1(0)+2(2)1(4)+2(1)3(0)+(−1)(2)3(4)+(−1)(1))=(46−211)AB = \begin{pmatrix} 1(0)+2(2) & 1(4)+2(1) \\ 3(0)+(-1)(2) & 3(4)+(-1)(1) \end{pmatrix} = \begin{pmatrix} 4 & 6 \\ -2 & 11 \end{pmatrix}. And BA=(0(1)+4(3)0(2)+4(−1)2(1)+1(3)2(2)+1(−1))=(12−453)BA = \begin{pmatrix} 0(1)+4(3) & 0(2)+4(-1) \\ 2(1)+1(3) & 2(2)+1(-1) \end{pmatrix} = \begin{pmatrix} 12 & -4 \\ 5 & 3 \end{pmatrix}. The two products differ: the matrix product is NOT commutative. It is the most important difference from the product of real numbers, and in computer graphics it has a concrete meaning: rotating then translating does not give the same image as translating then rotating.

c)

AT=(132−1)A^{T} = \begin{pmatrix} 1 & 3 \\ 2 & -1 \end{pmatrix} and BT=(0241)B^{T} = \begin{pmatrix} 0 & 2 \\ 4 & 1 \end{pmatrix}: the first row becomes the first column.

d)

(AB)T=(4−2611)(AB)^{T} = \begin{pmatrix} 4 & -2 \\ 6 & 11 \end{pmatrix}. And BTAT=(0(1)+2(2)0(3)+2(−1)4(1)+1(2)4(3)+1(−1))=(4−2611)B^{T}A^{T} = \begin{pmatrix} 0(1)+2(2) & 0(3)+2(-1) \\ 4(1)+1(2) & 4(3)+1(-1) \end{pmatrix} = \begin{pmatrix} 4 & -2 \\ 6 & 11 \end{pmatrix}. The two coincide. The point to remember is the REVERSAL of the order: the transpose of a product is the product of the transposes taken in reverse order. The same reversal happens for the inverse of a product.

e)

The product PQPQ exists if and only if the number of COLUMNS of PP equals the number of ROWS of QQ. If PP is of size m×nm \times n and QQ of size n×pn \times p, then PQPQ is of size m×pm \times p. This is the check to make before any computation: quite often, ABAB exists and BABA does not, which settles the question of commutativity without the slightest computation.

f)

A2=AA=(1(1)+2(3)1(2)+2(−1)3(1)+(−1)(3)3(2)+(−1)(−1))=(7007)A^{2} = AA = \begin{pmatrix} 1(1)+2(3) & 1(2)+2(-1) \\ 3(1)+(-1)(3) & 3(2)+(-1)(-1) \end{pmatrix} = \begin{pmatrix} 7 & 0 \\ 0 & 7 \end{pmatrix}. It is clearly not the matrix of the squares, which would be (1491)\begin{pmatrix} 1 & 4 \\ 9 & 1 \end{pmatrix}. The result is remarkable here: A2=7IA^{2} = 7I, so AA behaves like a square root of 7 times the identity. It follows at once that AA is invertible, with inverse 17A\dfrac{1}{7}A.

Exercise 4: Powers of the adjacency matrix and counting paths

We return to the matrix MM of the directed graph of exercise 2. The central theorem of the chapter: entry (i,j)(i,j) of MkM^{k} is the NUMBER of paths of length exactly kk from ii to jj. A path is allowed to pass again through a vertex it has already visited (some textbooks call such a path a walk, and keep the word path for a simple path).

The graph of exercise 2 is shown again below.

1234
  • a) Compute M2M^{2}.
  • b) Interpret entry (1,1)(1,1) of M2M^{2} by exhibiting the corresponding path.
  • c) Compute M3M^{3} and read off the number of paths of length 3 from 1 to 3. Write them all out.
  • d) Prove the theorem in the case k=2k=2.
  • e) How many paths of length 2 leave vertex 1, all destinations together?

Type your answers, the page tells you right or wrong 0/13

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) M2=(1011101001101000)M^{2} = \begin{pmatrix} 1 & 0 & 1 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 1 & 0 \\ 1 & 0 & 0 & 0 \end{pmatrix}
  • b) It is 1: the only cycle 1→3→11 \to 3 \to 1 of length 2
  • c) (M3)13=2(M^{3})_{13} = 2: 1→2→4→31 \to 2 \to 4 \to 3 and 1→3→1→31 \to 3 \to 1 \to 3
  • d) (M2)ij=∑kMikMkj(M^{2})_{ij} = \sum_{k} M_{ik}M_{kj}, each term being 1 for a path i→k→ji \to k \to j
  • e) 1+0+1+1=31+0+1+1 = 3, confirmed by the count through successors 2+1=32+1 = 3
a)

Multiply MM by itself. Row 1: vertex 1 points to 2 and 3, so row 1 of M2M^{2} is the sum of rows 2 and 3 of MM, that is, (0,0,1,1)+(1,0,0,0)=(1,0,1,1)(0,0,1,1) + (1,0,0,0) = (1,0,1,1). Row 2: vertex 2 points to 3 and 4, sum of rows 3 and 4: (1,0,0,0)+(0,0,1,0)=(1,0,1,0)(1,0,0,0)+(0,0,1,0) = (1,0,1,0). Row 3: vertex 3 points to 1, so row 1 of MM: (0,1,1,0)(0,1,1,0). Row 4: vertex 4 points to 3, so row 3 of MM: (1,0,0,0)(1,0,0,0). Hence M2=(1011101001101000)M^{2} = \begin{pmatrix} 1 & 0 & 1 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 1 & 0 \\ 1 & 0 & 0 & 0 \end{pmatrix}. This method, where each row of the product is a sum of rows, is much faster than computing entry by entry when the matrix contains only 0s and 1s.

b)

Entry (1,1)(1,1) of M2M^{2} is 1: there is exactly one path of length 2 from vertex 1 to itself, namely 1→3→11 \to 3 \to 1. It is a cycle of length 2, and it shows up on the diagonal: the diagonal entries of MkM^{k} count the closed paths of length kk through each vertex.

c)

M3=M2MM^{3} = M^{2}M. Row 1: (1,0,1,1)(1,0,1,1) picks out rows 1, 3 and 4 of MM, whose sum is (0,1,1,0)+(1,0,0,0)+(0,0,1,0)=(1,1,2,0)(0,1,1,0)+(1,0,0,0)+(0,0,1,0) = (1,1,2,0). Continuing: M3=(1120111010110110)M^{3} = \begin{pmatrix} 1 & 1 & 2 & 0 \\ 1 & 1 & 1 & 0 \\ 1 & 0 & 1 & 1 \\ 0 & 1 & 1 & 0 \end{pmatrix}. Entry (1,3)(1,3) is 2: there are two paths of length 3 from 1 to 3, namely 1→2→4→31 \to 2 \to 4 \to 3 and 1→3→1→31 \to 3 \to 1 \to 3. The second one passes again through vertices already seen, which is allowed: that is precisely why the entry is 2 and not 1. Confusing a path with a simple path is the mistake that makes students miss this theorem.

d)

By definition of the product, (M2)ij=∑kMikMkj(M^{2})_{ij} = \sum_{k} M_{ik}M_{kj}. Each term of this sum is 1 exactly when Mik=1M_{ik} = 1 AND Mkj=1M_{kj} = 1, that is, when there is an arc from ii to kk AND an arc from kk to jj: this describes precisely a path i→k→ji \to k \to j of length 2 through kk. The sum over kk therefore counts all the paths of length 2 from ii to jj, one for each possible intermediate vertex. The general case follows by induction, writing Mk+1=MkMM^{k+1} = M^{k}M and splitting each path of length k+1k+1 into a path of length kk followed by an arc.

e)

It is the sum of the first row of M2M^{2}: 1+0+1+1=31+0+1+1 = 3. Another reading: vertex 1 has two successors, 2 and 3, which have 2 and 1 successors respectively, giving 2+1=32+1 = 3 paths. The two counts agree, which validates the matrix computation.

Exercise 5: Connectivity and the reachability matrix

Knowing whether a path exists, regardless of its length, amounts to combining all the powers of the adjacency matrix while replacing addition by a logical OR. The resulting matrix is called the reachability matrix, or transitive closure: its entry (i,j)(i,j) is 1 as soon as some path leads from ii to jj.

The graph of exercise 2 is shown again below.

1234
  • a) Up to which power of MM must you go in a graph with nn vertices?
  • b) Determine the reachability matrix of the directed graph of exercise 2.
  • c) Is this graph strongly connected?
  • d) The undirected graph of exercise 1 is connected. How many edges must be removed to obtain a spanning tree, and how many will remain?
  • e) What is the cost in operations of Warshall's algorithm on nn vertices?

Type your answers, the page tells you right or wrong 0/11

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) Up to MnM^{n}: a return to a vertex itself may need nn arcs
  • b) Filled entirely with 1s; at M3M^{3} cell (4,4)(4,4) was still missing
  • c) Yes, strongly connected: the cycle 4→3→1→2→44 \to 3 \to 1 \to 2 \to 4 passes through all four vertices
  • d) A spanning tree has 5−1=45-1 = 4 edges: 2 must be removed, the cyclomatic number
  • e) Warshall costs n3n^{3}, against n4n^{4} for combining the powers
a)

A path that passes twice through the same vertex contains a cycle that can be deleted without changing its endpoints. For i≠ji \neq j, if there is a path from ii to jj, there is therefore one without repetition: it visits at most nn vertices and has at most n−1n-1 arcs. For a return from ii to itself, on the other hand, the cycle may visit all nn vertices and then have nn arcs. You must therefore go up to MnM^{n}, and Mn−1M^{n-1} is enough only if you restrict yourself to pairs of distinct vertices. The next question shows that this distinction is not just theoretical.

b)

Combine MM, M2M^{2}, M3M^{3} and M4M^{4}, putting a 1 wherever any one of the four has one. MM gives the pairs (1,2),(1,3),(2,3),(2,4),(3,1),(4,3)(1,2), (1,3), (2,3), (2,4), (3,1), (4,3); M2M^{2} adds (1,1),(1,4),(2,1),(3,2),(3,3),(4,1)(1,1), (1,4), (2,1), (3,2), (3,3), (4,1); M3M^{3} adds (2,2),(3,4),(4,2)(2,2), (3,4), (4,2). At this stage, a single position is still 0, position (4,4)(4,4): fifteen cells out of sixteen are filled. It is filled by M4M^{4}, thanks to the cycle 4→3→1→2→44 \to 3 \to 1 \to 2 \to 4, which has four arcs. The reachability matrix is therefore filled entirely with 1s. This is the exact illustration of question a): this cycle passes through all four vertices, so it needs four arcs, and stopping at M3M^{3} would have concluded, wrongly, that vertex 4 cannot return to itself.

c)

Yes. A directed graph is strongly connected when every vertex is reachable from every other one, which is read exactly on a reachability matrix filled entirely with 1s. The cycle 4→3→1→2→44 \to 3 \to 1 \to 2 \to 4 is in fact enough to convince yourself without a matrix: it passes through all four vertices, so by following it long enough you go from any vertex to any other.

d)

A spanning tree of a connected graph with nn vertices has exactly n−1n-1 edges, here 5−1=45-1 = 4. The graph has 6, so 6−4=26-4 = 2 must be removed, taking care not to break any connection. For example, removing BCBC and CDCD leaves ABAB, ACAC, BDBD, DEDE, which do link the five vertices without a cycle. The number of edges to remove, ∣E(G)∣−∣V(G)∣+1\lvert E(G) \rvert - \lvert V(G) \rvert + 1, is called the cyclomatic number and counts the independent cycles of the graph.

e)

Warshall's algorithm consists of three nested loops over the vertices, each of length nn: its cost is of the order of n3n^{3} operations. That is cheaper than computing and combining the first nn powers of MM, which would cost about nn matrix multiplications at n3n^{3} operations each, that is, n4n^{4}. The difference rests on a single idea: Warshall updates the matrix IN PLACE, allowing the intermediate vertices one at a time.

Part B: problems and reasoning (/50)

Exercise 6: Plane transformations: matrices and computer graphics

In computer graphics, a point of the plane is a column vector (xy)\begin{pmatrix} x \\ y \end{pmatrix}, and a linear transformation is a 2×22 \times 2 matrix that multiplies it. Here three transformations are applied to the triangle TT with vertices P1(1,1)P_{1}(1,1), P2(4,1)P_{2}(4,1) and P3(1,3)P_{3}(1,3).

-5-4-3-2-112345-112345T
  • a) Write the matrix HH of the dilation with factor 2 centred at the origin, and give the image of TT.
  • b) Write the matrix RR of the rotation through 9090 degrees about the origin, and give the image of TT.
  • c) Write the matrix SS of the reflection across the x-axis.
  • d) Compute the area of TT, then that of H(T)H(T) and of R(T)R(T).
  • e) What link do you see between the determinant of a matrix and the effect of the transformation on areas?
  • f) Can a translation by the vector (3,1)(3,1) be written as a 2×22 \times 2 matrix?

Type your answers, the page tells you right or wrong 0/17

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) H=2IH = 2I, and TT becomes (2,2)(2,2), (8,2)(8,2), (2,6)(2,6)
  • b) R=(0−110)R = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}, and TT becomes (−1,1)(-1,1), (−1,4)(-1,4), (−3,1)(-3,1)
  • c) S=(100−1)S = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}
  • d) Area of TT: 3; of H(T)H(T): 12; of R(T)R(T): 3
  • e) Area is multiplied by ∣det⁡∣\lvert \det \rvert: 4 for HH, 1 for RR, 1 for SS with the orientation reversed
  • f) No: a linear transformation fixes the origin. Homogeneous coordinates are needed
a)

H=(2002)=2IH = \begin{pmatrix} 2 & 0 \\ 0 & 2 \end{pmatrix} = 2I. The images are H(P1)=(2,2)H(P_{1}) = (2,2), H(P2)=(8,2)H(P_{2}) = (8,2) and H(P3)=(2,6)H(P_{3}) = (2,6): each coordinate is doubled. A dilation with factor kk centred at the origin is always kIkI, and these scalar matrices are the only ones that commute with all the others.

b)

The rotation through 9090 degrees sends (1,0)(1,0) to (0,1)(0,1) and (0,1)(0,1) to (−1,0)(-1,0). These images are the columns of the matrix, hence R=(0−110)R = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}, that is, (x,y)↦(−y,x)(x,y) \mapsto (-y,x). Images: R(P1)=(−1,1)R(P_{1}) = (-1,1), R(P2)=(−1,4)R(P_{2}) = (-1,4) and R(P3)=(−3,1)R(P_{3}) = (-3,1). The general rule is worth remembering: the columns of a matrix are the images of the basis vectors.

c)

The reflection across the x-axis leaves xx unchanged and changes the sign of yy: S=(100−1)S = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}. It is the transformation implicitly undergone by every image displayed on a screen, where the vertical axis points down.

d)

TT has a right angle at P1P_{1}, with legs P1P2=3P_{1}P_{2} = 3 and P1P3=2P_{1}P_{3} = 2: its area is 3×22=3\dfrac{3 \times 2}{2} = 3. After the dilation, both legs double and the area is multiplied by 4: it is 12. After the rotation, the figure is congruent to the original one: the area stays 3.

e)

Area is multiplied by ∣det⁡∣\lvert \det \rvert. Indeed det⁡H=4\det H = 4 and the area was multiplied by 4; det⁡R=0×0−(−1)(1)=1\det R = 0 \times 0 - (-1)(1) = 1 and the area is preserved; det⁡S=−1\det S = -1, the area is preserved but the orientation is reversed, which the negative sign signals. A matrix with determinant zero squashes the figure onto a line or a point: the area becomes zero, and the transformation cannot be reversed.

f)

No. A linear transformation always fixes the origin, since M(00)=(00)M \begin{pmatrix} 0 \\ 0 \end{pmatrix} = \begin{pmatrix} 0 \\ 0 \end{pmatrix}, whereas a translation moves the origin to (3,1)(3,1). No 2×22 \times 2 matrix can therefore translate. That is precisely the problem that homogeneous coordinates solve, by adding a third coordinate equal to 1 and working with 3×33 \times 3 matrices.

-5-4-3-2-112345-112345TR(T)

Exercise 7: Determinant, inverse matrix and reversibility

A square matrix is invertible exactly when its determinant is nonzero, and the inverse undoes what the matrix did. For a 2×22 \times 2 matrix, (abcd)−1=1ad−bc(d−b−ca)\begin{pmatrix} a & b \\ c & d \end{pmatrix}^{-1} = \dfrac{1}{ad-bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}: swap the diagonal entries, change the sign of the off-diagonal entries, divide by the determinant.

  • a) Compute det⁡A\det A for A=(123−1)A = \begin{pmatrix} 1 & 2 \\ 3 & -1 \end{pmatrix}, then A−1A^{-1}.
  • b) Check that AA−1=IAA^{-1} = I.
  • c) Compute the determinant of N=(210131012)N = \begin{pmatrix} 2 & 1 & 0 \\ 1 & 3 & 1 \\ 0 & 1 & 2 \end{pmatrix} by cofactor expansion along the first row.
  • d) Give the inverse of the rotation matrix RR of the previous exercise, and interpret it.
  • e) Is the matrix (2412)\begin{pmatrix} 2 & 4 \\ 1 & 2 \end{pmatrix} invertible? What does it do geometrically?
  • f) Why is (PQ)−1=Q−1P−1(PQ)^{-1} = Q^{-1}P^{-1} and not P−1Q−1P^{-1}Q^{-1}?

Type your answers, the page tells you right or wrong 0/13

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) det⁡A=−7\det A = -7 and A−1=17A=(1/72/73/7−1/7)A^{-1} = \dfrac{1}{7}A = \begin{pmatrix} 1/7 & 2/7 \\ 3/7 & -1/7 \end{pmatrix}
  • b) AA−1=17(7I)=IAA^{-1} = \dfrac{1}{7}(7I) = I
  • c) det⁡N=2(5)−1(2)+0=8\det N = 2(5) - 1(2) + 0 = 8
  • d) R−1=(01−10)R^{-1} = \begin{pmatrix} 0 & 1 \\ -1 & 0 \end{pmatrix}, the rotation through −90-90 degrees, and R−1=RTR^{-1} = R^{T}
  • e) No, its determinant is zero: it squashes the plane onto the line spanned by (2,1)(2,1)
  • f) Because you undo in reverse order: (PQ)(Q−1P−1)=I(PQ)(Q^{-1}P^{-1}) = I
a)

det⁡A=(1)(−1)−(2)(3)=−1−6=−7\det A = (1)(-1) - (2)(3) = -1 - 6 = -7, nonzero, so AA is invertible. The formula gives A−1=1−7(−1−2−31)=(1/72/73/7−1/7)A^{-1} = \dfrac{1}{-7}\begin{pmatrix} -1 & -2 \\ -3 & 1 \end{pmatrix} = \begin{pmatrix} 1/7 & 2/7 \\ 3/7 & -1/7 \end{pmatrix}. This matches the result of exercise 3: since A2=7IA^{2} = 7I, we do have A−1=17AA^{-1} = \dfrac{1}{7}A.

b)

AA−1=17A2=17(7I)=IAA^{-1} = \dfrac{1}{7}A^{2} = \dfrac{1}{7}(7I) = I. In detail: entry (1,1)(1,1) is 1(1/7)+2(3/7)=1/7+6/7=11(1/7) + 2(3/7) = 1/7 + 6/7 = 1; entry (1,2)(1,2) is 1(2/7)+2(−1/7)=01(2/7) + 2(-1/7) = 0. The check by the product is mandatory, because a sign error in the inverse formula cannot be seen any other way.

c)

Expand along the first row: det⁡N=2∣3112∣−1∣1102∣+0=2(6−1)−1(2−0)+0=10−2=8\det N = 2\begin{vmatrix} 3 & 1 \\ 1 & 2 \end{vmatrix} - 1\begin{vmatrix} 1 & 1 \\ 0 & 2 \end{vmatrix} + 0 = 2(6-1) - 1(2-0) + 0 = 10 - 2 = 8. The third term is zero because its coefficient is: choosing the row or column with the most zeros saves a whole minor, and here the first row as well as the third contains a zero.

d)

det⁡R=1\det R = 1, and the formula gives R−1=(01−10)R^{-1} = \begin{pmatrix} 0 & 1 \\ -1 & 0 \end{pmatrix}. It is the matrix of the rotation through −90-90 degrees: undoing a rotation means turning the other way. Note also that R−1=RTR^{-1} = R^{T}, which holds for every rotation matrix and makes inversion free in a graphics engine.

e)

det⁡=(2)(2)−(4)(1)=0\det = (2)(2) - (4)(1) = 0: it is not invertible. Geometrically, it squashes the whole plane onto a line: the two columns (2,1)(2,1) and (4,2)(4,2) are proportional, so all the images are multiples of (2,1)(2,1). Information is lost, several different points have the same image, and no matrix can undo that. That is exactly what a zero determinant expresses: area is squashed to zero.

f)

Because the operations must be undone in the reverse of the order in which they were done. Check: (PQ)(Q−1P−1)=P(QQ−1)P−1=PIP−1=PP−1=I(PQ)(Q^{-1}P^{-1}) = P(QQ^{-1})P^{-1} = PIP^{-1} = PP^{-1} = I. The same reversal appears for the transpose, (PQ)T=QTPT(PQ)^{T} = Q^{T}P^{T}. A concrete picture: to undo “put on your socks, then your shoes”, you take off the shoes first.

Exercise 8: Five statements to correct

Each of the following five statements is FALSE. Say why and give the correct statement.

  • 1) “The product of two matrices is commutative: PQ=QPPQ = QP.”
  • 2) “The sum of the degrees of a graph equals its number of edges.”
  • 3) “If det⁡(P)=0\det(P) = 0, then PP has an inverse matrix.”
  • 4) “The adjacency matrix of a directed graph is always symmetric.”
  • 5) “Entry (i,j)(i,j) of M2M^{2} gives the number of edges between ii and jj.”

Type your answers, the page tells you right or wrong 0/10

1)
2)
3)
4)
5)
Show the solution

Answers

  • 1) FALSE: AB≠BAAB \neq BA, and sometimes BABA does not even exist
  • 2) FALSE: ∑vdeg⁡(v)=2∣E∣\sum_{v} \deg(v) = 2\lvert E \rvert, that is, 12 for 6 edges
  • 3) FALSE: it is the opposite, PP is invertible if and only if det⁡(P)≠0\det(P) \neq 0
  • 4) FALSE: symmetry characterizes UNDIRECTED graphs
  • 5) FALSE: (Mk)ij(M^{k})_{ij} counts the PATHS of length kk, repetitions allowed
1)

FALSE. Counterexample from exercise 3: AB=(46−211)AB = \begin{pmatrix} 4 & 6 \\ -2 & 11 \end{pmatrix} whereas BA=(12−453)BA = \begin{pmatrix} 12 & -4 \\ 5 & 3 \end{pmatrix}. It even happens that PQPQ exists and QPQP does not, if the sizes only fit together one way. Correct statement: the matrix product is associative and distributive, but not commutative; only a few special families commute, such as the multiples of the identity.

2)

FALSE. Each edge counts 1 in the degree of each of its TWO endpoints. Counterexample: the graph of exercise 1 has 6 edges and a sum of degrees equal to 12. Correct statement: ∑vdeg⁡(v)=2∣E∣\sum_{v} \deg(v) = 2\lvert E \rvert, the handshaking lemma.

3)

FALSE. It is exactly the opposite: a matrix is invertible if and only if its determinant is NONZERO. Counterexample: (2412)\begin{pmatrix} 2 & 4 \\ 1 & 2 \end{pmatrix} has determinant zero and squashes the plane onto a line, so several points have the same image and no transformation can tell them apart. Correct statement: PP is invertible if and only if det⁡(P)≠0\det(P) \neq 0.

4)

FALSE. Symmetry characterizes UNDIRECTED graphs, where an edge can be travelled in both directions. Counterexample: in the graph of exercise 2, M12=1M_{12} = 1 and M21=0M_{21} = 0, because the arc goes from 1 to 2 and not the other way. Correct statement: the adjacency matrix is symmetric if and only if the graph is undirected.

5)

FALSE. The number of edges from ii to jj is read in MM itself, not in its square. Counterexample: in the graph of exercise 2, (M2)11=1(M^{2})_{11} = 1 although there is no loop at vertex 1. Correct statement: (Mk)ij(M^{k})_{ij} is the number of PATHS of length exactly kk from ii to jj, repeated vertices being allowed.

Exercise 9: Problem: a cable network and its distance matrix

Five data centres AA, BB, CC, DD and EE are joined by links whose length, in kilometres, is shown on the diagram. A missing link means that there is no direct cable between the two centres.

ABCDE42158102
  • a) Give the degree of each vertex and check the handshaking lemma.
  • b) Write the weight matrix WW, putting 0 on the diagonal and a dash where there is no link.
  • c) Give all the routes from AA to EE that do not pass twice through the same centre, with their length.
  • d) What is the shortest route from AA to EE?
  • e) Is the shortest route the one with the fewest links?
  • f) If link DEDE fails, does the network stay connected? What becomes the shortest route from AA to EE?

Type your answers, the page tells you right or wrong 0/19

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) 2,3,4,3,22, 3, 4, 3, 2, with sum 14=2×714 = 2 \times 7
  • b) Symmetric matrix, zero diagonal, a dash for ADAD, AEAE and BEBE
  • c) A→C→EA\to C\to E: 12; A→C→D→EA\to C\to D\to E: 12; A→B→D→EA\to B\to D\to E: 11; A→C→B→D→EA\to C\to B\to D\to E: 10; A→B→C→D→EA\to B\to C\to D\to E: 15; A→B→C→EA\to B\to C\to E: 15; A→B→D→C→EA\to B\to D\to C\to E: 27
  • d) A→C→B→D→EA \to C \to B \to D \to E, 10 km
  • e) No: it has FOUR links, against two for A→C→EA \to C \to E, which is 12 km
  • f) Yes, EE keeps its link to CC. The shortest becomes A→C→EA \to C \to E, 12 km, that is, 2 km more
a)

deg⁡(A)=2\deg(A) = 2 (to BB and CC), deg⁡(B)=3\deg(B) = 3 (to AA, CC, DD), deg⁡(C)=4\deg(C) = 4 (to AA, BB, DD, EE), deg⁡(D)=3\deg(D) = 3 (to BB, CC, EE), deg⁡(E)=2\deg(E) = 2 (to CC, DD). Sum: 2+3+4+3+2=142+3+4+3+2 = 14, and the network has 7 links: 14=2×714 = 2 \times 7. The lemma checks out, so no link was forgotten when reading off the diagram.

b)

Listing the centres in the order A,B,C,D,EA, B, C, D, E: the row of AA is 0,4,2,−,−0, 4, 2, -, -; that of BB is 4,0,1,5,−4, 0, 1, 5, -; that of CC is 2,1,0,8,102, 1, 0, 8, 10; that of DD is −,5,8,0,2-, 5, 8, 0, 2; that of EE is −,−,10,2,0-, -, 10, 2, 0. The matrix is symmetric, since the links are not directed. In a program, the dash is coded by a very large value, so that any route using it is automatically discarded when the minimum is computed.

c)

There are seven routes without repetition. Four of them are short. A→C→EA \to C \to E: 2+10=122+10 = 12. A→C→D→EA \to C \to D \to E: 2+8+2=122+8+2 = 12. A→B→D→EA \to B \to D \to E: 4+5+2=114+5+2 = 11. A→C→B→D→EA \to C \to B \to D \to E: 2+1+5+2=102+1+5+2 = 10. The other three are longer: A→B→C→D→EA \to B \to C \to D \to E: 4+1+8+2=154+1+8+2 = 15, A→B→C→EA \to B \to C \to E: 4+1+10=154+1+10 = 15, and A→B→D→C→EA \to B \to D \to C \to E: 4+5+8+10=274+5+8+10 = 27. The enumeration is done as a tree, fixing the first step and then the second, which guarantees that no route is forgotten: four routes start with A→BA \to B and three with A→CA \to C.

d)

The shortest is A→C→B→D→EA \to C \to B \to D \to E, with 10 kilometres. It narrowly beats A→B→D→EA \to B \to D \to E, which is 11.

e)

No, and that is the whole point of the example. The shortest route has FOUR links, whereas A→C→EA \to C \to E has only two, for 12 kilometres. Making a detour through CC then BB costs one more link but avoids the 10-kilometre cable. Minimizing the number of edges and minimizing the total weight are two different problems: the first is solved by a breadth-first search, the second requires an algorithm such as Dijkstra's.

f)

The network stays connected: EE keeps its link to CC. The shortest route from AA to EE becomes A→C→EA \to C \to E, that is, 12 kilometres, since every route to EE shorter than that ended with the link from DD to EE. The cost of the failure is therefore 2 kilometres. Since vertex EE has degree 2, it would have to lose both of its links to be isolated: that is the elementary measure of a network's robustness, and it explains why centres of degree 1 are avoided.

42158102ABCDEA → C → B → D → E: 10 km

Exercise 10: Problem: composing transformations in homogeneous coordinates

For a translation to become a matrix product, a third coordinate equal to 1 is added to the point: the point (x,y)(x,y) is written (xy1)\begin{pmatrix} x \\ y \\ 1 \end{pmatrix} and the transformations become 3×33 \times 3 matrices. Let TT be the translation by the vector (3,1)(3,1) and RR the rotation through 9090 degrees about the origin, and follow the point P(2,0)P(2,0).

The figure below shows the situation.

-2-1123456-1123456P(2, 0)
  • a) Write the 3×33 \times 3 matrices of TT and of RR.
  • b) Compute TRTR, then the image of PP under this matrix. Describe the order of the operations applied to PP.
  • c) Compute RTRT, then the image of PP. Compare with the previous question.
  • d) Give the matrix that brings the point back to its initial position after TRTR.
  • e) Check your answer on the image point found in b).
  • f) Why does a graphics processor prefer to compose the matrices rather than apply the transformations one by one?

Type your answers, the page tells you right or wrong 0/13

a)
b)
c)
d)
e)
f)
Show the solution

Answers

  • a) TT carries (3,1)(3,1) in its last column, RR carries the rotation block in the upper left corner, and the last row is (0,0,1)(0,0,1)
  • b) TR=(0−13101001)TR = \begin{pmatrix} 0 & -1 & 3 \\ 1 & 0 & 1 \\ 0 & 0 & 1 \end{pmatrix} and the image is (3,3)(3,3): RR acts first
  • c) RT=(0−1−1103001)RT = \begin{pmatrix} 0 & -1 & -1 \\ 1 & 0 & 3 \\ 0 & 0 & 1 \end{pmatrix} and the image is (−1,5)(-1,5), not (3,3)(3,3)
  • d) (TR)−1=R−1T−1=(01−1−103001)(TR)^{-1} = R^{-1}T^{-1} = \begin{pmatrix} 0 & 1 & -1 \\ -1 & 0 & 3 \\ 0 & 0 & 1 \end{pmatrix}
  • e) The image of (3,3)(3,3) is (2,0)(2,0): we do get back PP
  • f) Six products per vertex against a single one after composing: associativity guarantees the same result
a)

T=(103011001)T = \begin{pmatrix} 1 & 0 & 3 \\ 0 & 1 & 1 \\ 0 & 0 & 1 \end{pmatrix}: the last column carries the translation vector. R=(0−10100001)R = \begin{pmatrix} 0 & -1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{pmatrix}: the upper left 2×22 \times 2 block is the rotation, the rest completes the identity. The last row is always (0,0,1)(0,0,1), which guarantees that the third coordinate of the result stays 1.

b)

TR=(0−13101001)TR = \begin{pmatrix} 0 & -1 & 3 \\ 1 & 0 & 1 \\ 0 & 0 & 1 \end{pmatrix}. Applied to P=(2,0,1)P = (2,0,1): the first coordinate is 0(2)+(−1)(0)+3(1)=30(2) + (-1)(0) + 3(1) = 3, the second 1(2)+0(0)+1(1)=31(2) + 0(0) + 1(1) = 3. The image is (3,3)(3,3). The order is read from right to left: RR acts first, sending (2,0)(2,0) to (0,2)(0,2), then TT adds (3,1)(3,1) and gives (3,3)(3,3). The rightmost factor is applied FIRST, which is confusing until you tie it to the notation T(R(P))T(R(P)).

c)

RT=(0−1−1103001)RT = \begin{pmatrix} 0 & -1 & -1 \\ 1 & 0 & 3 \\ 0 & 0 & 1 \end{pmatrix}. Applied to PP: first coordinate 0(2)+(−1)(0)+(−1)(1)=−10(2) + (-1)(0) + (-1)(1) = -1, second 1(2)+0(0)+3(1)=51(2) + 0(0) + 3(1) = 5. The image is (−1,5)(-1,5), and not (3,3)(3,3). Direct check: TT first sends (2,0)(2,0) to (5,1)(5,1), then RR sends (5,1)(5,1) to (−1,5)(-1,5). Translating then rotating is not rotating then translating, because the rotation also carries along the displacement already made. This is the noncommutativity of the matrix product, seen on an image.

d)

You must undo in reverse order: first cancel TT, then cancel RR. The required matrix is (TR)−1=R−1T−1(TR)^{-1} = R^{-1}T^{-1}, with R−1R^{-1} the rotation through −90-90 degrees and T−1T^{-1} the translation by (−3,−1)(-3,-1). The product is (01−1−103001)\begin{pmatrix} 0 & 1 & -1 \\ -1 & 0 & 3 \\ 0 & 0 & 1 \end{pmatrix}.

e)

Apply this matrix to (3,3,1)(3,3,1): first coordinate 0(3)+1(3)+(−1)(1)=20(3) + 1(3) + (-1)(1) = 2, second (−1)(3)+0(3)+3(1)=0(-1)(3) + 0(3) + 3(1) = 0. We get back P=(2,0)P = (2,0). The check is conclusive, and it is better to do it on a point than to recompute the product (TR)(R−1T−1)(TR)(R^{-1}T^{-1}), which is longer and more exposed to sign errors.

f)

Because a scene contains hundreds of thousands of vertices and a transformation often consists of five or six chained steps. Applying the transformations one by one would cost six matrix-vector products PER vertex. By first composing the six matrices, which is done only once for the whole scene, a single product per vertex remains. The associativity of the matrix product guarantees that the result is identical, and the gain is a factor equal to the number of transformations: that is why a graphics engine handles a single matrix, called the model-view-projection matrix.

-2-1123456-2-1123456PTR(P) = (3, 3)RT(P) = (−1, 5)R(P)T(P)
Something missing from this set? A type of exercise your teacher gives, a problem you are stuck on, a mistake you spotted: tell me, I read every request.

Sending goes through your free space: that is how I can answer you and tell you when the exercise is added. An email is enough, no password. Create your space. Rather not create an account? Write to me directly.

What is it about?
I read every request myself, nobody else sees it. No need to write your name or your school.
When your request is dealt with, you hear about it by email and in your space.

By sending, you agree that I read your request to answer it. It is kept with your account, erased with it, and never published: privacy policy.

Previous chapter Induction and recursion, 201-N11 Next chapter Counting and probability, 201-N11

© Ahmed Squalli Houssaini. Exercise set published at www.letuteurscientifique.ca/en/exercices/cegep-n11-graphs-and-matrices. Free for personal and classroom use; republishing it elsewhere requires written permission (legal notice).

See also

Looking for a discrete mathematics tutor in Montreal?

Get in touch for a first session. With a B.Sc. in computer science from McGill and an M.Sc. in applied computer science from Concordia, I connect the matrices of the course with what they drive later on: graphs, networks and what you see on the screen.

Site by Studio Squalli