maps.dox
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 01 Mar 2010 02:28:44 +0100
changeset 57 18404ec968ca
parent 49 c8c5a2a4ec71
child 58 10b6a5b7d4c0
permissions -rw-r--r--
Various small fixes
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
Let us suppose that we have a traffic network stored in a LEMON digraph
kpeter@49
    40
structure with two arc maps \c length and \c speed, which
kpeter@49
    41
denote the physical length of each arc and the maximum (or average)
kpeter@49
    42
speed that can be achieved on the corresponding road-section,
kpeter@49
    43
respectively. If we are interested in the best traveling times,
kpeter@49
    44
the following code can be used.
kpeter@49
    45
kpeter@49
    46
\code
kpeter@49
    47
  dijkstra(g, divMap(length, speed)).distMap(dist).run(s);
kpeter@49
    48
\endcode
kpeter@49
    49
kpeter@49
    50
kpeter@49
    51
Maps play a central role in LEMON. As their name suggests, they map a
kpeter@49
    52
certain range of \e keys to certain \e values. Each map has two
kpeter@49
    53
<tt>typedef</tt>'s to determine the types of keys and values, like this:
kpeter@49
    54
kpeter@49
    55
\code
kpeter@49
    56
  typedef Arc Key;
kpeter@49
    57
  typedef double Value;
kpeter@49
    58
\endcode
kpeter@49
    59
kpeter@49
    60
A map can be 
kpeter@49
    61
\e readable (\ref lemon::concepts::ReadMap "ReadMap", for short),
kpeter@49
    62
\e writable (\ref lemon::concepts::WriteMap "WriteMap") or both
kpeter@49
    63
(\ref lemon::concepts::ReadWriteMap "ReadWriteMap").
kpeter@49
    64
There also exists a special type of
kpeter@49
    65
ReadWrite map called \ref lemon::concepts::ReferenceMap "reference map".
kpeter@49
    66
In addition that you can
kpeter@49
    67
read and write the values of a key, a reference map
kpeter@49
    68
can also give you a reference to the
kpeter@49
    69
value belonging to a key, so you have a direct access to the memory address
kpeter@49
    70
where it is stored.
kpeter@49
    71
kpeter@49
    72
Each digraph structure in LEMON provides two standard map templates called
kpeter@49
    73
\c ArcMap and \c NodeMap. Both are reference maps and you can easily
kpeter@49
    74
assign data to the nodes and to the arcs of the digraph. For example if you
kpeter@49
    75
have a digraph \c g defined as
kpeter@49
    76
\code
kpeter@49
    77
  ListDigraph g;
kpeter@49
    78
\endcode
kpeter@49
    79
and you want to assign a floating point value to each arc, you can do
kpeter@49
    80
it like this.
kpeter@49
    81
\code
kpeter@49
    82
  ListDigraph::ArcMap<double> length(g);
kpeter@49
    83
\endcode
kpeter@49
    84
Note that you must give the underlying digraph to the constructor.
kpeter@49
    85
kpeter@49
    86
The value of a readable map can be obtained by <tt>operator[]</tt>.
kpeter@49
    87
\code
kpeter@49
    88
  d=length[e];
kpeter@49
    89
\endcode
kpeter@49
    90
where \c e is an instance of \c ListDigraph::Arc.
kpeter@49
    91
(Or anything else
kpeter@49
    92
that converts to \c ListDigraph::Arc, like  \c ListDigraph::ArcIt or
kpeter@49
    93
\c ListDigraph::OutArcIt etc.)
kpeter@49
    94
kpeter@49
    95
There are two ways to assign a new value to a key
kpeter@49
    96
kpeter@49
    97
- In case of a <em>reference map</em> <tt>operator[]</tt>
kpeter@49
    98
gives you a reference to the
kpeter@49
    99
value, thus you can use this.
kpeter@49
   100
\code
kpeter@49
   101
  length[e]=3.5;
kpeter@49
   102
\endcode
kpeter@49
   103
- <em>Writable maps</em> have
kpeter@49
   104
a member function \c set(Key,const Value &)
kpeter@49
   105
