alpar@2391
|
1 |
/* -*- C++ -*-
|
alpar@2391
|
2 |
*
|
alpar@2391
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@2391
|
4 |
*
|
alpar@2391
|
5 |
* Copyright (C) 2003-2007
|
alpar@2391
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@2391
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@2391
|
8 |
*
|
alpar@2391
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@2391
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@2391
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@2391
|
12 |
*
|
alpar@2391
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@2391
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@2391
|
15 |
* purpose.
|
alpar@2391
|
16 |
*
|
alpar@2391
|
17 |
*/
|
alpar@2391
|
18 |
|
alpar@2195
|
19 |
/**
|
alpar@2195
|
20 |
\page getting_started Getting Started
|
alpar@2195
|
21 |
|
athos@2288
|
22 |
At the beginning we strongly suggest that you open your favorite text
|
athos@2288
|
23 |
editor and enter the code simultaneously as you read it. Compiling the
|
athos@2288
|
24 |
demos is also a good exercise.
|
alpar@2195
|
25 |
|
athos@2288
|
26 |
As the first example we show you a lemon style "Hello World"
|
athos@2288
|
27 |
program. Now we explain almost every line, but later we will skip the
|
athos@2288
|
28 |
basics and focus on new things.
|
alpar@2195
|
29 |
|
alpar@2195
|
30 |
\section hello_world Hello World in LEMON
|
alpar@2195
|
31 |
|
alpar@2195
|
32 |
In this little program we give you a taste of the LEMON programming.
|
alpar@2195
|
33 |
|
alpar@2195
|
34 |
Let's see the code fragment to fragment!
|
alpar@2195
|
35 |
|
alpar@2195
|
36 |
\dontinclude hello_world.cc
|
alpar@2195
|
37 |
\skip include
|
alpar@2195
|
38 |
\until iostream
|
alpar@2195
|
39 |
|
alpar@2195
|
40 |
We want to use a \c lemon::ListGraph so the include goes like this:
|
alpar@2195
|
41 |
\skip include
|
alpar@2195
|
42 |
\until list_graph
|
alpar@2195
|
43 |
|
alpar@2195
|
44 |
The next few lines are not necessary but useful shortcuts, if you don't
|
alpar@2195
|
45 |
want to type \c lemon::ListGraph::Node every time.
|
alpar@2195
|
46 |
\skip using
|
alpar@2195
|
47 |
\until Edge
|
alpar@2195
|
48 |
|
athos@2288
|
49 |
For this demo we need to declare a ListGraph and a special NodeMap to
|
athos@2288
|
50 |
store the characters associated to the graph's nodes.
|
alpar@2195
|
51 |
\skip main
|
alpar@2195
|
52 |
\until char_map
|
alpar@2195
|
53 |
|
alpar@2195
|
54 |
Adding nodes to the graph is very easy.
|
alpar@2195
|
55 |
\skip new_node
|
alpar@2195
|
56 |
\until addNode
|
alpar@2195
|
57 |
|
athos@2288
|
58 |
When a new node or edge is added to the graph the assigned maps are automatically resized.
|
athos@2288
|
59 |
So graphs can be built dynamically. The usage of a map is very natural.
|
alpar@2195
|
60 |
\skip char_map
|
alpar@2195
|
61 |
\until char_map
|
alpar@2195
|
62 |
|
athos@2288
|
63 |
Notice that no reference or additional assignment is needed to work with nodes.
|
alpar@2195
|
64 |
They won't become illegal or won't lead to throwing any exceptions.
|
athos@2288
|
65 |
You can declare and handle a node like every other basic type such as \c int.
|
alpar@2195
|
66 |
\skip Store
|
alpar@2195
|
67 |
\until char_map
|
alpar@2195
|
68 |
|
alpar@2195
|
69 |
As one expects adding an Edge is similar. You need to define the \b source node
|
alpar@2195
|
70 |
and the \b destination node. The nodes must belong to the graph of course. The
|
athos@2288
|
71 |
Edge has the direction from the source to the destination. In some cases you don't
|
alpar@2195
|
72 |
want the edges to be directed - then you use an undirected graph. For example
|
alpar@2195
|
73 |
lemon::ListUGraph.
|
alpar@2195
|
74 |
\skip addEdge
|
alpar@2195
|
75 |
\until addEdge
|
alpar@2195
|
76 |
|
alpar@2195
|
77 |
In the next few lines we add some more nodes and edges and to the graph we need.
|
alpar@2195
|
78 |
Those lines are not very interesting so we skip them, but you find the whole
|
alpar@2195
|
79 |
working program in file hello_lemon.cc in the demo section.
|
alpar@2195
|
80 |
|
alpar@2195
|
81 |
The next statement must be familiar. But what is that INVALID in the \c while
|
alpar@2195
|
82 |
test statement? In LEMON we usually use the INVALID to check if an object
|
alpar@2195
|
83 |
contains valid information.
|
alpar@2195
|
84 |
\skip current_node
|
alpar@2195
|
85 |
\until {
|
alpar@2195
|
86 |
|
alpar@2195
|
87 |
We take the current node and write out the character assigned to it. Is's easy
|
alpar@2195
|
88 |
with the \c char_map.
|
alpar@2195
|
89 |
\skip std
|
alpar@2195
|
90 |
\until std
|
alpar@2195
|
91 |
|
alpar@2195
|
92 |
And here comes the trick. OutEdgeIt iterates on outgoing edges of a given node.
|
alpar@2195
|
93 |
We pass the current node as argument to it, so the \c edge iterator will stand
|
alpar@2195
|
94 |
on the first outgoing edge of the current node, or will be INVALID if the node
|
alpar@2195
|
95 |
has no outgoing edges.
|
alpar@2195
|
96 |
\skip edge
|
alpar@2195
|
97 |
\until edge
|
alpar@2195
|
98 |
|
alpar@2195
|
99 |
The graph we built before is linear, so we know that it ends, when no more outgoing
|
alpar@2195
|
100 |
edges found. Otherwise the current node must be the node the edge points to.
|
alpar@2195
|
101 |
Basic information about an edge can be requested from the graph.
|
alpar@2195
|
102 |
\skip if
|
alpar@2195
|
103 |
\until }
|
alpar@2195
|
104 |
|
alpar@2195
|
105 |
Finish the code, just to be precise.
|
alpar@2195
|
106 |
\skip return
|
alpar@2195
|
107 |
\until }
|
alpar@2195
|
108 |
|
alpar@2195
|
109 |
|
alpar@2195
|
110 |
\section compile_hw Compiling Hello World
|
alpar@2195
|
111 |
To compile this program all you have to do is type in
|
alpar@2195
|
112 |
\code g++ -ohw hello_world.cc \endcode
|
alpar@2195
|
113 |
and press \c Enter! This is the case if you installed LEMON on your system.
|
alpar@2195
|
114 |
(For more information see the LEMON installation instructions.)
|
alpar@2195
|
115 |
|
alpar@2195
|
116 |
This is because LEMON is template library and most of it's code has to be available
|
alpar@2195
|
117 |
as source code during compilation.
|
alpar@2195
|
118 |
|
alpar@2195
|
119 |
Most programs using LEMON will compile as easy as this one unless you want to
|
alpar@2195
|
120 |
use some performance measuring tools LEMON can provide. Then you need to link
|
alpar@2195
|
121 |
an additional library against your program.
|
alpar@2195
|
122 |
*/
|