Skip to content

remove_isolated_nodes

remove_isolated_nodes(
    edge_index: array,
    edge_features: Optional[array] = None,
    num_nodes: Optional[int] = None,
) -> tuple[array, Optional[array], array]

Removes the isolated nodes of a graph and relabels the remaining ones to [0, num_remaining_nodes) in their original order.

A node is isolated if it has no edge to another node: a node whose only edges are self-loops is isolated, and its self-loops are removed. Each remaining node keeps at most one self-loop (the last one), placed after the other edges, in node order.

Parameters:

Name Type Description Default
edge_index array

a [2, num_edges] array representing the source and target nodes of each edge

required
edge_features Optional[array]

Optional features of each edge, of shape [num_edges, ...]. Default: None

None
num_nodes Optional[int]

Number of nodes in the graph. Inferred from edge_index if not given.

None

Returns:

Type Description
array

A tuple (edge_index, edge_features, node_mask) with the relabeled

Optional[array]

edges, their features (None if not given) and a boolean mask of

array

shape [num_nodes] of the nodes that were kept.

Example:

edge_index = mx.array([[0, 1, 2], [1, 0, 2]])
remove_isolated_nodes(edge_index, num_nodes=4)
# (mx.array([[0, 1], [1, 0]]), None,
#  mx.array([True, True, False, False]))
Source code in mlx_graphs/utils/loop.py
@validate_edge_index_and_features
def remove_isolated_nodes(
    edge_index: mx.array,
    edge_features: Optional[mx.array] = None,
    num_nodes: Optional[int] = None,
) -> tuple[mx.array, Optional[mx.array], mx.array]:
    """
    Removes the isolated nodes of a graph and relabels the remaining ones to
    ``[0, num_remaining_nodes)`` in their original order.

    A node is isolated if it has no edge to another node: a node whose only
    edges are self-loops is isolated, and its self-loops are removed. Each
    remaining node keeps at most one self-loop (the last one), placed after
    the other edges, in node order.

    Args:
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        edge_features: Optional features of each edge, of shape
            ``[num_edges, ...]``. Default: ``None``
        num_nodes: Number of nodes in the graph. Inferred from ``edge_index``
            if not given.

    Returns:
        A tuple ``(edge_index, edge_features, node_mask)`` with the relabeled
        edges, their features (``None`` if not given) and a boolean mask of
        shape ``[num_nodes]`` of the nodes that were kept.

    Example:

    .. code-block:: python

        edge_index = mx.array([[0, 1, 2], [1, 0, 2]])
        remove_isolated_nodes(edge_index, num_nodes=4)
        # (mx.array([[0, 1], [1, 0]]), None,
        #  mx.array([True, True, False, False]))
    """
    num_nodes = _check_num_nodes(edge_index, num_nodes)
    edges = _to_numpy(edge_index)
    node_mask = _connected_nodes(edge_index, num_nodes)

    mapping = np.full(num_nodes, -1, dtype=np.int64)
    mapping[node_mask] = np.arange(int(node_mask.sum()))

    is_loop = edges[0] == edges[1]
    keep = np.nonzero(~is_loop)[0]
    loop_positions = np.nonzero(is_loop)[0]
    # Last self-loop of each connected node, ordered by node.
    last_loop = np.full(num_nodes, -1, dtype=np.int64)
    last_loop[edges[0][loop_positions]] = loop_positions
    kept_loops = last_loop[node_mask & (last_loop >= 0)]

    positions = np.concatenate([keep, kept_loops])
    out_index = mx.array(mapping[edges[:, positions]], dtype=edge_index.dtype)
    if positions.size == 0:
        out_index = mx.zeros((2, 0), dtype=edge_index.dtype)

    out_features = None
    if edge_features is not None:
        out_features = _take_rows(edge_features, positions)
    return out_index, out_features, mx.array(node_mask)

from mlx_graphs.utils import remove_isolated_nodes · mlx_graphs/utils/loop.py