Skip to content

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 [2, num_edges]. The graph is expected to be undirected.

required
weight Optional[array]

Non-negative edge weights, of shape [num_edges]. Edges with a negative weight are never matched, as in PyG. Default: None

None
num_nodes Optional[int]

The number of nodes. Default: None (inferred from edge_index).

None

Returns:

Type Description
The cluster of each node, of shape `[N]` (`int64`

a node matched

array

with v has cluster min(u, v), an unmatched node u.

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
def graclus(
    edge_index: mx.array,
    weight: Optional[mx.array] = None,
    num_nodes: Optional[int] = None,
) -> mx.array:
    r"""A greedy clustering algorithm from the `"Weighted Graph Cuts without
    Eigenvectors: A Multilevel Approach" <http://www.cs.utexas.edu/users/
    inderjit/public_papers/multilevel_pami.pdf>`_ 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.

    Args:
        edge_index: The edges, of shape ``[2, num_edges]``. The graph is
            expected to be undirected.
        weight: Non-negative edge weights, of shape ``[num_edges]``. Edges with
            a negative weight are never matched, as in PyG. Default: ``None``
        num_nodes: The number of nodes. Default: ``None`` (inferred from
            ``edge_index``).

    Returns:
        The cluster of each node, of shape ``[N]`` (``int64``): a node matched
        with ``v`` has cluster ``min(u, v)``, an unmatched node ``u``.

    Example:

    .. code-block:: python

        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)
    """
    if num_nodes is None:
        num_nodes = int(edge_index.max().item()) + 1 if edge_index.size > 0 else 0
    if num_nodes == 0:
        return mx.zeros((0,), dtype=mx.int64)
    ei = _to_numpy(edge_index).astype(np.int64)
    keep = ei[0] != ei[1]
    if weight is not None:
        w = _to_numpy(weight).reshape(-1).astype(np.float32)
        keep &= w >= 0
        w = w[keep]
    else:
        w = np.ones(int(keep.sum()), dtype=np.float32)
    ei = ei[:, keep]
    row, col = mx.array(ei[0].astype(np.int32)), mx.array(ei[1].astype(np.int32))
    weight_arr = mx.array(w)
    cluster = mx.full((num_nodes,), -1, dtype=mx.int32)
    changed = True
    while changed:
        cluster, changed = _graclus_round(row, col, weight_arr, cluster, num_nodes)
        mx.eval(cluster)
    return cluster.astype(mx.int64)

from mlx_graphs.utils import graclus · mlx_graphs/utils/clustering.py