0

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).

RMeli
  • 188
  • 5
  • Doesn't look like the algorithm was intended for it – sehe Dec 12 '17 at 13:50
  • @sehe I came to the same conclusion. Do you have any idea how I can achieve, in a simple way, the same result of `scipy.sparse.csgraph.shortest_path` using BGL? – RMeli Dec 12 '17 at 14:14
  • After some research I think I found a way to find the shortest path, using `breadth_first_search` and a custom BFSVisitor. The `breadth_first_search` starts from vertex i and has to stop when vertex j is encountered. During this search he can record the list of predecessors. Now I just have to figure out how to define such a custom BFSVisitor. Probably I will need to throw an exception when the vertex j is encountered. – RMeli Dec 12 '17 at 14:58
  • Huh. Why stop at a certain vertex? `all_pairs` seemed to suggest you want... all pairs? (About stopping at a specific vertex: https://stackoverflow.com/questions/47518846/how-to-find-the-shortest-path-between-two-vertices-in-a-bgl-graph/47521157#47521157; see also https://stackoverflow.com/questions/47419307/recording-predecessors-in-a-dfs-search-in-an-undirected-graph/47419981#47419981) – sehe Dec 12 '17 at 15:56
  • @sehe I want all pairs at the beginning, to know how many edges there are between any couple of vertex (i, j). But then, for each to vertices separated by one, two and three edges along the shortest path, I have to compute some properties. The problem is that to compute these properties I need the vertices on the shortest path as well. For example, if the distance property matrix `DistanceMatrix` returned by `johnson_all_pairs_shortest_paths` tells me that between i and j there are two edges (i.e. the distance is 2), I need to know the additional vertex between i and j along the shortest path. – RMeli Dec 12 '17 at 16:54

0 Answers0