If $A$ is the adjacency matrix of graph $G$, then it is a well know property that $A^n$ is a matrix where the element $(i, j)$ gives the number of walks of length $n$ from vertex $i$ to vertex $j$.
The matrix of paths is different and there are closed forms for paths of length 2 and 3 (https://mathworld.wolfram.com/GraphPath.html). For example, the matrix of paths of length 3 is given by:
$$ P_3 = A^3 - \text{diag}(A^2) \cdot A - A \cdot \text{diag}(A^2) + A \times A^T - \text{diag}(A^3) $$
("$\cdot$" is normal matrix multiplication, "$\times$" is element-wise multiplication and "diag$(A)$" has the same principal diagonal as A with the remaining elements set to zero)
I am interested in the matrix of paths of length 3, for 3 (potentially different) graphs. The interpretation of the product of two different adjacency matrices $A \cdot B$ is (from this post):
"Cell $(i,j)$ in $A \cdot B$ contains the number of walks from $i$ to $j$ where the first step is in $A$, but the second step is in $B$"
Given three undirected graphs $G_1$, $G_2$ and $G_3$ with (symmetric since undirected) adjacency matrices $A$, $B$ and $C$, what I need is an expression for the matrix of 3-paths from $G_1$ to $G_2$ to $G_3$.
My attempt at this is:
$$ A \cdot B \cdot C + (A \cdot B \cdot C)^T - C \cdot \text{diag}(A \cdot B) - \text{diag}(A \cdot B) \cdot C - \text{diag}(A \cdot B \cdot C) $$
However, this is just based on observations and tweaking. It works on a non-trivial example, but I do not know if it is correct. What I'd like is:
- To confirm this is correct, or if it is not, find the correct expression.
- Have a sketch/intuition of a proof