What's the most efficient algorithm to check the number of cycles of length 4 in an undirected graph? What's the most efficient algorithm to check the number of cycles of length 4 in an undirected, unweighted graph?

I don't know whether this is the fastest possible, but this is how I'd do it if I had to do it fast: Use a hash map that maps pairs of vertices to the number of paths of length $2$ between them. Iterate over the vertices, adding $1$ to the entry for each pair of neighbours of the vertex. Then iterate over the entries, summing $\binom n2$ of the counts $n$. Divide the total by $2$ since you've counted each $4$-cycle twice. (Of course if you're really after speed you'd sum $n(n-1)$ and divide by $4$ in the end.)
