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