I have a very large graph dot file and want to extract a subgraph from a given vertex. This subgraph should contain the given vertex and all vertexes below it.
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/graphviz.hpp>
#include <iostream>
struct Vertex {
std::string node;
};
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, Vertex>;
template <typename Fn>
requires std::is_invocable_r_v<bool, Fn, Graph::vertex_descriptor>
void removeVertexIf(Graph& graph, Fn const& fn) {
Graph::vertex_descriptor count = num_vertices(graph);
Graph::vertex_descriptor i = 0;
while (i < count) {
if (fn(i)) {
clear_vertex(i, graph);
remove_vertex(i, graph);
--count;
}
else {
++i;
}
}
}
struct DFSVisitor : boost::default_dfs_visitor {
DFSVisitor(std::vector<bool>& reachable)
: reachable(reachable)
{}
void discover_vertex(Graph::vertex_descriptor const index, Graph const&) {
reachable[index] = true;
}
std::vector<bool>& reachable;
};
void removeUnreachable(Graph& graph, Graph::vertex_descriptor const start_index) {
std::vector<bool> reachable(num_vertices(graph), false);
DFSVisitor visitor(reachable);
depth_first_search(graph, boost::visitor(visitor).root_vertex(start_index));
removeVertexIf(graph, [&](Graph::vertex_descriptor const index) {
return reachable[index];
});
}
int main() {
std::istringstream input(
"digraph{"
"0;1;2;3;4;5;6;7;8;9;"
"0->1;1->2;2->3;2->6;3->4;4->5;5->8;6->7;6->5;7->8;8->9;"
"}");
Graph g(0);
boost::dynamic_properties dp(boost::ignore_other_properties);
dp.property("node_id", get(&Vertex::node, g));
boost::read_graphviz(input, g, dp);
// delete everything that is not below Node 6
removeUnreachable(g, 6);
boost::write_graphviz(std::cout, g);
}
In this minimal example, the given vertex has the node ID 6. The following graphic shows what I want to extract:
How can I remove the nodes and edges that are not below Node 6? My current removeUnreachable iterates over the entire graph instead of starting at start_index by depth_first_search.
Dot file to SVG graphic:
dot -Tsvg out.dot -o out.svg

You already figured out that
depht_first_searchdoes more than you thought it did. Instead of complicating the visitor, I'd suggest to usedepht_first_visitinstead: https://www.boost.org/doc/libs/1_83_0/libs/graph/doc/depth_first_visit.htmlOther Issues
Adjacency lists with vertex container selector
vecShave an implied contiguous integral vertex index, which doubles as the descriptor in that case. You must have been somewhat aware of this because your spelled it out:When you remove an early vertex, you are effectively renumbering all vertices with higher index. This make it so that your
removeVertexIfloop invalidates the values inside thereachablemap.One way to avoid this would be to go by the name property (
Vertex::nodein your example). Another way is to renumber yourunreachableentries in parallel with the removal, but this breaks the encapsulation of the predicate function: the predicate now must know about the remove algorithms internals.Another option, of course, would be to have a (temporary) extra mapping that indirects the original vertex index to the current index.
Lastly you could select a vertex container that has reference and descriptor stability (e.g.
setSandlistS).Plenty of options, let's go with the simplest:
ListS
See it Live On Coliru
Of course you can make it work for
vecSiff you remove in the correct order:Note though that you get the output you should expect: Live On Coliru
To keep the original node ids, make it explicit:
Now printing Live On Coliru:
BONUS: Why Remove At All?
The performance of
clear_vertexandremove_vertexis going to make you cry¹. Instead, just filter for your target vertices:Live On Coliru
Printing, again:
¹ see e.g. Remove 100,000+ nodes from a Boost graph