Leiden
SQL function: cugraph_leiden
Official cuGraph reference: C API
Find modularity-based communities with refinement steps that improve the internal connectedness of the partitions.
Quickstart
The call below supplies edges from registered relation target_edges with canonical src and dst columns and may include weight. Substitute your own registered relations.
SELECT * FROM cugraph_leiden(edges => (SELECT src, dst FROM target_edges));
Inputs
Every relation is a named parenthesized SELECT subquery. The required edges role uses canonical src and dst columns; every role, its canonical columns, and their accepted Arrow types are listed under Relation arguments. Metadata validation resolves registered tables named in its JSON request.
Endpoint columns accept numeric Int32, Int64 vertex IDs or logical string Utf8, LargeUtf8, Utf8View vertex IDs; string vertex-identity outputs are canonicalized to Utf8 (native mapping Int64) while scores, distances, counts, coordinates, and opaque labels stay numeric. The shared vertex-ID contract is summarized in Vertex ID support; the concrete call-specific schema comes from gpu_validate_call.
Logical string side-input limitations:
- edge ID columns and edge-ID predicate side inputs are not supported for logical string graphs
Arguments and options
Relation arguments
| Argument | Required | Columns | Description |
|---|---|---|---|
edges | yes |
| edge relation with canonical src and dst columns, plus optional weight and edge_id columns |
Every edge_id column must have the integer type of src and dst; string-keyed graphs accept no edge IDs.
Named value arguments
| Option | Type | Default | Constraints | Description |
|---|---|---|---|---|
max_level | integer | 100 | min 1; max 4294967295 | maximum hierarchy level, at least 1 |
resolution | number | 1 | > 0 | positive community resolution |
seed | integer | 0 | min 0; max 18446744073709552000 | random initialization seed |
theta | number | 1 | > 0 | positive Leiden refinement scale |
Graph construction options
This function requires directed=false (undirected/symmetric graph); all other graph construction options follow the shared defaults documented in Graph Construction Options.
Output
| Column | Type | Nullable | Description |
|---|---|---|---|
vertex | Int64|Utf8 | no | Vertex assigned to a Leiden community. |
partition | Int64 | no | Community identifier assigned by Leiden. |
These are generic descriptor schemas; run gpu_validate_call to get the concrete, table-specific output schema.
Examples
This example runs on the citation network demo dataset.
Where Leiden disagrees with Louvain
Leiden takes the same call shape as
cugraph_louvain but adds a
refinement phase that guarantees well-connected communities, splitting
clusters Louvain merges. Joining the two partitions on vertex shows exactly
where. The views are the Louvain example's 2010s AI subgraph:
CREATE OR REPLACE VIEW ai_nodes AS
SELECT paper_id FROM papers
WHERE year >= 2010 AND primary_fos IN (
'Deep learning', 'Artificial neural network', 'Convolutional neural network',
'Recurrent neural network', 'Natural language processing',
'Reinforcement learning', 'Image segmentation', 'Feature extraction',
'Object detection', 'Speech recognition');
CREATE OR REPLACE VIEW ai_edges AS
SELECT e.src, e.dst
FROM citation_edges e
JOIN ai_nodes a ON a.paper_id = e.src
JOIN ai_nodes b ON b.paper_id = e.dst;
WITH lv AS (
SELECT vertex, "partition" AS louvain_c FROM cugraph_louvain(edges => (SELECT src, dst FROM ai_edges))),
ld AS (
SELECT vertex, "partition" AS leiden_c FROM cugraph_leiden(edges => (SELECT src, dst FROM ai_edges))),
joint AS (
SELECT lv.louvain_c, ld.leiden_c, COUNT(*) AS n
FROM lv JOIN ld ON ld.vertex = lv.vertex
GROUP BY lv.louvain_c, ld.leiden_c)
SELECT louvain_c,
SUM(n) AS members,
COUNT(*) FILTER (WHERE n >= 50) AS leiden_parts
FROM joint
GROUP BY louvain_c
HAVING SUM(n) > 2500
ORDER BY leiden_parts DESC, members DESC;
| louvain_c | members | leiden_parts |
|---|---|---|
| 0 | 7,172 | 5 |
| 1 | 4,477 | 2 |
| 2 | 4,260 | 1 |
| 4 | 3,859 | 1 |
| 3 | 3,518 | 1 |
Louvain's 7,172-paper convolutional-network community splits into five Leiden
communities of 50+ members and its sequence-modeling community into two, while
the image-segmentation, reinforcement-learning, and object-detection
communities stay whole. The refinement does not simply produce more
communities: over the whole graph Louvain returns 1,233 and Leiden 851. When a
Louvain community is only held together by a few incidental citations,
Leiden's refinement phase is what separates it: the guarantee that every
returned community is internally connected is the reason to prefer Leiden as
the default. Both runs are
deterministic here (seed defaults to 0), which is why the community ids in
this table are stable across sessions.
Limits
No algorithm-specific limitations.
To dry-run validate relation metadata, column types, and options without execution, see gpu_validate_call.