maps.dox
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 01 Mar 2010 02:30:00 +0100
changeset 58 10b6a5b7d4c0
parent 57 18404ec968ca
permissions -rw-r--r--
Improve Algorithms section (it is still under construction)
kpeter@46
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@46
     2
 *
kpeter@46
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@46
     4
 *
kpeter@46
     5
 * Copyright (C) 2003-2010
kpeter@46
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@46
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@46
     8
 *
kpeter@46
     9
 * Permission to use, modify and distribute this software is granted
kpeter@46
    10
 * provided that this copyright notice appears in all copies. For
kpeter@46
    11
 * precise terms see the accompanying LICENSE file.
kpeter@46
    12
 *
kpeter@46
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@46
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@46
    15
 * purpose.
kpeter@46
    16
 *
kpeter@46
    17
 */
kpeter@46
    18
kpeter@46
    19
namespace lemon {
kpeter@46
    20
/**
kpeter@46
    21
[PAGE]sec_maps[PAGE] Maps
kpeter@46
    22
kpeter@46
    23
\todo This page is under construction.
kpeter@46
    24
kpeter@49
    25
\todo The following contents are ported from the LEMON 0.x tutorial,
kpeter@57
    26
thus they have to be thoroughly revised and reworked.
kpeter@57
    27
kpeter@57
    28
\warning Currently, this section may contain old or faulty contents.
kpeter@49
    29
kpeter@49
    30
The LEMON maps are not only just storage classes, but also
kpeter@49
    31
they are %concepts of any key--value based data access.
kpeter@49
    32
Beside the standard digraph maps, LEMON contains several "lightweight"
kpeter@49
    33
\e map \e adaptor \e classes, which perform various operations on the
kpeter@49
    34
data of the adapted maps when their access operations are called,
kpeter@49
    35
but without actually copying or modifying the original storage.
kpeter@49
    36
These classes also conform to the map %concepts, thus they can be used
kpeter@49
    37
like standard LEMON maps.
kpeter@49
    38
kpeter@49
    39
Maps play a central role in LEMON. As their name suggests, they map a
kpeter@49
    40
certain range of \e keys to certain \e values. Each map has two
kpeter@49
    41
<tt>typedef</tt>'s to determine the types of keys and values, like this:
kpeter@49
    42
kpeter@49
    43
\code
kpeter@49
    44
  typedef Arc Key;
kpeter@49
    45
  typedef double Value;
kpeter@49
    46
\endcode
kpeter@49
    47
kpeter@49
    48
A map can be 
kpeter@49
    49
\e readable (\ref lemon::concepts::ReadMap "ReadMap", for short),
kpeter@49
    50
\e writable (\ref lemon::concepts::WriteMap "WriteMap") or both
kpeter@49
    51
(\ref lemon::concepts::ReadWriteMap "ReadWriteMap").
kpeter@49
    52
There also exists a special type of
kpeter@49
    53
ReadWrite map called \ref lemon::concepts::ReferenceMap "reference map".
kpeter@49
    54
In addition that you can
kpeter@49
    55
read and write the values of a key, a reference map
kpeter@49
    56
can also give you a reference to the
kpeter@49
    57
value belonging to a key, so you have a direct access to the memory address
kpeter@49
    58
where it is stored.
kpeter@49
    59
kpeter@49
    60
Each digraph structure in LEMON provides two standard map templates called
kpeter@49
    61
\c ArcMap and \c NodeMap. Both are reference maps and you can easily
kpeter@49
    62
assign data to the nodes and to the arcs of the digraph. For example if you
kpeter@49
    63
have a digraph \c g defined as
kpeter@49
    64
\code
kpeter@49
    65
  ListDigraph g;
kpeter@49
    66
\endcode
kpeter@49
    67
and you want to assign a floating point value to each arc, you can do
kpeter@49
    68
it like this.
kpeter@49
    69
\code
kpeter@49
    70
  ListDigraph::ArcMap<double> length(g);
kpeter@49
    71
\endcode
kpeter@49
    72
Note that you must give the underlying digraph to the constructor.
kpeter@49
    73
kpeter@49
    74
The value of a readable map can be obtained by <tt>operator[]</tt>.
kpeter@49
    75
\code
kpeter@49
    76
  d=length[e];
kpeter@49
    77
\endcode
kpeter@49
    78
where \c e is an instance of \c ListDigraph::Arc.
kpeter@49
    79
(Or anything else
kpeter@49
    80
that converts to \c ListDigraph::Arc, like  \c ListDigraph::ArcIt or
kpeter@49
    81
\c ListDigraph::OutArcIt etc.)
kpeter@49
    82
kpeter@49
    83
There are two ways to assign a new value to a key
kpeter@49
    84
kpeter@49
    85
- In case of a <em>reference map</em> <tt>operator[]</tt>
kpeter@49
    86
gives you a reference to the
kpeter@49
    87
value, thus you can use this.
kpeter@49
    88
\code
kpeter@49
    89
  length[e]=3.5;
kpeter@49
    90
\endcode
kpeter@49
    91
- <em>Writable maps</em> have
kpeter@49
    92
a member function \c set(Key,const Value &)
kpeter@49
    93
for this purpose.
kpeter@49
    94
\code
kpeter@49
    95
  length.set(e,3.5);
kpeter@49
    96
\endcode
kpeter@49
    97
kpeter@49
    98
The first case is more comfortable and if you store complex structures in your
kpeter@49
    99
map, it might be more efficient. However, there are writable but
kpeter@49
   100
not reference maps, so if you want to write a generic algorithm, you should
kpeter@49
   101
insist on the second way.
kpeter@49
   102
kpeter@49
   103
\section how-to-write-your-own-map How to Write Your Own Maps
kpeter@49
   104
kpeter@49
   105
\subsection read-maps Readable Maps
kpeter@49
   106
kpeter@49
   107
Readable maps are very frequently used as the input of an
kpeter@49
   108
algorithm.  For this purpose the most straightforward way is the use of the
kpeter@49
   109
default maps provided by LEMON's digraph structures.
kpeter@49
   110
Very often however, it is more
kpeter@49
   111
convenient and/or more efficient to write your own readable map.
kpeter@49
   112
kpeter@49
   113
You can find some examples below. In these examples \c Digraph is the
kpeter@49
   114
type of the particular digraph structure you use.
kpeter@49
   115
kpeter@49
   116
kpeter@49
   117
This simple map assigns \f$\pi\f$ to each arc.
kpeter@49
   118
kpeter@49
   119
\code
kpeter@49
   120
struct MyMap 
kpeter@49
   121
{
kpeter@49
   122
  typedef double Value;
kpeter@49
   123
  typedef Digraph::Arc Key;
kpeter@49
   124
  double operator[](Key e) const { return PI;}
kpeter@49
   125
};
kpeter@49
   126
\endcode
kpeter@49
   127
kpeter@49
   128
An alternative way to define maps is to use \c MapBase
kpeter@49
   129
kpeter@49
   130
\code
kpeter@49
   131
struct MyMap : public MapBase<Digraph::Arc,double>
kpeter@49
   132
{
kpeter@49
   133
  Value operator[](Key e) const { return PI;}
kpeter@49
   134
};
kpeter@49
   135
\endcode
kpeter@49
   136
kpeter@49
   137
Here is a bit more complex example.
kpeter@49
   138
It provides a length function obtained
kpeter@49
   139
from a base length function shifted by a potential difference.
kpeter@49
   140
kpeter@49
   141
\code
kpeter@49
   142
class ReducedLengthMap  : public MapBase<Digraph::Arc,double>
kpeter@49
   143
{
kpeter@49
   144
  const Digraph &g;
kpeter@49
   145
  const Digraph::ArcMap<double> &orig_len;
kpeter@49
   146
  const Digraph::NodeMap<double> &pot;
kpeter@49
   147
  
kpeter@49
   148
public:
kpeter@49
   149
  Value operator[](Key e) const {
kpeter@49
   150
    return orig_len[e]-(pot[g.target(e)]-pot[g.source(e)]);
kpeter@49
   151
  }
kpeter@49
   152
  
kpeter@49
   153
  ReducedLengthMap(const Digraph &_g,
kpeter@49
   154
                   const Digraph::ArcMap &_o,
kpeter@49
   155
                   const Digraph::NodeMap &_p)
kpeter@49
   156
    : g(_g), orig_len(_o), pot(_p) {};
kpeter@49
   157
};
kpeter@49
   158
\endcode
kpeter@49
   159
kpeter@49
   160
Then, you can call e.g. Dijkstra algoritm on this map like this:
kpeter@49
   161
\code
kpeter@49
   162
  ...
kpeter@49
   163
  ReducedLengthMap rm(g,len,pot);
kpeter@49
   164
  Dijkstra<Digraph,ReducedLengthMap> dij(g,rm);
kpeter@49
   165
  dij.run(s);
kpeter@49
   166
  ...
kpeter@49
   167
\endcode
kpeter@49
   168
kpeter@49
   169
kpeter@49
   170
In the previous section we discussed digraph topology. That is the skeleton a complex
kpeter@49
   171
digraph represented data-set needs. But how to assign the data itself to that skeleton?<br>
kpeter@49
   172
Here come the \b maps in.
kpeter@49
   173
kpeter@49
   174
\section maps_intro Introduction to maps
kpeter@49
   175
Maps play a central role in LEMON. As their name suggests, they map a certain range of <i>keys</i> to certain <i>values</i>.
kpeter@49
   176
In LEMON there is many types of maps. Each map has two typedef's to determine the types of keys and values, like this:
kpeter@49
   177
\code
kpeter@49
   178
  typedef Arc Key;
kpeter@49
   179
  typedef double Value;
kpeter@49
   180
\endcode
kpeter@49
   181
(Except matrix maps, they have two key types.)
kpeter@49
   182
kpeter@49
   183
To make easy to use them - especially as template parameters - there are <i>map concepts</i> like by digraph classes.
kpeter@49
   184
<ul>
kpeter@49
   185
<li>\ref concepts::ReadMap "ReadMap" - values can be read out with the \c operator[].
kpeter@49
   186
\code value_typed_variable = map_instance[key_value]; \endcode
kpeter@49
   187
</li>
kpeter@49
   188
<li>\ref concepts::WriteMap "WriteMap" - values can be set with the \c set() member function.
kpeter@49
   189
\code map_instance.set(key_value, value_typed_expression); \endcode
kpeter@49
   190
</li>
kpeter@49
   191
<li>\ref concepts::ReadWriteMap "ReadWriteMap" - it's just a shortcut to indicate that the map is both
kpeter@49
   192
readable and writable. It is delivered from them.
kpeter@49
   193
</li>
kpeter@49
   194
<li>\ref concepts::ReferenceMap "ReferenceMap" - a subclass of ReadWriteMap. It has two additional typedefs
kpeter@49
   195
<i>Reference</i> and <i>ConstReference</i> and two overloads of \c operator[] to
kpeter@49
   196
providing you constant or non-constant reference to the value belonging to a key,
kpeter@49
   197
so you have a direct access to the memory address where it is stored.
kpeter@49
   198
</li>
kpeter@49
   199
<li>And there are the Matrix version of these maps, where the values are assigned to a pair of keys.
kpeter@49
   200
The keys can be different types. (\ref concepts::ReadMatrixMap "ReadMatrixMap", 
kpeter@49
   201
\ref concepts::WriteMatrixMap "WriteMatrixMap", \ref concepts::ReadWriteMatrixMap "ReadWriteMatrixMap",
kpeter@49
   202
\ref concepts::ReferenceMatrixMap "ReferenceMatrixMap")
kpeter@49
   203
</li>
kpeter@49
   204
</ul>
kpeter@49
   205
kpeter@49
   206
\section maps_graph Digraphs' maps
kpeter@49
   207
Every \ref MappableDigraphComponent "mappable" digraph class has two public templates: NodeMap<VALUE> and ArcMap<VALUE>
kpeter@49
   208
satisfying the \ref DigraphMap concept.
kpeter@49
   209
If you want to assign data to nodes, just declare a NodeMap with the corresponding
kpeter@49
   210
type. As an example, think of a arc-weighted digraph.
kpeter@49
   211
\code ListDigraph::ArcMap<int>  weight(digraph); \endcode
kpeter@49
   212
You can see that the map needs the digraph whose arcs will mapped, but nothing more.
kpeter@49
   213
kpeter@49
   214
If the digraph class is extendable or erasable the map will automatically follow
kpeter@49
   215
the changes you make. If a new node is added a default value is mapped to it.
kpeter@49
   216
You can define the default value by passing a second argument to the map's constructor.
kpeter@49
   217
\code ListDigraph::ArcMap<int>  weight(digraph, 13); \endcode
kpeter@49
   218
But keep in mind that \c VALUE has to have copy constructor.
kpeter@49
   219
kpeter@49
   220
Of course \c VALUE can be a rather complex type.
kpeter@49
   221
kpeter@49
   222
For practice let's see the following template function (from \ref maps_summary "maps-summary.cc" in the \ref demo directory)!
kpeter@49
   223
\dontinclude maps_summary.cc
kpeter@49
   224
\skip template
kpeter@49
   225
\until }
kpeter@49
   226
The task is simple. We need the summary of some kind of data assigned to a digraph's nodes.
kpeter@49
   227
(Whit a little trick the summary can be calculated only to a sub-digraph without changing
kpeter@49
   228
this code. See \ref SubDigraph techniques - that's LEMON's true potential.)
kpeter@49
   229
kpeter@49
   230
And the usage is simpler than the declaration suggests. The compiler deduces the
kpeter@49
   231
template specialization, so the usage is like a simple function call.
kpeter@49
   232
\skip std
kpeter@49
   233
\until ;
kpeter@49
   234
kpeter@49
   235
Most of the time you will probably use digraph maps, but keep in mind, that in LEMON maps are more general and can be used widely.
kpeter@49
   236
kpeter@49
   237
If you want some 'real-life' examples see the next page, where we discuss \ref algorithms
kpeter@49
   238
(coming soon) and will use maps hardly.
kpeter@49
   239
Or if you want to know more about maps read these \ref maps2 "advanced map techniques".
kpeter@49
   240
kpeter@49
   241
Here we discuss some advanced map techniques. Like writing your own maps or how to
kpeter@49
   242
extend/modify a maps functionality with adaptors.
kpeter@49
   243
kpeter@49
   244
\section custom_maps Writing Custom ReadMap
kpeter@49
   245
\subsection custom_read_maps Readable Maps
kpeter@49
   246
kpeter@49
   247
Readable maps are very frequently used as the input of an
kpeter@49
   248
algorithm.  For this purpose the most straightforward way is the use of the
kpeter@49
   249
default maps provided by LEMON's digraph structures.
kpeter@49
   250
Very often however, it is more
kpeter@49
   251
convenient and/or more efficient to write your own readable map.
kpeter@49
   252
kpeter@49
   253
You can find some examples below. In these examples \c Digraph is the
kpeter@49
   254
type of the particular digraph structure you use.
kpeter@49
   255
kpeter@49
   256
kpeter@49
   257
This simple map assigns \f$\pi\f$ to each arc.
kpeter@49
   258
kpeter@49
   259
\code
kpeter@49
   260
struct MyMap 
kpeter@49
   261
{
kpeter@49
   262
  typedef double Value;
kpeter@49
   263
  typedef Digraph::Arc Key;
kpeter@49
   264
  double operator[](const Key &e) const { return PI;}
kpeter@49
   265
};
kpeter@49
   266
\endcode
kpeter@49
   267
kpeter@49
   268
An alternative way to define maps is to use MapBase
kpeter@49
   269
kpeter@49
   270
\code
kpeter@49
   271
struct MyMap : public MapBase<Digraph::Arc,double>
kpeter@49
   272
{
kpeter@49
   273
  Value operator[](const Key& e) const { return PI;}
kpeter@49
   274
};
kpeter@49
   275
\endcode
kpeter@49
   276
kpeter@49
   277
Here is a bit more complex example.
kpeter@49
   278
It provides a length function obtained
kpeter@49
   279
from a base length function shifted by a potential difference.
kpeter@49
   280
kpeter@49
   281
\code
kpeter@49
   282
class ReducedLengthMap  : public MapBase<Digraph::Arc,double>
kpeter@49
   283
{
kpeter@49
   284
  const Digraph &g;
kpeter@49
   285
  const Digraph::ArcMap<double> &orig_len;
kpeter@49
   286
  const Digraph::NodeMap<double> &pot;
kpeter@49
   287
  
kpeter@49
   288
public:
kpeter@49
   289
  Value operator[](Key e) const {
kpeter@49
   290
    return orig_len[e]-(pot[g.target(e)]-pot[g.source(e)]);
kpeter@49
   291
  }
kpeter@49
   292
  
kpeter@49
   293
  ReducedLengthMap(const Digraph &_g,
kpeter@49
   294
                   const Digraph::ArcMap &_o,
kpeter@49
   295
                   const Digraph::NodeMap &_p)
kpeter@49
   296
    : g(_g), orig_len(_o), pot(_p) {};
kpeter@49
   297
};
kpeter@49
   298
\endcode
kpeter@49
   299
kpeter@49
   300
Then, you can call e.g. Dijkstra algoritm on this map like this:
kpeter@49
   301
\code
kpeter@49
   302
  ...
kpeter@49
   303
  ReducedLengthMap rm(g,len,pot);
kpeter@49
   304
  Dijkstra<Digraph,ReducedLengthMap> dij(g,rm);
kpeter@49
   305
  dij.run(s);
kpeter@49
   306
  ...
kpeter@49
   307
\endcode
kpeter@49
   308
kpeter@49
   309
kpeter@46
   310
[SEC]sec_map_concepts[SEC] Map Concepts
kpeter@46
   311
kpeter@46
   312
...
kpeter@46
   313
kpeter@46
   314
kpeter@46
   315
[SEC]sec_own_maps[SEC] Creating Own Maps
kpeter@46
   316
kpeter@46
   317
...
kpeter@46
   318
kpeter@46
   319
[SEC]sec_map_adaptors[SEC] Map Adaptors
kpeter@46
   320
kpeter@46
   321
See \ref map_adaptors in the reference manual.
kpeter@46
   322
kpeter@46
   323
kpeter@46
   324
[SEC]sec_algs_with_maps[SEC] Using Algorithms with Special Maps
kpeter@46
   325
kpeter@58
   326
kpeter@49
   327
The basic functionality of the algorithms can be highly extended using
kpeter@49
   328
special purpose map types for their internal data structures.
kpeter@49
   329
For example, the \ref Dijkstra class stores a 
kpeter@49
   330
ef ProcessedMap,
kpeter@49
   331
which has to be a writable node map of \ref bool value type.
kpeter@49
   332
The assigned value of each node is set to \ref true when the node is
kpeter@49
   333
processed, i.e., its actual distance is found.
kpeter@49
   334
Applying a special map, \ref LoggerBoolMap, the processed order of
kpeter@49
   335
the nodes can easily be stored in a standard container.
kpeter@49
   336
kpeter@49
   337
Such specific map types can be passed to the algorithms using the technique of
kpeter@49
   338
named template parameters. Similarly to the named function parameters,
kpeter@49
   339
they allow specifying any subset of the parameters and in arbitrary order.
kpeter@49
   340
kpeter@58
   341
Let us suppose that we have a traffic network stored in a LEMON digraph
kpeter@58
   342
structure with two arc maps \c length and \c speed, which
kpeter@58
   343
denote the physical length of each arc and the maximum (or average)
kpeter@58
   344
speed that can be achieved on the corresponding road-section,
kpeter@58
   345
respectively. If we are interested in the best traveling times,
kpeter@58
   346
the following code can be used.
kpeter@58
   347
kpeter@58
   348
\code
kpeter@58
   349
  dijkstra(g, divMap(length, speed)).distMap(dist).run(s);
kpeter@58
   350
\endcode
kpeter@58
   351
kpeter@49
   352
\code
kpeter@49
   353
  typedef vector<ListDigraph::Node> Container;
kpeter@49
   354
  typedef back_insert_iterator<Container> InsertIterator;
kpeter@49
   355
  typedef LoggerBoolMap<InsertIterator> ProcessedMap;
kpeter@49
   356
  Dijkstra<ListDigraph>
kpeter@49
   357
    ::SetProcessedMap<ProcessedMap>
kpeter@49
   358
    ::Create dijktra(g, length);
kpeter@49
   359
kpeter@49
   360
  Container container;
kpeter@49
   361
  InsertIterator iterator(container);
kpeter@49
   362
  ProcessedMap processed(iterator);
kpeter@49
   363
kpeter@49
   364
  dijkstra.processedMap(processed).run(s);
kpeter@49
   365
\endcode
kpeter@49
   366
kpeter@49
   367
The function-type interfaces are considerably simpler, but they can be
kpeter@49
   368
used in almost all practical cases. Surprisingly, even the above example
kpeter@49
   369
can also be implemented using the \ref dijkstra() function and
kpeter@49
   370
named parameters, as follows.
kpeter@49
   371
Note that the function-type interface has the major advantage
kpeter@49
   372
that temporary objects can be passed as parameters.
kpeter@49
   373
kpeter@49
   374
\code
kpeter@49
   375
  vector<ListDigraph::Node> process_order;
kpeter@49
   376
  dijkstra(g, length)
kpeter@49
   377
    .processedMap(loggerBoolMap(back_inserter(process_order)))
kpeter@49
   378
    .run(s);
kpeter@49
   379
\endcode
kpeter@49
   380
kpeter@58
   381
Let us see a bit more complex example to demonstrate Dfs's capabilities.
kpeter@58
   382
kpeter@58
   383
We will see a program, which solves the problem of <b>topological ordering</b>.
kpeter@58
   384
We need to know in which order we should put on our clothes. The program will do the following:
kpeter@58
   385
<ol>
kpeter@58
   386
<li>We run the dfs algorithm to all nodes.
kpeter@58
   387
<li>Put every node into a list when processed completely.
kpeter@58
   388
<li>Write out the list in reverse order.
kpeter@58
   389
</ol>
kpeter@58
   390
kpeter@58
   391
\dontinclude topological_ordering.cc
kpeter@58
   392
First of all we will need an own \ref lemon::Dfs::ProcessedMap "ProcessedMap". The ordering
kpeter@58
   393
will be done through it.
kpeter@58
   394
\skip MyOrdererMap
kpeter@58
   395
\until };
kpeter@58
   396
The class meets the \ref concepts::WriteMap "WriteMap" concept. In it's \c set() method the only thing
kpeter@58
   397
we need to do is insert the key - that is the node whose processing just finished - into the beginning
kpeter@58
   398
of the list.<br>
kpeter@58
   399
Although we implemented this needed helper class ourselves it was not necessary.
kpeter@58
   400
The \ref lemon::FrontInserterBoolMap "FrontInserterBoolMap" class does exactly
kpeter@58
   401
what we needed. To be correct it's more general - and it's all in \c LEMON. But
kpeter@58
   402
we wanted to show you, how easy is to add additional functionality.
kpeter@58
   403
kpeter@58
   404
First we declare the needed data structures: the digraph and a map to store the nodes' label.
kpeter@58
   405
\skip ListDigraph
kpeter@58
   406
\until label
kpeter@58
   407
kpeter@58
   408
Now we build a digraph. But keep in mind that it must be DAG because cyclic digraphs has no topological
kpeter@58
   409
ordering.
kpeter@58
   410
\skip belt
kpeter@58
   411
\until trousers
kpeter@58
   412
We label them...
kpeter@58
   413
\skip label
kpeter@58
   414
\until trousers
kpeter@58
   415
Then add arcs which represent the precedences between those items.
kpeter@58
   416
\skip trousers, belt
kpeter@58
   417
\until );
kpeter@58
   418
kpeter@58
   419
See how easy is to access the internal information of this algorithm trough maps.
kpeter@58
   420
We only need to set our own map as the class's \ref lemon::Dfs::ProcessedMap "ProcessedMap".
kpeter@58
   421
\skip Dfs
kpeter@58
   422
\until run
kpeter@58
   423
kpeter@58
   424
And now comes the third part. Write out the list in reverse order. But the list was
kpeter@58
   425
composed in reverse way (with \c push_front() instead of \c push_back() so we just iterate it.
kpeter@58
   426
\skip std
kpeter@58
   427
\until endl
kpeter@58
   428
kpeter@58
   429
The program is to be found in the \ref demo directory: \ref topological_ordering.cc
kpeter@58
   430
kpeter@49
   431
LEMON also contains visitor based algorithm classes for
kpeter@49
   432
BFS and DFS.
kpeter@49
   433
kpeter@49
   434
Skeleton visitor classes are defined for both BFS and DFS, the concrete
kpeter@49
   435
implementations can be inherited from them.
kpeter@49
   436
\code
kpeter@49
   437
  template <typename GR>
kpeter@49
   438
  struct DfsVisitor {
kpeter@49
   439
    void start(const typename GR::Node& node) {}
kpeter@49
   440
    void stop(const typename GR::Node& node) {}
kpeter@49
   441
    void reach(const typename GR::Node& node) {}
kpeter@49
   442
    void leave(const typename GR::Node& node) {}
kpeter@49
   443
    void discover(const typename GR::Arc& arc) {}
kpeter@49
   444
    void examine(const typename GR::Arc& arc) {}
kpeter@49
   445
    void backtrack(const typename GR::Arc& arc) {}
kpeter@49
   446
  };
kpeter@49
   447
\endcode
kpeter@49
   448
kpeter@49
   449
In the following example, the \ref discover()} and \code{examine()
kpeter@49
   450
events are processed and the DFS tree is stored in an arc map.
kpeter@49
   451
The values of this map indicate whether the corresponding arc
kpeter@49
   452
reaches a new node or its target node is already reached.
kpeter@49
   453
\code
kpeter@49
   454
  template <typename GR>
kpeter@49
   455
  struct TreeVisitor : public DfsVisitor<GR> {
kpeter@49
   456
    TreeVisitor(typename GR::ArcMap<bool>& tree)
kpeter@49
   457
      : _tree(tree) {}
kpeter@49
   458
    void discover(const typename GR::Arc& arc)
kpeter@49
   459
      { _tree[arc] = true; }
kpeter@49
   460
    void examine(const typename GR::Arc& arc)
kpeter@49
   461
      { _tree[arc] = false; }
kpeter@49
   462
    typename GR::ArcMap<bool>& _tree;
kpeter@49
   463
  };
kpeter@49
   464
\endcode
kpeter@49
   465
kpeter@46
   466
kpeter@46
   467
[TRAILER]
kpeter@46
   468
*/
kpeter@46
   469
}