Skip to main content

Clustering Algorithms

Use model.getClusters() to find sets of related nodes and groups. You can group vertices by their edges or by their two-dimensional positions. The call returns an array of Cluster objects and leaves the graph unchanged.

Choose an algorithm​

AlgorithmUse it whenOptions
Connected components (connected-components)Every vertex reachable through an edge path belongs in the same cluster. Edge direction is ignored.Default when you omit the options.
Louvain (louvain)A connected graph has communities with many internal edges and relatively few bridges.resolution defaults to 1. Higher values favor smaller communities. Supports seed and getEdgeWeight.
Label propagation (label-propagation)You want communities based on labels propagated from neighboring vertices.maxIterations defaults to 100. Supports seed and getEdgeWeight.
DBSCAN (dbscan)You want dense regions that can follow irregular shapes, such as rings, and leave isolated points unassigned.Requires epsilon (neighborhood radius) and minPoints (minimum neighbors, counting the point itself). includeNoise defaults to false.
K-means (kmeans)You know the desired number of clusters and expect compact groups around centroids.Requires positive integer k. maxIterations defaults to 100. Supports seed.
Agglomerative (agglomerative)You want to merge nearby vertices bottom-up and control when merging stops.Supports linkage, k, and distanceThreshold.

Louvain, label propagation, and k-means use a fixed seed by default. Pass seed to choose a different repeatable run.

Agglomerative clustering supports single (nearest pair), complete (farthest pair), average (average pairwise distance), and ward (minimize the increase in within-cluster variance, the default). Merging stops when it reaches k or the next merge would exceed distanceThreshold, whichever comes first. Omitting both merges all positioned vertices into one cluster.

Cluster by edges​

const components = model.getClusters()

const communities = model.getClusters({
type: "louvain",
resolution: 1,
getEdgeWeight: edge => edge.data?.strength ?? 1
})

Louvain and label propagation treat edges as undirected. Each edge has weight 1 by default. A custom getEdgeWeight(edge) can emphasize stronger connections. Edges with nonpositive or nonfinite weights are ignored.

Cluster by position​

DBSCAN, k-means, and agglomerative clustering all accept getPosition(vertex). Without it, the model reads left and top from vertex data, with x and y as fallbacks. Vertices with missing or nonfinite coordinates, or for which the accessor returns null, are skipped. With DBSCAN, set includeNoise: true to return each noise point as its own cluster.

const regions = model.getClusters({
type: "dbscan",
epsilon: 60,
minPoints: 3,
getPosition: ({ data }) => data.location ?? null
})

In this example, data.location is an object with numeric x and y coordinates. Choose epsilon in the same units as those coordinates.

Read the results​

cluster.vertices contains the member vertices. cluster.edges contains edges incident to those vertices, including edges that cross cluster boundaries. model.getCluster(vertex) returns the connected component containing the supplied vertex.

Louvain, label propagation, DBSCAN, k-means, and agglomerative clustering operate on top-level nodes and groups, so children inside a group are excluded as individual members. Louvain and label propagation use connections from nested vertices through their top-level groups.

To draw hulls or Voronoi regions around results, see the Clustering plugin guides for Apps and Diagrams.