for this purpose.
kpeter@49
   106
\code
kpeter@49
   107
  length.set(e,3.5);
kpeter@49
   108
\endcode
kpeter@49
   109
kpeter@49
   110
The first case is more comfortable and if you store complex structures in your
kpeter@49
   111
map, it might be more efficient. However, there are writable but
kpeter@49
   112
not reference maps, so if you want to write a generic algorithm, you should
kpeter@49
   113
insist on the second way.
kpeter@49
   114
kpeter@49
   115
\section how-to-write-your-own-map How to Write Your Own Maps
kpeter@49
   116
kpeter@49
   117
\subsection read-maps Readable Maps
kpeter@49
   118
kpeter@49
   119
Readable maps are very frequently used as the input of an
kpeter@49
   120
algorithm.  For this purpose the most straightforward way is the use of the
kpeter@49
   121
default maps provided by LEMON's digraph structures.
kpeter@49
   122
Very often however, it is more
kpeter@49
   123
convenient and/or more efficient to write your own readable map.
kpeter@49
   124
kpeter@49
   125
You can find some examples below. In these examples \c Digraph is the
kpeter@49
   126
type of the particular digraph structure you use.
kpeter@49
   127
kpeter@49
   128
kpeter@49
   129
This simple map assigns \f$\pi\f$ to each arc.
kpeter@49
   130
kpeter@49
   131
\code
kpeter@49
   132
struct MyMap 
kpeter@49
   133
{
kpeter@49
   134
  typedef double Value;
kpeter@49
   135
  typedef Digraph::Arc Key;
kpeter@49
   136
  double operator[](Key e) const { return PI;}
kpeter@49
   137
};
kpeter@49
   138
\endcode
kpeter@49
   139
kpeter@49
   140
An alternative way to define maps is to use \c MapBase
kpeter@49
   141
kpeter@49
   142
\code
kpeter@49
   143
struct MyMap : public MapBase<Digraph::Arc,double>
kpeter@49
   144
{
kpeter@49
   145
  Value operator[](Key e) const { return PI;}
kpeter@49
   146
};
kpeter@49
   147
\endcode
kpeter@49
   148
kpeter@49
   149
Here is a bit more complex example.
kpeter@49
   150
It provides a length function obtained
kpeter@49
   151
from a base length function shifted by a potential difference.
kpeter@49
   152
kpeter@49
   153
\code
kpeter@49
   154
class ReducedLengthMap  : public MapBase<Digraph::Arc,double>
kpeter@49
   155
{
kpeter@49
   156
  const Digraph &g;
kpeter@49
   157
  const Digraph::ArcMap<double> &orig_len;
kpeter@49
   158
  const Digraph::NodeMap<double> &pot;
kpeter@49
   159
  
kpeter@49
   160
public:
kpeter@49
   161
  Value operator[](Key e) const {
kpeter@49
   162
    return orig_len[e]-(pot[g.target(e)]-pot[g.source(e)]);
kpeter@49
   163
  }
kpeter@49
   164
  
kpeter@49
   165
  ReducedLengthMap(const Digraph &_g,
kpeter@49
   166
                   const Digraph::ArcMap &_o,
kpeter@49
   167
                   const Digraph::NodeMap &_p)
kpeter@49
   168
    : g(_g), orig_len(_o), pot(_p) {};
kpeter@49
   169
};
kpeter@49
   170
\endcode
kpeter@49
   171
kpeter@49
   172
Then, you can call e.g. Dijkstra algoritm on this map like this:
kpeter@49
   173
\code
kpeter@49
   174
  ...
kpeter@49
   175
  ReducedLengthMap rm(g,len,pot);
kpeter@49
   176
  Dijkstra<Digraph,ReducedLengthMap> dij(g,rm);
kpeter@49
   177
  dij.run(s);
kpeter@49
   178
  ...
kpeter@49
   179
\endcode
kpeter@49
   180
kpeter@49
   181
kpeter@49
   182
In the previous section we discussed digraph topology. That is the skeleton a complex
kpeter@49
   183
digraph represented data-set needs. But how to assign the data itself to that skeleton?<br>
kpeter@49
   184
