Skip to content

Commit 323d106

Browse files
authored
Merge pull request #17 from tigergraph/fix_tri_count
fix tri_count
2 parents f8735ab + 59e157b commit 323d106

File tree

4 files changed

+78
-54
lines changed

4 files changed

+78
-54
lines changed

algorithms/examples/Community/tri_count.gsql

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
CREATE QUERY tri_count() FOR GRAPH social {
22
# Compute the total number of triangles in the GRAPH. No input parameters are needed.
33
SumAccum<int> @@cnt;
4+
SetAccum<VERTEX> @self;
45

5-
Start = {ANY};
6+
all = {Person.*};
7+
all = SELECT s
8+
FROM all:s
9+
ACCUM s.@self += s;
610

711
# For each edge e, the number of triangles that contain e is equivalent
8-
# to the number of common neighbors between vertices s and t
9-
Result = SELECT t
10-
FROM Start:s -(:e)-> :t
11-
WHERE getvid(s) > getvid(t)
12-
ACCUM INT c = COUNT((s.neighbors()) INTERSECT (t.neighbors())),
13-
@@cnt += c
14-
;
15-
12+
# to the number of common neighbors between vertices s and t
13+
14+
tmp = SELECT t
15+
FROM all:s -((Coworker):e) -:t
16+
WHERE getvid(s) > getvid(t)
17+
ACCUM INT c = COUNT((s.neighbors("Coworker") MINUS s.@self) INTERSECT (t.neighbors("Coworker") MINUS t.@self)),
18+
@@cnt += c;
19+
1620
# Each triangle is counted 3 times for each edge, so final result is divided by 3
1721
PRINT @@cnt/3 AS num_triangles;
1822
}
19-
20-
#INSTALL QUERY tri_count

algorithms/examples/Community/tri_count_fast.gsql

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
CREATE QUERY tri_count_fast() FOR GRAPH social {
2-
# Compute the total number of triangles in the GRAPH. No input parameters are needed.
3-
# Use an algorithm which is fast but use additional memory for temporary storage
4-
SumAccum<int> @@cnt;
2+
# Compute the total number of triangles in the graph
3+
# This algorithm is faster than tri_count but uses additional memory for temporary storage
4+
SumAccum<int> @@cnt, @outdegree;
55
SetAccum<int> @neighbors;
6-
7-
Start = {ANY};
6+
7+
all = {Person.*};
8+
all = SELECT s
9+
FROM all:s
10+
ACCUM s.@outdegree += s.outdegree("Coworker");
11+
12+
tmp = SELECT s
13+
FROM all:s -((Coworker)) -:t
14+
ACCUM IF s == t THEN
15+
s.@outdegree += -1
16+
END;
817

918
# We build up our neighbor lists manually because we'll only build them up on the 2 smaller vertices on a triangle.
10-
T0 = SELECT t
11-
FROM Start:s-()-> :t
12-
WHERE (s.outdegree()) > (t.outdegree()) OR ((s.outdegree()) == (t.outdegree()) AND getvid(s) > getvid(t))
13-
ACCUM t.@neighbors += getvid(s);
14-
19+
20+
tmp = SELECT t
21+
FROM all:s-((Coworker))-> :t
22+
WHERE s.@outdegree > t.@outdegree OR (s.@outdegree == t.@outdegree AND getvid(s) > getvid(t))
23+
ACCUM t.@neighbors += getvid(s);
24+
1525
# Here we compute the intersection for 2 points on the triangle.
16-
T1 = SELECT t
17-
FROM Start:s-(:e)-> :t
18-
ACCUM @@cnt += COUNT(s.@neighbors INTERSECT t.@neighbors);
19-
26+
tmp = SELECT t
27+
FROM all:s-((Coworker))-> :t
28+
WHERE s != t
29+
ACCUM @@cnt += COUNT(s.@neighbors INTERSECT t.@neighbors);
30+
2031
# Divide by 2 because every triangle was counted twice
2132
PRINT @@cnt/2 AS num_triangles;
33+
2234
}
23-
24-
#INSTALL QUERY tri_count_fast

