I am using boost's BGL and I managed to compute the distance matrix in a graph where all the weights are set to one as follows:
using EdgeProperty = boost::property<boost::edge_weight_t, size_t>;
using UGraph =
boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::undirectedS,
boost::no_property,
EdgeProperty
>;
using DistanceProperty = boost::exterior_vertex_property<UGraph, size_t>;
using DistanceMatrix = DistanceProperty::matrix_type;
template<typename Matrix>
Matrix distance_matrix(const UGraph& ug){
const size_t n_vertices{ boost::num_vertices(ug) };
DistanceMatrix d{n_vertices};
boost::johnson_all_pairs_shortest_paths(ug, d);
Matrix dist{ linalg::zeros<Matrix>(n_vertices, n_vertices) };
for(size_t j{0}; j < n_vertices; j++){
for(size_t i{0}; i < n_vertices; i++){
dist(i,j) = d[i][j];
}
}
return dist;
}
The element (i,j) of the distance matrix returned by distance_matrix
corresponds to the number of edges between i and j along the shortest path (since the weight are set to one).
How can I obtain the information to reconstruct the shortest path from an all-pair problem? The list of predecessors seems available only for single-source problems (using dijkstra_shortest_paths
) and I can't see how to obtain a similar information in the case of johnson_all_pairs_shortest_paths
.
I would like to get the same result obtained in Python with scipy.sparse.csgraph.shortest_path
when setting return_predecessors=True
(see SciPy doc).