I am using Boost BGL C++ and I need the Graph to do a BFS from a Source vertex to a target vertex and return all the unique paths.
Right now, I thought of a way to use a filtered graph to get a subset of the graph containing paths from source to target but i realised that it was basically not filtering since the filtered graph included vertex that are visited but not part of a path from source to target. Is there any way to get this information or a different approach is better?
Code for reference:
boost::filtered_graph<DirectedGraph, boost::keep_all, std::function<bool(VertexDescr)>> Graph::getUniquePathsFromSource(VertexDescr source, VertexDescr target, DirectedGraph const & g)
{
std::vector<double> distances(num_vertices(g));
std::vector<boost::default_color_type> colormap(num_vertices(g));
// Run BFS and record all distances from the source node
breadth_first_search(g, source,
visitor(make_bfs_visitor(boost::record_distances(distances.data(), boost::on_tree_edge())))
.color_map(colormap.data())
);
for (auto vd : boost::make_iterator_range(vertices(g)))
if (colormap.at(vd) == boost::default_color_type{})
distances.at(vd) = -1;
distances[source] = -2;
boost::filtered_graph<DirectedGraph, boost::keep_all, std::function<bool(VertexDescr)>>
fg(g, {}, [&](VertexDescr vd) { return distances[vd] != -1; });
// Print edge list
std::cout << "filtered out-edges:" << std::endl;
std::cout << "Source Vertex: " << source << std::endl;
auto ei = boost::edges(fg);
typedef boost::property_map<DirectedGraph, boost::edge_weight_t>::type WeightMap;
WeightMap weights = get(boost::edge_weight, fg);
for (auto it = ei.first; it != ei.second; ++it)
{
if (source != boost::target(*it, g)) {
std::cout << "Edge Probability " << *it << ": " << get(weights, *it) << std::endl;
}
}
return fg;
}
Input (vertex1, vertex2, weight):
0 1 0.001
0 2 0.1
0 3 0.001
1 5 0.001
2 3 0.001
3 4 0.1
1 482 0.1
482 635 0.001
4 705 0.1
705 5 0.1
1 1491 0.01
1 1727 0.01
1 1765 0.01
Output (For Source = 0, Target = 5):
Source Vertex: 0
Edge Probability (0,1): 0.001
Edge Probability (0,2): 0.1
Edge Probability (0,3): 0.001
Edge Probability (1,5): 0.001
Edge Probability (1,482): 0.1
Edge Probability (1,1491): 0.01
Edge Probability (1,1727): 0.01
Edge Probability (1,1765): 0.01
Edge Probability (2,3): 0.001
Edge Probability (3,4): 0.1
Edge Probability (4,705): 0.1
Edge Probability (482,635): 0.001
Edge Probability (705,5): 0.1
Expected Output:
0->1->5
0->3->4->705->5
0->2->3->4->705->5