Skip to content

add_random_edge

add_random_edge(
    edge_index: array,
    p: float = 0.5,
    force_undirected: bool = False,
    num_nodes: Optional[Union[int, tuple[int, int]]] = None,
    training: bool = True,
) -> tuple[array, array]

Randomly adds round(p * num_edges) new edges to edge_index. The new edges are sampled with negative_sampling(): they are all different, are not self-loops and are not already in the graph.

Parameters:

Name Type Description Default
edge_index array

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

required
p float

Ratio of edges to add, relative to the number of edges. Default: 0.5

0.5
force_undirected bool

If True, the added edges are undirected (both directions are added and counted). Default: False

False
num_nodes Optional[Union[int, tuple[int, int]]]

Number of nodes in the graph, or a tuple (num_src_nodes, num_dst_nodes) for a bipartite graph. Inferred from edge_index if not given.

None
training bool

If False, nothing is added. Default: True

True

Returns:

Type Description
array

A tuple (edge_index, added_edges) with the edges of the graph

array

followed by the added edges, and the added edges alone.

Example:

edge_index = mx.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])
edge_index, added_edges = add_random_edge(edge_index, p=0.5)
# edge_index: e.g. mx.array([[0, 1, 1, 2, 2, 3, 2, 1, 3],
#                            [1, 0, 2, 1, 3, 2, 0, 3, 0]])
# added_edges: e.g. mx.array([[2, 1, 3], [0, 3, 0]])
Source code in mlx_graphs/utils/augmentation.py
@validate_edge_index
def add_random_edge(
    edge_index: mx.array,
    p: float = 0.5,
    force_undirected: bool = False,
    num_nodes: Optional[Union[int, tuple[int, int]]] = None,
    training: bool = True,
) -> tuple[mx.array, mx.array]:
    """
    Randomly adds ``round(p * num_edges)`` new edges to ``edge_index``. The
    new edges are sampled with :func:`~mlx_graphs.utils.negative_sampling`:
    they are all different, are not self-loops and are not already in the
    graph.

    Args:
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        p: Ratio of edges to add, relative to the number of edges.
            Default: ``0.5``
        force_undirected: If ``True``, the added edges are undirected (both
            directions are added and counted). Default: ``False``
        num_nodes: Number of nodes in the graph, or a tuple ``(num_src_nodes,
            num_dst_nodes)`` for a bipartite graph. Inferred from
            ``edge_index`` if not given.
        training: If ``False``, nothing is added. Default: ``True``

    Returns:
        A tuple ``(edge_index, added_edges)`` with the edges of the graph
        followed by the added edges, and the added edges alone.

    Example:

    .. code-block:: python

        edge_index = mx.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])
        edge_index, added_edges = add_random_edge(edge_index, p=0.5)
        # edge_index: e.g. mx.array([[0, 1, 1, 2, 2, 3, 2, 1, 3],
        #                            [1, 0, 2, 1, 3, 2, 0, 3, 0]])
        # added_edges: e.g. mx.array([[2, 1, 3], [0, 3, 0]])
    """
    if p < 0.0 or p > 1.0:
        raise ValueError(f"Ratio of added edges has to be between 0 and 1 (got {p})")
    if force_undirected and isinstance(num_nodes, (tuple, list)):
        raise ValueError("`force_undirected` is not supported for bipartite graphs")
    if not training or p == 0.0:
        return edge_index, _empty_like(edge_index)

    added = negative_sampling(
        edge_index,
        num_nodes=num_nodes,
        num_neg_samples=round(edge_index.shape[1] * p),
        force_undirected=force_undirected,
    )
    return mx.concatenate([edge_index, added], axis=1), added

from mlx_graphs.utils import add_random_edge · mlx_graphs/utils/augmentation.py