LEMON Tutorial 59
Throughout the tutorial we are working with the lemon namespace. To save a lot of typing, we assume that a
directive is added to the code at the beginning.
The core features of LEMON are the data structures, algorithms and auxiliary tools that make it possible to represent graphs and working with them easily and efficiently. This section tells you how to work with a directed graph (digraph, for short) in LEMON. Here we use ListDigraph, the most versatile digraph structure. (The library also provides other digraph types, see later.)
The default constructor of the class creates an empty digraph.
The nodes and the arcs of a graph are identified by two data types called ListDigraph::Node and ListDigraph::Arc. You can add new items to the graph using the member functions addNode() and addArc(), like this:
Of course, addArc() also returns the created arc:
Parallel and loop arcs are also supported.
Each graph item has a non-negative integer identifier, which can be obtained using the id() function of the graph structure. These identifiers are unique with respect to a certain item type in a graph, but a node and an arc may have the same id.
Arctypes are typically simple wrapper classes for a single
intvalue, which is the identifier of the item.
Let us assume you want to list the elements of the graph. For this purpose, the graph structures provide several iterators. For example, the following code will count the number of nodes in a graph.
ListDigraph::NodeIt is an iterator class that lists the nodes. The name of an iterator type starts with a name that refers to the iterated objects and ends with 'It'.
Using NodeIt, you must give the graph object to the constructor and the iterator will be set to the first node. The next node is obtained by the prefix ++ operator. If there are no more nodes in the graph, the iterator will be set to INVALID, which is exploited in the stop condition of the loop.
The iterators convert to the corresponding item types. For example, the identifiers of the nodes can be printed like this.
As an other example, the following code adds a full graph to the existing nodes.
operator*(). This is not confusing, since the program context always indicates whether we refer to the iterator or to the graph item (they do not have conflicting functionalities).
The graph items are also ordered by the 'less than' operator (with respect to their integer identifiers). For example, this code will add only one of the opposite arcs.
Finally, you can also list the arcs starting from or arriving at a certain node with ListDigraph::OutArcIt and ListDigraph::InArcIt. Their usage is the same, but you must also give the node to the constructor.
The concept of "maps" is another fundamental part of LEMON. They allow assigning values of any type to the nodes or arcs of a graph. The standard maps provided by the graph structures have a couple of nice properties.
By principle, the graph classes represent only the pure structure of the graph (i.e. the nodes and arcs and their connections). All data that are assigned to the items of the graph (e.g. node labels, arc costs or capacities) must be stored separately using maps.
std::map, since they provide O(1) time access to the elements instead of O(log n).
So, if you want to assign
int values to each node, you have to allocate a NodeMap<int>.
As you see, the graph you want to assign a map is given to the constructor. Then you can access its element as if it were a vector.
Any kind of data can be assigned to the graph items (assuming that the type has a default constructor).
As a more complex example, let us create a map that assigns
char labels to the nodes.
When you create a map, you can also give an initial value of the elements as a second parameter. For example, the following code puts the number of outgoing arcs for each node in a map.
In LEMON, there are some naming conventions you might already notice in the above examples.
The name of a source file is written lowercase and the words are separated with underscores (e.g. list_graph.h). All header files are located in the
lemon subdirectory, so you can include them like this.
The names of constants and macros look like the following (e.g. INVALID).
For more details, see LEMON Coding Style.