3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
23 \file graph_orientation.cc
24 \brief Graph orientation with lower bound requirement on the
25 in-degree of the nodes.
27 This demo shows an adaptation of the well-known "preflow push" algorithm to
28 a simple graph orientation problem.
30 The input of the problem is a(n undirected) graph and an integer value
31 <i>f(n)</i> assigned to each node \e n. The task is to find an orientation
32 of the edges for which the number of edge arriving at each node \e n is at
33 least least <i>f(n)</i>.
35 In fact, the algorithm reads a directed graph and computes a set of edges to
36 be reversed in order to achieve the in-degree requirement.
37 This input is given using
38 \ref graph-io-page ".lgf (Lemon Graph Format)" file. It should contain
39 three node maps. The one called "f" contains the in-degree requirements, while
40 "coordinate_x" and "coordinate_y" indicate the position of the nodes. These
41 latter ones are used to generate the output, which is a <tt>.eps</tt> file.
44 \section go-alg-dec The C++ source file
46 Here you find how to solve the problem above using lemon.
48 \subsection go-alg-head Headers and convenience typedefs
50 First we include some important headers.
52 The first one defines \ref lemon::ListGraph "ListGraph",
53 the "Swiss army knife" graph implementation.
54 \dontinclude graph_orientation.cc
57 The next is to read a \ref graph-io-page ".lgf" (Lemon Graph Format) file.
60 This provides us with some special purpose graph \ref maps "maps".
63 The following header defines a simple data structure to store and manipulate
64 planar coordinates. It will be used to draw the result.
67 And finally, this header contains a simple graph drawing utility.
70 As we don't want to type in \ref lemon "lemon::" million times, the
71 following line seems to be useful.
74 The following macro will also save a lot of typing by defining some
75 convenience <tt>typedef</tt>s.
79 Actually, the macro above would be equivalent with the following
83 typedef ListGraph::Node Node;
84 typedef ListGraph::NodeIt NodeIt;
85 typedef ListGraph::Edge Edge;
86 typedef ListGraph::EdgeIt EdgeIt;
87 typedef ListGraph::OutEdgeIt OutEdgeIt;
88 typedef ListGraph::InEdgeIt InEdgeIt;
91 \subsection go-alg-main The main() function
93 Well, we are ready to start <tt>main()</tt>.
97 First we check whether the program is called with exactly one parameter.
98 If it isn't, we print a short help message end exit.
99 The vast majority of people would probably skip this block.
103 Now, we read a graph \c g, and a map \c f containing
104 the in-deg requirements from a \ref graph-io-page ".lgf (Lemon Graph Format)"
105 file. To generate the output picture, we also read the node titles (\c label)
107 coordinates (\c coords).
108 So, first we create the graph
110 and the corresponding NodeMaps.
113 \note The graph must be given to the maps' constructor.
115 Then, the following block will read these data from the file, or exit if
116 the file is missing or corrupt.
121 The algorithm needs an integer value assigned to each node. We call this "level" and the nodes are on level 0 at the
122 beginning of the execution.
126 The deficiency (\c def) of a node is the in-degree requirement minus the
132 A node is \e active if its deficiency is positive (i.e. if it doesn't meet
133 the degree requirement).
137 We also store in a bool map indicating which edges are reverted.
138 Actually this map called \c rev is only
139 used to draw these edges with different color in the output picture. The
140 algorithm updates this map, but will not use it otherwise.
144 The variable \c nodeNum will refer to the number of nodes.
147 Here comes the algorithm itself.
148 In each iteration we choose an active node (\c act will do it for us).
150 no such a node, then the orientation is feasible so we are done.
154 Then we check if there exists an edge leaving this node and
155 stepping down exactly
160 If there exists, we decrease the "activity" of the node \c act by reverting
162 Fortunately, \ref lemon::ListGraph "ListGraph"
163 has a special function \ref lemon::ListGraph::reverseEdge() "reverseEdge()"
164 that makes this easy.
165 We also have to update the maps \c def and
170 Otherwise (i.e. if there is no edge stepping down one level). We lift up the
171 current active node \c act. If it reaches level \c nodeNum, then there
172 exists no appropriate orientation so we stop.
180 Believe it or not, this algorithm works and runs fast.
182 Finally, we print the obtained orientation. Note, how the different
184 \c rev are transformed into different \ref lemon::Color "RGB color"s
186 \ref lemon::Palette "Palette"
187 and the \ref map_adaptors "map adaptor" called
188 \ref lemon::ComposeMap "composeMap".
196 Finally here are again the list of the used include files (because I can't turn