Skip to main content

Louvain

SQL function: cugraph_louvain

Official cuGraph reference: C API

Build a hierarchy of communities by greedily moving vertices and aggregating partitions to maximize modularity.

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_louvain(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

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

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
thresholdnumber1e-7min 0non-negative convergence threshold

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

ColumnTypeNullableDescription
vertexInt64|Utf8noVertex assigned to a Louvain community.
partitionInt64noCommunity identifier assigned by Louvain.

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.

Compare citation communities against field labels

Two views select the 2010s AI literature (nodes by field-of-study label, edges where both endpoints qualify), and Louvain partitions it by citation structure alone. Cross-tabulating each community against primary_fos (with a window function to keep the top 3 labels per community) shows how closely the detected communities align with the assigned labels:

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 community_fos AS (
SELECT c."partition" AS community, p.primary_fos, COUNT(*) AS n
FROM cugraph_louvain(edges => (SELECT src, dst FROM ai_edges)) c
JOIN papers p ON p.paper_id = c.vertex
GROUP BY c."partition", p.primary_fos),
ranked AS (
SELECT SUM(n) OVER (PARTITION BY community) AS members,
ROW_NUMBER() OVER (PARTITION BY community ORDER BY n DESC) AS rn,
primary_fos,
n
FROM community_fos)
SELECT members, rn, primary_fos, n
FROM ranked
WHERE members > 2500 AND rn <= 3
ORDER BY members DESC, rn;
membersrnprimary_fosn
7,1721Convolutional neural network2,698
7,1722Deep learning2,527
7,1723Artificial neural network869
4,4771Deep learning1,332
4,4772Recurrent neural network1,280
4,4773Artificial neural network624
4,2601Image segmentation1,919
4,2602Convolutional neural network921
4,2603Deep learning799
3,8591Reinforcement learning3,129
3,8592Artificial neural network279
3,8593Deep learning226
3,5181Object detection2,193
3,5182Convolutional neural network467
3,5183Deep learning342

Louvain (1,233 communities over 38,054 papers) recovers recognizable subfield boundaries: convolutional-network, sequence-modeling, image-segmentation, and object-detection communities, plus a reinforcement-learning community that is 81% one label. Note the quoted "partition", since the output column name is a SQL keyword. cugraph_leiden is a drop-in replacement with the same call shape.

Limits

No algorithm-specific limitations.

To dry-run validate relation metadata, column types, and options without execution, see gpu_validate_call.