demo/hello_lemon.cc
changeset 16 ed4c8506e151
parent 5 29baa4a189bf
child 20 3ffc47b666b1
equal deleted inserted replaced
0:55ea3ffb5b7e 1:9acd8261ca2c
    15  * purpose.
    15  * purpose.
    16  *
    16  *
    17  */
    17  */
    18 
    18 
    19 ///\file
    19 ///\file
    20 ///\brief Demonstrating the basic concepts and fetures of LEMON
    20 ///\brief Simple "Hello World!" program for LEMON.
    21 ///
    21 ///
    22 /// This program is intended to be a "Hello World!" program that shows
    22 /// Simple "Hello World!" program for LEMON.
    23 /// the very basic notions of LEMON: \ref graphs "graphs" and
       
    24 /// \ref maps "maps".
       
    25 ///
       
    26 /// \include hello_lemon.cc
    23 /// \include hello_lemon.cc
    27 
    24 
    28 #include <iostream>
    25 #include <iostream>
    29 #include <lemon/list_graph.h>
    26 #include <lemon/list_graph.h>
    30 
    27 
    31 int main()
    28 int main()
    32 {
    29 {
    33   // Convenience type definitions
    30   typedef lemon::ListDigraph Graph;
    34   typedef lemon::ListDigraph Digraph;
    31   Graph g;
    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;
       
    41 
    32 
    42   // Create a directed graph
    33   Graph::Node u = g.addNode();
    43   Digraph g;
    34   Graph::Node v = g.addNode();
       
    35   Graph::Arc  e = g.addArc(u, v);
    44 
    36 
    45   // Add nodes to the digraph
    37   std::cout << "Hello World! This is LEMON library here." << std::endl;
    46   Node v1 = g.addNode();
    38   std::cout << "We have a directed graph with "
    47   Node v2 = g.addNode();
    39             << countNodes(g) << " nodes and "
    48   Node v3 = g.addNode();
    40             << countArcs(g) << " arc." << std::endl;
    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;
       
    94 
    41 
    95   return 0;
    42   return 0;
    96 }
    43 }