SciPy

scipy.sparse.csgraph.reconstruct_path

scipy.sparse.csgraph.reconstruct_path(csgraph, predecessors, directed=True)

Construct a tree from a graph and a predecessor list.

New in version 0.11.0.

Parameters:

csgraph : array_like or sparse matrix

The N x N matrix representing the directed or undirected graph from which the predecessors are drawn.

predecessors : array_like, one dimension

The length-N array of indices of predecessors for the tree. The index of the parent of node i is given by predecessors[i].

directed : bool, optional

If True (default), then operate on a directed graph: only move from point i to point j along paths csgraph[i, j]. If False, then operate on an undirected graph: the algorithm can progress from point i to j along csgraph[i, j] or csgraph[j, i].

Returns:

cstree : csr matrix

The N x N directed compressed-sparse representation of the tree drawn from csgraph which is encoded by the predecessor list.