Skip to content

Commit

Permalink
Merge pull request #17 from tigergraph/fix_tri_count
Browse files Browse the repository at this point in the history
fix tri_count
  • Loading branch information
Suxiaocai authored Feb 12, 2020
2 parents f8735ab + 59e157b commit 323d106
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 54 deletions.
24 changes: 13 additions & 11 deletions algorithms/examples/Community/tri_count.gsql
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
CREATE QUERY tri_count() FOR GRAPH social {
# Compute the total number of triangles in the GRAPH. No input parameters are needed.
SumAccum<int> @@cnt;
SetAccum<VERTEX> @self;

Start = {ANY};
all = {Person.*};
all = SELECT s
FROM all:s
ACCUM s.@self += s;

# For each edge e, the number of triangles that contain e is equivalent
# to the number of common neighbors between vertices s and t
Result = SELECT t
FROM Start:s -(:e)-> :t
WHERE getvid(s) > getvid(t)
ACCUM INT c = COUNT((s.neighbors()) INTERSECT (t.neighbors())),
@@cnt += c
;

# to the number of common neighbors between vertices s and t

tmp = SELECT t
FROM all:s -((Coworker):e) -:t
WHERE getvid(s) > getvid(t)
ACCUM INT c = COUNT((s.neighbors("Coworker") MINUS s.@self) INTERSECT (t.neighbors("Coworker") MINUS t.@self)),
@@cnt += c;
# Each triangle is counted 3 times for each edge, so final result is divided by 3
PRINT @@cnt/3 AS num_triangles;
}

#INSTALL QUERY tri_count
42 changes: 26 additions & 16 deletions algorithms/examples/Community/tri_count_fast.gsql
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
CREATE QUERY tri_count_fast() FOR GRAPH social {
# Compute the total number of triangles in the GRAPH. No input parameters are needed.
# Use an algorithm which is fast but use additional memory for temporary storage
SumAccum<int> @@cnt;
# Compute the total number of triangles in the graph
# This algorithm is faster than tri_count but uses additional memory for temporary storage
SumAccum<int> @@cnt, @outdegree;
SetAccum<int> @neighbors;

Start = {ANY};

all = {Person.*};
all = SELECT s
FROM all:s
ACCUM s.@outdegree += s.outdegree("Coworker");

tmp = SELECT s
FROM all:s -((Coworker)) -:t
ACCUM IF s == t THEN
s.@outdegree += -1
END;

# We build up our neighbor lists manually because we'll only build them up on the 2 smaller vertices on a triangle.
T0 = SELECT t
FROM Start:s-()-> :t
WHERE (s.outdegree()) > (t.outdegree()) OR ((s.outdegree()) == (t.outdegree()) AND getvid(s) > getvid(t))
ACCUM t.@neighbors += getvid(s);


tmp = SELECT t
FROM all:s-((Coworker))-> :t
WHERE s.@outdegree > t.@outdegree OR (s.@outdegree == t.@outdegree AND getvid(s) > getvid(t))
ACCUM t.@neighbors += getvid(s);

# Here we compute the intersection for 2 points on the triangle.
T1 = SELECT t
FROM Start:s-(:e)-> :t
ACCUM @@cnt += COUNT(s.@neighbors INTERSECT t.@neighbors);

tmp = SELECT t
FROM all:s-((Coworker))-> :t
WHERE s != t
ACCUM @@cnt += COUNT(s.@neighbors INTERSECT t.@neighbors);

# Divide by 2 because every triangle was counted twice
PRINT @@cnt/2 AS num_triangles;

}

#INSTALL QUERY tri_count_fast
24 changes: 13 additions & 11 deletions algorithms/templates/tri_count.gtmp
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
CREATE QUERY tri_count() FOR GRAPH *graph* {
# Compute the total number of triangles in the GRAPH. No input parameters are needed.
SumAccum<int> @@cnt;
SetAccum<VERTEX> @self;

Start = {*vertex-types*};
all = {*vertex-types*};
all = SELECT s
FROM all:s
ACCUM s.@self += s;

# For each edge e, the number of triangles that contain e is equivalent
# to the number of common neighbors between vertices s and t
Result = SELECT t
FROM Start:s -(*edge-types*:e)-> :t
WHERE getvid(s) > getvid(t)
ACCUM INT c = COUNT((*s_neighbors*) INTERSECT (*t_neighbors*)),
@@cnt += c
;

# to the number of common neighbors between vertices s and t

tmp = SELECT t
FROM all:s -(*edge-types*:e) -:t
WHERE getvid(s) > getvid(t)
ACCUM INT c = COUNT((*s_neighbors* MINUS s.@self) INTERSECT (*t_neighbors* MINUS t.@self)),
@@cnt += c;
# Each triangle is counted 3 times for each edge, so final result is divided by 3
PRINT @@cnt/3 AS num_triangles;
}

#INSTALL QUERY tri_count
42 changes: 26 additions & 16 deletions algorithms/templates/tri_count_fast.gtmp
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
CREATE QUERY tri_count_fast() FOR GRAPH *graph* {
# Compute the total number of triangles in the GRAPH. No input parameters are needed.
# Use an algorithm which is fast but use additional memory for temporary storage
SumAccum<int> @@cnt;
# Compute the total number of triangles in the graph
# This algorithm is faster than tri_count but uses additional memory for temporary storage
SumAccum<int> @@cnt, @outdegree;
SetAccum<int> @neighbors;

Start = {*vertex-types*};

all = {*vertex-types*};
all = SELECT s
FROM all:s
ACCUM s.@outdegree += *s_outdegrees*;

tmp = SELECT s
FROM all:s -(*edge-types*) -:t
ACCUM IF s == t THEN
s.@outdegree += -1
END;

# We build up our neighbor lists manually because we'll only build them up on the 2 smaller vertices on a triangle.
T0 = SELECT t
FROM Start:s-(*edge-types*)-> :t
WHERE (*s_outdegrees*) > (*t_outdegrees*) OR ((*s_outdegrees*) == (*t_outdegrees*) AND getvid(s) > getvid(t))
ACCUM t.@neighbors += getvid(s);


tmp = SELECT t
FROM all:s-(*edge-types*)-> :t
WHERE s.@outdegree > t.@outdegree OR (s.@outdegree == t.@outdegree AND getvid(s) > getvid(t))
ACCUM t.@neighbors += getvid(s);

# Here we compute the intersection for 2 points on the triangle.
T1 = SELECT t
FROM Start:s-(*edge-types*:e)-> :t
ACCUM @@cnt += COUNT(s.@neighbors INTERSECT t.@neighbors);

tmp = SELECT t
FROM all:s-(*edge-types*)-> :t
WHERE s != t
ACCUM @@cnt += COUNT(s.@neighbors INTERSECT t.@neighbors);

# Divide by 2 because every triangle was counted twice
PRINT @@cnt/2 AS num_triangles;

}

#INSTALL QUERY tri_count_fast

0 comments on commit 323d106

Please sign in to comment.