Graph algorithms¶
Cypher queries match patterns: they start from some nodes and follow relationships to find paths. Graph algorithms such as PageRank, connected components or community detection work differently. They read the whole graph and compute one value for every node, a score or a group. Libraries such as icebug, igraph and scipy implement them efficiently, and they all take the graph in the same form: compressed sparse rows (CSR) over dense node ids.
CypherEngine.export_graph builds that form from your node and
relationship tables. It reads only the key and endpoint columns, plus a weight column when you ask for one,
from the same dataset versions your queries see, and returns Arrow arrays that these libraries take without
copying. Every parameter is optional, and all but rel_types are keyword-only:
| Parameter | Default | What it controls |
|---|---|---|
rel_types |
None |
The relationship types to include, or every type with integer endpoints; the nodes are those of the labels at either end. See Directed and undirected graphs. |
directed |
True |
True returns out-edge and in-edge arrays; False returns one set listing every edge both ways. See Directed and undirected graphs. |
weight |
None |
A column name, or a dict of relationship type to column, exported as float64 edge weights; None exports none. See Weighted graphs. |
combine_edges |
"sum" |
How an edge listed more than once combines its weights: "sum", "min" or "max". See Weighted graphs. |
default_weight |
None |
The weight of every edge of a type with no weight column; None makes such a type an error. See Several relationship types. |
To export only the part of the graph a Cypher query selects, use export_subgraph, described in
Subgraphs.
What the export contains¶
A CSR numbers the nodes 0 to n - 1, its dense ids, and stores the edges in two arrays:
indiceslists every node's neighbours, one node after another, each list sorted;indptrhasn + 1offsets: the neighbours of nodeiareindices[indptr[i]:indptr[i + 1]].
Your node keys are rarely 0 to n - 1, so the export also returns node_keys, the key of each dense id. Here
is a small graph with keys 10 to 40, where 40 has no edges:
import pyarrow as pa
from lance_csr import CypherEngine, GraphConfig
config = (
GraphConfig.builder()
.node_table("Person", "id")
.rel_table("FOLLOWS", "Person", "Person", "src", "dst")
.build()
)
people = pa.table({"id": [30, 10, 20, 40], "name": ["Carol", "Alice", "Bob", "Dan"]})
follows = pa.table({"src": [10, 10, 20, 30], "dst": [20, 30, 30, 10]})
engine = CypherEngine(config, {"Person": people, "FOLLOWS": follows})
g = engine.export_graph()
print(g.node_keys.to_pylist()) # [30, 10, 20, 40]
print(g.out_indptr.to_pylist()) # [0, 1, 3, 4, 4]
print(g.out_indices.to_pylist()) # [1, 0, 2, 0]
Dense ids follow the order of the node table, so 30 is id 0 and 10 is id 1. Node 1 (key 10) follows ids 0 and 2
(keys 30 and 20), and node 3 (key 40) has an empty range. The arrays are uint64; node_keys is int64.
The returned GraphExport also holds:
| Field | Meaning |
|---|---|
num_nodes, directed |
The number of nodes and whether the arrays are directed |
in_indptr, in_indices |
The same arrays for incoming edges; None for an undirected export |
label_ranges |
Each node label's dense ids, as (start, end) with end excluded |
out_weights, in_weights |
Edge weights, when asked for (see Weighted graphs); otherwise None |
dropped_edges |
Edges whose endpoint key isn't in its node table, or whose weight is null |
The export follows the same rules as a query's join between relationship and node tables. An edge whose
endpoint key isn't in its node table is left out (and counted in dropped_edges), an edge with a null endpoint is
skipped, and the same edge listed twice appears once. Keys must be unique within a label, and both keys and
endpoints must be integer columns up to int64.
Directed and undirected graphs¶
Some algorithms treat relationships as one-way and others don't. PageRank follows the direction of each edge, and reads the edges into each node, so a directed export returns the in-edges too:
print(g.in_indptr.to_pylist()) # [0, 2, 3, 4, 4]
print(g.in_indices.to_pylist()) # [1, 2, 0, 1]
Connected components and most community detection algorithms ignore direction. directed=False returns one
set of arrays that lists every edge in both directions, and a self-loop once:
u = engine.export_graph(["FOLLOWS"], directed=False)
print(u.out_indptr.to_pylist()) # [0, 2, 4, 6, 6]
print(u.in_indptr) # None
rel_types picks the relationship types to include; left out, every type whose dataset has integer endpoints
is included. The nodes are those of the labels at either end. Each label gets its own range of dense ids, in label
name order, so equal keys in two tables stay distinct nodes:
config2 = (
GraphConfig.builder()
.node_table("Person", "id")
.node_table("City", "id")
.rel_table("LIVES_IN", "Person", "City", "src", "dst")
.build()
)
cities = pa.table({"id": [1, 2]})
lives_in = pa.table({"src": [10, 20], "dst": [1, 2]})
engine2 = CypherEngine(config2, {"Person": people, "City": cities, "LIVES_IN": lives_in})
print(engine2.export_graph().label_ranges) # {'City': (0, 2), 'Person': (2, 6)}
Weighted graphs¶
Some algorithms read a number on each edge as well as the edge itself: a distance for shortest paths, a
strength for weighted PageRank or community detection. In a relationship table that number is a column, such
as the messages sent along each FOLLOWS edge. Name it with weight and the export returns it as float64
arrays that line up with out_indices and in_indices:
messages = pa.table(
{"src": [10, 10, 20, 30, 10], "dst": [20, 30, 30, 10, 20], "messages": [3, 1, 5, 2, 4]}
)
weighted_engine = CypherEngine(config, {"Person": people, "FOLLOWS": messages})
w = weighted_engine.export_graph(weight="messages")
print(w.out_indices.to_pylist()) # [1, 0, 2, 0]
print(w.out_weights.to_pylist()) # [2.0, 1.0, 7.0, 5.0]
The weight of out_indices[i] is out_weights[i], and the same holds for the in-edges. The weight column
must be numeric. Only the endpoint and weight columns are read, so other properties of the relationship table
play no part.
Several relationship types¶
When the export includes several relationship types, each needs a weight. They may store it under different
names, or one of them may have none. A weight dict names the column of each type, and default_weight gives
the weight of every edge of a type without a column:
config3 = (
GraphConfig.builder()
.node_table("Person", "id")
.rel_table("FOLLOWS", "Person", "Person", "src", "dst")
.rel_table("MENTIONS", "Person", "Person", "src", "dst")
.build()
)
mentions = pa.table({"src": [20], "dst": [10], "times": [9]})
mixed = CypherEngine(config3, {"Person": people, "FOLLOWS": messages, "MENTIONS": mentions})
both = mixed.export_graph(weight={"FOLLOWS": "messages", "MENTIONS": "times"})
print(both.out_weights.to_pylist()) # [2.0, 1.0, 7.0, 5.0, 9.0]
some = mixed.export_graph(weight={"FOLLOWS": "messages"}, default_weight=1.0)
print(some.out_weights.to_pylist()) # [2.0, 1.0, 7.0, 5.0, 1.0]
A string weight with default_weight works the same way: types that have the column use it, and the others
get the default. Without default_weight, a type with no weight column is an error, since a guessed weight
can change an algorithm's answer without warning. A few mistakes are errors even with a default: a column the
dict names that doesn't exist, a dict entry for a type that isn't exported, and a string column that no type
has. To leave a type out of the export instead, list the others in rel_types.
When the types' weights mean different things, such as messages and mentions, make sure adding them up, or comparing them, makes sense for the algorithm.
A null weight means the weight is unknown, and no number stands in for it safely: 1.0 would make a costly
edge look cheap to a shortest-path search. So the export leaves the edge out and counts it in dropped_edges,
as it does an edge whose endpoint is missing:
gaps = pa.table({"src": [10, 20], "dst": [20, 30], "messages": [3, None]})
partial = CypherEngine(config, {"Person": people, "FOLLOWS": gaps}).export_graph(weight="messages")
print(partial.out_weights.to_pylist(), partial.dropped_edges) # [3.0] 1
The export lists each edge once, so an edge that appears in several rows needs one weight, and
combine_edges says how to get it. The default, "sum", adds the weights, which suits counts and amounts:
10 → 20 appears twice, with 3 and 4 messages, and gets 7. When the weight is a distance or a cost, the
cheapest edge is usually the one that matters, and "min" keeps it; "max" keeps the largest:
cheapest = weighted_engine.export_graph(weight="messages", combine_edges="min")
print(cheapest.out_weights.to_pylist()) # [2.0, 1.0, 3.0, 5.0]
An undirected export applies the same rule to an edge stored in both directions, so a → b and b → a
become one edge with one combined weight. A self-loop's weight counts once.
Weights cost memory only when you ask for them. Without weight, the export reads no extra column and
out_weights and in_weights are None.
Subgraphs¶
An analysis often needs only part of the graph: the busiest accounts and their contacts, or one region's roads.
Ranking the whole graph and filtering the scores afterwards gives a different answer, since PageRank on a
subgraph only counts the edges inside it. export_subgraph runs a
Cypher query that selects the edges, and builds the same arrays as export_graph from its result:
sub = engine.export_subgraph(
"MATCH (a:Person)-[:FOLLOWS]->(b:Person) WHERE a.id = $who RETURN a.id, b.id",
{"who": 10},
)
print(sub.node_keys.to_pylist()) # [10, 20, 30]
print(sub.out_indptr.to_pylist(), sub.out_indices.to_pylist()) # [0, 2, 2, 2] [1, 2]
print(sub.label_ranges) # {'Person': (0, 3)}
The query returns one row per edge, with the source and target node keys as its first two columns. The nodes are
the keys present in the result, sorted, so Dan (40) and anyone else outside the selected edges aren't in the
export. The edges follow the same rules as export_graph: an edge listed twice appears once, directed=False
lists each edge both ways, and a self-loop counts once.
When both key columns are a labeled node's key, as a.id here with a a Person and id its key, the export
groups the ids per label as export_graph does. label_ranges is filled, and
write_node_column can write an algorithm's results back to the node table, with
nulls for the nodes outside the subgraph. When a column is computed, or its node has no single label, all keys
share one id space and label_ranges is empty; join node_keys to your own table instead.
The options are those of export_graph, except that the query does the selecting, so there is no rel_types,
and weight names a column of the query result:
| Parameter | Default | What it controls |
|---|---|---|
query, params |
The Cypher query and its parameters, as for execute. |
|
directed |
True |
True returns out-edge and in-edge arrays; False returns one set listing every edge both ways. |
weight |
None |
A numeric column of the result, such as r.cost AS cost, exported as float64 edge weights. |
combine_edges |
"sum" |
How an edge listed more than once combines its weights: "sum", "min" or "max". |
default_weight |
None |
Without weight, the weight of every row. |
default_weight=1.0 without weight gives each edge the number of rows that list it. That turns a result with
repeated rows into a graph where repeated edges count more, as PageRank on a multigraph does:
counted = weighted_engine.export_subgraph(
"MATCH (a:Person)-[:FOLLOWS]->(b:Person) RETURN a.id, b.id", default_weight=1.0
)
print(counted.out_weights.to_pylist()) # [2.0, 1.0, 1.0, 1.0]
The query's result is held in memory while the arrays are built, as with
execute.
Running an algorithm with icebug¶
icebug is a fork of NetworKit whose
graphs are built directly on Arrow CSR arrays. Graph.fromCSR takes the export's arrays without copying them, so
the only work left is the algorithm itself:
import icebug as ib
graph = ib.Graph.fromCSR( # (1)!
g.num_nodes, g.directed, g.out_indices, g.out_indptr, g.in_indices, g.in_indptr
)
pagerank = ib.centrality.PageRank(
graph, distributeSinks=ib.centrality.SinkHandling.DistributeSinks # (2)!
)
scores = pa.table({"id": g.node_keys, "pagerank": pagerank.run().scores()})
- Keep
graphin a variable for as long as an algorithm uses it. The algorithm objects don't keep the graph alive, and a graph freed early gives wrong results without an error. - Spreads the score of nodes without out-edges over all nodes, as networkx and most definitions of PageRank do. icebug's default leaves it out.
Installing icebug
icebug is a separate package (uv add icebug); lance-csr doesn't depend on it. Version 13.6 requires
exactly pyarrow==26.0.1, imports only when networkx is installed, and on macOS needs OpenMP from
Homebrew (brew install libomp).
For a weighted graph, pass the weights after the in-edge arrays:
ib.Graph.fromCSR(w.num_nodes, w.directed, w.out_indices, w.out_indptr, w.in_indices, w.in_indptr,
w.out_weights, w.in_weights). Algorithms such as ib.distance.Dijkstra then use them.
Other libraries take the same arrays. For example, scipy.sparse.csr_array((data, g.out_indices, g.out_indptr))
builds an adjacency matrix, after converting the arrays with to_numpy(), which doesn't copy them either.
Writing results back to Lance¶
An algorithm's results are a value per dense id. To query them alongside the rest of a node's properties, they
have to become a column of the node table, matched to each node by its key.
write_node_column does this in one call. The export already says
which dense ids belong to which label (label_ranges) and what each node's key is (node_keys), and the config
says which dataset and key column each label has, so the engine can route every value to its row.
Here the out-degree of each node, computed from out_indptr, stands in for an algorithm's result:
import lance
lance.write_dataset(people, "people.lance")
lance.write_dataset(follows, "follows.lance")
engine = CypherEngine(
config, {"Person": lance.dataset("people.lance"), "FOLLOWS": lance.dataset("follows.lance")}
)
g = engine.export_graph()
indptr = g.out_indptr.to_numpy()
engine.write_node_column(g, "out_degree", indptr[1:] - indptr[:-1]) # (1)!
top = engine.execute(
"MATCH (p:Person) RETURN p.name, p.out_degree ORDER BY p.out_degree DESC, p.name LIMIT 2"
)
print(top.to_pylist())
# [{'p.name': 'Alice', 'p.out_degree': 2}, {'p.name': 'Bob', 'p.out_degree': 1}]
valuescan be a pyarrow array, a NumPy array or a list, such as the scores icebug returns. With the icebug example above, this isengine.write_node_column(g, "pagerank", pagerank.run().scores()).
For each label in the export, the engine pairs that label's values with its keys and uses Lance's merge to
join them to the node table on the key column. Lance writes only the new column's data files, as a new version
of the dataset. A node row that the export left out because its key is null gets a null.
The engine then moves to the new versions of those node tables, so the next query sees the column without a
checkout_latest. Other datasets stay at the versions the engine was pinned to.
Relationship tables don't change, so the CSR index isn't rebuilt.
Some cases need care:
- Running an algorithm again. Writing to a column that already exists raises
ValueError, so a result isn't replaced by accident. Passoverwrite=Trueto drop the old column and write the new one. - Versions build up. Each write adds a version to every node table it touches, two with
overwrite=True, and a replaced column's files stay on disk until old versions are cleaned up.optimize_datasetscleans them up and keeps the engine in step. - Several labels. Each label is a separate Lance write. A failure partway through leaves the labels before it written.
- Arrow inputs. Only Lance datasets can be written to. For a node table passed as a PyArrow table, pair the
values with the keys yourself:
pa.table({"id": g.node_keys, "out_degree": degree}). - Keeping the node table unchanged. To keep results apart from the node table, for example to compare
several runs, write that same table to a separate dataset with
lance.write_dataset.
Limits¶
- The export holds the whole graph in memory: eight bytes per edge in each direction for
indices, plus the offsets and keys. A directed export of 10 million edges takes about 160 MB, and building it briefly needs about as much again. Weights add eight bytes per edge in each direction, and building a weighted export briefly needs up to three times the memory of an unweighted one, because each edge is sorted together with its weight. An unweighted export needs no memory for weights. - Keys and endpoints must be integers up to
int64; string anduint64keys aren't supported. - Only one edge property is exported, as a weight; other edge properties aren't.