Question: BFS implementation is provided. You need to implement DFS . #ifndef _ GRAPH _ ALGORITHMS _ H _ #define _ GRAPH _ ALGORITHMS _ H
BFS implementation is provided. You need to implement DFS
#ifndef GRAPHALGORITHMSH
#define GRAPHALGORITHMSH
#include
#include
#include Uncomment this if you have boost installed
This is an example list of the basic algorithms we will work with in class.
In general this is what the following template parameters are:
Graph: type of graph, literally your adjacency list graph.
ParentMap: associative container between vertexdescriptors and parent
vertexdescriptors. This is a representation of the free
treesforests created by these search methods.
DistanceMap: associative container between vertexdescriptors and
EdgeProperties. This represents the summation of the path
weights to the vertex itself.
Assume EdgeProperties can be added and compared with less thanless than or
equal.
@brief Implement breadthfirst search.
template
void breadthfirstsearchconst Graph& g ParentMap& p
typedef typename Graph::vertexdescriptor vertexdescriptor;
typedef typename Graph::edgedescriptor edgedescriptor;
typedef typename Graph::constvertexiterator vertexiterator;
typedef typename Graph::constedgeiterator edgeiterator;
typedef typename Graph::constadjedgeiterator adjedgeiterator;
setup
std::queue q;
std::unorderedset edgesunexplored;
std::set edgesunexplored;
std::unorderedset verticesunexplored;
initialize
pclear;
forvertexiterator vi gverticescbegin; vi gverticescend; vi
vertexdescriptor vd videscriptor;
verticesunexplored.emplacevd;
pvd;
foredgeiterator ei gedgescbegin; ei gedgescend; ei
edgesunexplored.emplaceeidescriptor;
for each CC
forvertexiterator vi gverticescbegin; vi gverticescend; vi
vertexdescriptor vd videscriptor;
ifverticesunexplored.countvd
qpushvd;
verticesunexplored.erasevd;
whileqempty
vertexdescriptor vd qfront;
qpop;
auto& v gfindvertexvd;
foradjedgeiterator aei vbegin; aei vend; aei
auto el edgesunexplored.findaeidescriptor;
ifel edgesunexplored.end
vertexdescriptor t aeitarget;
ifverticesunexplored.countt
discovery edge
edgesunexplored.eraseel;
pt vdescriptor;
qpusht;
verticesunexplored.eraset;
else cross edge
@todo Implement depthfirst search.
template
void depthfirstsearchconst Graph& g ParentMap& p
#endif
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
