rete · Guides Graph algorithms

Graph algorithms over .rete

A .rete file is a graph — so everything the network-science toolbox knows how to do (centrality, community detection, similarity, path finding, embeddings, link prediction, graph ML) applies to it. This page is the catalog: which algorithm lives in which Python library, how each library connects to a .rete, and what the engine already computes natively.

The pattern is always the same three steps:

  1. Project — a SPARQL query pulls the sub-graph you care about out of the file as an edge list. Opens are lazy, so only the byte ranges the query touches are ever downloaded — projecting from a multi-GB remote file costs megabytes, not the file.
  2. Compute — the edge list feeds NetworkX, SciPy, scikit-learn, and friends. All of the "browser" rows below run in Pyodide, so the whole loop works inside a browser tab.
  3. Write back — results become new triples in a derived .rete built with the Builder: your analytics end up as versioned, queryable, self-describing RDF instead of a throwaway dict.

The worked example for everything on this page is the graph data science notebook (JupyterLite — nothing to install), which runs the full catalog against the Mark Lombardi networks dataset. A minimal projection:

import rete_graph as rete
import networkx as nx

g = rete.open("https://data.graphplaza.com/lombardi/lombardi-2026-07-20.rete")
edges = g.query_df("""
    PREFIX lomb: <https://w3id.org/rete/lombardi/>
    SELECT ?src ?tgt ?type WHERE {
      ?arc lomb:source ?src ; lomb:target ?tgt ; lomb:arcType ?type .
      FILTER(?src != ?tgt)
    }""")

G = nx.from_pandas_edgelist(edges, "src", "tgt")   # and compute away

Projection is also where filtering happens — FILTER, VALUES and FILTER NOT EXISTS restrict relationship types and node classes before a single algorithm runs. And because a .rete can carry its own OWL ontology, reason=True turns the class/property hierarchy into projection config: ask for a super-property and entailment widens the edge set with every declared sub-property, no re-projection needed (see Reasoning).

What the engine does natively — no Python required

Some graph analytics never need an export at all:

TaskHow
Degree centralityplain SPARQL GROUP BY + COUNT
Reachability / transitive closureSPARQL property paths — (p|^p)+ from a bound endpoint
Community detection (Louvain)rete communities — the same partition that builds the file's pyramid summary
Community labellingrete communities --profile — top words, classes, predicates per community
Class/link structurethe embedded schema profile — classes, instance counts, class-to-class relations
Full-text candidate searchthe text index (--text-index builds, text_search() queries)
Ontology entailmentOWL 2 QL by query rewriting — reason=True / --entail

For latent-theme labelling on top of the native communities, see Topic modeling (LDA); for partitions that combine structure with attributes, see Multi-criteria communities.

The algorithm catalog

"Browser" means the library is pure Python or ships Pyodide wheels, so the row runs in a tab (JupyterLite/marimo) as well as natively. "Native" means CPython only — same projection code, run it locally.

Centrality

AlgorithmCallRuns
DegreeSPARQL GROUP BY — or G.degreeengine / browser
PageRanknx.pagerankbrowser
Betweenness (exact or sampled)nx.betweenness_centrality(k=…)browser
Closeness / Harmonicnx.closeness_centrality, nx.harmonic_centralitybrowser
Eigenvectornx.eigenvector_centralitybrowser
Katznx.katz_centralitybrowser
HITS (hubs & authorities)nx.hitsbrowser
Articulation points / Bridgesnx.articulation_points, nx.bridgesbrowser

Community detection

AlgorithmCallRuns
Louvainnx.community.louvain_communities — or rete communitiesbrowser / engine
Leidensknetwork.clustering.Leiden, leidenalg (igraph)native
Label propagationnx.community.label_propagation_communitiesbrowser
Greedy modularitynx.community.greedy_modularity_communitiesbrowser
Connected components (weak/strong)nx.connected_components, nx.strongly_connected_componentsbrowser
Triangle count / clustering coefficientnx.triangles, nx.average_clusteringbrowser
K-core decompositionnx.core_numberbrowser
Graph coloringnx.greedy_colorbrowser
Modularity / conductance metricsnx.community.modularity, nx.conductancebrowser
K-Means / HDBSCAN on embeddingssklearn.clusterbrowser

Similarity & nearest neighbours

AlgorithmCallRuns
Jaccard / overlap node similarityset ops over neighbourhoodsbrowser
Cosine / Euclidean similarityscipy.spatial.distancebrowser
K-nearest neighbours (on embeddings)sklearn.neighbors.NearestNeighborsbrowser

Path finding

