Run graph algorithms on Lance with Icebug
Learn how lance-csr, Apache Arrow, and icebug can run fast, memory-efficient graph algorithms over Lance datasets.
On this page
In our previous post on why we built lance-csr, we followed exact paths through a social graph stored in Lance, using Cypher and associative search. Paths like these answer questions about a single person, but some of the most useful questions about connected data concern graph structure: which communities are present, and who are the key players holding the groups together?
Questions about the global structure of the graph call for graph algorithms because they surface larger patterns. In Python, the typical approach is to copy the graph’s edges into a library like NetworkX, run the algorithm, and then map its node IDs back to the original records. This is inefficient and memory-hungry, especially for large graphs, and for our use requires transforming data types in lance-csr’s Rust core to Python types.
To avoid that overhead, lance-csr exports the graph’s connections as Apache Arrow arrays that icebug can use directly. Icebug is an open-source fork of NetworKit that lets us call parallel C++ graph algorithms from Python. The algorithms operate on the same Arrow buffers, so the relationships stored in Lance never have to become Python objects.
lance-csr doesn’t depend on icebug, so it must be installed separately with uv add icebug.
We write the algorithm results back to the Lance table that already stores each person’s name and portrait image. A query can then use those results to retrieve the matching people’s names and portraits in a single fetch, because all the source data is in one place.
In the rest of this post, we’ll walk through what’s going on under the hood by running community detection and betweenness centrality algorithms on a small graph.
Revisit our Maker Night dataset
In our previous post, we introduced a small fictional dataset of people who met at a social event called “Maker Night”. The graph contained 26 people and 44 FRIEND_OF relationships. What made that dataset interesting is that its friends tended to gather around four interests: photography, robotics, music, and gardening.
All the data is stored in two Lance tables. Person is a node table that stores each person’s attributes and a synthetic portrait stored as bytes in the portrait column. FRIEND_OF is a relationship table containing the edges of the graph, with src_id and dst_id columns that reference the person_id primary key of the Person table.
Now imagine that our central character, Maya, is organizing the next Maker Night event and wants to seat people with others in their community, so that conversations flow naturally.
If you look at the data, you’ll see that natural structures are emerging from the data itself. Larger grouping patterns are already encoded in the graph’s structure, because people tend to befriend others who share their hobbies.
Simple path traversal queries in Cypher (as we did in the previous post) can’t address Maya’s question about who belongs to which community directly. Thankfully, two well-known graph algorithms can:
- Louvain community detection: This algorithm groups people who share more friendships with each other than we’d expect by chance. It measures the strength of a grouping using a quantity called modularity, and iteratively reassigns people between groups to maximize modularity.
- Betweenness centrality: This algorithm measures influence by looking at how often a person lies on shortest paths between other people. A person with high betweenness can be considered a “bridge”: if they are removed, many paths between groups get longer.
Run the algorithms on Maker Night
Running Louvain and betweenness centrality on Maker Night can be done in the following three steps: 1) export the graph as CSR arrays from Lance, such that icebug can directly consume them as Arrow buffers; 2) run the algorithms (in parallel) in icebug’s C++ layer; and 3) write back the resulting data to Lance.
1 Export the graph from Lance
The data that’s in lance-csr needs to be exposed to icebug via the Arrow bridge. This is done using the export_graph method, which turns the Person and FRIEND_OF tables into compact integer Arrow arrays, the form that graph-algorithm libraries like icebug expect. We’ll look at how this works later in the post. Each FRIEND_OF row stores a friendship once, but friendships go both ways, so we ask for an undirected export, which lists each friendship under both people:
from lance_csr import CypherEngine
from maker_night import GRAPH as graph_config, open_datasets
# open_datasets() returns the Lance tables used by the engine
engine = CypherEngine(graph_config, open_datasets(), use_csr=True)
# Export the graph as Arrow arrays in an undirected manner for icebug
g = engine.export_graph(directed=False)The result, g, contains Maker Night’s 26 people and 44 friendships as Arrow arrays, plus node_keys, which maps each node back to its person_id. Everything else, such as names and portrait image bytes, stays in Lance.
2 Run the algorithms in icebug
Icebug directly consumes the exported Arrow arrays from lance-csr, so the edges never need to be copied or rebuilt as Python objects, however large the graph is. Louvain then runs in parallel over that graph to find the groups:
import icebug as ib
# Undirected graph over the exported arrays (no copy)
graph = ib.Graph.fromCSR(g.num_nodes, False, g.out_indices, g.out_indptr)
# We need to set a fixed seed, because Louvain breaks ties randomly
ib.engineering.setSeed(42, False)
# PLM is icebug's parallel Louvain modularity implementation
# refine=True runs a second pass to improve modularity
groups = ib.community.PLM(graph, refine=True).run().getPartition().getVector()Betweenness centrality scores each node by how often it lies on the shortest paths between other nodes. Icebug runs a shortest-path search outward from every node, counts which nodes each path passes through, and spreads those searches across CPU cores:
# BC returns scores as a Python list, with scores scaled from 0 to 1
betweenness = ib.centrality.Betweenness(graph, normalized=True).run().scores()3 Write the results back to Lance
Louvain returns a group ID and betweenness returns a centrality score for each Person node, in the same order as the exported nodes. To store those results alongside the rest of the data, we need to write them back to Lance. This can be done via a simple helper method as follows:
# Each call adds a column to Person, matched on person_id
engine.write_node_column(g, "table_group", groups)
engine.write_node_column(g, "betweenness", betweenness)This adds the columns to the Person Lance table, while creating a new version of the dataset without rewriting the rest of it. The next Cypher query sees each person’s group and score right alongside their name, interests and portrait.1
Use the results
Once the algorithm results are stored as columns, they can be queried like any other property. Maya can now plan her whole event, from the groups to the hosts and their portraits, from the same Person table.
Louvain communities
The following query returns each Louvain group and the people in it, which gives Maya her four table groups:
MATCH (p:Person)
RETURN
p.table_group AS table_group,
collect(p.name) AS people
ORDER BY table_group| Table | People | Shared interest |
|---|---|---|
| 0 | Abby, Elliott, Jonas, Leo, Maya, Rafael, William | Robotics |
| 1 | Boris, Farah, Hannah, Ines, Sherry, Xi, Yusuf | Music |
| 2 | Carla, Nina, Omar, Pedro, Tara | Photography |
| 3 | Dmitri, Gabriel, Kenneth, Qian, Uma, Valentina, Zoe | Gardening |
The Louvain algorithm discovers groups that naturally form around a particular interest. People tend to befriend others who share their hobbies, so the interests were, in essence, already encoded in the graph’s structure. A modularity of 0.564, against roughly 0 for a random split, confirms that these groups are far more tightly knit than we’d expect by chance.
One person stands out: Maya likes photography, but she’s seated at the robotics table. She has exactly two friends, Nina in photography and Leo in robotics, so her edges don’t favour either group, and the tie is broken at random.
Betweenness centrality
For each table’s host, Maya picks the person with the highest betweenness centrality score in that group. The query below finds each group’s top score, matches the person who has it, and returns their portrait in the same result. The rows follow Louvain’s group numbers, which are arbitrary labels rather than a ranking:
MATCH (p:Person)
WITH p.table_group AS table_group, max(p.betweenness) AS top
MATCH (h:Person)
WHERE h.table_group = table_group
AND h.betweenness = top
RETURN
table_group,
h.name AS name,
h.betweenness AS betweenness,
h.interests AS interests,
h.portrait AS portrait
ORDER BY table_group| Table | Host | Betweenness centrality | Interests |
|---|---|---|---|
| 0 | Elliott | 0.212 | Robotics |
| 1 | Ines | 0.285 | Photography, music |
| 2 | Pedro | 0.178 | Photography, robotics |
| 3 | Kenneth | 0.223 | Music, gardening |
The people with the highest betweenness centrality in their group are suitable hosts at their table, because more of the graph’s shortest paths between other guests run through them than through anyone else at their table. For Ines, Pedro and Kenneth, those paths cross into other groups because they have friends at other tables, which makes them natural connectors for introducing guests across tables.
Together, the two algorithms help answer Maya’s original question. The groups are dense in friendships: 36 of the 44 friendships fall within a table, and everyone has at least one friend at theirs. This makes it easier to start conversations.
How graph algorithms benefit from CSR arrays
Both algorithms need to know who is connected to whom. During export, lance-csr assigns each person a dense ID and packs their friends’ IDs together in Arrow-based CSR arrays. When icebug needs Maya’s neighbours, it can use her ID to jump straight to her CSR list and read it from start to finish. No matter what algorithm is called, icebug can reuse the exported relationships from lance-csr without building a second copy of the graph.
What is a dense ID?
Each node in a Lance table is identified by a key chosen by the application. The problem is that keys may be large integers with gaps between them, stored in no particular order. On the other hand, graph algorithm libraries like icebug need a dense ID, so each node gets one integer in the sequence from to , where is the number of nodes.
| Name | Key in Lance table | Dense ID |
|---|---|---|
| Maya | 13 | 0 |
| Leo | 12 | 1 |
| Nina | 14 | 2 |
| Pedro | 16 | 3 |
The dense IDs are largely required for performance reasons, so a graph algorithm can store each node’s result (such as its Louvain group or centrality score) at that node’s position in an array, with no lookup from key to node. This requires an additional step in the data processing pipeline, which lance-csr handles by building these IDs up front, before calling icebug.2
What is a CSR array?
Once every node has a dense ID, the edges can be stored as CSR (compressed sparse row) arrays, which we covered in the what is a CSR index? section of our previous post. A CSR is just two flat arrays of integers: indices lists every node’s neighbours back to back, and indptr (index pointer) marks where each node’s list begins and ends. lance-csr’s export_graph builds the dense IDs and CSR arrays in a single call, running in parallel in Rust using Rayon 🦀.
Hand the arrays to icebug
Icebug runs its graph algorithms in parallel C++, and its Graph.fromCSR method builds a graph directly from CSR arrays. Because export_graph returns those arrays in Arrow format, icebug reads the same buffers in place, without copying the edges into Python objects or a second graph structure. Here’s the whole round trip from Lance to icebug and back:
Benchmarks vs. NetworkX
When it comes to running graph algorithms, columnar export and zero-copy processing are the key to better performance and a low memory footprint. We timed the above steps using icebug on a synthetic benchmark graph with 100,000 nodes and 2.4 million edges, with all the data and results stored in Lance tables.
Alongside PageRank and Louvain, we benchmarked weakly connected components (WCC). The algorithm treats each FOLLOWS edge as a relationship that can be followed in either direction. Two nodes belong to the same weakly connected component if and only if a path connects them in the underlying undirected graph.
| Algorithm | NetworkX | lance-csr + icebug | Speed-up | Peak memory (MiB) |
|---|---|---|---|---|
| PageRank | 2.53 s | 0.092 s | 27× | 1,289 → 268 |
| WCC | 1.68 s | 0.099 s | 17× | 813 → 284 |
| Louvain | 24.5 s | 0.351 s | 70× | 1,728 → 293 |
The key takeaways are listed below:
- Much faster. Icebug runs 17× to 70× faster than NetworkX, with the biggest gain on Louvain, the most expensive algorithm here.
- Far less memory. Peak memory is 3× to 6× lower than with NetworkX, because the graph is held as compact integer arrays rather than millions of Python objects.
- A small export cost at this size. Building the CSR arrays is the largest memory step on the lance-csr side, at about 60 MiB, a fraction of what NetworkX needs just to build its graph. Icebug then reads those arrays without copying them.
How this scales
To see what the graph export costs at scale, we also generated a larger synthetic graph containing 4 million nodes and 69 million edges, about 28× the size of the first graph. Its edges are skewed towards a few hubs, so the most-followed node has over 430,000 followers, against about 5,000 in the first graph. We didn’t run NetworkX here: based on the memory per edge it used on the first graph, PageRank alone would need well over 30 GiB, more than many laptops have.
| Algorithm | Open engine | export_graph | Run algorithm | Total | Peak memory |
|---|---|---|---|---|---|
| PageRank | 3.5 s | 2.2 s, +1.2 GiB | 1.7 s | 7.5 s | 2.0 GiB |
| WCC | 3.5 s | 2.2 s, +1.7 GiB | 0.7 s | 6.5 s | 2.3 GiB |
| Louvain | 3.6 s | 2.2 s, +1.5 GiB | 34.7 s | 40.6 s | 3.8 GiB |
At this size, the arrays returned by export_graph() take about 1.1 GiB, and while building them, export_graph raises peak memory by 1.2 to 1.7 GiB, because it sorts the edges in a temporary buffer first. The undirected exports, used for components and Louvain, needed the most. This is a one-time copy of the graph’s structure, made once per export, and icebug then works on it without copying it again, so the overall memory usage can be within bounds for even larger graphs.
Conclusions
With lance-csr, a single copy of the data in Lance now serves Cypher traversal queries, associative search over hypervectors, and graph algorithms. For our Maker Night host Maya, this meant traversing friendships stored in Lance tables to come up with a full seating plan, with each host’s portrait, without needing to store or fetch data from anywhere else.
The best part is that lance-csr users also benefit from innovation across the columnar storage and compute ecosystem. Because Lance “speaks” Arrow, improvements in Lance, DataFusion and Arrow-native libraries flow directly into it, enabling lance-csr’s graph export to work seamlessly with any library that accepts CSR arrays. Icebug is a young graph algorithm toolkit, but it’s already showing a lot of promise in scaling graph algorithms to very large graphs on a single machine, and we’re excited to see where it goes.
We’re excited about how far we can take in-memory graph analysis on a single machine. At HDC Labs, we’re also building a distributed graph processing engine for larger workloads involving graphs and hypervectors. If you’re working on problems at that scale, we’d like to hear from you.
lance-csr, icebug and LanceDB are all open source. To run graph algorithms with this stack on your own data, start with the graph algorithms guide in the docs, and see the source code for this post’s demo in the link below. We’d love to see what you build, whether it’s a new algorithm example or an experiment with another CSR-based library. 🚀
Run the Maker Night demo Recreate the seating plan and run the graph benchmarks from this post.Footnotes
-
Each write adds a new version to the table, and a replaced column’s old files stay on disk until old versions are cleaned up. Calling
engine.optimize_datasets()from time to time compacts the tables, moves the engine onto the result and deletes versions older than seven days. See compaction and old versions in the lance-csr docs. ↩ -
lance-csr’s own CSR index, which serves multi-hop Cypher queries, has no notion of contiguous IDs. Lance data can be written in any order, and the storage layout doesn’t enforce any numbering on the keys, so the index maps each original key to the range of its neighbours instead. Indexing an array directly by keys such as 1001 and 9000000000 would need billions of mostly empty slots. Keys also keep their meaning as the Lance datasets change, whereas a dense numbering only holds for the set and order of nodes in one export. ↩