Getting Started

At the beginning we strongly suggest that you open your favorite text editor and enter the code simultaneously as you read it. Compiling the demos is also a good exercise.

As the first example we show you a lemon style "Hello World" program. Now we explain almost every line, but later we will skip the basics and focus on new things.

Hello World in LEMON

In this little program we give you a taste of the LEMON programming.

Let's see the code fragment to fragment!


#include <iostream>

We want to use a lemon::ListGraph so the include goes like this:

#include <lemon/list_graph.h>

The next few lines are not necessary but useful shortcuts, if you don't want to type lemon::ListGraph::Node every time.

using namespace lemon;

typedef ListGraph::Node  Node;
typedef ListGraph::Edge  Edge;

For this demo we need to declare a ListGraph and a special NodeMap to store the characters associated to the graph's nodes.

int main()
{
  // Declare the graph itself and a NodeMap, witch will store the characters
  // assigned to the nodes
  ListGraph  graph;
  ListGraph::NodeMap<char>  char_map(graph);

Adding nodes to the graph is very easy.

  Node  new_node = graph.addNode();

When a new node or edge is added to the graph the assigned maps are automatically resized. So graphs can be built dynamically. The usage of a map is very natural.

  char_map[new_node] = 'H';

Notice that no reference or additional assignment is needed to work with nodes. They won't become illegal or won't lead to throwing any exceptions. You can declare and handle a node like every other basic type such as int.

  // Store the start node
  Node  start_node = new_node;

  Node  from_node = new_node;
  new_node = graph.addNode();
  char_map[new_node] = 'e';

As one expects adding an Edge is similar. You need to define the source node and the destination node. The nodes must belong to the graph of course. The Edge has the direction from the source to the destination. In some cases you don't want the edges to be directed - then you use an undirected graph. For example lemon::ListUGraph.

  graph.addEdge( from_node, new_node );

In the next few lines we add some more nodes and edges and to the graph we need. Those lines are not very interesting so we skip them, but you find the whole working program in file hello_world.cc in the demo section.

The next statement must be familiar. But what is that INVALID in the while test statement? In LEMON we usually use the INVALID to check if an object contains valid information.

  Node  current_node = start_node;
  while( current_node != INVALID )
  {

We take the current node and write out the character assigned to it. Is's easy with the char_map.

    std::cout << char_map[current_node] << std::flush;

And here comes the trick. OutEdgeIt iterates on outgoing edges of a given node. We pass the current node as argument to it, so the edge iterator will stand on the first outgoing edge of the current node, or will be INVALID if the node has no outgoing edges.

    ListGraph::OutEdgeIt  edge(graph, current_node);

The graph we built before is linear, so we know that it ends, when no more outgoing edges found. Otherwise the current node must be the node the edge points to. Basic information about an edge can be requested from the graph.

    if( edge != INVALID )
      current_node = graph.target( edge );
    else
      current_node = INVALID;
  }

Finish the code, just to be precise.

  return 0;
}

Compiling Hello World

To compile this program all you have to do is type in
 g++ -ohello_world hello_world.cc 
and press Enter! This is the case if you installed LEMON on your system. (For more information see the LEMON installation instructions.)

This is because LEMON is template library and most of it's code has to be available as source code during compilation.

Most programs using LEMON will compile as easy as this one unless you want to use some performance measuring tools LEMON can provide. Then you need to link an additional library against your program.


Generated on Thu Jun 4 04:03:12 2009 for LEMON by  doxygen 1.5.9