Search code examples
c++depth-first-searchboost-graph

C++ Boost Graph Library:- Specifying Root Node for a DFS


Using the standard on a directed graph,

std::vector<size_type> dtime(N);
std::vector<size_type> ftime(N);
size_type t = 0;
dfs_time_visitor<size_type*> vis(&dtime[0], &ftime[0], t);
depth_first_search(graph, visitor(vis));

appears to always start the dfs from node 0.

How does one tell the algorithm to start from a known "root node"?


Solution

  • Here you can find a list of all the overloads for depth_first_search. The one you need is the "named parameter version". The parameter you need to use is "root_vertex" and your invocation of depth_first_search would simply be:

    depth_first_search(graph, visitor(vis).root_vertex(root_vertex_descriptor));