graclus
¶
graclus(
edge_index: array,
weight: Optional[array] = None,
num_nodes: Optional[int] = None,
) -> array
A greedy clustering algorithm from the "Weighted Graph Cuts without Eigenvectors: A Multilevel Approach" paper, which matches each
node with one of its unmarked neighbors (the one maximizing the edge
weight, if given).
The matching is computed in parallel rounds, as PyG's GPU implementation:
unmatched nodes are randomly split into proposers and receivers, each
proposer proposes to its best unmatched neighbor, and each receiver accepts
its best proposal. The result is a maximal matching that depends on the
random coloring (mx.random), as PyG's depends on the order in which
the nodes are visited.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
The edges, of shape |
required |
weight
|
Optional[array]
|
Non-negative edge weights, of shape |
None
|
num_nodes
|
Optional[int]
|
The number of nodes. Default: |
None
|
Returns:
| Type | Description |
|---|---|
The cluster of each node, of shape `[N]` (`int64`
|
a node matched |
array
|
with |
Example:
import mlx.core as mx
from mlx_graphs.utils import graclus
edge_index = mx.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])
weight = mx.array([1.0, 1.0, 0.5, 0.5, 2.0, 2.0])
>>> graclus(edge_index, weight)
array([0, 0, 2, 2], dtype=int64)
Source code in mlx_graphs/utils/clustering.py
from mlx_graphs.utils import graclus · mlx_graphs/utils/clustering.py