Formula for counting the number of subgraphs in a given graph $G$ I want to find a formula for counting the number of subgraphs in a given graph $G$ with adjacency matrix $A$.

The subgraph I want to count its number of occurences is of the structure above. So my thoughts are that if we consider the vertex $v_1$ we want to count the number of vertices that have degree three and also a path from itself to itself of length three. So the total number of occurences of this subgraph on a graph is:
$$
\frac{1}{6}\sum_{i}\binom{d_i}{3} \cdot tr(A_{ii}^3)
$$

And on the following subgraph by the same logic will be:
$$
\frac{1}{6}\sum_{i}\binom{d_i}{4} \cdot tr(A_{ii}^3)
$$
Is that correct ?
 A: If you multiply $\binom{d_i}{3}$ by $(A^3)_{ii}$, this corresponds to choosing $3$ edges out of vertex $i$ and separately choosing a length-$3$ walk that starts at $i$ and returns to $i$. The path you choose might not be related to the edges you picked, so it will not necessarily look like the graph you drew.
Instead, start with $(A^3)_{ii}$ to account for the triangle, and then multiply by $d_i - 2$ to pick the third edge.
Your factor of $\frac16$ is also incorrect. It is present in $\frac16 \sum_i (A^3)_{ii}$, the formula for counting triangles, because each triangle is counted by six length-$3$ walks: you could start at any of the vertices, and you could go clockwise or counterclockwise.
Here, there is only one vertex that could be the "center" of the subgraph: the vertex that corresponds to vertex $1$ in your diagram. We can still use two different length-$3$ walks to get the same subgraph, because we can still go clockwise or counterclockwise. So we should divide by $2$: the final formula is $$\frac12 \sum_i (A^3)_{ii} (d_i - 2).$$
For the graph with two leaves, replace $d_i - 2$ by $\binom{d_i-2}{2}$.