Here come the \b maps in.
kpeter@49
   185
kpeter@49
   186
\section maps_intro Introduction to maps
kpeter@49
   187
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
   188
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
   189
\code
kpeter@49
   190
  typedef Arc Key;
kpeter@49
   191
  typedef double Value;
kpeter@49
   192
\endcode
kpeter@49
   193
(Except matrix maps, they have two key types.)
kpeter@49
   194
kpeter@49
   195
To make easy to use them - especially as template parameters - there are <i>map concepts</i> like by digraph classes.
kpeter@49
   196
<ul>
kpeter@49
   197
<li>\ref concepts::ReadMap "ReadMap" - values can be read out with the \c operator[].
kpeter@49
   198
\code value_typed_variable = map_instance[key_value]; \endcode
kpeter@49
   199
</li>
kpeter@49
   200
<li>\ref concepts::WriteMap "WriteMap" - values can be set with the \c set() member function.
kpeter@49
   201
\code map_instance.set(key_value, value_typed_expression); \endcode
kpeter@49
   202
</li>
kpeter@49
   203
<li>\ref concepts::ReadWriteMap "ReadWriteMap" - it's just a shortcut to indicate that the map is both
kpeter@49
   204
readable and writable. It is delivered from them.
kpeter@49
   205
</li>
kpeter@49
   206
<li>\ref concepts::ReferenceMap "ReferenceMap" - a subclass of ReadWriteMap. It has two additional typedefs
kpeter@49
   207
<i>Reference</i> and <i>ConstReference</i> and two overloads of \c operator[] to
kpeter@49
   208
providing you constant or non-constant reference to the value belonging to a key,
kpeter@49
   209
so you have a direct access to the memory address where it is stored.
kpeter@49
   210
</li>
kpeter@49
   211
<li>And there are the Matrix version of these maps, where the values are assigned to a pair of keys.
kpeter@49
   212
The keys can be different types. (\ref concepts::ReadMatrixMap "ReadMatrixMap", 
kpeter@49
   213
\ref concepts::WriteMatrixMap "WriteMatrixMap", \ref concepts::ReadWriteMatrixMap "ReadWriteMatrixMap",
kpeter@49
   214
\ref concepts::ReferenceMatrixMap "ReferenceMatrixMap")
kpeter@49
   215
</li>
kpeter@49
   216
</ul>
kpeter@49
   217
kpeter@49
   218
\section maps_graph Digraphs' maps
kpeter@49
   219
Every \ref MappableDigraphComponent "mappable" digraph class has two public templates: NodeMap<VALUE> and ArcMap<VALUE>
kpeter@49
   220
satisfying the \ref DigraphMap concept.
kpeter@49
   221
If you want to assign data to nodes, just declare a NodeMap with the corresponding
kpeter@49
   222
type. As an example, think of a arc-weighted digraph.
kpeter@49
   223
\code ListDigraph::ArcMap<int>  weight(digraph); \endcode
kpeter@49
   224
You can see that the map needs the digraph whose arcs will mapped, but nothing more.
kpeter@49
   225
kpeter@49
   226
If the digraph class is extendable or erasable the map will automatically follow
kpeter@49
   227
the changes you make. If a new node is added a default value is mapped to it.
kpeter@49
   228
You can define the default value by passing a second argument to the map's constructor.
kpeter@49
   229
\code ListDigraph::ArcMap<int>  weight(digraph, 13); \endcode
kpeter@49
   230
But keep in mind that \c VALUE has to have copy constructor.
kpeter@49
   231
kpeter@49
   232
Of course \c VALUE can be a rather complex type.
kpeter@49
   233
kpeter@49
   234
For practice let's see the following template function (from \ref maps_summary "maps-summary.cc" in the \ref demo directory)!
kpeter@49
   235
\dontinclude maps_summary.cc
kpeter@49
   236
\skip template
kpeter@49
   237
\until }
kpeter@49
   238
The task is simple. We need the summary of some kind of data assigned to a digraph's nodes.
kpeter@49
   239
