# Source code for sknetwork.path.distances

```#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created in May 2023
@author: Thomas Bonald <bonald@enst.fr>
"""
from typing import Iterable, Optional, Union, Tuple

import numpy as np
from scipy import sparse

[docs]def get_distances(input_matrix: sparse.csr_matrix, source: Optional[Union[int, Iterable]] = None,
source_row: Optional[Union[int, Iterable]] = None,
source_col: Optional[Union[int, Iterable]] = None, force_bipartite: bool = False) \
-> Union[np.ndarray, Tuple[np.ndarray, np.ndarray], Tuple[np.ndarray, np.ndarray]]:
"""Get the distances from a source (or a set of sources) in number of hops.

Parameters
----------
input_matrix :
source :
If an integer, index of the source node.
If a list, indices of source nodes (the shortest distances to one of these nodes in returned).
source_row, source_col :
For bipartite graphs, index of source nodes on rows and columns.
The parameter source_row is an alias for source (at least one of them must be ``None``).
force_bipartite :
If ``True``, consider the input matrix as the biadjacency matrix of a bipartite graph.
Set to ``True`` is the parameters source_row or source_col re specified.

Returns
-------
distances : np.ndarray of shape (n_nodes,)
Vector of distances from source (distance = -1 if no path exists from the source).
For a bipartite graph, two vectors are returned, one for the rows and one for the columns.

Examples
--------
>>> from sknetwork.data import cyclic_digraph
array([0, 1, 2])
array([0, 1, 0])
"""
if source_row is not None or source_col is not None:
force_bipartite = True
n_row, n_col = input_matrix.shape

if bipartite:
if source is not None:
if source_row is not None:
raise ValueError('Only one of the parameters source and source_row can be specified.')
source_row = source
if source_row is None and source_col is None:
raise ValueError('At least one of the parameters source_row or source_col must be specified.')
if source_row is not None:
if source_col is not None:
else:
if source is None:
raise ValueError('The parameter source must be specified.')

distances = -np.ones(n_nodes, dtype=int)

distance = 0

while 1:
distance += 1