Traverses a 2D graph in different ways (enumeration, breadth-first traversal, depth-first traversal).
- See also
- Using graphs
# Commands
$ ./examples/graph/graphTraversal
# see files graphTraversal-enum.eps, graphTraversal-bfs.eps, graphTraversal-dfs-range.eps.
Coloring vertices of an object graph according to the enumeration order.
Coloring vertices of an object graph according to the topological distance to a seed (breadth-first traversal).
Coloring vertices of an object graph according to their order given by a depth-first traversal.
#include <iostream>
#include <vector>
#include <iterator>
#include "DGtal/io/Color.h"
#include "DGtal/io/colormaps/GradientColorMap.h"
#include "DGtal/io/colormaps/HueShadeColorMap.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/shapes/Shapes.h"
#include "DGtal/topology/Object.h"
#include "DGtal/graph/BreadthFirstVisitor.h"
#include "DGtal/graph/DepthFirstVisitor.h"
#include "DGtal/graph/GraphVisitorRange.h"
#include "DGtal/graph/CUndirectedSimpleGraph.h"
using namespace std;
{
using namespace Z2i;
Point p1( -41, -36 ), p2( 18, 18 );
string specificStyle = p1.className() + "/Paving";
int n = 0;
it != itEnd; ++it, ++n )
{
cmap_hue( n ) ) )
<< vtx;
}
board.
saveEPS(
"graphTraversal-enum.eps");
{
int nn = 0;
int mm = 0;
std::vector<Vertex> neighbors;
it != itEnd; ++it, ++nn )
{
std::back_insert_iterator< std::vector<Vertex> > neighIt
= std::back_inserter( neighbors );
g.writeNeighbors( neighIt, vtx );
mm += neighbors.size();
neighbors.clear();
}
trace.
info() <<
"Graph has " << nn <<
" vertices and "
<< (mm/2) << " edges." << std::endl;
}
typedef BFSVisitor::Node Node;
BFSVisitor bfv( g,
Point( -2, -1 ) );
while( ! bfv.finished() )
{
Node node = bfv.current();
cmap_grad( node.second ) ) )
<< node.first;
bfv.expand();
}
board.
saveEPS(
"graphTraversal-bfs.eps");
VisitorRange range(
new DFSVisitor( g,
Point( -2, -1 ) ) );
n = 0;
it != itEnd; ++it, ++n )
{
cmap_hue( n ) ) )
<< vtx;
}
board.
saveEPS(
"graphTraversal-dfs-range.eps");
return 0;
}
std::string className() const