construct_dist_matrix#
- scipy.sparse.csgraph.construct_dist_matrix(graph, predecessors, directed=True, null_value=np.inf)#
- Construct distance matrix from a predecessor matrix - Added in version 0.11.0. - Parameters:
- grapharray_like or sparse
- The N x N matrix representation of a directed or undirected graph. If dense, then non-edges are indicated by zeros or infinities. 
- predecessorsarray_like
- The N x N matrix of predecessors of each node (see Notes below). 
- directedbool, 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]. 
- null_valuebool, optional
- value to use for distances between unconnected nodes. Default is np.inf 
 
- Returns:
- dist_matrixndarray
- The N x N matrix of distances between nodes along the path specified by the predecessor matrix. If no path exists, the distance is zero. 
 
 - Notes - The predecessor matrix is of the form optionally returned by - shortest_path. Row i of the predecessor matrix contains information on the shortest paths from point i: each entry predecessors[i, j] gives the index of the previous node in the path from point i to point j. If no path exists between point i and j, then predecessors[i, j] = -9999- It should be noted that - shortest_pathonly returns distance matrix by default. With- return_predecessors=True, it returns a tuple with distance matrix as its first element and predecessors array as second element.- Examples - >>> import numpy as np >>> from scipy.sparse import csr_array >>> from scipy.sparse.csgraph import construct_dist_matrix - >>> graph = [ ... [0, 1, 2, 0], ... [0, 0, 0, 1], ... [0, 0, 0, 3], ... [0, 0, 0, 0] ... ] >>> graph = csr_array(graph) >>> print(graph) <Compressed Sparse Row sparse array of dtype 'int64' with 4 stored elements and shape (4, 4)> Coords Values (0, 1) 1 (0, 2) 2 (1, 3) 1 (2, 3) 3 - >>> pred = np.array([[-9999, 0, 0, 2], ... [1, -9999, 0, 1], ... [2, 0, -9999, 2], ... [1, 3, 3, -9999]], dtype=np.int32) - >>> construct_dist_matrix(graph=graph, predecessors=pred, directed=False) array([[0., 1., 2., 5.], [1., 0., 3., 1.], [2., 3., 0., 3.], [2., 1., 3., 0.]])