Skip to content

sort_edge_index

sort_edge_index(edge_index: array) -> tuple[array, array]

Sort the edge index.

Parameters:

Name Type Description Default
edge_index array

A [2, num_edges] array representing edge indices, where the first row contains source indices and the second row contains target indices.

required

Returns:

Type Description
array

A tuple containing the sorted edge index and the

array

corresponding sorting indices.

Source code in mlx_graphs/utils/sorting.py
@validate_edge_index
def sort_edge_index(edge_index: mx.array) -> tuple[mx.array, mx.array]:
    """Sort the edge index.

    Args:
        edge_index: A [2, num_edges] array representing edge indices,
            where the first row contains source indices and the second row contains
            target indices.

    Returns:
        A tuple containing the sorted edge index and the
        corresponding sorting indices.
    """
    sorted_target_indices = mx.argsort(edge_index[1])
    target_sorted_index = edge_index[:, sorted_target_indices]
    sorted_source_indices = mx.argsort(target_sorted_index[0])
    sorted_edge_index = target_sorted_index[:, sorted_source_indices]
    sorting_indices = sorted_target_indices[sorted_source_indices]
    return sorted_edge_index, sorting_indices

from mlx_graphs.utils.sorting import sort_edge_index · mlx_graphs/utils/sorting.py