reconstruct_path(csgraph, predecessors, directed=True)
The N x N matrix representing the directed or undirected graph from which the predecessors are drawn.
The length-N array of indices of predecessors for the tree. The index of the parent of node i is given by predecessors[i].
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].
The N x N directed compressed-sparse representation of the tree drawn from csgraph which is encoded by the predecessor list.
Construct a tree from a graph and a predecessor list.
>>> from scipy.sparse import csr_matrix
... from scipy.sparse.csgraph import reconstruct_path
>>> graph = [
... [0, 1, 2, 0],
... [0, 0, 0, 1],
... [0, 0, 0, 3],
... [0, 0, 0, 0]
... ]
... graph = csr_matrix(graph)
... print(graph) (0, 1) 1 (0, 2) 2 (1, 3) 1 (2, 3) 3
>>> pred = np.array([-9999, 0, 0, 1], dtype=np.int32)
>>> cstree = reconstruct_path(csgraph=graph, predecessors=pred, directed=False)See :
... cstree.todense() matrix([[0., 1., 2., 0.], [0., 0., 0., 1.], [0., 0., 0., 0.], [0., 0., 0., 0.]])
The following pages refer to to this document either explicitly or contain code examples using this.
scipy.sparse.csgraph._tools.reconstruct_path
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them