LEMON Tutorial
59

In typical algorithms and applications related to graphs and networks, we usually encounter situations in which a specific alteration of a graph has to be considered. If some nodes or arcs have to be hidden (maybe temporarily) or the reverse oriented graph has to be used, then this is the case. However, actually modifying physical storage of the graph or making a copy of the graph structure along with the required maps could be rather expensive (in time or in memory usage) compared to the operations that should be performed on the altered graph. In such cases, the LEMON graph adaptor classes could be used.
Let us suppose that we have an instance g
of a directed graph type, say ListDigraph and an algorithm
is needed to run on the reverse oriented digraph. In this situation, a certain adaptor class
can be used.
The graph adaptors are special classes that serve for considering other graph structures in different ways. They can be used exactly the same as "real" graphs, i.e. they conform to the graph concepts, thus all generic algorithms can be performed on them. However, the adaptor classes cannot be used alone but only in conjunction with actual graph representations. They do not alter the physical graph storage, they just give another view of it. When the methods of the adaptors are called, they use the underlying graph structures and their operations, thus these classes have only negligible memory usage and do not perform sophisticated algorithmic actions.
This technique yields convenient tools that help writing compact and elegant code, and makes it possible to easily implement complex algorithms based on well tested standard components.
For solving the problem introduced above, we could use the following code.
Note that the original digraph g
remains untouched during the whole procedure.
LEMON also provides simple "creator functions" for the adaptor classes to make their usage even simpler. For example, reverseDigraph() returns an instance of ReverseDigraph, thus the above code can be written like this.
Another essential feature of the adaptors is that their Node
and Arc
types convert to the original item types. Therefore, the maps of the original graph can be used in connection with the adaptor.
In the following code, Dijksta's algorithm is run on the reverse oriented graph but using the original node and arc maps.
In the above examples, we used ReverseDigraph in such a way that the underlying digraph was not changed. However, the adaptor class can even be used for modifying the original graph structure. It allows adding and deleting arcs or nodes, and these operations are carried out by calling suitable functions of the underlying digraph (if it supports them).
For this, ReverseDigraph<GR> has a constructor of the following form.
This means that in a situation, when the modification of the original graph has to be avoided (e.g. it is given as a const reference), then the adaptor class has to be instantiated with GR
set to be const
type (e.g. GR = const ListDigraph
), as in the following example.
As a more complex example, let us see how ReverseDigraph can be used together with a graph search algorithm to decide whether a directed graph is strongly connected or not. We exploit the fact the a digraph is strongly connected if and only if for an arbitrarily selected node u
, each other node is reachable from u
(along a directed path) and u
is reachable from each node. The latter condition is the same that each node is reachable from u
in the reversed digraph.
Note that we have to use the adaptor with 'const Digraph
' type, since g
is a const
reference to the original graph structure. The stronglyConnected() function provided in LEMON has a quite similar implementation.
Another typical requirement is the use of certain subgraphs of a graph, or in other words, hiding nodes and/or arcs from a graph. LEMON provides several convenient adaptors for these purposes. In the following image, a SubDigraph adaptor is applied to an underlying digraph structure to obtain a suitable subgraph.
FilterArcs can be used when some arcs have to be hidden from a digraph. A filter map has to be given to the constructor, which assign bool
values to the arcs specifying whether they have to be shown or not in the subgraph structure. Suppose we have a ListDigraph structure g
. Then we can construct a subgraph in which some arcs (a1
, a2
etc.) are hidden as follows.
The following more complex code runs Dijkstra's algorithm on a digraph that is obtained from another digraph by hiding all arcs having negative lengths.
Note the extensive use of map adaptors and creator functions, which makes the code really compact and elegant.
FilterEdges can be used for hiding edges from an undirected graph (like FilterArcs is used for digraphs). FilterNodes serves for filtering nodes along with the incident arcs or edges in a directed or undirected graph. If both arcs/edges and nodes have to be hidden, then you could use SubDigraph or SubGraph adaptors.
As you see, we needed two filter maps in this case: one for the nodes and another for the edges. If a node is hidden, then all of its incident edges are also considered to be hidden independently of their own filter values.
The subgraph adaptors also make it possible to modify the filter values even after the construction of the adaptor class, thus the corresponding graph items can be hidden or shown on the fly. The adaptors store references to the filter maps, thus the map values can be set directly and even by using the enable()
, disable()
and status()
functions.
The above example prints out this line.
Similarly to ReverseDigraph, the subgraph adaptors also allow the modification of the underlying graph structures unless the graph template parameter is set to be const
type. Moreover the item types of the original graphs and the subgraphs are convertible to each other.
The iterators of the subgraph adaptors use the iterators of the original graph structures in such a way that each item with false
filter value is skipped. If both the node and arc sets are filtered, then the arc iterators check for each arc the status of its end nodes in addition to its own assigned filter value. If the arc or one of its end nodes is hidden, then the arc is left out and the next arc is considered. (It is the same for edges in undirected graphs.) Therefore, the iterators of these adaptors are significantly slower than the original iterators.
Using adaptors, these efficiency aspects should be kept in mind. For example, if rather complex algorithms have to be performed on a subgraph (e.g. the nodes and arcs need to be traversed several times), then it could worth copying the altered graph into an efficient structure (e.g. StaticDigraph) and run the algorithm on it. Note that the adaptor classes can also be used for doing this easily, without having to copy the graph manually, as shown in the following example.
Two other practical adaptors are Undirector and Orienter. Undirector makes an undirected graph from a digraph disregarding the orientations of the arcs. More precisely, an arc of the original digraph is considered as an edge (and two arcs, as well) in the adaptor. Orienter can be used for the reverse alteration, it assigns a certain orientation to each edge of an undirected graph to form a directed graph. A bool
edge map of the underlying graph must be given to the constructor of the class, which define the direction of the arcs in the created adaptor (with respect to the inherent orientation of the original edges).
Sine the adaptor classes conform to the graph concepts, we can even apply an adaptor to another one. The following image illustrates a situation when a SubDigraph and an Undirector adaptor is applied to a digraph.
LEMON also provides some more complex adaptors, for instance, SplitNodes, which can be used for splitting each node of a directed graph into an innode and an outnode. Formally, the adaptor replaces each node u in the graph with two nodes, namely u_{in} and u_{out}. Each arc (u,v) of the original graph will correspond to an arc (u_{out},v_{in}). The adaptor also adds an additional bind arc (u_{in},u_{out}) for each node u of the original digraph.
The aim of this class is to assign costs or capacities to the nodes when using algorithms which would otherwise consider arc costs or capacities only. For example, let us suppose that we have a digraph g
with costs assigned to both the nodes and the arcs. Then Dijkstra's algorithm can be used in connection with SplitNodes as follows.
Another nice application is the problem of finding disjoint paths in a digraph. The maximum number of edge disjoint paths from a source node to a sink node in a digraph can be easily computed using a maximum flow algorithm with all arc capacities set to 1. For example, in the following digraph, four arc disjoint paths can be found from the node on the left to the node on the right.
On the other hand, node disjoint paths cannot be found directly using a standard algorithm. However, SplitNodes adaptor makes it really simple. If a maximum flow computation is performed on this adaptor, then the bottleneck of the flow (i.e. the minimum cut) will be formed by bind arcs, thus the found flow will correspond to the union of some node disjoint paths in terms of the original digraph. For example, in the above digraph, there are only three node disjoint paths.
In flow, circulation and matching problems, the residual network is of particular importance, which is implemented in ResidualDigraph. Combining this adaptor with various algorithms, a range of weighted and cardinality optimization methods can be implemented easily.
To construct a residual network, a digraph structure, a flow map and a capacity map have to be given to the constructor of the adaptor as shown in the following code.
<< 8 Graph Structures  Home  10 Linear Programming Interface >>