Skip to content

Commit

Permalink
Merge pull request #149 from tigergraph/degree-cent
Browse files Browse the repository at this point in the history
Degree cent
  • Loading branch information
RobRossmiller-TG authored May 28, 2024
2 parents ce724c0 + 090362c commit 356c36f
Show file tree
Hide file tree
Showing 187 changed files with 428 additions and 190 deletions.
98 changes: 50 additions & 48 deletions GDBMS_ALGO/centrality/degree_cent.gsql
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
CREATE TEMPLATE QUERY GDBMS_ALGO.centrality.degree_cent(SET<STRING> v_type_set, SET<STRING> e_type_set, SET<STRING> reverse_e_type_set, BOOL in_degree = TRUE, BOOL out_degree = TRUE,
INT top_k=100, BOOL print_results = TRUE, STRING result_attribute = "",STRING file_path = "") SYNTAX V1 {

INT top_k=100, BOOL print_results = TRUE, STRING result_attribute = "",STRING file_path = "", BOOL normalize = TRUE) 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>
First Author: <First Author Name>
First Commit Date: <First Commit Date>

Recent Author: Rob Rossmiller
Recent Commit Date: 05/2024

Repository:
https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Centrality

Maturity:
Production
Repository:
https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Centrality

Description:
Compute degree Centrality for each VERTEX.
for undirected graph, you only need to set e_type_set and indegree
Maturity:
Production

Publications:
NA
Description:
Compute degree Centrality for each VERTEX.
for undirected graph, you only need to set e_type_set and indegree

TigerGraph Documentation:
https://docs.tigergraph.com/graph-ml/current/centrality-algorithms/degree-centrality
Publications:
NA

Parameters:
v_type_set:
vertex types to traverse
e_type_set:
edge types to traverse
reverse_e_type_set:
for indegree use
in_degree:
If True, count incoming relationships
out_degree:
If True, count outcoming relationships
top_k:
report only this many top scores
print_results:
If True, print the result
result_attribute:
attribute to write result to
file_path:
file to write CSV output to
TigerGraph Documentation:
https://docs.tigergraph.com/graph-ml/current/centrality-algorithms/degree-centrality

Parameters:
v_type_set:
vertex types to traverse
e_type_set:
edge types to traverse
reverse_e_type_set:
for indegree use
in_degree:
If True, count incoming relationships
out_degree:
If True, count outcoming relationships
top_k:
report only this many top scores
print_results:
If True, print the result
result_attribute:
attribute to write result to
file_path:
file to write CSV output to
normailize:
If True, return the normalized centrality. Default: True
*/

TYPEDEF TUPLE<VERTEX Vertex_ID, FLOAT score> Vertex_Score;
HeapAccum<Vertex_Score>(top_k, score DESC) @@top_scores_heap;
SumAccum<INT> @sum_degree_score;
SumAccum<DOUBLE> @sum_degree_score;
FILE f (file_path);

all = {v_type_set};
sll = SELECT s
FROM all:s
ACCUM IF in_degree THEN
FOREACH edge_type in reverse_e_type_set DO
s.@sum_degree_score+=s.outdegree(edge_type)
END
END,
IF out_degree THEN
FOREACH edge_type in e_type_set DO
s.@sum_degree_score+=s.outdegree(edge_type)
END
END;
ACCUM
IF in_degree THEN
s.@sum_degree_score += s.outdegree(reverse_e_type_set)
END,
IF out_degree THEN
s.@sum_degree_score += s.outdegree(e_type_set)
END
POST-ACCUM
IF normalize THEN
s.@sum_degree_score = s.@sum_degree_score / (all.size() - 1)
END;

