adaptors.dox
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 21 Feb 2010 15:07:59 +0100
changeset 39 31a1a79019bb
parent 32 ef12f83752f6
child 40 e1725bb7e821
permissions -rw-r--r--
Fully rework and extend the adaptors section
kpeter@29
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@29
     2
 *
kpeter@29
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@29
     4
 *
kpeter@32
     5
 * Copyright (C) 2003-2010
kpeter@29
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@29
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@29
     8
 *
kpeter@29
     9
 * Permission to use, modify and distribute this software is granted
kpeter@29
    10
 * provided that this copyright notice appears in all copies. For
kpeter@29
    11
 * precise terms see the accompanying LICENSE file.
kpeter@29
    12
 *
kpeter@29
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@29
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@29
    15
 * purpose.
kpeter@29
    16
 *
kpeter@29
    17
 */
kpeter@29
    18
kpeter@29
    19
namespace lemon {
kpeter@29
    20
/**
kpeter@29
    21
[PAGE]sec_graph_adaptors[PAGE] Graph Adaptors
kpeter@29
    22
kpeter@39
    23
In typical algorithms and applications related to graphs and networks,
kpeter@39
    24
we usually encounter situations in which a specific alteration of a graph
kpeter@39
    25
has to be considered.
kpeter@39
    26
If some nodes or arcs have to be hidden (maybe temporarily) or the reverse
kpeter@39
    27
oriented graph has to be used, then this is the case.
kpeter@39
    28
However, actually modifing physical storage of the graph or
kpeter@39
    29
making a copy of the graph structure along with the required maps
kpeter@39
    30
could be rather expensive (in time or in memory usage) compared to the
kpeter@39
    31
operations that should be performed on the altered graph.
kpeter@39
    32
In such cases, the LEMON \e graph \e adaptor \e classes could be used.
kpeter@29
    33
kpeter@29
    34
kpeter@39
    35
[SEC]sec_reverse_digraph[SEC] Reverse Oriented Digraph
kpeter@29
    36
kpeter@39
    37
Let us suppose that we have an instance \c g of a directed graph type, say
kpeter@39
    38
\ref ListDigraph and an algorithm
kpeter@29
    39
\code
kpeter@39
    40
  template <typename Digraph>
kpeter@39
    41
  int algorithm(const Digraph&);
kpeter@29
    42
\endcode
kpeter@39
    43
is needed to run on the reverse oriented digraph.
kpeter@39
    44
In this situation, a certain adaptor class
kpeter@29
    45
\code
kpeter@39
    46
  template <typename Digraph>
kpeter@39
    47
  class ReverseDigraph;
kpeter@29
    48
\endcode
kpeter@39
    49
can be used.
kpeter@39
    50
kpeter@39
    51
The graph adaptors are special classes that serve for considering other graph
kpeter@39
    52
structures in different ways. They can be used exactly the same as "real"
kpeter@39
    53
graphs, i.e. they conform to the \ref graph_concepts "graph concepts", thus all
kpeter@39
    54
generic algorithms can be performed on them. However, the adaptor classes
kpeter@39
    55
cannot be used alone but only in conjunction with actual graph representations.
kpeter@39
    56
They do not alter the physical graph storage, they just give another view of it.
kpeter@39
    57
When the methods of the adaptors are called, they use the underlying
kpeter@39
    58
graph structures and their operations, thus these classes have only negligible
kpeter@39
    59
memory usage and do not perform sophisticated algorithmic actions.
kpeter@39
    60
kpeter@39
    61
This technique yields convenient tools that help writing compact and elegant
kpeter@39
    62
code, and makes it possible to easily implement complex algorithms based on
kpeter@39
    63
well tested standard components.
kpeter@39
    64
kpeter@39
    65
For solving the problem introduced above, we could use the follwing code.
kpeter@39
    66
kpeter@29
    67
\code
kpeter@39
    68
  ListDigraph g;
kpeter@39
    69
  ReverseDigraph<ListDigraph> rg(g);
kpeter@39
    70
  int result = algorithm(rg);
kpeter@29
    71
\endcode
kpeter@29
    72
kpeter@39
    73
Note that the original digraph \c g remains untouched during the whole
kpeter@39
    74
procedure.
kpeter@29
    75
kpeter@39
    76
LEMON also provides simple "creator functions" for the adaptor
kpeter@39
    77
classes to make their usage even simpler.
kpeter@39
    78
For example, \ref reverseDigraph() returns an instance of \ref ReverseDigraph,
kpeter@39
    79
thus the above code can be written like this.
kpeter@29
    80
kpeter@29
    81
\code
kpeter@39
    82
  ListDigraph g;
kpeter@39
    83
  int result = algorithm(reverseDigraph(g));
kpeter@29
    84
\endcode
kpeter@39
    85
kpeter@39
    86
Another essential feature of the adaptors is that their \c Node and \c Arc
kpeter@39
    87
types convert to the original item types.
kpeter@39
    88
Therefore, the maps of the original graph can be used in connection with
kpeter@39
    89
the adaptor.
kpeter@39
    90
kpeter@39
    91
In the following code, Dijksta's algorithm is run on the reverse oriented
kpeter@39
    92
graph but using the original node and arc maps.
kpeter@39
    93
kpeter@39
    94
\code
kpeter@39
    95
  ListDigraph g;
kpeter@39
    96
  ListDigraph::ArcMap length(g);
kpeter@39
    97
  ListDigraph::NodeMap dist(g);
kpeter@39
    98
kpeter@39
    99
  ListDigraph::Node s = g.addNode();
kpeter@39
   100
  // add more nodes and arcs
kpeter@39
   101
kpeter@39
   102
  dijkstra(reverseDigraph(g), length).distMap(dist).run(s);
kpeter@39
   103
\endcode
kpeter@39
   104
kpeter@39
   105
In the above examples, we used \ref ReverseDigraph in such a way that the
kpeter@39
   106
underlying digraph was not changed. However, the adaptor class can even be
kpeter@39
   107
used for modifying the original graph structure.
kpeter@39
   108
It allows adding and deleting arcs or nodes, and these operations are carried
kpeter@39
   109
out by calling suitable functions of the underlying digraph (if it supports
kpeter@39
   110
them).
kpeter@39
   111
kpeter@39
   112
For this, \ref ReverseDigraph "ReverseDigraph<GR>" has a constructor of the
kpeter@39
   113
following form.
kpeter@39
   114
\code
kpeter@39
   115
  ReverseDigraph(GR& gr);
kpeter@39
   116
\endcode
kpeter@39
   117
kpeter@39
   118
This means that in a situation, when the modification of the original graph
kpeter@39
   119
has to be avoided (e.g. it is given as a const reference), then the adaptor
kpeter@39
   120
class has to be instantiated with \c GR set to be \c const type
kpeter@39
   121
(e.g. <tt>GR = const %ListDigraph</tt>), as in the following example.
kpeter@39
   122
kpeter@29
   123
\code
kpeter@29
   124
int algorithm1(const ListDigraph& g) {
kpeter@29
   125
  ReverseDigraph<const ListDigraph> rg(g);
kpeter@29
   126
  return algorithm2(rg);
kpeter@29
   127
}
kpeter@29
   128
\endcode
kpeter@29
   129
kpeter@39
   130
\note Modification capabilities are not supported for all adaptors.
kpeter@39
   131
E.g. for \ref ResidualDigraph (see \ref sec_other_adaptors "later"),
kpeter@39
   132
this makes no sense.
kpeter@29
   133
kpeter@39
   134
As a more complex example, let us see how \ref ReverseDigraph can be used
kpeter@39
   135
together with a graph search algorithm to decide whether a directed graph is
kpeter@39
   136
strongly connected or not.
kpeter@39
   137
We exploit the fact the a digraph is strongly connected if and only if
kpeter@39
   138
for an arbitrarily selected node \c u, each other node is reachable from
kpeter@39
   139
\c u (along a directed path) and \c u is reachable from each node.
kpeter@39
   140
The latter condition is the same that each node is reachable from \c u
kpeter@39
   141
in the reversed digraph.
kpeter@29
   142
kpeter@29
   143
\code
kpeter@39
   144
  template <typename Digraph>
kpeter@39
   145
  bool stronglyConnected(const Digraph& g) {
kpeter@39
   146
    typedef typename Digraph::NodeIt NodeIt;
kpeter@39
   147
    NodeIt u(g);
kpeter@39
   148
    if (u == INVALID) return true;
kpeter@39
   149
kpeter@39
   150
    // Run BFS on the original digraph
kpeter@39
   151
    Bfs<Digraph> bfs(g);
kpeter@39
   152
    bfs.run(u);
kpeter@39
   153
    for (NodeIt n(g); n != INVALID; ++n) {
kpeter@39
   154
      if (!bfs.reached(n)) return false;
kpeter@39
   155
    }
kpeter@39
   156
kpeter@39
   157
    // Run BFS on the reverse oriented digraph
kpeter@39
   158
    typedef ReverseDigraph<const Digraph> RDigraph;
kpeter@39
   159
    RDigraph rg(g);
kpeter@39
   160
    Bfs<RDigraph> rbfs(rg);
kpeter@39
   161
    rbfs.run(u);
kpeter@39
   162
    for (NodeIt n(g); n != INVALID; ++n) {
kpeter@39
   163
      if (!rbfs.reached(n)) return false;
kpeter@39
   164
    }
kpeter@39
   165
kpeter@39
   166
    return true;
kpeter@39
   167
  }
kpeter@29
   168
\endcode
kpeter@29
   169
kpeter@39
   170
Note that we have to use the adaptor with '<tt>const Digraph</tt>' type, since
kpeter@39
   171
\c g is a \c const reference to the original graph structure.
kpeter@39
   172
The \ref stronglyConnected() function provided in LEMON has a quite
kpeter@39
   173
similar implementation.
kpeter@29
   174
kpeter@39
   175
kpeter@39
   176
[SEC]sec_subgraphs[SEC] Subgraph Adaptorts
kpeter@39
   177
kpeter@39
   178
Another typical requirement is the use of certain subgraphs of a graph,
kpeter@39
   179
or in other words, hiding nodes and/or arcs from a graph.
kpeter@39
   180
LEMON provides several convenient adaptors for these purposes.
kpeter@39
   181
kpeter@39
   182
\ref FilterArcs can be used when some arcs have to be hidden from a digraph.
kpeter@39
   183
A \e filter \e map has to be given to the constructor, which assign \c bool
kpeter@39
   184
values to the arcs specifying whether they have to be shown or not in the
kpeter@39
   185
subgraph structure.
kpeter@39
   186
Suppose we have a \ref ListDigraph structure \c g.
kpeter@39
   187
Then we can construct a subgraph in which some arcs (\c a1, \c a2 etc.)
kpeter@39
   188
are hidden as follows.
kpeter@39
   189
kpeter@39
   190
\code
kpeter@39
   191
  ListDigraph::ArcMap filter(g, true);
kpeter@39
   192
  filter[a1] = false;
kpeter@39
   193
  filter[a2] = false;
kpeter@39
   194
  // ...
kpeter@39
   195
  FilterArcs<ListDigraph> subgraph(g, filter);
kpeter@39
   196
\endcode
kpeter@39
   197
kpeter@39
   198
The following more complex code runs Dijkstra's algorithm on a digraph
kpeter@39
   199
that is obtained from another digraph by hiding all arcs having negative
kpeter@39
   200
lengths.
kpeter@39
   201
kpeter@39
   202
\code
kpeter@39
   203
  ListDigraph::ArcMap<int> length(g);
kpeter@39
   204
  ListDigraph::NodeMap<int> dist(g);
kpeter@39
   205
kpeter@39
   206
  dijkstra(filterArcs( g, lessMap(length, constMap<ListDigraph::Arc>(0)) ),
kpeter@39
   207
           length).distMap(dist).run(s);
kpeter@39
   208
\endcode
kpeter@39
   209
kpeter@39
   210
Note the extensive use of map adaptors and creator functions, which makes
kpeter@39
   211
the code really compact and elegant.
kpeter@39
   212
kpeter@39
   213
\note Implicit maps and graphs (e.g. created using functions) can only be
kpeter@39
   214
used with the function-type interfaces of the algorithms, since they store
kpeter@39
   215
only references for the used structures.
kpeter@39
   216
kpeter@39
   217
\ref FilterEdges can be used for hiding edges from an undirected graph (like
kpeter@39
   218
\ref FilterArcs is used for digraphs). \ref FilterNodes serves for filtering
kpeter@39
   219
nodes along with the incident arcs or edges in a directed or undirected graph.
kpeter@39
   220
If both arcs/edges and nodes have to be hidden, then you could use
kpeter@39
   221
\ref SubDigraph or \ref SubGraph adaptors.
kpeter@39
   222
kpeter@39
   223
\code
kpeter@39
   224
  ListGraph ug;
kpeter@39
   225
  ListGraph::NodeMap<bool> node_filter(ug);
kpeter@39
   226
  ListGraph::EdgeMap<bool> edge_filter(ug);
kpeter@39
   227
  
kpeter@39
   228
  SubGraph<ListGraph> sg(ug, node_filter, edge_filter);
kpeter@39
   229
\endcode
kpeter@39
   230
kpeter@39
   231
As you see, we needed two filter maps in this case: one for the nodes and
kpeter@39
   232
another for the edges. If a node is hidden, then all of its incident edges
kpeter@39
   233
are also considered to be hidden independently of their own filter values.
kpeter@39
   234
kpeter@39
   235
The subgraph adaptors also make it possible to modify the filter values
kpeter@39
   236
even after the construction of the adaptor class, thus the corresponding
kpeter@39
   237
graph items can be hidden or shown on the fly.
kpeter@39
   238
The adaptors store references to the filter maps, thus the map values can be
kpeter@39
   239
set directly and even by using the \c enable(), \c disable() and \c status()
kpeter@39
   240
functions.
kpeter@39
   241
kpeter@39
   242
\code
kpeter@39
   243
  ListDigraph g;
kpeter@39
   244
  ListDigraph::Node x = g.addNode();
kpeter@39
   245
  ListDigraph::Node y = g.addNode();
kpeter@39
   246
  ListDigraph::Node z = g.addNode();
kpeter@39
   247
  
kpeter@39
   248
  ListDigraph::NodeMap<bool> filter(g, true);
kpeter@39
   249
  FilterNodes<ListDigraph> subgraph(g, filter);
kpeter@39
   250
  std::cout << countNodes(subgraph) << ", ";
kpeter@39
   251
kpeter@39
   252
  filter[x] = false;
kpeter@39
   253
  std::cout << countNodes(subgraph) << ", ";
kpeter@39
   254
kpeter@39
   255
  subgraph.enable(x);
kpeter@39
   256
  subgraph.disable(y);
kpeter@39
   257
  subgraph.status(z, !subgraph.status(z));
kpeter@39
   258
  std::cout << countNodes(subgraph) << std::endl;
kpeter@39
   259
\endcode
kpeter@39
   260
kpeter@39
   261
The above example prints out this line.
kpeter@39
   262
\code
kpeter@39
   263
  3, 2, 1
kpeter@39
   264
\endcode
kpeter@39
   265
kpeter@39
   266
Similarly to \ref ReverseDigraph, the subgraph adaptors also allow the
kpeter@39
   267
modification of the underlying graph structures unless the graph template
kpeter@39
   268
parameter is set to be \c const type.
kpeter@39
   269
Moreover the item types of the original graphs and the subgraphs are
kpeter@39
   270
convertible to each other.
kpeter@39
   271
kpeter@39
   272
The iterators of the subgraph adaptors use the iterators of the original
kpeter@39
   273
graph structures in such a way that each item with \c false filter value
kpeter@39
   274
is skipped. If both the node and arc sets are filtered, then the arc iterators
kpeter@39
   275
check for each arc the status of its end nodes in addition to its own assigned
kpeter@39
   276
filter value. If the arc or one of its end nodes is hidden, then the arc
kpeter@39
   277
is left out and the next arc is considered.
kpeter@39
   278
(It is the same for edges in undirected graphs.)
kpeter@39
   279
Therefore, the iterators of these adaptors are significantly slower than the
kpeter@39
   280
original iterators.
kpeter@39
   281
kpeter@39
   282
Using adaptors, these efficiency aspects should be kept in mind.
kpeter@39
   283
For example, if rather complex algorithms have to be performed on a
kpeter@39
   284
subgraph (e.g. the nodes and arcs need to be traversed several times),
kpeter@39
   285
then it could worth copying the altered graph into an efficient
kpeter@39
   286
structure (e.g. \ref StaticDigraph) and run the algorithm on it.
kpeter@29
   287
Note that the adaptor classes can also be used for doing this easily,
kpeter@29
   288
without having to copy the graph manually, as shown in the following
kpeter@29
   289
example.
kpeter@29
   290
kpeter@29
   291
\code
kpeter@29
   292
  ListDigraph g;
kpeter@29
   293
  ListDigraph::NodeMap<bool> filter_map(g);
kpeter@29
   294
  // construct the graph and fill the filter map
kpeter@29
   295
kpeter@29
   296
  {
kpeter@39
   297
    StaticDigraph tmp_graph;
kpeter@39
   298
    ListDigraph::NodeMap<StaticDigraph::Node> node_ref(g);
kpeter@39
   299
    digraphCopy(filterNodes(g, filter_map), tmp_graph)
kpeter@29
   300
      .nodeRef(node_ref).run();
kpeter@29
   301
kpeter@39
   302
    // use tmp_graph
kpeter@29
   303
  }
kpeter@29
   304
\endcode
kpeter@29
   305
kpeter@39
   306
\note Using \ref ReverseDigraph could be as efficient as working with the
kpeter@39
   307
original graph, but most of the adaptors cannot be so fast, of course. 
kpeter@29
   308
kpeter@29
   309
kpeter@39
   310
[SEC]sec_other_adaptors[SEC] Other Graph Adaptors
kpeter@39
   311
kpeter@39
   312
Two other practical adaptors are \ref Undirector and \ref Orienter.
kpeter@39
   313
\ref Undirector makes an undirected graph from a digraph disregarding the
kpeter@39
   314
orientations of the arcs. More precisely, an arc of the original digraph
kpeter@39
   315
is considered as an edge (and two arcs, as well) in the adaptor.
kpeter@39
   316
\ref Orienter can be used for the reverse alteration, it assigns a certain
kpeter@39
   317
orientation to each edge of an undirected graph to form a directed graph.
kpeter@39
   318
A \c bool edge map of the underlying graph must be given to the constructor
kpeter@39
   319
of the class, which define the direction of the arcs in the created adaptor
kpeter@39
   320
(with respect to the inherent orientation of the original edges).
kpeter@39
   321
kpeter@39
   322
\code
kpeter@39
   323
  ListGraph graph;
kpeter@39
   324
  ListGraph::EdgeMap<bool> dir_map(graph, true);
kpeter@39
   325
  Orienter<ListGraph> directed_graph(graph, dir_map);
kpeter@39
   326
\endcode
kpeter@39
   327
kpeter@39
   328
LEMON also provides some more complex adaptors, for
kpeter@39
   329
instance, \ref SplitNodes, which can be used for splitting each node of a
kpeter@39
   330
directed graph into an in-node and an out-node.
kpeter@39
   331
Formally, the adaptor replaces each node u in the graph with two nodes,
kpeter@39
   332
namely u<sub>in</sub> and u<sub>out</sub>. Each arc (u,v) of the original
kpeter@39
   333
graph will correspond to an arc (u<sub>out</sub>,v<sub>in</sub>).
kpeter@39
   334
The adaptor also adds an additional bind arc (u<sub>in</sub>,u<sub>out</sub>)
kpeter@39
   335
for each node u of the original digraph.
kpeter@39
   336
kpeter@39
   337
The aim of this class is to assign costs or capacities to the nodes when using
kpeter@39
   338
algorithms which would otherwise consider arc costs or capacities only.
kpeter@39
   339
For example, let us suppose that we have a digraph \c g with costs assigned to
kpeter@39
   340
both the nodes and the arcs. Then Dijkstra's algorithm can be used in
kpeter@39
   341
connection with \ref SplitNodes as follows.
kpeter@29
   342
kpeter@29
   343
\code
kpeter@29
   344
  typedef SplitNodes<ListDigraph> SplitGraph;
kpeter@29
   345
  SplitGraph sg(g);
kpeter@29
   346
  SplitGraph::CombinedArcMap<NodeCostMap, ArcCostMap>
kpeter@29
   347
    combined_cost(node_cost, arc_cost);
kpeter@29
   348
  SplitGraph::NodeMap<double> dist(sg);
kpeter@29
   349
  dijkstra(sg, combined_cost).distMap(dist).run(sg.outNode(u));
kpeter@29
   350
\endcode
kpeter@29
   351
kpeter@39
   352
\note This problem can also be solved using map adaptors to create
kpeter@39
   353
an implicit arc map that assigns for each arc the sum of its cost
kpeter@39
   354
and the cost of its target node. This map can be used with the original
kpeter@39
   355
graph more efficiently than using the above solution.
kpeter@29
   356
kpeter@39
   357
Another nice application is the problem of finding disjoint paths in
kpeter@39
   358
a digraph.
kpeter@39
   359
The maximum number of \e edge \e disjoint paths from a source node to
kpeter@39
   360
a sink node in a digraph can be easily computed using a maximum flow
kpeter@39
   361
algorithm with all arc capacities set to 1.
kpeter@39
   362
On the other hand, \e node \e disjoint paths cannot be found directly
kpeter@39
   363
using a standard algorithm.
kpeter@39
   364
However, \ref SplitNodes adaptor makes it really simple.
kpeter@39
   365
If a maximum flow computation is performed on this adaptor, then the
kpeter@39
   366
bottleneck of the flow (i.e. the minimum cut) will be formed by bind arcs,
kpeter@39
   367
thus the found flow will correspond to the union of some node disjoint
kpeter@39
   368
paths in terms of the original digraph.
kpeter@39
   369
kpeter@39
   370
In flow, circulation and matching problems, the residual network is of
kpeter@39
   371
particular importance, which is implemented in \ref ResidualDigraph.
kpeter@39
   372
Combining this adaptor with various algorithms, a range of weighted and
kpeter@39
   373
cardinality optimization methods can be implemented easily.
kpeter@39
   374
kpeter@39
   375
To construct a residual network, a digraph structure, a flow map and a
kpeter@39
   376
capacity map have to be given to the constructor of the adaptor as shown
kpeter@39
   377
in the following code.
kpeter@39
   378
kpeter@39
   379
\code
kpeter@39
   380
  ListDigraph g;
kpeter@39
   381
  ListDigraph::ArcMap<int> flow(g);
kpeter@39
   382
  ListDigraph::ArcMap<int> capacity(g);
kpeter@39
   383
kpeter@39
   384
  ResidualDigraph<ListDigraph> res_graph(g, capacity, flow); 
kpeter@39
   385
\endcode
kpeter@39
   386
kpeter@39
   387
\note In fact, this class is implemented using two other adaptors:
kpeter@39
   388
\ref Undirector and \ref FilterArcs.
kpeter@29
   389
kpeter@29
   390
[TRAILER]
kpeter@29
   391
*/
kpeter@32
   392
}