Skip to content

bipartite_subgraph

bipartite_subgraph(
    subset: tuple[NodeSubset, NodeSubset],
    edge_index: array,
    edge_features: Optional[array] = None,
    relabel_nodes: bool = False,
    size: Optional[tuple[int, int]] = None,
    return_edge_mask: bool = False,
) -> Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

Returns the subgraph of a bipartite graph induced by a subset of the source nodes and a subset of the destination nodes, i.e. the edges whose source is in subset[0] and destination is in subset[1].

Parameters:

Name Type Description Default
subset tuple[NodeSubset, NodeSubset]

A tuple (src_subset, dst_subset) of the source and destination nodes to keep, each given as an array (or list) of node indices or as a boolean mask.

required
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
relabel_nodes bool

If True, the source (destination) nodes of the returned edge_index are relabeled to their position in src_subset (dst_subset), or their rank among the selected nodes for a boolean mask. Default: False

False
size Optional[tuple[int, int]]

The number of source and destination nodes (num_src, num_dst). Inferred from edge_index if not given.

None
return_edge_mask bool

If True, also returns the boolean mask of shape [num_edges] of the edges that were kept. Default: False

False

Returns:

Type Description
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

A tuple (edge_index, edge_features) with the edges of the subgraph

Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

and their features (None if edge_features was not given),

Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

followed by the edge mask if return_edge_mask is True.

Example:

edge_index = mx.array(
    [[0, 5, 2, 3, 3, 4, 4, 3, 5, 5, 6], [0, 0, 3, 2, 0, 0, 2, 1, 2, 3, 1]]
)
subset = (mx.array([2, 3, 5]), mx.array([2, 3]))

bipartite_subgraph(subset, edge_index, relabel_nodes=True)
# (mx.array([[0, 1, 2, 2], [1, 0, 0, 1]]), None)
Source code in mlx_graphs/utils/subgraph.py
def bipartite_subgraph(
    subset: tuple[NodeSubset, NodeSubset],
    edge_index: mx.array,
    edge_features: Optional[mx.array] = None,
    relabel_nodes: bool = False,
    size: Optional[tuple[int, int]] = None,
    return_edge_mask: bool = False,
) -> Union[
    tuple[mx.array, Optional[mx.array]],
    tuple[mx.array, Optional[mx.array], mx.array],
]:
    """
    Returns the subgraph of a bipartite graph induced by a subset of the
    source nodes and a subset of the destination nodes, i.e. the edges whose
    source is in ``subset[0]`` and destination is in ``subset[1]``.

    Args:
        subset: A tuple ``(src_subset, dst_subset)`` of the source and
            destination nodes to keep, each given as an array (or list) of
            node indices or as a boolean mask.
        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``
        relabel_nodes: If ``True``, the source (destination) nodes of the
            returned ``edge_index`` are relabeled to their position in
            ``src_subset`` (``dst_subset``), or their rank among the selected
            nodes for a boolean mask. Default: ``False``
        size: The number of source and destination nodes ``(num_src,
            num_dst)``. Inferred from ``edge_index`` if not given.
        return_edge_mask: If ``True``, also returns the boolean mask of shape
            ``[num_edges]`` of the edges that were kept. Default: ``False``

    Returns:
        A tuple ``(edge_index, edge_features)`` with the edges of the subgraph
        and their features (``None`` if ``edge_features`` was not given),
        followed by the edge mask if ``return_edge_mask`` is ``True``.

    Example:

    .. code-block:: python

        edge_index = mx.array(
            [[0, 5, 2, 3, 3, 4, 4, 3, 5, 5, 6], [0, 0, 3, 2, 0, 0, 2, 1, 2, 3, 1]]
        )
        subset = (mx.array([2, 3, 5]), mx.array([2, 3]))

        bipartite_subgraph(subset, edge_index, relabel_nodes=True)
        # (mx.array([[0, 1, 2, 2], [1, 0, 0, 1]]), None)
    """
    sub_edge_index, sub_edge_features, edge_mask = _bipartite_subgraph(
        edge_index, subset, edge_features, relabel_nodes, size
    )
    if return_edge_mask:
        return sub_edge_index, sub_edge_features, mx.array(edge_mask)
    return sub_edge_index, sub_edge_features

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