Remarks
Hierarchical clustering creates a hierarchy of clusters in a bottom-to-top approach based on some distance metric and linkage.
The clustering is performed using the agglomerative strategy i.e., a bottom-up approach according to which initially each node comprises its own cluster. At each step pairs of clusters are merged while moving up the hierarchy. The dissimilarity between clusters is determined based on the given linkage and the given node distances metric. The algorithm continues until all nodes belong to the same cluster.
Predefined metrics are available as constants on HierarchicalClustering:
- EUCLIDEAN distance
- Squared Euclidean distance
- MANHATTAN distance
Internally the HierarchicalClusteringResult stores a dendrogram which represents the result of the clustering algorithm as a binary tree structure. Based on the dendrogram the graph's nodes are partitioned into clusters according to either clusterCount or cutoff. Since both methods to compute the clusters only require the dendrogram, the HierarchicalClusteringResult offers methods to create a new clustering based on either criterion as long as either the graph or the subgraph specification did not change in between (which would require re-computing the dendrogram).
Other Clustering Algorithms
yFiles for HTML supports a number of other clustering algorithms:
- KMeansClustering – partitions the graph into clusters based on the distance between nodes and the cluster midpoints.
- EdgeBetweennessClustering – partitions the graph into clusters based on edge-betweenness centrality.
- BiconnectedComponentClustering – partitions the graph into clusters based on its biconnected components.
- LouvainModularityClustering – partitions the graph into clusters by applying the Louvain modularity method.
- LabelPropagationClustering – partitions the graph into clusters by applying a label propagation algorithm.
Complexity
Examples
// prepare the hierarchical clustering algorithm
const algorithm = new HierarchicalClustering()
// run the algorithm
const result = algorithm.run(graph)
// highlight the nodes of the clusters with different styles
for (const node of graph.nodes) {
const componentId = result.nodeClusterIds.get(node)!
graph.setStyle(node, clusterStyles.get(componentId)!)
}See Also
Developer's Guide
Members
Constructors
Properties
If set to a negative number or 0, the cutoff value is used to calculate the clusters. If both values are negative, a number of 1 is used (one cluster containing all nodes in the graph).
A new result for a different number of clusters can be obtained efficiently with changeClusterCount on a valid HierarchicalClusteringResult, as long as the graph, linkage, and metric are still the same.
See Also
Developer's Guide
The clusters are calculated based on the given cut-off value that is used for cutting the hierarchical tree at a point such that the dissimilarity values of the nodes that remain at the dendrogram are less than this value.
This value will only be used if clusterCount is 0 or negative.
This value must be non-negative, otherwise the result will be a single cluster which contains all nodes of the graph.
A new result for a different cut-off value can be obtained efficiently with changeCutoff on a valid HierarchicalClusteringResult, as long as the graph, linkage, and metric are still the same.
See Also
Developer's Guide
Gets or sets the method for determining the distance between clusters.
The result of the function must not be negative.
Predefined common metrics are available as constants on HierarchicalClustering:
- EUCLIDEAN distance (the default)
- Squared Euclidean distance
- MANHATTAN distance
See Also
Developer's Guide
Gets or sets the collection of edges which define a subset of the graph for the algorithms to work on.
If nothing is set, all edges of the graph will be processed.
If only the excludes are set, all edges in the graph except those provided in the excludes are processed.
Note that edges which start or end at nodes which are not in the subgraphNodes are automatically not considered by the algorithm.
ItemCollection<T> instances may be shared among algorithm instances and will be (re-)evaluated upon (re-)execution of the algorithm.
Examples
// prepare the hierarchical clustering algorithm
const algorithm = new HierarchicalClustering({
// Ignore edges without target arrow heads
subgraphEdges: {
excludes: (edge: IEdge): boolean =>
edge.style instanceof PolylineEdgeStyle &&
edge.style.targetArrow instanceof Arrow &&
edge.style.targetArrow.type === ArrowType.NONE,
},
})
// run the algorithm
const result = algorithm.run(graph)
// highlight the nodes of the clusters with different styles
for (const node of graph.nodes) {
const componentId = result.nodeClusterIds.get(node)!
graph.setStyle(node, clusterStyles.get(componentId)!)
}Gets or sets the collection of nodes which define a subset of the graph for the algorithms to work on.
If nothing is set, all nodes of the graph will be processed.
If only the excludes are set, all nodes in the graph except those provided in the excludes are processed.
ItemCollection<T> instances may be shared among algorithm instances and will be (re-)evaluated upon (re-)execution of the algorithm.
Examples
// prepare the hierarchical clustering algorithm
const algorithm = new HierarchicalClustering({
subgraphNodes: {
// only consider elliptical nodes in the graph
includes: (node: INode): boolean =>
node.style instanceof ShapeNodeStyle &&
node.style.shape === ShapeNodeShape.ELLIPSE,
// but ignore the first node, regardless of its shape
excludes: graph.nodes.first()!,
},
})
// run the algorithm
const result = algorithm.run(graph)
// highlight the nodes of the clusters with different styles
for (const node of graph.nodes) {
const componentId = result.nodeClusterIds.get(node)!
graph.setStyle(node, clusterStyles.get(componentId)!)
}Methods
Partitions the graph into clusters using hierarchical clustering.
Parameters
- graph: IGraph
- The input graph to run the algorithm on.
Return Value
- HierarchicalClusteringResult
- A HierarchicalClusteringResult containing the computed clusters and dendrogram.
Throws
- Exception ({ name: 'InvalidOperationError' })
- If the algorithm can't create a valid result due to an invalid graph structure or wrongly configured properties.