SQL REFERENCE

Graph Algorithms

Teide provides five built-in graph algorithms that can be called as functions inside GRAPH_TABLE COLUMNS clauses. Each algorithm operates on the property graph defined by CREATE PROPERTY GRAPH and returns a scalar value per matched node or node pair.

Overview

Algorithm Function Names Returns Description
PageRank PAGERANK F64 Iterative importance ranking
Connected Components COMPONENT, CONNECTED_COMPONENT I64 Label propagation, undirected
Shortest Distance SHORTEST_DISTANCE, DIJKSTRA F64 Weighted shortest path
Community Detection COMMUNITY, LOUVAIN I64 Modularity optimization, undirected
Clustering Coefficient CLUSTERING_COEFFICIENT, CLUSTERING_COEFF F64 Local clustering, range 0.0–1.0

PAGERANK

Computes the PageRank score for each node using iterative power iteration. Runs 20 iterations with a damping factor of 0.85. Higher scores indicate more "important" nodes in the graph.

Signature

PAGERANK(graph_name, node_variable) -> F64

Example

const ranks = ctx.executeSync(`
  SELECT * FROM GRAPH_TABLE (social
    MATCH (p:Person)
    COLUMNS (
      p.name,
      PAGERANK(social, p) AS rank
    )
  )
  ORDER BY rank DESC
`);

// name    | rank
// --------|--------
// Alice   | 0.2871
// Charlie | 0.2193
// Eve     | 0.1842
// ...

COMPONENT / CONNECTED_COMPONENT

Assigns each node to a connected component using label propagation. The algorithm treats all edges as undirected. Nodes in the same component share the same integer label.

Signature

COMPONENT(graph_name, node_variable) -> I64
CONNECTED_COMPONENT(graph_name, node_variable) -> I64

Example

const components = ctx.executeSync(`
  SELECT * FROM GRAPH_TABLE (social
    MATCH (p:Person)
    COLUMNS (
      p.name,
      CONNECTED_COMPONENT(social, p) AS component_id
    )
  )
  ORDER BY component_id, p.name
`);

// name    | component_id
// --------|-------------
// Alice   | 0
// Bob     | 0
// Charlie | 0
// Diana   | 0
// Eve     | 0

SHORTEST_DISTANCE / DIJKSTRA

Computes the shortest weighted path between two specific nodes using Dijkstra's algorithm. You provide the source node ID, destination node ID, and the name of the edge weight column. Returns the total path weight as F64, or NULL if no path exists.

Signature

SHORTEST_DISTANCE(graph_name, source_id, destination_id, 'weight_column') -> F64
DIJKSTRA(graph_name, source_id, destination_id, 'weight_column') -> F64

Example

// First, create a graph with weighted edges
ctx.executeSync(`
  CREATE TABLE routes (
    id INTEGER, src INTEGER, dst INTEGER, distance REAL
  )
`);
ctx.executeSync(`
  INSERT INTO routes VALUES
    (1, 1, 2, 10.0),
    (2, 2, 3, 5.0),
    (3, 1, 3, 20.0)
`);

ctx.executeSync(`
  CREATE PROPERTY GRAPH road_network
    VERTEX TABLES (
      persons KEY (id) LABEL City PROPERTIES (name)
    )
    EDGE TABLES (
      routes KEY (id)
        SOURCE KEY (src) REFERENCES persons (id)
        DESTINATION KEY (dst) REFERENCES persons (id)
        LABEL Road
        PROPERTIES (distance)
    )
`);

const dist = ctx.executeSync(`
  SELECT SHORTEST_DISTANCE(road_network, 1, 3, 'distance') AS shortest
`);

// shortest
// --------
// 15.0       (via node 2: 10.0 + 5.0)

COMMUNITY / LOUVAIN

Detects communities using the Louvain modularity optimization algorithm. Treats all edges as undirected. Each node is assigned an integer community label. Nodes with the same label belong to the same community.

Signature

COMMUNITY(graph_name, node_variable) -> I64
LOUVAIN(graph_name, node_variable) -> I64

Example

const communities = ctx.executeSync(`
  SELECT * FROM GRAPH_TABLE (social
    MATCH (p:Person)
    COLUMNS (
      p.name,
      p.city,
      LOUVAIN(social, p) AS community
    )
  )
  ORDER BY community, p.name
`);

// name    | city   | community
// --------|--------|----------
// Alice   | London | 0
// Bob     | Paris  | 0
// Charlie | London | 1
// Diana   | Berlin | 1
// Eve     | Paris  | 1

CLUSTERING_COEFFICIENT / CLUSTERING_COEFF

Computes the local clustering coefficient for each node: the fraction of possible triangles through that node that actually exist. Values range from 0.0 (no clustering) to 1.0 (fully connected neighborhood).

Signature

CLUSTERING_COEFFICIENT(graph_name, node_variable) -> F64
CLUSTERING_COEFF(graph_name, node_variable) -> F64

Example

const clustering = ctx.executeSync(`
  SELECT * FROM GRAPH_TABLE (social
    MATCH (p:Person)
    COLUMNS (
      p.name,
      CLUSTERING_COEFF(social, p) AS coeff
    )
  )
  ORDER BY coeff DESC
`);

// name    | coeff
// --------|------
// Diana   | 1.0
// Eve     | 1.0
// Alice   | 0.0
// Bob     | 0.0
// Charlie | 0.0

Combining Multiple Algorithms

You can call multiple algorithms in a single COLUMNS clause:

const analysis = ctx.executeSync(`
  SELECT * FROM GRAPH_TABLE (social
    MATCH (p:Person)
    COLUMNS (
      p.name,
      PAGERANK(social, p) AS rank,
      LOUVAIN(social, p) AS community,
      CLUSTERING_COEFF(social, p) AS clustering
    )
  )
  ORDER BY rank DESC
`);

Performance Characteristics

Algorithm Time Complexity Space Complexity
PageRank O(k(V + E)) where k = iterations O(V)
Connected Components O(V + E) O(V)
Shortest Distance (Dijkstra) O((V + E) log V) O(V)
Community (Louvain) O(V log V) amortized O(V + E)
Clustering Coefficient O(V · d2) where d = max degree O(V)