Skip to content

balanced_partition

balanced_partition(
    edge_index: Union[array, ndarray],
    num_nodes: int,
    num_parts: int,
    num_iters: int = 10,
    imbalance: float = 0.05,
) -> ndarray

Dependency-free balanced graph partitioning.

Nodes are ordered by a breadth-first traversal of the (undirected) graph, the order is cut into num_parts chunks of equal size, and the partitions are refined with balanced label propagation (a node moves to the partition holding most of its neighbors while sizes stay within imbalance of the average). Partitions are therefore connected-ish and balanced, but their edge cut is usually worse than METIS'.

Randomness comes from np.random (seed it for reproducible partitions).

Parameters:

Name Type Description Default
edge_index Union[array, ndarray]

[2, num_edges] edges.

required
num_nodes int

Number of nodes.

required
num_parts int

Number of partitions.

required
num_iters int

Number of label propagation rounds. Defaults to 10

10
imbalance float

Allowed relative deviation of the partition sizes from the average during refinement. Defaults to 0.05

0.05

Returns:

Type Description
ndarray

The partition id of every node, as a [num_nodes] numpy array.

Source code in mlx_graphs/loaders/cluster.py
def balanced_partition(
    edge_index: Union[mx.array, np.ndarray],
    num_nodes: int,
    num_parts: int,
    num_iters: int = 10,
    imbalance: float = 0.05,
) -> np.ndarray:
    """Dependency-free balanced graph partitioning.

    Nodes are ordered by a breadth-first traversal of the (undirected) graph,
    the order is cut into ``num_parts`` chunks of equal size, and the
    partitions are refined with balanced label propagation (a node moves to the
    partition holding most of its neighbors while sizes stay within
    ``imbalance`` of the average). Partitions are therefore connected-ish and
    balanced, but their edge cut is usually worse than METIS'.

    Randomness comes from ``np.random`` (seed it for reproducible partitions).

    Args:
        edge_index: ``[2, num_edges]`` edges.
        num_nodes: Number of nodes.
        num_parts: Number of partitions.
        num_iters: Number of label propagation rounds. Defaults to 10
        imbalance: Allowed relative deviation of the partition sizes from the
            average during refinement. Defaults to 0.05

    Returns:
        The partition id of every node, as a ``[num_nodes]`` numpy array.
    """
    if num_parts < 1:
        raise ValueError(f"`num_parts` must be positive (got {num_parts})")
    adj = Adjacency(edge_index, num_nodes)
    rowptr, col = _symmetric_csr(adj)
    order = _bfs_order(rowptr, col)
    part = np.empty(num_nodes, dtype=np.int64)
    part[order] = (np.arange(num_nodes) * num_parts) // max(num_nodes, 1)
    if num_parts > 1 and num_iters > 0:
        part = _refine(part, rowptr, col, num_parts, num_iters, imbalance)
    return part

from mlx_graphs.loaders import balanced_partition · mlx_graphs/loaders/cluster.py