Traverses a 2D graph in different ways (enumeration, breadth-first traversal, 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 Z2i;
Point p1( -41, -36 ), p2( 18, 18 );
Object4_8 g( dt4_8, shape_set );
typedef Object4_8 Graph;
string specificStyle = p1.className() + "/Paving";
int n = 0;
for ( Graph::ConstIterator it = g.begin(), itEnd = g.end();
it != itEnd; ++it, ++n )
{
cmap_hue( n ) ) )
<< vtx;
}
board.
saveEPS(
"graphTraversal-enum.eps");
{
int nn = 0;
int mm = 0;
std::vector<Vertex> neighbors;
for ( Graph::ConstIterator it = g.begin(), itEnd = g.end();
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;
for ( VisitorRange::ConstIterator it = range.begin(), itEnd = range.end();
it != itEnd; ++it, ++n )
{
cmap_hue( n ) ) )
<< vtx;
}
board.
saveEPS(
"graphTraversal-dfs-range.eps");
return 0;
}
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
Structure representing an RGB triple with alpha component.
Aim: This class is useful to perform a depth-first exploration of a graph given a starting point or s...
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
void addColor(const Color &color)
Aim: Transforms a graph visitor into a single pass input range.
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: A utility class for constructing different shapes (balls, diamonds, and others).
void clear(const DGtal::Color &color=DGtal::Color::None)
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
DGtal is the top-level namespace which contains all DGtal functions and types.
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Modifier class in a Board2D stream. Useful to choose your own mode for a given class....
Aim: Represents the concept of local graph: each vertex has neighboring vertices, but we do not neces...
HyperRectDomain< Space > Domain
Z2i::DigitalSet DigitalSet