Leiden
UDTF: 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
Every edge_id column must have the integer type of src and dst; string-keyed graphs accept no edge IDs.
Named value arguments
Graph construction options
This UDTF requires directed=false (undirected/symmetric graph); all other graph construction options follow the shared defaults documented in Graph Construction Options.
Output
These are generic descriptor schemas; run gpu_validate_call to get the concrete, table-specific output schema.
Examples
- Citation network
- MerRec marketplace
This example runs on the citation network demo dataset.
Would another community algorithm split our largest research groups?
A research catalog may use citation communities to organize a field into reading groups. Before treating those groups as fixed boundaries, compare Louvain and Leiden on the same 2010s AI subgraph. For each large Louvain group, this query counts how many Leiden groups contain at least 50 of its papers. The overlap measures sensitivity to the choice of algorithm.
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;
In this capture, the largest Louvain group overlaps five Leiden groups with
at least 50 papers each. A leiden_parts value of 1 allows smaller fragments
below that threshold; it does not establish that the group remained intact.
Community IDs are local labels, and membership can vary between GPU runs even
with a fixed seed. Compare the membership of papers, rather than label values
from separate executions.
This example uses the October 2023 MerRec fixture and graph preparation.
Which browsing neighborhoods could become draft marketplace collections?
This query joins two graph views of observed item_view transitions. Leiden
groups product groups on an unweighted, undirected graph. PageRank scores
markets on a directed graph whose transition weights count distinct viewers.
For each Leiden group with at least ten markets, the query returns the five
markets with the highest weighted PageRank.
The default community 11 contains 58 grouped markets. Its five representatives include Nintendo games, Pokemon single cards, Sony games, Nintendo consoles, and Microsoft games. Other captured groups include Disney pins and stuffed animals, Squishmallows, Loungefly, and fashion dolls. Merchandising teams can review these cross-category patterns when drafting navigation collections from observed browsing. This query does not evaluate purchases, conversion, or sales.
The graph's product_id represents a brand and leaf-category group; item_id
identifies an individual listing. viewers counts distinct users with an
item_view in that group. Missing brand and leaf-category values appear as
“Brand not listed” and “Leaf category not listed” in the table.
Community 11
58 grouped marketsFive market groups ranked by weighted PageRank within this captured community.
| Rank | Product group | Brand | Category | Leaf category | Viewers | PageRank |
|---|---|---|---|---|---|---|
| 1 | 15928_1315 | Nintendo | Electronics | Games | 262 | 0.004766 |
| 2 | 17470_3525 | Pokemon | Toys & Collectibles | Single Cards | 135 | 0.004078 |
| 3 | 20652_1315 | Sony | Electronics | Games | 170 | 0.003160 |
| 4 | 15928_1314 | Nintendo | Electronics | Consoles | 180 | 0.002297 |
| 5 | 14034_1315 | Microsoft | Electronics | Games | 87 | 0.001692 |
Community numbers are labels from this query's Leiden fit. They describe this
capture and do not establish run-to-run stability. The CPU assessment reports
zero market-metadata mismatches, PageRank maximum absolute error 0.0 against
the saved full PageRank output, and correct five-row rank ordering for each of
the 22 returned groups. The validation does not prove that query 24's labels
match a separately executed full Leiden assignment, because that query fits
Leiden independently.
The captured plan has two native GPU fragments, Leiden and PageRank, and 15
host DataFusion boundaries for joins, repartitioning, and ranked-window
processing. The single observed Flight elapsed time was 0.2762586469762027
seconds. It includes planning and complete result receipt, excludes graph
preparation, and came from a reused server after many previous queries. It is
not a performance benchmark.
Download all 110 result rows, including the executed SQL, schema, query ID, timing boundary, and source SHA-256 values. The CPU community validation records its comparison scope and limits.
Executed SQL, query 6490d83e-5bce-4ada-ad7a-77c742ecebab
-- Review representative markets from each sufficiently large Leiden community.
-- Community labels are query-local; PageRank uses directed weighted transitions.
WITH assignments AS (
SELECT
r.vertex,
r."partition" AS community,
COUNT(*) OVER (PARTITION BY r."partition") AS community_size
FROM cugraph_leiden(
edges => (SELECT src, dst FROM mr_undirected),
directed => false,
seed => 42
) r
), weighted_pagerank AS (
SELECT vertex, value AS pagerank
FROM cugraph_pagerank(
edges => (SELECT src, dst, weight FROM mr_edges),
directed => true
)
), ranked_markets AS (
SELECT
a.vertex,
a.community,
a.community_size,
ROW_NUMBER() OVER (
PARTITION BY a.community
ORDER BY p.pagerank DESC, n.product_id ASC
) AS representative_rank,
n.product_id,
n.brand_name,
n.category,
n.leaf_category,
n.viewers,
p.pagerank
FROM assignments a
JOIN mr_nodes n ON n.id = a.vertex
JOIN weighted_pagerank p ON p.vertex = a.vertex
)
SELECT
vertex,
community,
community_size,
representative_rank,
product_id,
brand_name,
category,
leaf_category,
viewers,
pagerank
FROM ranked_markets
WHERE community_size >= 10
AND representative_rank <= 5
ORDER BY community, representative_rank;
Limits
No algorithm-specific limitations.
To dry-run validate relation metadata, column types, and options without execution, see gpu_validate_call.