Skip to content

k_hop_subgraph

k_hop_subgraph(
    node_idx: Union[int, Sequence[int], array],
    num_hops: int,
    edge_index: array,
    relabel_nodes: bool = False,
    num_nodes: Optional[int] = None,
    directed: bool = False,
) -> tuple[array, array, array, array]

Computes the num_hops-hop subgraph around the nodes node_idx.

Edges are followed backwards from the target nodes: the subgraph contains every node whose messages reach node_idx within num_hops message passing steps (i.e. the receptive field of node_idx for a GNN with num_hops layers).

Parameters:

Name Type Description Default
node_idx Union[int, Sequence[int], array]

The central node(s), as an int, a list or an array.

required
num_hops int

Number of hops to follow.

required
edge_index array

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

required
relabel_nodes bool

If True, the returned edge_index uses new node ids in [0, len(subset)), following the order of the returned subset. Default: False

False
num_nodes Optional[int]

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

None
directed bool

If True, only the edges that were followed to reach the nodes are kept. If False (default), all the edges between the nodes of the subgraph are kept.

False

Returns:

Type Description
array

A tuple (subset, edge_index, mapping, edge_mask) with

- `subset`

the sorted ids of the nodes of the subgraph;

- `edge_index`

the edges of the subgraph;

- `mapping`

the position of each node of node_idx in subset (its new id when relabel_nodes=True);

- `edge_mask`

boolean mask of shape [num_edges] of the edges that were kept.

Example:

edge_index = mx.array([[0, 1, 2, 3], [1, 2, 3, 4]])  # path 0 -> ... -> 4

subset, sub_edge_index, mapping, edge_mask = k_hop_subgraph(3, 2, edge_index)
# subset: mx.array([1, 2, 3])
# sub_edge_index: mx.array([[1, 2], [2, 3]])
# mapping: mx.array([2])
# edge_mask: mx.array([False, True, True, False])
Source code in mlx_graphs/utils/subgraph.py
def k_hop_subgraph(
    node_idx: Union[int, Sequence[int], mx.array],
    num_hops: int,
    edge_index: mx.array,
    relabel_nodes: bool = False,
    num_nodes: Optional[int] = None,
    directed: bool = False,
) -> tuple[mx.array, mx.array, mx.array, mx.array]:
    """
    Computes the ``num_hops``-hop subgraph around the nodes ``node_idx``.

    Edges are followed backwards from the target nodes: the subgraph contains
    every node whose messages reach ``node_idx`` within ``num_hops`` message
    passing steps (i.e. the receptive field of ``node_idx`` for a GNN with
    ``num_hops`` layers).

    Args:
        node_idx: The central node(s), as an int, a list or an array.
        num_hops: Number of hops to follow.
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        relabel_nodes: If ``True``, the returned ``edge_index`` uses new node
            ids in ``[0, len(subset))``, following the order of the returned
            ``subset``. Default: ``False``
        num_nodes: Number of nodes in the graph. Inferred from ``edge_index``
            if not given.
        directed: If ``True``, only the edges that were followed to reach the
            nodes are kept. If ``False`` (default), all the edges between the
            nodes of the subgraph are kept.

    Returns:
        A tuple ``(subset, edge_index, mapping, edge_mask)`` with

        - ``subset``: the sorted ids of the nodes of the subgraph;
        - ``edge_index``: the edges of the subgraph;
        - ``mapping``: the position of each node of ``node_idx`` in ``subset``
            (its new id when ``relabel_nodes=True``);
        - ``edge_mask``: boolean mask of shape ``[num_edges]`` of the edges
            that were kept.

    Example:

    .. code-block:: python

        edge_index = mx.array([[0, 1, 2, 3], [1, 2, 3, 4]])  # path 0 -> ... -> 4

        subset, sub_edge_index, mapping, edge_mask = k_hop_subgraph(3, 2, edge_index)
        # subset: mx.array([1, 2, 3])
        # sub_edge_index: mx.array([[1, 2], [2, 3]])
        # mapping: mx.array([2])
        # edge_mask: mx.array([False, True, True, False])
    """
    return _k_hop_subgraph(
        edge_index, node_idx, num_hops, relabel_nodes, num_nodes, directed
    )

from mlx_graphs.utils import k_hop_subgraph · mlx_graphs/utils/subgraph.py