#Output
IF file_path != "" THEN
f.println("Vertex_ID", "Degree");
Expand Down
99 changes: 50 additions & 49 deletions algorithms/Centrality/degree/unweighted/tg_degree_cent.gsql
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
CREATE QUERY tg_degree_cent(SET<STRING> v_type_set, SET<STRING> e_type_set, SET<STRING> reverse_e_type_set, BOOL in_degree = TRUE, BOOL out_degree = TRUE,
INT top_k=100, BOOL print_results = TRUE, STRING result_attribute = "",STRING file_path = "") SYNTAX V1 {

CREATE QUERY tg_degree_cent(SET<STRING> v_type_set, SET<STRING> e_type_set, SET<STRING> reverse_e_type_set, BOOL in_degree = TRUE, BOOL out_degree = TRUE, INT top_k=100, BOOL print_results = TRUE, STRING result_attribute = "",STRING file_path = "", BOOL normalize = TRUE) 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>
First Author: <First Author Name>
First Commit Date: <First Commit Date>

Recent Author: Rob Rossmiller
Recent Commit Date: 05/2024

Repository:
https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Centrality

Maturity:
Production
Repository:
https://github.com/tigergraph/gsql-graph-algorithms/tree/master/algorithms/Centrality

Description:
Compute degree Centrality for each VERTEX.
for undirected graph, you only need to set e_type_set and indegree
Maturity:
Production

Publications:
NA
Description:
Compute degree Centrality for each VERTEX.
for undirected graph, you only need to set e_type_set and indegree

TigerGraph Documentation:
https://docs.tigergraph.com/graph-ml/current/centrality-algorithms/degree-centrality
Publications:
NA

Parameters:
v_type_set:
vertex types to traverse
e_type_set:
edge types to traverse
reverse_e_type_set:
for indegree use
in_degree:
If True, count incoming relationships
out_degree:
If True, count outcoming relationships
top_k:
report only this many top scores
print_results:
If True, print the result
result_attribute:
attribute to write result to
file_path:
file to write CSV output to
TigerGraph Documentation:
https://docs.tigergraph.com/graph-ml/current/centrality-algorithms/degree-centrality

Parameters:
v_type_set:
vertex types to traverse
e_type_set:
edge types to traverse
reverse_e_type_set:
for indegree use
in_degree:
If True, count incoming relationships
out_degree:
If True, count outcoming relationships
top_k:
report only this many top scores
print_results:
If True, print the result
result_attribute:
attribute to write result to
file_path:
file to write CSV output to
normailize:
If True, return the normalized centrality. Default: True
*/

TYPEDEF TUPLE<VERTEX Vertex_ID, FLOAT score> Vertex_Score;
HeapAccum<Vertex_Score>(top_k, score DESC) @@top_scores_heap;
SumAccum<INT> @sum_degree_score;
SumAccum<DOUBLE> @sum_degree_score;
FILE f (file_path);

all = {v_type_set};
sll = SELECT s
FROM all:s
ACCUM IF in_degree THEN
FOREACH edge_type in reverse_e_type_set DO
s.@sum_degree_score+=s.outdegree(edge_type)
END
END,
IF out_degree THEN
FOREACH edge_type in e_type_set DO
s.@sum_degree_score+=s.outdegree(edge_type)
END
END;
ACCUM
IF in_degree THEN
s.@sum_degree_score += s.outdegree(reverse_e_type_set)
END,
IF out_degree THEN
s.@sum_degree_score += s.outdegree(e_type_set)
END
POST-ACCUM
IF normalize THEN
s.@sum_degree_score = s.@sum_degree_score / (all.size() - 1)
END;

#Output
IF file_path != "" THEN
f.println("Vertex_ID", "Degree");
Expand Down
6 changes: 6 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Algorithm Testing

[networkX](https://networkx.org/documentation/stable/reference/index.html) is used as
the baseline for our tests where we can.

A peculiarity to note about nx is that it counts self edges in undirected graphs as two separate edges. So, you'll see that some baselines use a modified version of the code that is found within nx. Take `run_degree_baseline_complete`, which generates the baseline for degree centrality on complete graphs. It uses the same code that can be found in `nx.centrality.degree_centrality` with the exception of subtracting a node's degree by one.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 1.1428571428571428}, {"Vertex_ID": "B", "score": 1.1428571428571428}, {"Vertex_ID": "C", "score": 1.1428571428571428}, {"Vertex_ID": "D", "score": 1.1428571428571428}, {"Vertex_ID": "E", "score": 1.1428571428571428}, {"Vertex_ID": "F", "score": 1.1428571428571428}, {"Vertex_ID": "G", "score": 1.1428571428571428}, {"Vertex_ID": "H", "score": 1.1428571428571428}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 1.0}, {"Vertex_ID": "B", "score": 0.05263157894736842}, {"Vertex_ID": "C", "score": 0.05263157894736842}, {"Vertex_ID": "D", "score": 0.05263157894736842}, {"Vertex_ID": "E", "score": 0.05263157894736842}, {"Vertex_ID": "F", "score": 0.05263157894736842}, {"Vertex_ID": "G", "score": 0.05263157894736842}, {"Vertex_ID": "H", "score": 0.05263157894736842}, {"Vertex_ID": "I", "score": 0.05263157894736842}, {"Vertex_ID": "J", "score": 0.05263157894736842}, {"Vertex_ID": "K", "score": 0.05263157894736842}, {"Vertex_ID": "L", "score": 0.05263157894736842}, {"Vertex_ID": "M", "score": 0.05263157894736842}, {"Vertex_ID": "N", "score": 0.05263157894736842}, {"Vertex_ID": "O", "score": 0.05263157894736842}, {"Vertex_ID": "P", "score": 0.05263157894736842}, {"Vertex_ID": "Q", "score": 0.05263157894736842}, {"Vertex_ID": "R", "score": 0.05263157894736842}, {"Vertex_ID": "S", "score": 0.05263157894736842}, {"Vertex_ID": "T", "score": 0.05263157894736842}]}]
1 change: 1 addition & 0 deletions tests/data/baseline/centrality/degree_centrality/Line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 0.05263157894736842}, {"Vertex_ID": "B", "score": 0.10526315789473684}, {"Vertex_ID": "C", "score": 0.10526315789473684}, {"Vertex_ID": "D", "score": 0.10526315789473684}, {"Vertex_ID": "E", "score": 0.10526315789473684}, {"Vertex_ID": "F", "score": 0.10526315789473684}, {"Vertex_ID": "G", "score": 0.10526315789473684}, {"Vertex_ID": "H", "score": 0.10526315789473684}, {"Vertex_ID": "I", "score": 0.10526315789473684}, {"Vertex_ID": "J", "score": 0.10526315789473684}, {"Vertex_ID": "K", "score": 0.10526315789473684}, {"Vertex_ID": "L", "score": 0.10526315789473684}, {"Vertex_ID": "M", "score": 0.10526315789473684}, {"Vertex_ID": "N", "score": 0.10526315789473684}, {"Vertex_ID": "O", "score": 0.10526315789473684}, {"Vertex_ID": "P", "score": 0.10526315789473684}, {"Vertex_ID": "Q", "score": 0.10526315789473684}, {"Vertex_ID": "R", "score": 0.10526315789473684}, {"Vertex_ID": "S", "score": 0.10526315789473684}, {"Vertex_ID": "T", "score": 0.05263157894736842}]}]
1 change: 1 addition & 0 deletions tests/data/baseline/centrality/degree_centrality/Ring.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 0.10526315789473684}, {"Vertex_ID": "B", "score": 0.10526315789473684}, {"Vertex_ID": "C", "score": 0.10526315789473684}, {"Vertex_ID": "D", "score": 0.10526315789473684}, {"Vertex_ID": "E", "score": 0.10526315789473684}, {"Vertex_ID": "F", "score": 0.10526315789473684}, {"Vertex_ID": "G", "score": 0.10526315789473684}, {"Vertex_ID": "H", "score": 0.10526315789473684}, {"Vertex_ID": "I", "score": 0.10526315789473684}, {"Vertex_ID": "J", "score": 0.10526315789473684}, {"Vertex_ID": "K", "score": 0.10526315789473684}, {"Vertex_ID": "L", "score": 0.10526315789473684}, {"Vertex_ID": "M", "score": 0.10526315789473684}, {"Vertex_ID": "N", "score": 0.10526315789473684}, {"Vertex_ID": "O", "score": 0.10526315789473684}, {"Vertex_ID": "P", "score": 0.10526315789473684}, {"Vertex_ID": "Q", "score": 0.10526315789473684}, {"Vertex_ID": "R", "score": 0.10526315789473684}, {"Vertex_ID": "S", "score": 0.10526315789473684}, {"Vertex_ID": "T", "score": 0.10526315789473684}]}]
1 change: 1 addition & 0 deletions tests/data/baseline/centrality/degree_centrality/Tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 0.10526315789473684}, {"Vertex_ID": "B", "score": 0.15789473684210525}, {"Vertex_ID": "C", "score": 0.15789473684210525}, {"Vertex_ID": "D", "score": 0.15789473684210525}, {"Vertex_ID": "E", "score": 0.15789473684210525}, {"Vertex_ID": "F", "score": 0.15789473684210525}, {"Vertex_ID": "G", "score": 0.15789473684210525}, {"Vertex_ID": "H", "score": 0.15789473684210525}, {"Vertex_ID": "I", "score": 0.15789473684210525}, {"Vertex_ID": "J", "score": 0.10526315789473684}, {"Vertex_ID": "K", "score": 0.05263157894736842}, {"Vertex_ID": "L", "score": 0.05263157894736842}, {"Vertex_ID": "M", "score": 0.05263157894736842}, {"Vertex_ID": "N", "score": 0.05263157894736842}, {"Vertex_ID": "O", "score": 0.05263157894736842}, {"Vertex_ID": "P", "score": 0.05263157894736842}, {"Vertex_ID": "Q", "score": 0.05263157894736842}, {"Vertex_ID": "R", "score": 0.05263157894736842}, {"Vertex_ID": "S", "score": 0.05263157894736842}, {"Vertex_ID": "T", "score": 0.05263157894736842}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 0.0}, {"Vertex_ID": "B", "score": 0.05263157894736842}, {"Vertex_ID": "C", "score": 0.05263157894736842}, {"Vertex_ID": "D", "score": 0.05263157894736842}, {"Vertex_ID": "E", "score": 0.05263157894736842}, {"Vertex_ID": "F", "score": 0.05263157894736842}, {"Vertex_ID": "G", "score": 0.05263157894736842}, {"Vertex_ID": "H", "score": 0.05263157894736842}, {"Vertex_ID": "I", "score": 0.05263157894736842}, {"Vertex_ID": "J", "score": 0.05263157894736842}, {"Vertex_ID": "K", "score": 0.05263157894736842}, {"Vertex_ID": "L", "score": 0.05263157894736842}, {"Vertex_ID": "M", "score": 0.05263157894736842}, {"Vertex_ID": "N", "score": 0.05263157894736842}, {"Vertex_ID": "O", "score": 0.05263157894736842}, {"Vertex_ID": "P", "score": 0.05263157894736842}, {"Vertex_ID": "Q", "score": 0.05263157894736842}, {"Vertex_ID": "R", "score": 0.05263157894736842}, {"Vertex_ID": "S", "score": 0.05263157894736842}, {"Vertex_ID": "T", "score": 0.05263157894736842}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 0.0}, {"Vertex_ID": "B", "score": 0.05263157894736842}, {"Vertex_ID": "C", "score": 0.05263157894736842}, {"Vertex_ID": "D", "score": 0.05263157894736842}, {"Vertex_ID": "E", "score": 0.05263157894736842}, {"Vertex_ID": "F", "score": 0.05263157894736842}, {"Vertex_ID": "G", "score": 0.05263157894736842}, {"Vertex_ID": "H", "score": 0.05263157894736842}, {"Vertex_ID": "I", "score": 0.05263157894736842}, {"Vertex_ID": "J", "score": 0.05263157894736842}, {"Vertex_ID": "K", "score": 0.05263157894736842}, {"Vertex_ID": "L", "score": 0.05263157894736842}, {"Vertex_ID": "M", "score": 0.05263157894736842}, {"Vertex_ID": "N", "score": 0.05263157894736842}, {"Vertex_ID": "O", "score": 0.05263157894736842}, {"Vertex_ID": "P", "score": 0.05263157894736842}, {"Vertex_ID": "Q", "score": 0.05263157894736842}, {"Vertex_ID": "R", "score": 0.05263157894736842}, {"Vertex_ID": "S", "score": 0.05263157894736842}, {"Vertex_ID": "T", "score": 0.05263157894736842}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 0.05263157894736842}, {"Vertex_ID": "B", "score": 0.05263157894736842}, {"Vertex_ID": "C", "score": 0.05263157894736842}, {"Vertex_ID": "D", "score": 0.05263157894736842}, {"Vertex_ID": "E", "score": 0.05263157894736842}, {"Vertex_ID": "F", "score": 0.05263157894736842}, {"Vertex_ID": "G", "score": 0.05263157894736842}, {"Vertex_ID": "H", "score": 0.05263157894736842}, {"Vertex_ID": "I", "score": 0.05263157894736842}, {"Vertex_ID": "J", "score": 0.05263157894736842}, {"Vertex_ID": "K", "score": 0.05263157894736842}, {"Vertex_ID": "L", "score": 0.05263157894736842}, {"Vertex_ID": "M", "score": 0.05263157894736842}, {"Vertex_ID": "N", "score": 0.05263157894736842}, {"Vertex_ID": "O", "score": 0.05263157894736842}, {"Vertex_ID": "P", "score": 0.05263157894736842}, {"Vertex_ID": "Q", "score": 0.05263157894736842}, {"Vertex_ID": "R", "score": 0.05263157894736842}, {"Vertex_ID": "S", "score": 0.05263157894736842}, {"Vertex_ID": "T", "score": 0.05263157894736842}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 0.0}, {"Vertex_ID": "B", "score": 0.05263157894736842}, {"Vertex_ID": "C", "score": 0.05263157894736842}, {"Vertex_ID": "D", "score": 0.05263157894736842}, {"Vertex_ID": "E", "score": 0.05263157894736842}, {"Vertex_ID": "F", "score": 0.05263157894736842}, {"Vertex_ID": "G", "score": 0.05263157894736842}, {"Vertex_ID": "H", "score": 0.05263157894736842}, {"Vertex_ID": "I", "score": 0.05263157894736842}, {"Vertex_ID": "J", "score": 0.05263157894736842}, {"Vertex_ID": "K", "score": 0.05263157894736842}, {"Vertex_ID": "L", "score": 0.05263157894736842}, {"Vertex_ID": "M", "score": 0.05263157894736842}, {"Vertex_ID": "N", "score": 0.05263157894736842}, {"Vertex_ID": "O", "score": 0.05263157894736842}, {"Vertex_ID": "P", "score": 0.05263157894736842}, {"Vertex_ID": "Q", "score": 0.05263157894736842}, {"Vertex_ID": "R", "score": 0.05263157894736842}, {"Vertex_ID": "S", "score": 0.05263157894736842}, {"Vertex_ID": "T", "score": 0.05263157894736842}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 1.0}, {"Vertex_ID": "B", "score": 0.0}, {"Vertex_ID": "C", "score": 0.0}, {"Vertex_ID": "D", "score": 0.0}, {"Vertex_ID": "E", "score": 0.0}, {"Vertex_ID": "F", "score": 0.0}, {"Vertex_ID": "G", "score": 0.0}, {"Vertex_ID": "H", "score": 0.0}, {"Vertex_ID": "I", "score": 0.0}, {"Vertex_ID": "J", "score": 0.0}, {"Vertex_ID": "K", "score": 0.0}, {"Vertex_ID": "L", "score": 0.0}, {"Vertex_ID": "M", "score": 0.0}, {"Vertex_ID": "N", "score": 0.0}, {"Vertex_ID": "O", "score": 0.0}, {"Vertex_ID": "P", "score": 0.0}, {"Vertex_ID": "Q", "score": 0.0}, {"Vertex_ID": "R", "score": 0.0}, {"Vertex_ID": "S", "score": 0.0}, {"Vertex_ID": "T", "score": 0.0}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"top_scores": [{"Vertex_ID": "A", "score": 0.05263157894736842}, {"Vertex_ID": "B", "score": 0.05263157894736842}, {"Vertex_ID": "C", "score": 0.05263157894736842}, {"Vertex_ID": "D", "score": 0.05263157894736842}, {"Vertex_ID": "E", "score": 0.05263157894736842}, {"Vertex_ID": "F", "score": 0.05263157894736842}, {"Vertex_ID": "G", "score": 0.05263157894736842}, {"Vertex_ID": "H", "score": 0.05263157894736842}, {"Vertex_ID": "I", "score": 0.05263157894736842}, {"Vertex_ID": "J", "score": 0.05263157894736842}, {"Vertex_ID": "K", "score": 0.05263157894736842}, {"Vertex_ID": "L", "score": 0.05263157894736842}, {"Vertex_ID": "M", "score": 0.05263157894736842}, {"Vertex_ID": "N", "score": 0.05263157894736842}, {"Vertex_ID": "O", "score": 0.05263157894736842}, {"Vertex_ID": "P", "score": 0.05263157894736842}, {"Vertex_ID": "Q", "score": 0.05263157894736842}, {"Vertex_ID": "R", "score": 0.05263157894736842}, {"Vertex_ID": "S", "score": 0.05263157894736842}, {"Vertex_ID": "T", "score": 0.0}]}]
Loading

0 comments on commit 356c36f

Please sign in to comment.