(Whit a little trick the summary can be calculated only to a sub-digraph without changing
kpeter@49
   240
this code. See \ref SubDigraph techniques - that's LEMON's true potential.)
kpeter@49
   241
kpeter@49
   242
And the usage is simpler than the declaration suggests. The compiler deduces the
kpeter@49
   243
template specialization, so the usage is like a simple function call.
kpeter@49
   244
\skip std
kpeter@49
   245
\until ;
kpeter@49
   246
kpeter@49
   247
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
   248
kpeter@49
   249
If you want some 'real-life' examples see the next page, where we discuss \ref algorithms
kpeter@49
   250
(coming soon) and will use maps hardly.
kpeter@49
   251
Or if you want to know more about maps read these \ref maps2 "advanced map techniques".
kpeter@49
   252
kpeter@49
   253
Here we discuss some advanced map techniques. Like writing your own maps or how to
kpeter@49
   254
extend/modify a maps functionality with adaptors.
kpeter@49
   255
kpeter@49
   256
\section custom_maps Writing Custom ReadMap
kpeter@49
   257
\subsection custom_read_maps Readable Maps
kpeter@49
   258
kpeter@49
   259
Readable maps are very frequently used as the input of an
kpeter@49
   260
algorithm.  For this purpose the most straightforward way is the use of the
kpeter@49
   261
default maps provided by LEMON's digraph structures.
kpeter@49
   262
Very often however, it is more
kpeter@49
   263
convenient and/or more efficient to write your own readable map.
kpeter@49
   264
kpeter@49
   265
You can find some examples below. In these examples \c Digraph is the
kpeter@49
   266
type of the particular digraph structure you use.
kpeter@49
   267
kpeter@49
   268
kpeter@49
   269
This simple map assigns \f$\pi\f$ to each arc.
kpeter@49
   270
kpeter@49
   271
\code
kpeter@49
   272
struct MyMap 
kpeter@49
   273
{
kpeter@49
   274
  typedef double Value;
kpeter@49
   275
  typedef Digraph::Arc Key;
kpeter@49
   276
  double operator[](const Key &e) const { return PI;}
kpeter@49
   277
};
kpeter@49
   278
\endcode
kpeter@49
   279
kpeter@49
   280
An alternative way to define maps is to use MapBase
kpeter@49
   281
kpeter@49
   282
\code
kpeter@49
   283
struct MyMap : public MapBase<Digraph::Arc,double>
kpeter@49
   284
{
kpeter@49
   285
  Value operator[](const Key& e) const { return PI;}
kpeter@49
   286
};
kpeter@49
   287
\endcode
kpeter@49
   288
kpeter@49
   289
Here is a bit more complex example.
kpeter@49
   290
It provides a length function obtained
kpeter@49
   291
from a base length function shifted by a potential difference.
kpeter@49
   292
kpeter@49
   293
\code
kpeter@49
   294
class ReducedLengthMap  : public MapBase<Digraph::Arc,double>
kpeter@49
   295
{
kpeter@49
   296
  const Digraph &g;
kpeter@49
   297
  const Digraph::ArcMap<double> &orig_len;
kpeter@49
   298
  const Digraph::NodeMap<double> &pot;
kpeter@49
   299
  
kpeter@49
   300
public:
kpeter@49
   301
  Value operator[](Key e) const {
kpeter@49
   302
    return orig_len[e]-(pot[g.target(e)]-pot[g.source(e)]);
kpeter@49
   303
  }
kpeter@49
   304
  
kpeter@49
   305
  ReducedLengthMap(const Digraph &_g,
kpeter@49
   306
                   const Digraph::ArcMap &_o,
kpeter@49
   307
                   const Digraph::NodeMap &_p)
kpeter@49
   308
    : g(_g), orig_len(_o), pot(_p) {};
kpeter@49
   309
};
kpeter@49
   310
\endcode
kpeter@49
   311
kpeter@49
   312
Then, you can call e.g. Dijkstra algoritm on this map like this:
kpeter@49
   313
\code
kpeter@49
   314
  ...
kpeter@49
   315
  ReducedLengthMap rm(g,len,pot);
kpeter@49
   316
  Dijkstra<Digraph,ReducedLengthMap> dij(g,rm);
