- The first thing to discuss is the way one can create data structures
like graphs and maps in a program using LEMON.
//There are more graph types
//implemented in LEMON and you can implement your own graph type just as well:
//read more about this in the already mentioned page on \ref graphs "graphs".
First we show how to add nodes and edges to a graph manually. We will also
define a map on the edges of the graph. After this we show the way one can
read a graph (and perhaps maps on it) from a stream (e.g. a file). Of course
we also have routines that write a graph (and perhaps maps) to a stream
(file): this will also be shown. LEMON supports the DIMACS file formats to
read network optimization problems, but more importantly we also have our own
file format that gives a more flexible way to store data related to network
optimization.
- The following code shows how to build a graph from scratch and iterate on its nodes and edges. This example also shows how to give a map on the edges of the graph. The type Listgraph is one of the LEMON graph types: the typedefs in the beginning are for convenience and we will assume them later as well. \dontinclude hello_lemon.cc \skip include \until } See the whole program in file \ref hello_lemon.cc in the \c demo subdir of LEMON package. If you want to read more on the LEMON graph structures and concepts, read the page about \ref graphs "graphs".
- LEMON has an own file format for storing graphs, maps on edges/nodes and some other things. Instead of any explanation let us give a short example file in this format: read the detailed description of the LEMON graph file format and input-output routines here: \ref graph-io-page. So here is a file describing a graph of 6 nodes (0 to 5), two nodemaps (called \c coordinates_x and \c coordinates_y), several edges, an edge map called \c capacity and two designated nodes (called \c source and \c target). \verbatim @nodeset id coordinates_x coordinates_y 5 796.398 208.035 4 573.002 63.002 3 568.549 401.748 2 277.889 68.476 1 288.248 397.327 0 102.239 257.532 @edgeset id capacity 4 5 6 8 3 5 5 8 2 4 4 5 1 4 3 8 1 3 2 5 0 2 1 10 0 1 0 10 #This is a comment here @nodes source 0 target 5 @edges @attributes author "Attila BERNATH" @end \endverbatim Finally let us give a simple example that reads a graph from a file and writes it to the standard output. \dontinclude reader_writer_demo.cc \skip include \until return \until } See the whole program in file \ref reader_writer_demo.cc.
- The following code shows how to read a graph from a stream (e.g. a file) in the DIMACS file format (find the documentation of the DIMACS file formats on the web). \code Graph g; std::ifstream f("graph.dim"); readDimacs(f, g); \endcode One can also store network (graph+capacity on the edges) instances and other things (minimum cost flow instances etc.) in DIMACS format and read these in LEMON: to see the details read the documentation of the \ref dimacs.h "Dimacs file format reader".

- If you want to solve some transportation problems in a network then you will want to find shortest paths between nodes of a graph. This is usually solved using Dijkstra's algorithm. A utility that solves this is the \ref lemon::Dijkstra "LEMON Dijkstra class". The following code is a simple program using the \ref lemon::Dijkstra "LEMON Dijkstra class": it calculates the shortest path between node \c s and \c t in a graph \c g. We omit the part reading the graph \c g and the length map \c len. \dontinclude dijkstra_demo.cc \skip ListGraph \until Graph g ... \skip Dijkstra algorithm \until std::cout << g.id(s) See the whole program in \ref dijkstra_demo.cc. Some explanation: after instantiating a member of the Dijkstra class we run the Dijkstra algorithm from node \c s. After this we read some of the results. You can do much more with the Dijkstra class, for example you can run it step by step and gain full control of the execution. For a detailed description, see the documentation of the \ref lemon::Dijkstra "LEMON Dijkstra class".
- If you want to design a network and want to minimize 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: the function \ref lemon::kruskal "LEMON Kruskal " does this job for you. First make a graph \c g and a cost map \c edge_cost_map, then make a bool edgemap \c tree_map or a vector \c tree_edge_vec for the algorithm output. After calling the function it gives back the weight of the minimum spanning tree and the \c tree_map or the \c tree_edge_vec contains the edges of the tree. If you want to store the edges in a bool edgemap, then use the function as follows: \dontinclude kruskal_demo.cc \skip Kruskal with boolmap; \until std::endl And if you rather use a vector instead of a bool map: \skip Kruskal with vector; \until std::endl See the whole program in \ref kruskal_demo.cc. - Many problems in network optimization can be formalized by means
of a linear programming problem (LP problem, for short). In our
library we decided not to write an LP solver, since such packages are
available in the commercial world just as well as in the open source
world, and it is also a difficult task to compete these. Instead we
decided to develop an interface that makes it easier to use these
solvers together with LEMON. The advantage of this approach is
twofold. Firstly our C++ interface is more comfortable than the
solvers' native interface. Secondly, changing the underlying solver in
a certain software using LEMON's LP interface needs zero effort. So,
for example, one may try his idea using a free solver, demonstrate its
usability for a customer and if it works well, but the performance
should be improved, then one may decide to purchase and use a better
commercial solver.
So far we have an
interface for the commercial LP solver software \b CPLEX (developed by ILOG)
and for the open source solver \b GLPK (a shorthand for Gnu Linear Programming
Toolkit).
We will show two examples, the first one shows how simple it is to formalize
and solve an LP problem in LEMON, while the second one shows how LEMON
facilitates solving network optimization problems using LP solvers.
- The following code shows how to solve an LP problem using the LEMON lp interface. The code together with the comments is self-explanatory. \dontinclude lp_demo.cc \skip A default solver is taken \until End of LEMON style code See the whole code in \ref lp_demo.cc.
- The second example shows how easy it is to formalize a max-flow
problem as an LP problem using the LEMON LP interface: we are looking
for a real valued function defined on the edges of the digraph
satisfying the nonnegativity-, the capacity constraints and the
flow-conservation constraints and giving the largest flow value
between to designated nodes.
In the following code we suppose that we already have the graph \c g,
the capacity map \c cap, the source node \c s and the target node \c t
in the memory. We will also omit the typedefs.
\dontinclude lp_maxflow_demo.cc
\skip Define a map on the edges for the variables of the LP problem
\until lp.max();
\skip Solve with the underlying solver
\until lp.solve();
The complete program can be found in file \ref lp_maxflow_demo.cc. After compiling run it in the form:
`./lp_maxflow_demo < sample.lgf`where sample.lgf is a file in the lemon format containing a maxflow instance (designated "source", "target" nodes and "capacity" map on the edges).