Skip to main content

Leiden

UDTF: cugraph_leiden

Official cuGraph reference: C API

Find modularity-based communities with a refinement step, and split each community into the connected components of its internal edges, so every returned community induces a connected subgraph.

Quickstart​

The call below supplies edges from registered relation target_edges with canonical src and dst columns and may include weight.

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.

The vertex-ID types this UDTF accepts, the numeric types a per-call codebook maps to native ids, and its string-key limits are listed in Vertex ID support; the concrete call-specific schema comes from gpu_validate_call.

Arguments and options​

Relation arguments​

ArgumentRequiredColumnsDescription
edgesyes
  • src, dst: Int32, Int64, Utf8, LargeUtf8, Utf8View
  • weight (optional): Float32, Float64
  • edge_id (optional): Int32, Int64
edge relation with canonical src and dst columns, plus optional weight and edge_id columns; this algorithm ignores weight

Every edge_id column must have the integer type of src and dst; string-keyed graphs accept no edge IDs.

Named value arguments​

OptionTypeDefaultConstraintsDescription
max_levelinteger100min 1; max 4294967295maximum hierarchy level, at least 1
resolutionnumber1> 0positive community resolution
seedinteger0min 0; max 18446744073709552000random initialization seed

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​

ColumnTypeNullableDescription
vertexInt64|Utf8noVertex assigned to a Leiden community.
communityInt64noCommunity identifier assigned by Leiden; every community induces a connected subgraph.

These are generic descriptor schemas. Run gpu_validate_call to get the concrete, table-specific output schema.

Examples​

Which brands belong on the same discovery page?​

The query groups brands by overlapping audiences, including combinations that cross catalog categories. It uses May to October 2023 likes: a fan is a distinct shopper who liked a brand.

SQL reduces 182,889,825 like events to user-brand pairs and selects the 5,000 brands with the most fans. Jaccard measures shared audience divided by combined audience for each brand pair. Similarities of at least 0.02 become edges for Leiden. The resulting groups join back to brand names. Leiden uses the retained connections and currently ignores edge weights.

WITH fans AS (
SELECT -(e.user_id + 1) AS src, CAST(i.brand_id AS BIGINT) AS dst
FROM merrec_events e JOIN merrec_items i ON i.item_id = e.item_id
WHERE e.event_type = 'item_like' AND i.brand_id <> 0
GROUP BY e.user_id, i.brand_id
), top_brands AS (
SELECT dst AS brand_id, COUNT(*) AS fans FROM fans GROUP BY dst ORDER BY fans DESC, dst LIMIT 5000
), worlds AS (
SELECT vertex AS brand_id, community AS world
FROM cugraph_leiden(
edges => (
SELECT first AS src, second AS dst, CAST(similarity AS DOUBLE) AS weight
FROM cugraph_jaccard(
edges => (SELECT src, dst FROM fans),
vertex_pairs => (SELECT a.brand_id AS first, b.brand_id AS second
FROM top_brands a JOIN top_brands b ON a.brand_id < b.brand_id),
directed => false)
WHERE similarity >= 0.02),
directed => false, resolution => 3, seed => 42)
), members AS (
SELECT w.world, n.brand_name, t.fans
FROM worlds w
JOIN top_brands t ON t.brand_id = w.brand_id
JOIN (SELECT CAST(brand_id AS BIGINT) AS brand_id, MIN(brand_name) AS brand_name
FROM merrec_items WHERE brand_id <> 0 GROUP BY brand_id) n ON n.brand_id = w.brand_id
), ranked AS (
SELECT world, brand_name, fans,
ROW_NUMBER() OVER (PARTITION BY world ORDER BY fans DESC, brand_name) AS rk,
COUNT(*) OVER (PARTITION BY world) AS brands,
SUM(fans) OVER (PARTITION BY world) AS fan_links
FROM members
)
SELECT world, brands, fan_links, rk, brand_name, fans
FROM ranked
WHERE rk <= 5 AND brands >= 15
ORDER BY fan_links DESC, rk;

The query returns five leading brands for each of 38 groups with at least 15 members. Three selected groups illustrate different audience relationships:

GroupBrandsFive brands with the most fans
59101Milwaukee, DeWALT, Mainstays, Ninja, KitchenAid
20448Bandai, Microsoft, BTS, PlayStation, Manga
33207Chanel, Burberry, Prada, Christian Dior, Dior

The 101-brand group places power tools beside kitchen appliances, providing a candidate home-project collection to inspect.

The saved capture contains every returned group and the execution record. Leiden returned the same groups in all 4 runs under different group numbers.

Limits​

  • seed sets the random choices in the refinement step; the same seed does not guarantee identical communities across runs