Skip to content
Blog

Weakly Connected Components

A weakly connected component (WCC) in a directed graph is a maximal subgraph where any two vertices are connected to each other by some path, regardless of edge direction.

Kuzu implements a parallelized version of the WCC algorithm based on Ligra.

Syntax

CALL weakly_connected_components(
graph_name,
maxIterations := 100
)
RETURN node, group_id

Alias: wcc

Parameters

ParameterTypeOptionalDefaultDescription
graph_nameSTRINGNo-Name of the projected graph to run the algorithm on
maxIterationsINT64Yes100Maximum number of iterations to run

Return

ColumnTypeDescription
nodeNODENode object
group_idINT64The ID of the WCC that the node belongs to

Example

Define schema

CREATE NODE TABLE Node(id STRING PRIMARY KEY);
CREATE REL TABLE Edge(FROM Node to Node);

Insert nodes and edges

CREATE (u0:Node {id: 'A'}),
(u1:Node {id: 'B'}),
(u2:Node {id: 'C'}),
(u3:Node {id: 'D'}),
(u4:Node {id: 'E'}),
(u5:Node {id: 'F'}),
(u6:Node {id: 'G'}),
(u7:Node {id: 'H'}),
(u8:Node {id: 'I'}),
(u0)-[:Edge]->(u1),
(u1)-[:Edge]->(u2),
(u5)-[:Edge]->(u4),
(u6)-[:Edge]->(u4),
(u6)-[:Edge]->(u5),
(u6)-[:Edge]->(u7),
(u7)-[:Edge]->(u4),
(u6)-[:Edge]->(u5);

We first create a projected graph from the node and edge tables.

CALL project_graph('Graph', ['Node'], ['Edge']);

Then, you can run WCC as follows:

CALL weakly_connected_components('Graph') RETURN group_id, collect(node.id);
┌──────────┬──────────────────┐
│ group_id │ COLLECT(node.id) │
│ INT64 │ STRING[] │
├──────────┼──────────────────┤
│ 5 │ [G,F,H,E] │
│ 0 │ [D] │
│ 2 │ [B,C,A] │
│ 1 │ [I] │
└──────────┴──────────────────┘

Frequently asked questions

How is group_id assigned?

group_id is assigned based on Kuzu’s internal node offsets. Currently there is no way to assign group_id based on node properties.