algorithms/templates/tri_count.gtmp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
CREATE QUERY tri_count() FOR GRAPH *graph* {
22
# Compute the total number of triangles in the GRAPH. No input parameters are needed.
33
SumAccum<int> @@cnt;
4+
SetAccum<VERTEX> @self;
45

5-
Start = {*vertex-types*};
6+
all = {*vertex-types*};
7+
all = SELECT s
8+
FROM all:s
9+
ACCUM s.@self += s;
610

711
# For each edge e, the number of triangles that contain e is equivalent
8-
# to the number of common neighbors between vertices s and t
9-
Result = SELECT t
10-
FROM Start:s -(*edge-types*:e)-> :t
11-
WHERE getvid(s) > getvid(t)
12-
ACCUM INT c = COUNT((*s_neighbors*) INTERSECT (*t_neighbors*)),
13-
@@cnt += c
14-
;
15-
12+
# to the number of common neighbors between vertices s and t
13+
14+
tmp = SELECT t
15+
FROM all:s -(*edge-types*:e) -:t
16+
WHERE getvid(s) > getvid(t)
17+
ACCUM INT c = COUNT((*s_neighbors* MINUS s.@self) INTERSECT (*t_neighbors* MINUS t.@self)),
18+
@@cnt += c;
19+
1620
# Each triangle is counted 3 times for each edge, so final result is divided by 3
1721
PRINT @@cnt/3 AS num_triangles;
1822
}
19-
20-
#INSTALL QUERY tri_count

algorithms/templates/tri_count_fast.gtmp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
CREATE QUERY tri_count_fast() FOR GRAPH *graph* {
2-
# Compute the total number of triangles in the GRAPH. No input parameters are needed.
3-
# Use an algorithm which is fast but use additional memory for temporary storage
4-
SumAccum<int> @@cnt;
2+
# Compute the total number of triangles in the graph
3+
# This algorithm is faster than tri_count but uses additional memory for temporary storage
4+
SumAccum<int> @@cnt, @outdegree;
55
SetAccum<int> @neighbors;
6-
7-
Start = {*vertex-types*};
6+
7+
all = {*vertex-types*};
8+
all = SELECT s
9+
FROM all:s
10+
ACCUM s.@outdegree += *s_outdegrees*;
11+
12+
tmp = SELECT s
13+
FROM all:s -(*edge-types*) -:t
14+
ACCUM IF s == t THEN
15+
s.@outdegree += -1
16+
END;
817

918
# We build up our neighbor lists manually because we'll only build them up on the 2 smaller vertices on a triangle.
10-
T0 = SELECT t
11-
FROM Start:s-(*edge-types*)-> :t
12-
WHERE (*s_outdegrees*) > (*t_outdegrees*) OR ((*s_outdegrees*) == (*t_outdegrees*) AND getvid(s) > getvid(t))
13-
ACCUM t.@neighbors += getvid(s);
14-
19+
20+
tmp = SELECT t
21+
FROM all:s-(*edge-types*)-> :t
22+
WHERE s.@outdegree > t.@outdegree OR (s.@outdegree == t.@outdegree AND getvid(s) > getvid(t))
23+
ACCUM t.@neighbors += getvid(s);
24+
1525
# Here we compute the intersection for 2 points on the triangle.
16-
T1 = SELECT t
17-
FROM Start:s-(*edge-types*:e)-> :t
18-
ACCUM @@cnt += COUNT(s.@neighbors INTERSECT t.@neighbors);
19-
26+
tmp = SELECT t
27+
FROM all:s-(*edge-types*)-> :t
28+
WHERE s != t
29+
ACCUM @@cnt += COUNT(s.@neighbors INTERSECT t.@neighbors);
30+
2031
# Divide by 2 because every triangle was counted twice
2132
PRINT @@cnt/2 AS num_triangles;
33+
2234
}
23-
24-
#INSTALL QUERY tri_count_fast

0 commit comments

Comments
 (0)