How do we count the numbers of traingles (i.e., different triads of vertex $u,v,w$ such that edges $u-v,v-w,w-u \in E$ ) in a given graph $G$?
It is obvious that it can be done in $O(N^2)$: is there anything better?
How do we count the numbers of traingles (i.e., different triads of vertex $u,v,w$ such that edges $u-v,v-w,w-u \in E$ ) in a given graph $G$?
It is obvious that it can be done in $O(N^2)$: is there anything better?
I'm assuming that the graph is given as an adjacency matrix. The trivial algorithm for counting triangles goes over all triples of vertices $u,v,w$ and checks whether $\{u,v,w\}$ is a triangle, for a total running time of $\Theta(N^3)$. This can be improved using matrix multiplication: if $A$ is the adjacency matrix, then the number of triangles can be extracted from $\operatorname{Tr} A^3$, taking into account the number of vertices and edges in the graph (details left to you). This takes time $O(n^\omega)$, where $\omega$ is the exponent of matrix multiplication.