What's an easy algorithm to check for triangle-freeness?

164 Views Asked by At

This is from a text that I'm reading.

enter image description here

By "query the status of any pair of vertices", I assume the author means "to check whether the two vertices are adjacent"? From there how do they go about checking for triangle-freeness? What would the order of the number of steps required be?

2

There are 2 best solutions below

2
On

There are only $\Theta(n^2)$ pairs of vertices in the graph, so you can query every pair; then you simply check every triplet in the graph to see if they form a triangle.

1
On

Let $A$ be the adjacency matrix of the graph. Then the graph is triangle free if and only if $tr(A^3)=0$. This relies on the fact that the powers of the adjacency matrix count the number of paths of that length.

While this is concise and looks easy I doubt this is a fast way to check triangle-freeness for a large graph.