demo/hello_lemon.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 23 Oct 2008 14:07:30 +0200
changeset 6 da96f28684f7
child 16 ed4c8506e151
permissions -rw-r--r--
Extend and improve the first two chapters
kpeter@5
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@5
     2
 *
kpeter@5
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@5
     4
 *
kpeter@5
     5
 * Copyright (C) 2003-2008
kpeter@5
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@5
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@5
     8
 *
kpeter@5
     9
 * Permission to use, modify and distribute this software is granted
kpeter@5
    10
 * provided that this copyright notice appears in all copies. For
kpeter@5
    11
 * precise terms see the accompanying LICENSE file.
kpeter@5
    12
 *
kpeter@5
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@5
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@5
    15
 * purpose.
kpeter@5
    16
 *
kpeter@5
    17
 */
kpeter@5
    18
kpeter@5
    19
///\file
kpeter@5
    20
///\brief Demonstrating the basic concepts and fetures of LEMON
kpeter@5
    21
///
kpeter@5
    22
/// This program is intended to be a "Hello World!" program that shows
kpeter@5
    23
/// the very basic notions of LEMON: \ref graphs "graphs" and
kpeter@5
    24
/// \ref maps "maps".
kpeter@5
    25
///
kpeter@5
    26
/// \include hello_lemon.cc
kpeter@5
    27
kpeter@5
    28
#include <iostream>
kpeter@5
    29
#include <lemon/list_graph.h>
kpeter@5
    30
kpeter@5
    31
int main()
kpeter@5
    32
{
kpeter@5
    33
  // Convenience type definitions
kpeter@5
    34
  typedef lemon::ListDigraph Digraph;
kpeter@5
    35
  typedef Digraph::Node Node;
kpeter@5
    36
  typedef Digraph::Arc Arc;
kpeter@5
    37
  typedef Digraph::NodeIt NodeIt;
kpeter@5
    38
  typedef Digraph::ArcIt ArcIt;
kpeter@5
    39
  typedef Digraph::ArcMap<int> LengthMap;
kpeter@5
    40
  using lemon::INVALID;
kpeter@5
    41
kpeter@5
    42
  // Create a directed graph
kpeter@5
    43
  Digraph g;
kpeter@5
    44
kpeter@5
    45
  // Add nodes to the digraph
kpeter@5
    46
  Node v1 = g.addNode();
kpeter@5
    47
  Node v2 = g.addNode();
kpeter@5
    48
  Node v3 = g.addNode();
kpeter@5
    49
  Node v4 = g.addNode();
kpeter@5
    50
kpeter@5
    51
  // Add arcs to the digraph
kpeter@5
    52
  Arc v1_v2 = g.addArc(v1, v2);
kpeter@5
    53
  Arc v1_v3 = g.addArc(v1, v3);
kpeter@5
    54
  Arc v2_v3 = g.addArc(v2, v3);
kpeter@5
    55
  Arc v2_v4 = g.addArc(v2, v4);
kpeter@5
    56
  Arc v3_v4 = g.addArc(v3, v4);
kpeter@5
    57
kpeter@5
    58
  // Create an arc map (length)
kpeter@5
    59
  LengthMap length(g);
kpeter@5
    60
kpeter@5
    61
  // Set the length of each arc
kpeter@5
    62
  length[v1_v2]  = 10;
kpeter@5
    63
  length[v1_v3]  = 20;
kpeter@5
    64
  length[v2_v3] = 5;
kpeter@5
    65
  length[v2_v4] = 25;
kpeter@5
    66
  length[v3_v4] = 10;
kpeter@5
    67
kpeter@5
    68
  // Welcome message
kpeter@5
    69
  std::cout << "Hello World!" << std::endl;
kpeter@5
    70
  std::cout << "This is LEMON library here. We have a direceted graph.";
kpeter@5
    71
  std::cout << std::endl << std::endl;
kpeter@5
    72
kpeter@5
    73
  // Iterate through the nodes and print their IDs
kpeter@5
    74
  std::cout << "Nodes:";
kpeter@5
    75
  for (NodeIt n(g); n != INVALID; ++n)
kpeter@5
    76
    std::cout << " " << g.id(n);
kpeter@5
    77
  std::cout << std::endl;
kpeter@5
    78
kpeter@5
    79
  // Iterate through the arcs and print the IDs of their
kpeter@5
    80
  // source and target nodes
kpeter@5
    81
  std::cout << "Arcs:";
kpeter@5
    82
  for (ArcIt a(g); a != INVALID; ++a)
kpeter@5
    83
    std::cout << " (" << g.id(g.source(a)) << ","
kpeter@5
    84
              << g.id(g.target(a)) << ")";
kpeter@5
    85
  std::cout << std::endl << std::endl;
kpeter@5
    86
kpeter@5
    87
  // Iterate through the arcs and print their length
kpeter@5
    88
  std::cout << "There is a map on the arcs (length):" << std::endl;
kpeter@5
    89
  std::cout << std::endl;
kpeter@5
    90
  for (ArcIt a(g); a != INVALID; ++a)
kpeter@5
    91
    std::cout << "length(" << g.id(g.source(a)) << ","
kpeter@5
    92
              << g.id(g.target(a)) << ")=" << length[a] << std::endl;
kpeter@5
    93
  std::cout << std::endl;
kpeter@5
    94
kpeter@5
    95
  return 0;
kpeter@5
    96
}