I am trying to find a way to perform the depth-first algorithm from a specific vertex by using the boost graph library.
The depth-first algorithm provided by Boost library evaluates the graph beginning from the start vertex to the last vertex. But what if the graph has to be searched from a specific vertex?
Any suggestions?
In this tutorial, we'll take a closer look at three types of depth-first traversal: in-order, post-order and pre-order. We'll be applying what we learn on a binary tree because they're easier to represent and the examples will be easier to trace.
Depth-first search is often used as a subroutine in network flow algorithms such as the Ford-Fulkerson algorithm. DFS is also used as a subroutine in matching algorithms in graph theory such as the Hopcroft–Karp algorithm. Depth-first searches are used in mapping routes, scheduling, and finding spanning trees.
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.
Have a look at BGL's documentation.
There is an overload where you can provide the start vertex.
template <class Graph, class DFSVisitor, class ColorMap>
void depth_first_search(const Graph& g, DFSVisitor vis, ColorMap color,
typename graph_traits<Graph>::vertex_descriptor start)
BGL Provides two mechanisms for setting the starting vertex of depth_first_search. You can either use the overload operator which requires supplying a ColorMap, or you can directly set the property of your visitor:
boost::depth_first_search(myGraph, boost::visitor(myVisitor).root_vertex(myVertex));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With