-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtg_lcc.gsql
110 lines (95 loc) · 3.5 KB
/
tg_lcc.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
CREATE DISTRIBUTED QUERY tg_lcc (STRING v_type, STRING e_type,INT top_k=100,BOOL print_results = True, STRING result_attribute = "",
STRING file_path = "", BOOL display_edges = FALSE) SYNTAX V1 {
/*
First Author: <First Author Name>
First Commit Date: <First Commit Date>
Recent Author: <Recent Commit Author Name>
Recent Commit Date: <Recent Commit Date>
Repository:
https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Community
Maturity:
Production
Description:
The Local Clustering Coefficient algorithm computes the local clustering coefficient
for each node in the graph.
lcc = Number_trangles/((n-1)n/2)
Here n is the outdegreeof vertex.
Publications:
NA
TigerGraph Documentation:
https://docs.tigergraph.com/graph-ml/current/community-algorithms/local-clustering-coefficient
Parameters:
v_type:
vertex types to traverse
print_results:
If True, print JSON output
e_type:
edge types to traverse
result_attribute:
INT attribute to store results to
top_k:
report only this many top scores
file_path:
file to write CSV output to
display_edges:
If True, output edges for visualization
*/
TYPEDEF TUPLE<VERTEX Vertex_ID, FLOAT score> Vertex_Score;
HeapAccum<Vertex_Score>(top_k, score DESC) @@top_scores_heap;
SumAccum<FLOAT> @sum_tri; #number of trangles
SumAccum<FLOAT> @sum_lcc; #lcc value
SetAccum<int> @neighbors_set; #neighbors set
OrAccum @or_self_con; #check if the vertex is self-connect
SetAccum<EDGE> @@edge_set;
FILE f (file_path);
# Here we compute the intersection for 2 points on the triangle.
Start = {v_type};
Start = SELECT s
FROM Start:s-(e_type)-v_type:t
ACCUM
IF getvid(s) != getvid(t) THEN
t.@neighbors_set += getvid(s)
ELSE
t.@or_self_con+=TRUE
END;# check id the vertex is self-connect
Start = SELECT s
FROM Start:s-(e_type)-v_type:t
WHERE s.outdegree(e_type)>1
ACCUM
s.@sum_tri+=COUNT((s.@neighbors_set INTERSECT t.@neighbors_set))
POST-ACCUM
IF s.@or_self_con AND s.outdegree(e_type)<3 THEN
s.@sum_lcc+=0
ELSE IF s.@or_self_con AND s.outdegree(e_type)>2 THEN
s.@sum_lcc+= (((s.@sum_tri+1-s.outdegree(e_type)))/((s.outdegree(e_type)-2)*(s.outdegree(e_type)-1)))
ELSE
s.@sum_lcc+= ((s.@sum_tri)/((s.outdegree(e_type)-0)*(s.outdegree(e_type)-1)))
END;
#output
Start = SELECT s
FROM Start:s
# Calculate Closeness Centrality for each vertex
POST-ACCUM
IF result_attribute != "" THEN
s.setAttr(result_attribute, s.@sum_lcc)
END,
IF print_results THEN
@@top_scores_heap += Vertex_Score(s, s.@sum_lcc)
END,
IF file_path != "" THEN
f.println(s, s.@sum_lcc)
END;
IF file_path != "" THEN
f.println("Vertex_ID", "lcc");
END;
IF print_results THEN
PRINT @@top_scores_heap AS top_scores;
IF display_edges THEN
PRINT Start[Start.@sum_lcc];
Start = SELECT s
FROM Start:s -(e_type:e)-:t
ACCUM @@edge_set += e;
PRINT @@edge_set;
END;
END;
}