Skip to content

is_undirected

is_undirected(edge_index: array, edge_features: Optional[array] = None) -> bool

Determines whether a graph is undirected based on the given edge index and optional edge features.

Parameters:

Name Type Description Default
edge_index array

The edge index of the graph.

required
edge_features Optional[array]

Edge features associated with each edge. If provided, the function considers both edge indices and features for the check.

None

Returns:

Type Description
bool

True if the graph is undirected, False otherwise.

Source code in mlx_graphs/utils/topology.py
@validate_edge_index_and_features
def is_undirected(
    edge_index: mx.array, edge_features: Optional[mx.array] = None
) -> bool:
    """
    Determines whether a graph is undirected based on the given edge index
    and optional edge features.

    Args:
        edge_index: The edge index of the graph.
        edge_features: Edge features associated
            with each edge. If provided, the function considers both edge indices
            and features for the check.

    Returns:
        True if the graph is undirected, False otherwise.
    """
    # The function checks if the sorted order of source-destination pairs is equal
    # to the sorted order of destination-source pairs. If edge features are provided,
    # it also checks for equality in their order.
    if edge_features is None:
        src_dst_sort, _ = sort_edge_index(edge_index)
        dst_src_sort, _ = sort_edge_index(mx.stack([edge_index[1], edge_index[0]]))
        if mx.array_equal(src_dst_sort, dst_src_sort):
            return True
    else:
        src_dst_sort, src_dst_feat = sort_edge_index_and_features(
            edge_index, edge_features
        )
        dst_src_sort, dst_src_feat = sort_edge_index_and_features(
            mx.stack([edge_index[1], edge_index[0]]), edge_features
        )
        if mx.array_equal(src_dst_sort, dst_src_sort) and mx.array_equal(
            src_dst_feat, dst_src_feat
        ):
            return True

    return False

from mlx_graphs.utils.topology import is_undirected · mlx_graphs/utils/topology.py