Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing depth-first algorithm from a specific vertex

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?

like image 392
sa11 Avatar asked Jan 07 '11 15:01

sa11


People also ask

What are the 3 types for depth first traversals?

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.

Which algorithm is applied in depth first search?

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.

What is depth first search explain with example?

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.


2 Answers

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)
like image 146
Benoît Avatar answered Sep 27 '22 16:09

Benoît


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

like image 42
Carter12s Avatar answered Sep 27 '22 17:09

Carter12s