COIN-OR::LEMON - Graph Library

Changeset 16:ed4c8506e151 in lemon-tutorial


Ignore:
Timestamp:
11/03/08 23:46:30 (16 years ago)
Author:
Peter Kovacs <kpeter@…>
Branch:
default
Phase:
public
Message:

Simplify the first example code + fix toc

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • demo/hello_lemon.cc

    r5 r16  
    1818
    1919///\file
    20 ///\brief Demonstrating the basic concepts and fetures of LEMON
     20///\brief Simple "Hello World!" program for LEMON.
    2121///
    22 /// This program is intended to be a "Hello World!" program that shows
    23 /// the very basic notions of LEMON: \ref graphs "graphs" and
    24 /// \ref maps "maps".
    25 ///
     22/// Simple "Hello World!" program for LEMON.
    2623/// \include hello_lemon.cc
    2724
     
    3128int main()
    3229{
    33   // Convenience type definitions
    34   typedef lemon::ListDigraph Digraph;
    35   typedef Digraph::Node Node;
    36   typedef Digraph::Arc Arc;
    37   typedef Digraph::NodeIt NodeIt;
    38   typedef Digraph::ArcIt ArcIt;
    39   typedef Digraph::ArcMap<int> LengthMap;
    40   using lemon::INVALID;
     30  typedef lemon::ListDigraph Graph;
     31  Graph g;
    4132
    42   // Create a directed graph
    43   Digraph g;
     33  Graph::Node u = g.addNode();
     34  Graph::Node v = g.addNode();
     35  Graph::Arc  e = g.addArc(u, v);
    4436
    45   // Add nodes to the digraph
    46   Node v1 = g.addNode();
    47   Node v2 = g.addNode();
    48   Node v3 = g.addNode();
    49   Node v4 = g.addNode();
    50 
    51   // Add arcs to the digraph
    52   Arc v1_v2 = g.addArc(v1, v2);
    53   Arc v1_v3 = g.addArc(v1, v3);
    54   Arc v2_v3 = g.addArc(v2, v3);
    55   Arc v2_v4 = g.addArc(v2, v4);
    56   Arc v3_v4 = g.addArc(v3, v4);
    57 
    58   // Create an arc map (length)
    59   LengthMap length(g);
    60 
    61   // Set the length of each arc
    62   length[v1_v2]  = 10;
    63   length[v1_v3]  = 20;
    64   length[v2_v3] = 5;
    65   length[v2_v4] = 25;
    66   length[v3_v4] = 10;
    67 
    68   // Welcome message
    69   std::cout << "Hello World!" << std::endl;
    70   std::cout << "This is LEMON library here. We have a direceted graph.";
    71   std::cout << std::endl << std::endl;
    72 
    73   // Iterate through the nodes and print their IDs
    74   std::cout << "Nodes:";
    75   for (NodeIt n(g); n != INVALID; ++n)
    76     std::cout << " " << g.id(n);
    77   std::cout << std::endl;
    78 
    79   // Iterate through the arcs and print the IDs of their
    80   // source and target nodes
    81   std::cout << "Arcs:";
    82   for (ArcIt a(g); a != INVALID; ++a)
    83     std::cout << " (" << g.id(g.source(a)) << ","
    84               << g.id(g.target(a)) << ")";
    85   std::cout << std::endl << std::endl;
    86 
    87   // Iterate through the arcs and print their length
    88   std::cout << "There is a map on the arcs (length):" << std::endl;
    89   std::cout << std::endl;
    90   for (ArcIt a(g); a != INVALID; ++a)
    91     std::cout << "length(" << g.id(g.source(a)) << ","
    92               << g.id(g.target(a)) << ")=" << length[a] << std::endl;
    93   std::cout << std::endl;
     37  std::cout << "Hello World! This is LEMON library here." << std::endl;
     38  std::cout << "We have a directed graph with "
     39            << countNodes(g) << " nodes and "
     40            << countArcs(g) << " arc." << std::endl;
    9441
    9542  return 0;
  • getting_started.dox

    r12 r16  
    3030\until }
    3131
    32 First let us briefly explain how this example program works.
    33 (The used notions will be discussed in detail in the following sections.)
     32In this small example a directed graph is created with two nodes and
     33an arc added to it.
    3434
    35 After some convenience typedefs we create a directed graph (\e digraph)
    36 and add some nodes and arcs to it.
    37 ListDigraph is one of the digraph classes implemented in LEMON.
    38 It is based on linked lists, therefore iterating through its nodes and
    39 arcs is fast.
     35Now let us compile this code.
     36(We suppose that you have it in a file called <tt>hello_lemon.cc</tt>.)
    4037
    41 Then we iterate through all nodes of the digraph and print their unique
    42 IDs. We use a constructor of the node iterator to initialize it to the
    43 first node.
    44 The <tt>operator++</tt> is used to step to the next node. After the last
    45 node the iterator becomes invalid (i.e. it is set to \c INVALID).
    46 This is what we exploit in the stop condition.
    47 We iterate through all arcs of the digraph very similarly and print the
    48 IDs of their source (tail) and target (head) nodes using the \c source()
    49 and \c target() member functions.
    50 
    51 After that we create an arc map, which is actually a mapping that assigns
    52 an \c int value (length) to each arc, and we set this value for each arc.
    53 Finally we iterate through all arcs again and print their lengths.
    54 
    55 Now let us compile this simple example program.
    56 
    57 [SEC]hello_lemon_system[SEC] If LEMON is Installed System-Wide
    58 
    59 If LEMON is installed system-wide (into directory \c /usr/local),
     38If LEMON is installed <b>system-wide</b> (into directory \c /usr/local),
    6039then it is very easy to compile this program with the
    6140following command (the argument <tt>-lemon</tt> tells the compiler
    62 that we are using the installed LEMON):
     41that we are using the installed LEMON).
    6342
    6443\verbatim
     
    7352\endverbatim
    7453
    75 [SEC]hello_lemon_user[SEC] If LEMON is Installed User-Local
    76 
    77 Compiling the code is a bit more difficult if you installed LEMON
    78 user-local into a directory (e.g. <tt>~/lemon</tt>) or if you just
    79 skipped the step <tt>make install</tt>.
     54If LEMON is installed <b>user-local</b> into a directory
     55(e.g. <tt>~/lemon</tt>) or if you just skipped the step
     56<tt>make install</tt>, then compiling the code is a bit more difficult.
    8057You have to issue a command like this.
    8158
     
    8764
    8865\verbatim
    89 Hello World!
    90 This is LEMON library here. We have a direceted graph.
    91 
    92 Nodes: 3 2 1 0
    93 Arcs: (2,3) (1,3) (1,2) (0,2) (0,1)
    94 
    95 There is a map on the arcs (length):
    96 
    97 length(2,3)=10
    98 length(1,3)=25
    99 length(1,2)=5
    100 length(0,2)=20
    101 length(0,1)=10
     66Hello World! This is LEMON library here.
     67We have a directed graph with 2 nodes and 1 arc.
    10268\endverbatim
    10369
    104 You may note that iterating through the nodes and arcs is done in the
    105 reverse order compared to the creating order (the IDs are in decreasing
    106 order).
    107 This is due to implementation aspects, that may differ at other graph
    108 types, moreover it may be changed in the next releases.
    109 Thus you should not exploit this method in any way, you should not
    110 suppose anything about the iteration order.
    111 
    11270If you managed to compile and run this example code without any problems,
    113 you can go on reading this tutorial to get to know more features and tools
    114 of LEMON.
    115 Otherwise if you encountered problems that you did not manage to solve,
    116 do not hesitate to
     71you may go on reading this tutorial to get to know the basic notions,
     72features and tools of LEMON. However if you encountered problems that
     73you did not manage to solve, do not hesitate to
    11774<a href="mailto:lemon-user@lemon.cs.elte.hu"><b>contact us</b></a>.
    11875
  • toc.txt

    r10 r16  
    33** intro_tutorial
    44* hello_lemon
    5 ** hello_lemon_system
    6 ** hello_lemon_user
    7 **_digraph_build
    8 **_digraph_iterate
    9 **_standard_maps
    10 **_algorithms
    11 ***_alg_bfs_dfs
    12 ***_alg_dijkstra
    13 ***_alg_kruskal
    14 **_undir_graphs
     5*_digraph_build
     6*_digraph_iterate
     7*_standard_maps
     8*_algorithms
     9**_alg_bfs_dfs
     10**_alg_dijkstra
     11**_alg_kruskal
     12*_undir_graphs
    1513*_tools
    1614**_lgf
Note: See TracChangeset for help on using the changeset viewer.