A complete worked solution using adjacency matrices, matrix powers, and generating functions.
Let \(G=(V,E)\) be the undirected multigraph shown above, with vertex set \(V=\{1,2,3,4\}\). Its edge multiset is \(E=\{\{1,2\},\{1,4\},\{2,4\},\{2,3\},\{2,3\}\}\). The two edges between vertices 2 and 3 are distinct, and therefore contribute a multiplicity of two.
For an undirected multigraph, the adjacency matrix entry \(a_{ij}\) equals the number of edges joining vertices \(i\) and \(j\). The vertex ordering is \((1,2,3,4)\).
Because the graph is undirected, \(A=A^{\mathsf T}\). The diagonal entries are zero because there are no loops. The entries \(a_{23}=a_{32}=2\) account for the two parallel edges.
A walk of length \(n\) is a sequence of \(n\) edges joining \(n+1\) vertices; vertices and edges may be revisited. The standard adjacency-matrix theorem states that
First, multiplying \(A\) by itself gives
Multiplying once more yields the requested result:
For example, \((A^3)_{13}=2\): there are two length-three walks from vertex 1 to vertex 3. Both follow the vertex sequence \(1\to4\to2\to3\), but the final step can use either of the two parallel edges between 2 and 3. They are counted as distinct edge walks.
Let \(w_{ij}(n)=(A^n)_{ij}\) be the number of length-\(n\) walks from \(i\) to \(j\). Define the ordinary generating function
Collecting all vertex-pair generating functions into a matrix gives the matrix geometric series
Multiplication by \(I-xA\) produces the identity matrix, so as a formal power series (or analytically for sufficiently small \(|x|\)):
Here
The determinant is
We require the entry in row 1, column 3 of \((I-xA)^{-1}\). Using the adjugate formula,
where \(C_{31}(M)\) is the cofactor of the entry in row 3, column 1. Direct evaluation gives
Therefore,
Canceling the common factor \((1+x)\) gives the simplified closed form:
Expanding about \(x=0\),
The coefficient of \(x^n\) is exactly the number of length-\(n\) walks from vertex 1 to vertex 3. In particular, the coefficient of \(x^3\) is 2, matching the \((1,3)\) entry of \(A^3\).
Writing \(G_{13}(x)=\sum_{n\ge0}a_nx^n\), the denominator of the simplified rational function implies
with \(a_0=0,\ a_1=0,\ a_2=2\). Thus \(a_3=2\), \(a_4=14\), and so on.