-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtri_count.gsql
55 lines (43 loc) · 1.59 KB
/
tri_count.gsql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
CREATE TEMPLATE QUERY GDBMS_ALGO.community.tri_count(
SET<STRING> v_type_set,
SET<STRING> e_type_set
) SYNTAX V1 {
/*
First Author: [email protected]
First Commit Date: 2024-07-17
Recent Author: [email protected]
Recent Commit Date: 2024-07-17
Repository:
https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Community
Maturity:
Production
Description:
This query computes the total number of triangles in the graph.
Publications:
NA
TigerGraph Documentation:
https://docs.tigergraph.com/graph-ml/current/community-algorithms/triangle-counting
Parameters:
v_type_set:
The set of vertex types to traverse.
e_type_set:
The set of edge types to traverse.
*/
SumAccum<INT> @@sum_tri_count;
SetAccum<VERTEX> @set_neighbors;
Nodes = {v_type_set};
// Build neighbor sets manually, only for vertices with smaller IDs in the triangle.
// This ensures that only two of the three vertices in a triangle will build neighbor sets.
Tmp = SELECT t
FROM Nodes:s-(e_type_set)- v_type_set:t
WHERE getvid(s) > getvid(t)
ACCUM t.@set_neighbors += s;
// Compute the intersection of neighbor sets to count triangles.
// This step ensures that each triangle is counted only once.
Tmp = SELECT t
FROM Nodes:s-(e_type_set)- :t
WHERE getvid(s) > getvid(t)
ACCUM @@sum_tri_count += COUNT(s.@set_neighbors INTERSECT t.@set_neighbors);
// Output the results
PRINT @@sum_tri_count AS num_triangles;
}