UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • Running Algorithms
    • Degree Centrality
    • Closeness Centrality
    • Harmonic Centrality
    • Eccentricity Centrality
    • Betweenness Centrality
    • Bridges
    • Articulation Points
    • Eigenvector Centrality
    • Katz Centrality
    • CELF
    • PageRank
    • ArticleRank
    • TextRank
    • HITS
    • SybilRank
    • Jaccard Similarity
    • Overlap Similarity
    • Cosine Similarity
    • Pearson Correlation Coefficient
    • Euclidean Distance
    • KNN
    • Vector Similarity
    • Bipartite Graph
    • HyperANF
    • Weakly Connected Components (WCC)
    • Strongly Connected Components (SCC)
    • k-Edge Connected Components
    • Local Clustering Coefficient
    • Triangle Count
    • Clique Count
    • k-Core
    • k-Truss
    • p-Cohesion
    • Induced Subgraph
    • Topological Sort
    • Breadth-First Search (BFS)
    • Depth-First Search (DFS)
    • Dijkstra's Shortest Path
    • A* Shortest Path
    • Yen's K-Shortest Paths
    • Shortest Path (BFS)
    • Delta-Stepping SSSP
    • Shortest Path Faster Algorithm (SPFA)
    • All-Pairs Shortest Path (APSP)
    • Minimum Spanning Tree (MST)
    • K-Spanning Tree
    • Steiner Tree
    • Prize-Collecting Steiner Tree (PCST)
    • Minimum Cost Flow
    • Maximum Flow
    • K-Hop Fast
    • Longest Path (DAG)
    • Random Walk
    • Adamic-Adar Index
    • Common Neighbors
    • Preferential Attachment
    • Resource Allocation
    • Total Neighbors
    • Same Community
    • Louvain
    • Leiden
    • Modularity Optimization
    • Label Propagation
    • HANP
    • SLPA
    • k-Means
    • HDBSCAN
    • K-1 Coloring
    • Modularity
    • Conductance
    • Max k-Cut
      • Node2Vec
      • Struc2Vec
      • LINE
      • Fast Random Projection
      • Summary of Graph Embedding
      • Gradient Descent
      • Backpropagation
      • Skip-gram
      • Skip-gram Optimization
  1. Docs
  2. /
  3. Graph Algorithms
  4. /
  5. Graph Structure

Triangle Count

Overview

The Triangle Count algorithm identifies and counts triangles in a graph, where each triangle consists of three mutually connected nodes. Triangles indicate the presence of loops and strong connectivity patterns, making them important for graph structure analysis.

In social networks, triangles indicate cohesive communities. In financial or transaction networks, triangles may signal suspicious or fraudulent behavior.

Concepts

Triangle

A triangle is formed by three nodes that are all connected to each other. The graph below contains 2 triangles: {a, b, c} and {b, c, d}.

The algorithm counts the number of triangles each node participates in and also computes the local clustering coefficient as a byproduct.

Considerations

  • The algorithm treats all edges as undirected.
  • Triangles are counted by nodes — multi-edges between the same pair of nodes are deduplicated and counted as a single connection.

Example Graph

GQL
INSERT (C1:default {_id: "C1"}), (C2:default {_id: "C2"}),
       (C3:default {_id: "C3"}), (C4:default {_id: "C4"}),
       (C5:default {_id: "C5"}), (C6:default {_id: "C6"}),
       (C4)-[:default]->(C1), (C4)-[:default]->(C1),
       (C4)-[:default]->(C2), (C1)-[:default]->(C2),
       (C2)-[:default]->(C3), (C1)-[:default]->(C3),
       (C3)-[:default]->(C5), (C3)-[:default]->(C6)

Parameters

NameTypeDefaultDescription
limitINT-1Limits the number of results returned (-1 = all).
orderSTRING/Sorts the results by triangleCount: asc or desc.

Run Mode

Returns:

ColumnTypeDescription
nodeIdSTRINGNode identifier (_id)
triangleCountINTNumber of triangles the node participates in
coefficientFLOATLocal clustering coefficient
GQL
CALL algo.trianglecount({
  order: "desc"
}) YIELD nodeId, triangleCount, coefficient

Result:

nodeIdtriangleCountcoefficient
C220.6666666666666666
C120.6666666666666666
C310.16666666666666666
C411
C600
C500

Stream Mode

Returns the same columns as run mode, streamed for memory efficiency.

GQL
CALL algo.trianglecount.stream() YIELD nodeId, triangleCount
FILTER triangleCount > 0
RETURN nodeId, triangleCount

Result:

nodeIdtriangleCount
C22
C31
C12
C41

Stats Mode

Returns:

ColumnTypeDescription
nodeCountINTTotal number of nodes
totalTrianglesINTTotal number of unique triangles in the graph
avgCoefficientFLOATAverage clustering coefficient
GQL
CALL algo.trianglecount.stats() YIELD nodeCount, totalTriangles, avgCoefficient

Result:

nodeCounttotalTrianglesavgCoefficient
620.4166666666666667

Write Mode

Computes results and writes them back to node properties. The write configuration is passed as a second argument map.

Write parameters:

NameTypeDescription
db.propertySTRING or MAPNode property to write results to. String: writes the triangleCount column in results to a property. Map: explicit column-to-property mapping (e.g., {triangleCount: 'tri_count', coefficient: 'lcc'}).

Writable columns:

ColumnTypeDescription
triangleCountINTTriangle count
coefficientFLOATLocal clustering coefficient

Returns:

ColumnTypeDescription
task_idSTRINGTask identifier for tracking via SHOW TASKS
nodesWrittenINTNumber of nodes with properties written
computeTimeMsINTTime spent computing the algorithm (milliseconds)
writeTimeMsINTTime spent writing properties to storage (milliseconds)
GQL
CALL algo.trianglecount.write({}, {
  db: {
    property: "tri_count"
  }
}) YIELD task_id, nodesWritten, computeTimeMs, writeTimeMs