Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Centrality Queries #10

Open
wants to merge 1 commit into
base: 3.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CREATE QUERY README() FOR GRAPH MyGraph {
STRING graph_description = "Determine the entity in your network that " +
"is most central to the others";

STRING query_order = "1. add_weights";

STRING add_weights = "Haversine formula to calculate the distances " +
"between airports by using their latitude and longitude coordinates. " +
"The calculated distances are measured in miles and are added as edge " +
"weights.";
STRING bc_subquery = "The TigerGraph implementation is based on A " +
"Faster Algorithm for Betweenness Centrality";
STRING betweenness_cent = "Betweenness Centrality main query";
STRING cc_by_country = "Closeness Centrality main query filtered by " +
"country";
STRING cc_subquery = "Subquery returns closeness centrality for vertex " +
"source in graph with num_vert vertices ";
STRING cc = "Closeness Centrality main query";
STRING page_rank_by_country = "PageRank Algorithm";
STRING page_rank_pers = "Personalized PageRank Algorithm";
STRING page_rank = "PageRank Algorithm";
STRING shortest_ss_no_wt = "Shortest Path Algorithm without weights";
STRING shortest_ss_pos_wt_limits = "Shortest Path Algorithm with " +
"weights and limits";
STRING shortest_ss_pos_wt = "Shortest Path Algorithm with weights";

PRINT graph_description, query_order, add_weights, bc_subquery,
betweenness_cent, cc_by_country, cc_subquery, cc, page_rank_by_country,
page_rank_pers, page_rank, shortest_ss_no_wt, shortest_ss_pos_wt_limits,
shortest_ss_pos_wt;
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
CREATE QUERY add_weights(STRING e_type, BOOL overwrite=True) FOR GRAPH MyGraph SYNTAX V2 {
/* This query uses the haversine formula to calculate the distances
between airports by using their latitude and longitude coordinates.
The calculated distances are measured in miles and are added as edge weights.
CREATE QUERY add_weights(STRING e_type, BOOL overwrite=TRUE)
FOR GRAPH MyGraph SYNTAX V2 {
/*
Haversine formula to calculate the distances between airports by using
their latitude and longitude coordinates. The calculated distances are
measured in miles and are added as edge weights.

Sample inputs:
e_type: flight_to
overwrite: TRUE | FALSE

Starting with all Airport vertices,
(1) Get connected Airport vertices
(2) Calculate the distances
*/

ListAccum<edge> @@dont_change_list;
ListAccum<EDGE> @@dont_change_list;
SumAccum<INT> @@num_changed;
double pi = 3.14159265359; // pi
double R = 3958.8; // earth's radius in miles

//to_vertex_set("CNX-3931", "Airport");
DOUBLE pi = 3.14159265359; // pi
DOUBLE R = 3958.8; // earth's radius in miles

start = {Airport.*};

heavy = SELECT t FROM start:s-(e_type:e)-Airport:t
ACCUM
IF overwrite == False AND e.miles != 0 THEN
IF overwrite == FALSE AND e.miles != 0 THEN
@@dont_change_list += e
ELSE
double lat1 = s.latitude * pi / 180, // lat1 to radians
double lat2 = t.latitude * pi / 180, // lat2 to radians
double deltalat = (t.latitude - s.latitude) * pi / 180, // lat change in radians
double deltalong = (t.longitude - s.longitude) * pi / 180, // long change in radians
double a = sin(deltalat/2) * sin(deltalat/2)
+ cos(lat1) * cos(lat2)
* sin(deltalong/2) * sin(deltalong/2),
//double atanp1 = sqrt(a), // temp
//double atanp2 = sqrt(1-a), // temp
double c = 2 * atan2(sqrt(a), sqrt(1-a)),
e.miles = ceil(R * c),
@@num_changed += 1
DOUBLE lat1 = s.latitude * pi / 180, // lat1 to radians
DOUBLE lat2 = t.latitude * pi / 180, // lat2 to radians
DOUBLE deltalat = (t.latitude - s.latitude) * pi / 180, /* lat
change in radians */
DOUBLE deltalong = (t.longitude - s.longitude) * pi / 180, /* lon
change in radians */
DOUBLE a = sin(deltalat/2) * sin(deltalat/2)
+ cos(lat1) * cos(lat2)
* sin(deltalong/2) * sin(deltalong/2),
DOUBLE c = 2 * atan2(sqrt(a), sqrt(1-a)),
e.miles = ceil(R * c),
@@num_changed += 1
END;
PRINT @@num_changed, @@dont_change_list;

PRINT @@num_changed, @@dont_change_list;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CREATE QUERY bc_subquery (VERTEX source, INT max_hops) FOR GRAPH MyGraph RETURNS(MapAccum<vertex, float>) SYNTAX V2 {
/* The TigerGraph implementation is based on A Faster Algorithm for Betweenness Centrality by Ulrik Brandes, Journal of Mathematical Sociology 25(2):163-177, (2001). According to the algorithm, sigma is the number of shortest paths from source; delta is the pair dependency from source; and dist is the shortest distance from source. The subquery returns a map of (s.id->s.@delta)
CREATE QUERY bc_subquery (VERTEX source, INT max_hops) FOR GRAPH MyGraph RETURNS(MapAccum<VERTEX, FLOAT>) SYNTAX V2 {
/* The TigerGraph implementation is based on A Faster Algorithm for Betweenness Centrality
by Ulrik Brandes, Journal of Mathematical Sociology 25(2):163-177, (2001). According to the algorithm,
sigma is the number of shortest paths from source; delta is the pair dependency from source;
and dist is the shortest distance from source. The subquery returns a map of (s.id->s.@delta)
*/
SumAccum<float> @sigma;
SumAccum<float> @delta;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
CREATE QUERY betweenness_cent (INT max_hops, INT maxItems, STRING country) FOR GRAPH MyGraph SYNTAX V2 {
# Betweenness Centrality main query
CREATE QUERY betweenness_cent (INT max_hops, INT maxItems, STRING country)
FOR GRAPH MyGraph SYNTAX V2 {
// Betweenness Centrality main query

MapAccum<VERTEX,SumAccum<float>> @@BC;
SumAccum<float> @cent;
MapAccum<VERTEX,SumAccum<FLOAT>> @@BC;
SumAccum<FLOAT> @cent;

start = {ANY};
IF country != "" THEN
start = SELECT v FROM start:v
WHERE v.country == country;
END;
start = SELECT s FROM start:s
ACCUM @@BC += bc_subquery(s, max_hops);
# Write scores to local accumulators of vertices.
start = SELECT s FROM start:s
POST-ACCUM s.@cent += @@BC.get(s)
ORDER BY s.@cent DESC
LIMIT maxItems;
PRINT start[start.name, start.@cent];
start = {ANY};
IF country != "" THEN
start = SELECT v FROM start:v
WHERE v.country == country; // filter by country if given
END;

start = SELECT s FROM start:s
ACCUM @@BC += bc_subquery(s, max_hops); // run betweenness subquery

// Write scores to local accumulators of vertices.
start = SELECT s FROM start:s
POST-ACCUM s.@cent += @@BC.get(s)
ORDER BY s.@cent DESC
LIMIT maxItems;
PRINT start[start.name, start.@cent];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE QUERY cc (BOOL display, INT output_limit, INT max_hops) FOR GRAPH MyGraph SYNTAX V2 {
# Closeness Centrality main query
# Closeness Centrality main query

TYPEDEF TUPLE<VERTEX Vertex_ID, FLOAT score> Vertex_Score;
HeapAccum<Vertex_Score>(output_limit, score DESC) @@top_scores;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
CREATE QUERY cc_by_country (BOOL display, INT output_limit, INT maxHops, STRING country) FOR GRAPH MyGraph SYNTAX V2 {
# Closeness Centrality main query
# Closeness Centrality main query filtered by country

TYPEDEF TUPLE<VERTEX Vertex_ID, STRING name, FLOAT score> Vertex_Score;
HeapAccum<Vertex_Score>(output_limit, score DESC) @@top_scores;
SumAccum<float> @score;
SetAccum<EDGE> @@edge_set; # list of all edges, if display is needed

INT num_vert;
#INT maxHops = 10; # measure distance for vertices up to 10 hops away

start = {Airport.*};
IF country != "" THEN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CREATE QUERY shortest_ss_no_wt(VERTEX source, BOOL display) FOR GRAPH MyGraph SYNTAX V2 {
/* This query is Single-source_set Shortest Path without weights on edges. It calculates the shortest distance from the given vertex source_set to all other connected vertices, and shows one shortest path between them.
/* This query is Single-source_set Shortest Path without weights on edges.
It calculates the shortest distance from the given vertex source_set to all other connected vertices,
and shows one shortest path between them.
The JSON version also show visualization of the network.
The attribute version only store the distance into attribute, not the path.
*/
Expand Down