doc/graph_orientation.dox
author deba
Mon, 12 Sep 2005 09:15:59 +0000
changeset 1679 e825655c24a4
child 1684 df3820d7989d
permissions -rw-r--r--
Some bugfixes.
alpar@1678
     1
namespace lemon {
alpar@1678
     2
/**
alpar@1678
     3
alpar@1678
     4
\ingroup demos
alpar@1678
     5
\file graph_orientation.cc
alpar@1678
     6
\brief Graph orientation with lower bound requirement on the
alpar@1678
     7
in-degree of the nodes.
alpar@1678
     8
alpar@1678
     9
alpar@1678
    10
alpar@1678
    11
First we include some important headers.
alpar@1678
    12
alpar@1678
    13
The first one defines \ref lemon::ListGraph "ListGraph",
alpar@1678
    14
the "Swiss army knife" graph implementation.
alpar@1678
    15
\dontinclude graph_orientation.cc
alpar@1678
    16
\skipline list_graph
alpar@1678
    17
alpar@1678
    18
The next is  to read a \ref graph-io-page ".lgf" (Lemon Graph Format) file.
alpar@1678
    19
\skipline reader
alpar@1678
    20
alpar@1678
    21
This provides us with some special purpose graph \ref maps "maps".
alpar@1678
    22
\skipline iterable
alpar@1678
    23
alpar@1678
    24
The following header defines a simple data structure to store and manipulate
alpar@1678
    25
planar coordinates. It will be used to draw the result.
alpar@1678
    26
\skipline xy
alpar@1678
    27
alpar@1678
    28
And finally, this header contains a simple graph drawing utility.
alpar@1678
    29
\skipline eps
alpar@1678
    30
alpar@1678
    31
As we don't want to type in \ref lemon "lemon::" million times, the
alpar@1678
    32
following line seems to be useful.
alpar@1678
    33
\skipline namespace
alpar@1678
    34
alpar@1678
    35
The following <tt>typedef</tt>s will also save a lot of typing.
alpar@1678
    36
\skip typedef
alpar@1678
    37
\until InEdgeIt
alpar@1678
    38
alpar@1678
    39
Well, we are ready to start <tt>main()</tt>.
alpar@1678
    40
\skip main
alpar@1678
    41
\until {
alpar@1678
    42
alpar@1678
    43
First we check whether the program is called with exactly 1 parameter.
alpar@1678
    44
If it isn't, we print a short help message end exit.
alpar@1678
    45
The vast majority of people would probably skip this block.
alpar@1678
    46
\skip if
alpar@1678
    47
\until }
alpar@1678
    48
alpar@1678
    49
Now, we read a graph \c g, and a map \c f containing
alpar@1678
    50
the in-deg requirements from a \ref graph-io-page ".lgf" (Lemon Graph Format)
alpar@1678
    51
file. To generate the output picture, we also read the node titles (\c id) and
alpar@1678
    52
coordinates (\c coords).
alpar@1678
    53
So, first we create the graph
alpar@1678
    54
\skipline ListGraph
alpar@1678
    55
and the corresponding NodeMaps.
alpar@1678
    56
\skipline NodeMap
alpar@1678
    57
\until coords
alpar@1678
    58
\note The graph must be given to the maps' constructor.
alpar@1678
    59
alpar@1678
    60
Then, the following block will read these data from the file, or exit if
alpar@1678
    61
the file is missing or corrupt.
alpar@1678
    62
\skip try
alpar@1678
    63
\until }
alpar@1678
    64
\until }
alpar@1678
    65
alpar@1678
    66
The algorithm needs a "level" integer value assigned to each node. In the
alpar@1678
    67
beginning, the nodes are on level 0.
alpar@1678
    68
\skipline level
alpar@1678
    69
alpar@1678
    70
The deficiency (\c def) of a node is the in-degree requirement minus the 
alpar@1678
    71
actual in-degree.
alpar@1678
    72
alpar@1678
    73
\skip def
alpar@1678
    74
\until subMap
alpar@1678
    75
alpar@1678
    76
A node is \e active if its deficiency is positive (i.e. if it doesn't meet
alpar@1678
    77
the degree requirement).
alpar@1678
    78
\skip active
alpar@1678
    79
\until def
alpar@1678
    80
alpar@1678
    81
We also store in a bool map which edges are reverted. Actually this is only
alpar@1678
    82
used to draw these edges with different color in the output picture. The
alpar@1678
    83
algorithm will update this map called \c rev, but will not use it otherwise.
alpar@1678
    84
\skip rev
alpar@1678
    85
\until reversed
alpar@1678
    86
alpar@1678
    87
The variable \c nodeNum will refer to the number of nodes.
alpar@1678
    88
\skipline nodeNum
alpar@1678
    89
alpar@1678
    90
Here comes the algorithms itself. 
alpar@1678
    91
In each iteration we choose an active node (\c act will store it). If there is
alpar@1678
    92
no such a node, then the orientation is feasible so we are done.
alpar@1678
    93
\skip act
alpar@1678
    94
\until while
alpar@1678
    95
alpar@1678
    96
Then we check if there exists an edge leaving this node that steps down exactly
alpar@1678
    97
one level.
alpar@1678
    98
\skip OutEdge
alpar@1678
    99
\until while
alpar@1678
   100
alpar@1678
   101
If there exists, we decrease the "activity" of the node \c act by reverting
alpar@1678
   102
this egde.
alpar@1678
   103
Fortunately, \ref lemon::ListGraph "ListGraph"
alpar@1678
   104
has a special function \ref lemon::ListGraph::reverseEdge() "reverseEdge()"
alpar@1678
   105
that makes this easy.
alpar@1678
   106
We also have to update the maps \c def and
alpar@1678
   107
\c rev.
alpar@1678
   108
\skipline if
alpar@1678
   109
\skip if
alpar@1678
   110
\until }
alpar@1678
   111
Otherwise (i.e. if there is no edge stepping down one level). We lift up the
alpar@1678
   112
current active node \c act. If it reaches level \c nodeNum, then there
alpar@1678
   113
exists no appropriate orientation so we stop.
alpar@1678
   114
\skipline else
alpar@1678
   115
\skipline if
alpar@1678
   116
\skipline return
alpar@1678
   117
\until }
alpar@1678
   118
\until }
alpar@1678
   119
\until }
alpar@1678
   120
alpar@1678
   121
Believe it or not, this algorithm works and runs fast.
alpar@1678
   122
alpar@1678
   123
Finally, we print the obtained orientation. Note, how the different
alpar@1678
   124
\c bool values of
alpar@1678
   125
\c rev are transformed into different \ref lemon::Color "RGB color"s
alpar@1678
   126
using the class
alpar@1678
   127
\ref lemon::ColorSet "ColorSet"
alpar@1678
   128
and the \ref map_adaptors "map adaptor" called
alpar@1678
   129
\ref lemon::ComposeMap "composeMap".
alpar@1678
   130
alpar@1678
   131
\skip graphToEps
alpar@1678
   132
\until run
alpar@1678
   133
alpar@1678
   134
alpar@1678
   135
\until end of main
alpar@1678
   136
alpar@1678
   137
Finally here are again the list of the used include files (because I can't turn
alpar@1678
   138
this section off.)
alpar@1678
   139
alpar@1678
   140
*/
alpar@1678
   141
alpar@1678
   142
}