LEMON Tutorial
59

This page is under construction.
This section should be revised and extended.
In addition to the graph structures, the most important parts of LEMON are the various algorithms related to graph theory and combinatorial optimization. The library provides quite flexible and efficient implementations for wellknown fundamental algorithms, such as breadthfirst search (BFS), depthfirst search (DFS), Dijkstra algorithm, Kruskal algorithm and methods for discovering graph properties like connectivity, bipartiteness or Euler property, as well as more complex optimization algorithms for finding maximum flows, minimum cuts, matchings, minimum cost flows etc.
In this section, we present only some of the most fundamental algorithms. For a complete overview, see the Algorithms module of the reference manual.
The common graph search algorithms, namely breadthfirst search (BFS) and depthfirst search (DFS) are implemented in highly adaptable and efficient algorithm classes Bfs and Dfs. In LEMON, the algorithms are typically placed in separated files, which are named after the algorithm itself but with lower case like all other header files. For example, we have to include bfs.h
for using Bfs.
Basically, all algorithms are implemented in template classes. The template parameters typically specify the used graph type (for more information, see 8 Graph Structures) and the required map types. For example, an instance of the BFs class can be created like this.
This class provides a simple but powerful interface to control the execution of the algorithm and to obtain all the results. You can execute the algorithm from a given source node by calling the run() function.
This operation finds the shortest paths from s
to all other nodes. If you are looking for an st path for a certain target node t
, you can also call the run() function with two parameters. In this case, the BFS search will terminate once it has found the shortest path from s
to t
.
By default, the distances and the path information are stored in internal maps, which you can access with member functions like distMap() and predMap() or more directly with other member functions like dist(), path(), predNode(), predArc(). Once the execution of the algorithm is finished, these query functions can be called.
For an example, let us say we want to print the shortest path of those nodes that are at most in a certain distance max_dist
.
The class interfaces of the algorithms also support a finer control on the execution. For example, we can specify more source nodes and we can even run the algorithm stepbystep. If you need such control on the execution, you have to use more functions instead of run(). First, you have to call init() to initialize the internal data structures.
Then you can add one or more source nodes to the queue with addSource(). They will be processed, as they would be reached by the algorithm before. And yes, you can even add more sources during the execution.
Finally, the actual path computation of the algorithm can be performed with the start() function.
Instead of using start(), you can even execute the algorithm stepbystep, so you can write your own loop to process the nodes onebyone. For example, the following code will executes the algorithm in such a way, that it reaches all nodes in the digraph, namely the algorithm is started for each node that is not visited before.
bfs.start()
is only a shortcut of the following code.
Since the DFS algorithm is very similar to BFS with a few tiny differences, the Dfs class can be used similarly to Bfs.
If you would like to solve some transportation problems in a network, then you will most likely want to find shortest paths between nodes of a graph. This is usually solved using Dijkstra's algorithm. The following code is a simple program using the LEMON Dijkstra class through the functiontype interface dijkstra(). It calculates the shortest path between node s
and t
in a digraph g
.
In LEMON, the algorithms are implemented basically as classes, but for some of them, functiontype interfaces are also available for the sake of convenience. For instance, the Dijkstra algorithm is implemented in the Dijkstra template class, but the dijkstra() function is also defined, which can still be used quite flexibly due to named parameters.
The above sample code could also use the class interface as follows.
The previous code is obviously longer than the original, but the execution can be controlled to a higher extent. While using the functiontype interface, only one source can be added to the algorithm, the class interface makes it possible to specify several root nodes. Moreover, the algorithm can also be executed stepbystep. For instance, the following code can be used instead of dijkstra.start().
LEMON provides several other algorithms for findign shortest paths in specific or more general cases. For example, BellmanFord can be used instead of Dijkstra when the graph contains an arc with negative cost. You may check the Shortest Path Algorithms module of the reference manual for more details.
See Preflow.
<< 3 Basic Concepts  Home  5 Undirected Graphs >>