kpeter@49
   317
  dij.run(s);
kpeter@49
   318
  ...
kpeter@49
   319
\endcode
kpeter@49
   320
kpeter@49
   321
kpeter@46
   322
[SEC]sec_map_concepts[SEC] Map Concepts
kpeter@46
   323
kpeter@46
   324
...
kpeter@46
   325
kpeter@46
   326
kpeter@46
   327
[SEC]sec_own_maps[SEC] Creating Own Maps
kpeter@46
   328
kpeter@46
   329
...
kpeter@46
   330
kpeter@46
   331
[SEC]sec_map_adaptors[SEC] Map Adaptors
kpeter@46
   332
kpeter@46
   333
See \ref map_adaptors in the reference manual.
kpeter@46
   334
kpeter@46
   335
kpeter@46
   336
[SEC]sec_algs_with_maps[SEC] Using Algorithms with Special Maps
kpeter@46
   337
kpeter@49
   338
The basic functionality of the algorithms can be highly extended using
kpeter@49
   339
special purpose map types for their internal data structures.
kpeter@49
   340
For example, the \ref Dijkstra class stores a 
kpeter@49
   341
ef ProcessedMap,
kpeter@49
   342
which has to be a writable node map of \ref bool value type.
kpeter@49
   343
The assigned value of each node is set to \ref true when the node is
kpeter@49
   344
processed, i.e., its actual distance is found.
kpeter@49
   345
Applying a special map, \ref LoggerBoolMap, the processed order of
kpeter@49
   346
the nodes can easily be stored in a standard container.
kpeter@49
   347
kpeter@49
   348
Such specific map types can be passed to the algorithms using the technique of
kpeter@49
   349
named template parameters. Similarly to the named function parameters,
kpeter@49
   350
they allow specifying any subset of the parameters and in arbitrary order.
kpeter@49
   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@49
   381
LEMON also contains visitor based algorithm classes for
kpeter@49
   382
BFS and DFS.
kpeter@49
   383
kpeter@49
   384
Skeleton visitor classes are defined for both BFS and DFS, the concrete
kpeter@49
   385
implementations can be inherited from them.
kpeter@49
   386
\code
kpeter@49
   387
  template <typename GR>
kpeter@49
   388
  struct DfsVisitor {
kpeter@49
   389
    void start(const typename GR::Node& node) {}
kpeter@49
   390
    void stop(const typename GR::Node& node) {}
kpeter@49
   391
    void reach(const typename GR::Node& node) {}
kpeter@49
   392
    void leave(const typename GR::Node& node) {}
kpeter@49
   393
    void discover(const typename GR::Arc& arc) {}
kpeter@49
   394
    void examine(const typename GR::Arc& arc) {}
kpeter@49
   395
    void backtrack(const typename GR::Arc& arc) {}
kpeter@49
   396
  };
kpeter@49
   397
\endcode
kpeter@49
   398
kpeter@49
   399
In the following example, the \ref discover()} and \code{examine()
kpeter@49
   400
events are processed and the DFS tree is stored in an arc map.
kpeter@49
   401
The values of this map indicate whether the corresponding arc
kpeter@49
   402
reaches a new node or its target node is already reached.
kpeter@49
   403
\code
kpeter@49
   404
  template <typename GR>
kpeter@49
   405
  struct TreeVisitor : public DfsVisitor<GR> {
kpeter@49
   406
    TreeVisitor(typename GR::ArcMap<bool>& tree)
kpeter@49
   407
      : _tree(tree) {}
kpeter@49
   408
    void discover(const typename GR::Arc& arc)
kpeter@49
   409
      { _tree[arc] = true; }
kpeter@49
   410
    void examine(const typename GR::Arc& arc)
kpeter@49
   411
      { _tree[arc] = false; }
kpeter@49
   412
    typename GR::ArcMap<bool>& _tree;
kpeter@49
   413
  };
kpeter@49
   414
\endcode
kpeter@49
   415
kpeter@46
   416
kpeter@46
   417
[TRAILER]
kpeter@46
   418
*/
kpeter@46
   419
}