Skip to content
Graph Intelligence Hub
Menu

Algorithm

Triangle counting

activeNeeds verification

Counts triangles (3-cliques) per node or in total; the basis of clustering coefficients and a common fraud and spam signal.

Definition

Intuition

Two of my friends who are also friends with each other close a triangle; dense triangle regions indicate cohesive groups.

Complexity

TimeO(E^{1.5}) with degree ordering
SpaceO(V + E)

Python

import networkx as nx
t = nx.triangles(G)
cc = nx.clustering(G)

Cypher (Neo4j GDS)

CALL gds.triangleCount.stream('myGraph')
YIELD nodeId, triangleCount RETURN gds.util.asNode(nodeId).name, triangleCount

Procedure names and options vary between GDS versions; check the GDS docs for your version.

An interactive step-by-step visualizer for this algorithm is planned for phase 3.

Profile

Time complexityO(E^{1.5}) with degree ordering
Space complexityO(V + E)

Relationships