Algorithms
The algorithms, statistics and traversals are table functions. They go in FROM, take the graph as the first argument, and take named parameters with =>. An algorithm returns one row per node: the node’s id, and a column named after the algorithm. SELECT * FROM ursa_functions() lists every function, and the function reference describes each one.
The examples on this page share one session. They use the karate club graph from Get started:
CREATE VIEW members AS SELECT * FROM 'karate_nodes.csv';CREATE VIEW ties AS SELECT * FROM 'karate_edges.csv';CREATE PROPERTY GRAPH karate VERTEX TABLES (members KEY (id)) EDGE TABLES ( ties SOURCE KEY (src) REFERENCES members (id) DESTINATION KEY (dst) REFERENCES members (id) );Run an algorithm
Section titled “Run an algorithm”SELECT * FROM degree(karate, direction => 'both') ORDER BY degree DESC LIMIT 1;┌───────┬────────┐│ id │ degree ││ Int64 │ UInt32 │├───────┼────────┤│ 33 │ 17 │└───────┴────────┘Parameters are named, and every one has a default: pagerank(karate, damping => 0.9), louvain(karate, seed => 1). A number outside a parameter’s range is an error that says the range. In the shell, .functions pagerank lists a function’s parameters.
What there is:
- Centrality:
pagerank,degree,betweenness,closeness. - Communities and components:
louvain,label_propagation,connected_components. - Structure:
triangle_countandclustering_coefficient. - Statistics of the whole graph, one row:
density,avg_path_length,diameteranddescribe. - Neighbour aggregation:
neighbors_agg, a value of each node aggregated over its neighbours. - Layouts:
layout_fa2andlayout_fr, a position (x,y) for each node. - Traversals:
hop,random_walk,shortest_pathandinduced_edges(below).
Direction
Section titled “Direction”Every edge is stored from src to dst. Algorithms that walk edges take a direction:
| Value | Follows |
|---|---|
'out' (the default) |
edges from a node |
'in' |
edges into a node |
'both' |
edges in either direction |
The karate edges are stored once per friendship, so direction => 'both' gives the undirected answer. Some algorithms only make sense one way and take no direction: triangle_count, clustering_coefficient, louvain and label_propagation read the graph as undirected.
Weights
Section titled “Weights”A weight is a column of the edge table, or an expression over its columns. Les Misérables has a weight column counting how often two characters appear together (download lesmis_edges.csv as you did the karate files):
CREATE VIEW lesmis_edges AS SELECT * FROM 'lesmis_edges.csv';CREATE PROPERTY GRAPH lesmis EDGE TABLES (lesmis_edges SOURCE KEY (src) DESTINATION KEY (dst));
SELECT id, round(pagerank, 4) AS pagerankFROM pagerank(lesmis, weight => weight, direction => 'both')ORDER BY pagerank DESCLIMIT 3;┌─────────┬──────────┐│ id │ pagerank ││ Utf8 │ Float64 │├─────────┼──────────┤│ Valjean │ 0.0996 ││ Marius │ 0.0517 ││ Myriel │ 0.0392 │└─────────┴──────────┘The weight can be any expression over the edge columns, such as weight => log10(1 + weight). Weights must be non-negative and not null. The graph index does not depend on the weight, so trying several weightings reuses one index.
Several algorithms at once
Section titled “Several algorithms at once”algorithms runs several algorithms over one graph and returns one row per node with a column for each. Name a column with name =>; otherwise it is named after the algorithm. Join it to the node table on id for the attributes. The five best-connected members, with their club, how many triangles they are part of, and their community:
WITH top AS ( SELECT * FROM algorithms(karate, degree(direction => 'both'), triangles => triangle_count(), community => louvain(seed => 1)) ORDER BY degree DESC LIMIT 5)SELECT id, club, degree, triangles, communityFROM top JOIN karate.nodes USING (id)ORDER BY degree DESC;┌───────┬─────────┬────────┬───────────┬───────────┐│ id │ club │ degree │ triangles │ community ││ Int64 │ Utf8 │ UInt32 │ UInt64 │ UInt32 │├───────┼─────────┼────────┼───────────┼───────────┤│ 33 │ Officer │ 17 │ 15 │ 2 ││ 0 │ Mr. Hi │ 16 │ 18 │ 0 ││ 32 │ Officer │ 12 │ 13 │ 2 ││ 2 │ Mr. Hi │ 10 │ 11 │ 0 ││ 1 │ Mr. Hi │ 9 │ 12 │ 0 │└───────┴─────────┴────────┴───────────┴───────────┘All three run over one index of the graph. The query takes the five rows it wants first, and joins the club to those five: on a graph of millions of nodes, that carries far less through the sort than joining every node first.
Aggregate over neighbours
Section titled “Aggregate over neighbours”neighbors_agg aggregates a value of each node over its neighbours, one row per node. attribute => takes an expression over the node table, and agg => is mean (the default), sum, min, max, count or n_unique. Some members have friends in both clubs; this counts them, by club:
SELECT club, count(*) AS membersFROM karate.nodesJOIN neighbors_agg(karate, attribute => club, agg => 'n_unique', direction => 'both') USING (id)WHERE neighbors_agg = 2GROUP BY clubORDER BY club;┌─────────┬─────────┐│ club │ members ││ Utf8 │ Int64 │├─────────┼─────────┤│ Mr. Hi │ 6 ││ Officer │ 7 │└─────────┴─────────┘values => takes any query of two columns, node ids then values, so the values can come from another algorithm.
Traversals
Section titled “Traversals”Traversals are table functions too, and they return rows of their own: the nodes reached, a path’s edges, or walks. For patterns and paths in SQL/PGQ’s syntax, see pattern matching.
hop returns the nodes within steps steps of a set of seed nodes, one (seed, id, distance) row per seed and node reached. The seeds can be a list or a query:
SELECT count(*) AS reachedFROM hop(karate, seeds => [0], steps => 1, direction => 'both');┌─────────┐│ reached ││ Int64 │├─────────┤│ 16 │└─────────┘SELECT * FROM hop(karate, seeds => (SELECT id FROM karate.nodes WHERE club = 'Officer'), steps => 2);The rows come back in no particular order; add ORDER BY when the order matters.
shortest_path
Section titled “shortest_path”shortest_path returns the path between two nodes, one edge per row, with its step number and the cost so far:
SELECT * FROM shortest_path(karate, source => 0, target => 33, direction => 'both');┌───────┬───────┬───────┬─────────┐│ src │ dst │ step │ cost ││ Int64 │ Int64 │ Int64 │ Float64 │├───────┼───────┼───────┼─────────┤│ 0 │ 8 │ 0 │ 1.0 ││ 8 │ 33 │ 1 │ 2.0 │└───────┴───────┴───────┴─────────┘random_walk
Section titled “random_walk”random_walk returns walks as rows: one per step, with the walk’s number and position. Give it a random_seed for walks you can reproduce:
SELECT * FROM random_walk(karate, seeds => [0, 1], steps => 4, walks_per_node => 2, random_seed => 7);induced_edges
Section titled “induced_edges”induced_edges returns the graph’s edges among a set of nodes: the edges whose two ends are both in the set. With a query for the nodes, it returns the part of a graph another function picked, such as the ten highest-ranked members and the ties among them:
SELECT count(*) AS tiesFROM induced_edges(karate, nodes => ( SELECT id FROM pagerank(karate, direction => 'both') ORDER BY pagerank DESC LIMIT 10));The function reference lists every traversal’s parameters.