AlgorithmCallRuns
Shortest path (Dijkstra)nx.shortest_path, nx.single_source_dijkstrabrowser
A*nx.astar_pathbrowser
k-shortest simple pathsnx.shortest_simple_pathsbrowser
Bellman-Fordnx.bellman_ford_pathbrowser
BFS / DFSnx.bfs_tree, nx.dfs_treebrowser
All-pairs shortest pathsnx.all_pairs_shortest_path_lengthbrowser
Minimum spanning treenx.minimum_spanning_treebrowser
Steiner tree (approx.)nx.algorithms.approximation.steiner_treebrowser
Max-flow / min-cost flownx.maximum_flow, nx.min_cost_flowbrowser
Topological sort / DAG longest pathnx.topological_sort, nx.dag_longest_pathbrowser
Random walksa few lines of NumPy over the CSR matrixbrowser
ReachabilitySPARQL property paths — no exportengine

Node embeddings

AlgorithmCallRuns
Fast random projection (FastRP)~15 lines of NumPy — sparse random projection + powers of the normalized adjacency (in the notebook)browser
Spectral embeddingsklearn.manifold.SpectralEmbedding, sknetwork.embeddingbrowser / native
node2vecrandom walks + gensim word2vecnative
GraphSAGE / GCN / GAT …PyTorch Geometricnative
2-D layout for plotssklearn.decomposition.PCA, UMAPbrowser / native
AlgorithmCallRuns
Adamic-Adarnx.adamic_adar_indexbrowser
Common neighboursnx.common_neighborsbrowser
Preferential attachmentnx.preferential_attachmentbrowser
Resource allocationnx.resource_allocation_indexbrowser
Learned link predictionsklearn classifier on pair features, or PyGbrowser / native

Restrict candidates to distance-2 pairs (nodes sharing a neighbour) — scoring all non-edges is quadratic and almost never what you want.

Graph machine learning

TaskCallRuns
Node classification / regressionsklearn (logistic regression, random forest, MLP) on typed-edge counts + embeddingsbrowser
Train/test disciplinesklearn.model_selectionbrowser
GNN pipelinesPyTorch Geometric (Data, HeteroData, GCNConv, …)native

One finding from the worked example worth internalizing: on typed RDF graphs, the predicates are the features. Counting a node's incident edges by predicate beat both pure-structure embeddings and a 2-layer GCN for class prediction — the typed edges that a property-graph projection flattens away are first-class signal here.

The library connections

pandas — query_df() returns projections as DataFrames; every bridge below starts from one. Install: pip install rete-graph[pandas].

NetworkX — the default compute engine: pure Python, complete algorithm coverage, runs in Pyodide. nx.from_pandas_edgelist(edges, "src", "tgt"), or add edges in a loop when you want multiplicity → weights.

SciPy sparse — the lingua franca. Build a CSR adjacency once and it feeds FastRP (three @ products), spectral methods, and scikit-network unchanged:

import numpy as np, scipy.sparse as sp
nodes = pd.unique(pd.concat([edges["src"], edges["tgt"]]))
ix = {n: i for i, n in enumerate(nodes)}
r = edges["src"].map(ix); c = edges["tgt"].map(ix)
A = sp.csr_matrix((np.ones(len(edges)), (r, c)), shape=(len(nodes),) * 2)
A = A + A.T                                       # undirected

scikit-learn — everything downstream of a matrix: kNN, K-Means/HDBSCAN, PCA, classifiers, cross-validation. In Pyodide out of the box.

scikit-network — compiled Louvain/Leiden/PageRank/embeddings that consume exactly the CSR above (Louvain().fit_predict(A)), orders of magnitude faster than NetworkX. Native only — reach for it past ~10⁶ edges.

PyTorch Geometric — the GNN bridge. edge_index is the projection's two columns factorized; and HeteroData is shaped like RDF: one relation per predicate.

import torch
from torch_geometric.data import Data, HeteroData

ei = torch.tensor([edges["src"].map(ix).values,
                   edges["tgt"].map(ix).values], dtype=torch.long)
data = Data(edge_index=ei, num_nodes=len(nodes))

hetero = HeteroData()
hetero["actor"].num_nodes = len(nodes)
for t, sub in edges.groupby("type"):              # one relation per predicate
    hetero["actor", t.split("#")[-1], "actor"].edge_index = torch.tensor(
        [sub["src"].map(ix).values, sub["tgt"].map(ix).values])

Node features come from the graph too: literal values, class one-hots, or the per-predicate incidence counts that worked so well above. Native only (no Pyodide torch); train GCN/GraphSAGE/GAT as usual from there.

igraph / leidenalg, gensim — Leiden partitions and word2vec-style node2vec respectively; both native, both fed by the same edge list.

Scale honesty

  • In a browser tab: NetworkX is comfortable to ~10⁵–10⁶ edges. Lazy reads mean the download is bounded by the projection, not the file.
  • Natively: the same code, then scikit-network / igraph / PyG for the heavy lifting. Still one pip install, still no database server.
  • Beyond: published datasets ship Parquet companions for columnar bulk analytics, and any .rete URL is a standard SPARQL endpoint for server-side tooling.
  • Write-back: derive a new .rete with the results (Builder), so scores and partitions become citable data with provenance — see the final section of the notebook.

Where to start

Source & issues: https://github.com/caviri/rete · © 2026 Carlos Vivar Ríos, released under the Apache License 2.0.