LEMON Tutorial
59
|
In 3 Basic Concepts, we have introduced a general digraph structure, ListDigraph. LEMON also contains undirected graph classes, for example, ListGraph is the undirected versions of ListDigraph.
The undirected graphs also fulfill the concept of directed graphs, in such a way that each undirected edge of a graph can also be regarded as two oppositely directed arcs. As a result, all directed graph algorithms automatically run on undirected graphs, as well.
Undirected graphs provide an Edge
type for the undirected edges and an Arc
type for the directed arcs. The Arc
type is convertible to Edge
(or inherited from it), thus the corresponding edge can always be obtained from an arc. Of course, only nodes and edges can be added to or removed from an undirected graph and the corresponding arcs are added or removed automatically (there are twice as many arcs as edges)
For example,
Each edge has an inherent orientation, thus it can be defined whether an arc is forward or backward oriented in an undirected graph with respect to this default orientation of the represented edge. The direction of an arc can be obtained and set using the functions direction() and direct(), respectively.
For example,
The end nodes of an edge can be obtained using the functions u() and v(), while the source() and target() can be used for arcs.
Similarly to the digraphs, the undirected graphs also provide iterators NodeIt, ArcIt, OutArcIt and InArcIt, which can be used the same way. However, they also have iterator classes for edges. EdgeIt traverses all edges in the graph and IncEdgeIt lists the incident edges of a certain node.
For example, the degree of each node can be printed out like this:
In an undirected graph, both OutArcIt and InArcIt iterates on the same edges but with opposite direction. They are convertible to both Arc
and Edge
types. IncEdgeIt also iterates on these edges, but it is not convertible to Arc
, only to Edge
.
Apart from the node and arc maps, an undirected graph also defines a member class for constructing edge maps. These maps can be used in conjunction with both edges and arcs.
For example,
If you would like to design an electric network minimizing the total length of wires, then you might be looking for a minimum spanning tree in an undirected graph. This can be found using the Kruskal algorithm.
Let us suppose that the network is stored in a ListGraph object g
with a cost map cost
. We create a bool
valued edge map tree_map
or a vector tree_vector
for storing the tree that is found by the algorithm. After that, we could call the kruskal() function. It gives back the weight of the minimum spanning tree and tree_map
or tree_vector
will contain the found spanning tree.
If you want to store the arcs in a bool
valued edge map, then you can use the function as follows.
If you would like to store the edges in a standard container, you can do it like this.
<< 4 Algorithms | Home | 6 Basic Graph Utilities >>