Port demo/hello_lemon.cc from SVN -r3501
authorPeter Kovacs <kpeter@inf.elte.hu>
Thu, 23 Oct 2008 14:06:53 +0200
changeset 529baa4a189bf
parent 4 89f6fff82e93
child 6 da96f28684f7
Port demo/hello_lemon.cc from SVN -r3501
demo/hello_lemon.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/hello_lemon.cc	Thu Oct 23 14:06:53 2008 +0200
     1.3 @@ -0,0 +1,96 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +///\file
    1.23 +///\brief Demonstrating the basic concepts and fetures of LEMON
    1.24 +///
    1.25 +/// This program is intended to be a "Hello World!" program that shows
    1.26 +/// the very basic notions of LEMON: \ref graphs "graphs" and
    1.27 +/// \ref maps "maps".
    1.28 +///
    1.29 +/// \include hello_lemon.cc
    1.30 +
    1.31 +#include <iostream>
    1.32 +#include <lemon/list_graph.h>
    1.33 +
    1.34 +int main()
    1.35 +{
    1.36 +  // Convenience type definitions
    1.37 +  typedef lemon::ListDigraph Digraph;
    1.38 +  typedef Digraph::Node Node;
    1.39 +  typedef Digraph::Arc Arc;
    1.40 +  typedef Digraph::NodeIt NodeIt;
    1.41 +  typedef Digraph::ArcIt ArcIt;
    1.42 +  typedef Digraph::ArcMap<int> LengthMap;
    1.43 +  using lemon::INVALID;
    1.44 +
    1.45 +  // Create a directed graph
    1.46 +  Digraph g;
    1.47 +
    1.48 +  // Add nodes to the digraph
    1.49 +  Node v1 = g.addNode();
    1.50 +  Node v2 = g.addNode();
    1.51 +  Node v3 = g.addNode();
    1.52 +  Node v4 = g.addNode();
    1.53 +
    1.54 +  // Add arcs to the digraph
    1.55 +  Arc v1_v2 = g.addArc(v1, v2);
    1.56 +  Arc v1_v3 = g.addArc(v1, v3);
    1.57 +  Arc v2_v3 = g.addArc(v2, v3);
    1.58 +  Arc v2_v4 = g.addArc(v2, v4);
    1.59 +  Arc v3_v4 = g.addArc(v3, v4);
    1.60 +
    1.61 +  // Create an arc map (length)
    1.62 +  LengthMap length(g);
    1.63 +
    1.64 +  // Set the length of each arc
    1.65 +  length[v1_v2]  = 10;
    1.66 +  length[v1_v3]  = 20;
    1.67 +  length[v2_v3] = 5;
    1.68 +  length[v2_v4] = 25;
    1.69 +  length[v3_v4] = 10;
    1.70 +
    1.71 +  // Welcome message
    1.72 +  std::cout << "Hello World!" << std::endl;
    1.73 +  std::cout << "This is LEMON library here. We have a direceted graph.";
    1.74 +  std::cout << std::endl << std::endl;
    1.75 +
    1.76 +  // Iterate through the nodes and print their IDs
    1.77 +  std::cout << "Nodes:";
    1.78 +  for (NodeIt n(g); n != INVALID; ++n)
    1.79 +    std::cout << " " << g.id(n);
    1.80 +  std::cout << std::endl;
    1.81 +
    1.82 +  // Iterate through the arcs and print the IDs of their
    1.83 +  // source and target nodes
    1.84 +  std::cout << "Arcs:";
    1.85 +  for (ArcIt a(g); a != INVALID; ++a)
    1.86 +    std::cout << " (" << g.id(g.source(a)) << ","
    1.87 +              << g.id(g.target(a)) << ")";
    1.88 +  std::cout << std::endl << std::endl;
    1.89 +
    1.90 +  // Iterate through the arcs and print their length
    1.91 +  std::cout << "There is a map on the arcs (length):" << std::endl;
    1.92 +  std::cout << std::endl;
    1.93 +  for (ArcIt a(g); a != INVALID; ++a)
    1.94 +    std::cout << "length(" << g.id(g.source(a)) << ","
    1.95 +              << g.id(g.target(a)) << ")=" << length[a] << std::endl;
    1.96 +  std::cout << std::endl;
    1.97 +
    1.98 +  return 0;
    1.99 +}