doc/graph_orientation.dox
author kpeter
Tue, 05 Feb 2008 11:24:32 +0000
changeset 2563 5841132a89fd
parent 2454 c4554a95b0d3
permissions -rw-r--r--
Small fixes in README.
alpar@2391
     1
/* -*- C++ -*-
alpar@2391
     2
 *
alpar@2391
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2391
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@2391
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2391
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2391
     8
 *
alpar@2391
     9
 * Permission to use, modify and distribute this software is granted
alpar@2391
    10
 * provided that this copyright notice appears in all copies. For
alpar@2391
    11
 * precise terms see the accompanying LICENSE file.
alpar@2391
    12
 *
alpar@2391
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2391
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2391
    15
 * purpose.
alpar@2391
    16
 *
alpar@2391
    17
 */
alpar@2391
    18
alpar@1678
    19
namespace lemon {
alpar@1678
    20
/**
alpar@1678
    21
alpar@1678
    22
\ingroup demos
alpar@1678
    23
\file graph_orientation.cc
alpar@1678
    24
\brief Graph orientation with lower bound requirement on the
alpar@1678
    25
in-degree of the nodes.
alpar@1678
    26
alpar@1684
    27
This demo shows an adaptation of the well-known "preflow push" algorithm to
alpar@1684
    28
a simple graph orientation problem.
alpar@1678
    29
alpar@1684
    30
The input of the problem is a(n undirected) graph and an integer value
alpar@1684
    31
<i>f(n)</i> assigned to each node \e n. The task is to find an orientation
alpar@2158
    32
of the edges for which the number of edge arriving at each node \e n is at
alpar@1684
    33
least least <i>f(n)</i>.
alpar@1684
    34
alpar@1684
    35
In fact, the algorithm reads a directed graph and computes a set of edges to
alpar@1684
    36
be reversed in order to achieve the in-degree requirement.
alpar@1684
    37
This input is given using 
alpar@1684
    38
\ref graph-io-page ".lgf (Lemon Graph Format)" file. It should contain
alpar@1684
    39
three node maps. The one called "f" contains the in-degree requirements, while
alpar@1684
    40
"coordinate_x" and "coordinate_y" indicate the position of the nodes. These
alpar@1684
    41
latter ones are used to generate the output, which is a <tt>.eps</tt> file.
alpar@1684
    42
alpar@1684
    43
alpar@1684
    44
\section go-alg-dec The C++ source file
alpar@1684
    45
alpar@1684
    46
Here you find how to solve the problem above using lemon.
alpar@1684
    47
alpar@1684
    48
\subsection go-alg-head Headers and convenience typedefs
alpar@1678
    49
alpar@1678
    50
First we include some important headers.
alpar@1678
    51
alpar@1678
    52
The first one defines \ref lemon::ListGraph "ListGraph",
alpar@1678
    53
the "Swiss army knife" graph implementation.
alpar@1678
    54
\dontinclude graph_orientation.cc
alpar@1678
    55
\skipline list_graph
alpar@1678
    56
alpar@1678
    57
The next is  to read a \ref graph-io-page ".lgf" (Lemon Graph Format) file.
alpar@1678
    58
\skipline reader
alpar@1678
    59
alpar@1678
    60
This provides us with some special purpose graph \ref maps "maps".
alpar@1678
    61
\skipline iterable
alpar@1678
    62
alpar@1678
    63
The following header defines a simple data structure to store and manipulate
alpar@1678
    64
planar coordinates. It will be used to draw the result.
alpar@2310
    65
\skipline dim2
alpar@1678
    66
alpar@1678
    67
And finally, this header contains a simple graph drawing utility.
alpar@1678
    68
\skipline eps
alpar@1678
    69
alpar@1678
    70
As we don't want to type in \ref lemon "lemon::" million times, the
alpar@1678
    71
following line seems to be useful.
alpar@1678
    72
\skipline namespace
alpar@1678
    73
alpar@2172
    74
The following macro will also save a lot of typing by defining some
alpar@2172
    75
convenience <tt>typedef</tt>s.
alpar@2172
    76
alpar@2172
    77
\skipline TYPEDEF
alpar@2172
    78
alpar@2172
    79
Actually, the macro above would be equivalent with the following
alpar@2172
    80
<tt>typedef</tt>s.
alpar@2172
    81
alpar@2172
    82
\code
alpar@2172
    83
typedef ListGraph::Node Node;
alpar@2172
    84
typedef ListGraph::NodeIt NodeIt;
alpar@2172
    85
typedef ListGraph::Edge Edge;
alpar@2172
    86
typedef ListGraph::EdgeIt EdgeIt;
alpar@2172
    87
typedef ListGraph::OutEdgeIt OutEdgeIt;
alpar@2172
    88
typedef ListGraph::InEdgeIt InEdgeIt;
alpar@2172
    89
\endcode
alpar@1678
    90
alpar@1684
    91
\subsection go-alg-main The main() function
alpar@1684
    92
alpar@1678
    93
Well, we are ready to start <tt>main()</tt>.
alpar@1678
    94
\skip main
alpar@1678
    95
\until {
alpar@1678
    96
alpar@1953
    97
First we check whether the program is called with exactly one parameter.
alpar@1678
    98
If it isn't, we print a short help message end exit.
alpar@1678
    99
The vast majority of people would probably skip this block.
alpar@1678
   100
\skip if
alpar@1678
   101
\until }
alpar@1678
   102
alpar@1678
   103
Now, we read a graph \c g, and a map \c f containing
alpar@1684
   104
the in-deg requirements from a \ref graph-io-page ".lgf (Lemon Graph Format)"
alpar@1953
   105
file. To generate the output picture, we also read the node titles (\c label)
alpar@1953
   106
and
alpar@1678
   107
coordinates (\c coords).
alpar@1678
   108
So, first we create the graph
alpar@1678
   109
\skipline ListGraph
alpar@1678
   110
and the corresponding NodeMaps.
alpar@1678
   111
\skipline NodeMap
alpar@1678
   112
\until coords
alpar@1678
   113
\note The graph must be given to the maps' constructor.
alpar@1678
   114
alpar@1678
   115
Then, the following block will read these data from the file, or exit if
alpar@1678
   116
the file is missing or corrupt.
alpar@1678
   117
\skip try
alpar@1678
   118
\until }
alpar@1678
   119
\until }
alpar@1678
   120
alpar@1953
   121
The algorithm needs an integer value assigned to each node. We call this "level" and the nodes are on level 0 at the
alpar@1953
   122
beginning of the execution.
alpar@1953
   123
alpar@1678
   124
\skipline level
alpar@1678
   125
alpar@1678
   126
The deficiency (\c def) of a node is the in-degree requirement minus the 
alpar@1678
   127
actual in-degree.
alpar@1678
   128
alpar@1678
   129
\skip def
alpar@1678
   130
\until subMap
alpar@1678
   131
alpar@1678
   132
A node is \e active if its deficiency is positive (i.e. if it doesn't meet
alpar@1678
   133
the degree requirement).
alpar@1678
   134
\skip active
alpar@1678
   135
\until def
alpar@1678
   136
alpar@2454
   137
We also store a bool map indicating which edges are reverted.
alpar@1953
   138
Actually this map called \c rev is only
alpar@1678
   139
used to draw these edges with different color in the output picture. The
alpar@1953
   140
algorithm updates this map, but will not use it otherwise.
alpar@1678
   141
\skip rev
alpar@1678
   142
\until reversed
alpar@1678
   143
alpar@1678
   144
The variable \c nodeNum will refer to the number of nodes.
alpar@1678
   145
\skipline nodeNum
alpar@1678
   146
alpar@2158
   147
Here comes the algorithm itself. 
alpar@1953
   148
In each iteration we choose an active node (\c act will do it for us).
alpar@1953
   149
If there is
alpar@1678
   150
no such a node, then the orientation is feasible so we are done.
alpar@1678
   151
\skip act
alpar@1678
   152
\until while
alpar@1678
   153
alpar@2158
   154
Then we check if there exists an edge leaving this node and
alpar@2158
   155
stepping down exactly
alpar@1678
   156
one level.
alpar@1678
   157
\skip OutEdge
alpar@1678
   158
\until while
alpar@1678
   159
alpar@1678
   160
If there exists, we decrease the "activity" of the node \c act by reverting
alpar@1678
   161
this egde.
alpar@1678
   162
Fortunately, \ref lemon::ListGraph "ListGraph"
alpar@1678
   163
has a special function \ref lemon::ListGraph::reverseEdge() "reverseEdge()"
alpar@1678
   164
that makes this easy.
alpar@1678
   165
We also have to update the maps \c def and
alpar@1678
   166
\c rev.
alpar@1678
   167
\skipline if
alpar@1678
   168
\skip if
alpar@1678
   169
\until }
alpar@2454
   170
Otherwise (i.e. if there is no edge stepping down one level) we lift up the
alpar@1678
   171
current active node \c act. If it reaches level \c nodeNum, then there
alpar@1678
   172
exists no appropriate orientation so we stop.
alpar@1678
   173
\skipline else
alpar@1678
   174
\skipline if
alpar@1678
   175
\skipline return
alpar@1678
   176
\until }
alpar@1678
   177
\until }
alpar@1678
   178
\until }
alpar@1678
   179
alpar@1678
   180
Believe it or not, this algorithm works and runs fast.
alpar@1678
   181
alpar@1678
   182
Finally, we print the obtained orientation. Note, how the different
alpar@1678
   183
\c bool values of
alpar@1678
   184
\c rev are transformed into different \ref lemon::Color "RGB color"s
alpar@1678
   185
using the class
alpar@2172
   186
\ref lemon::Palette "Palette"
alpar@1678
   187
and the \ref map_adaptors "map adaptor" called
alpar@1678
   188
\ref lemon::ComposeMap "composeMap".
alpar@1678
   189
alpar@1678
   190
\skip graphToEps
alpar@1678
   191
\until run
alpar@1678
   192
alpar@1678
   193
alpar@1678
   194
\until end of main
alpar@1678
   195
alpar@1678
   196
Finally here are again the list of the used include files (because I can't turn
alpar@1678
   197
this section off.)
alpar@1678
   198
alpar@1678
   199
*/
alpar@1678
   200
alpar@2391
   201
}