pysdic.fem.compute_distance_matrix#
- compute_distance_matrix(graph, *, max_distance=None, sparse=False)[source]#
Compute the shortest path distances between all the pairs of nodes of an adjacency graph.
Warning
The dense output has shape \((N, N)\) and its memory grows quadratically with the number of nodes (about 800 MB for \(N = 10^4\)). For large graphs, use
sparse=Truewith a smallmax_distance.Warning
No tests are performed to check if the input graph is valid (e.g., if the graph is not symmetric, …). The behavior of the function is undefined if the input graph is not valid.
- Parameters:
graph (Union[ArrayLike, scipy.sparse.spmatrix]) – The adjacency graph as a square matrix of shape \((N, N)\) (sparse or dense), where a non-zero entry at \((i, j)\) means that nodes \(i\) and \(j\) are neighbors.
max_distance (Optional[Integral], optional (default:
None)) – The maximum distance to compute. Pairs of nodes farther thanmax_distanceare considered unreachable. If None, all the distances are computed.sparse (bool, optional (default:
False)) – If True, a sparse matrix storing only the distances between distinct reachable nodes is returned (see below).
- Returns:
distance_matrix – A symmetric matrix of shape \((N, N)\) where the entry \((i, j)\) is the distance between nodes \(i\) and \(j\).
Dense output: an integer array with 0 on the diagonal and -1 for unreachable pairs.
Sparse output: an integer
csr_matrixstoring only the distances \(\geq 1\). The diagonal (distance 0) and the unreachable pairs are not stored, sodistance_matrix[i].indicesare the nodes reachable from \(i\) (excluding \(i\)).
- Return type:
Union[numpy.ndarray, scipy.sparse.csr_matrix]
Notes
The distance between nodes \(i\) and \(j\) is the number of edges of the shortest path between them (see
pysdic.fem.bfs_distance()).The dense output is computed with
scipy.sparse.csgraph.dijkstraon the unweighted graph. The sparse output is computed level by level with boolean sparse products: the nodes at distance \(k\) are the nodes reachable in \(k\) steps (\((I + A)^{k}\)) but not in \(k - 1\) steps, so the memory only depends on the size of the neighborhoods.See also
pysdic.fem.bfs_distanceDistances from a single node to all the nodes of a graph.
pysdic.fem.compute_vertices_distance_matrixDistance matrix of the vertices of a mesh.
pysdic.fem.compute_elements_distance_matrixDistance matrix of the elements of a mesh.
Examples
Create a simple disconnected graph of 7 nodes and compute the distance matrix.
Disconnected graph with 7 nodes for distance matrix example.#
1import numpy 2from pysdic.fem import compute_distance_matrix 3 4edges = numpy.array([[0, 1], [0, 2], [1, 2], [2, 3], [3, 4], [5, 6]]) 5graph = numpy.zeros((7, 7), dtype=bool) 6graph[edges[:, 0], edges[:, 1]] = True 7graph[edges[:, 1], edges[:, 0]] = True 8 9distance_matrix = compute_distance_matrix(graph) 10print(f"distance matrix (shape={distance_matrix.shape}):") 11print(distance_matrix) 12 13sparse_distance_matrix = compute_distance_matrix(graph, max_distance=2, sparse=True) 14print(f"nodes within a distance of 2 from node 0: {sparse_distance_matrix[0].indices}") 15print(f"their distances: {sparse_distance_matrix[0].data}")
distance matrix (shape=(7, 7)): [[ 0 1 1 2 3 -1 -1] [ 1 0 1 2 3 -1 -1] [ 1 1 0 1 2 -1 -1] [ 2 2 1 0 1 -1 -1] [ 3 3 2 1 0 -1 -1] [-1 -1 -1 -1 -1 0 1] [-1 -1 -1 -1 -1 1 0]] nodes within a distance of 2 from node 0: [1 2 3] their distances: [1 1 2]