-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from tigergraph/fix_tri_count
fix tri_count
- Loading branch information
Showing
4 changed files
with
78 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |