demo/lgf_demo.cc
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 17 Apr 2008 15:54:30 +0100
changeset 135 6e7aee618f03
child 143 c3b45bb643b0
permissions -rw-r--r--
Merge with graph_to_eps.h & Co.
deba@127
     1
/* -*- C++ -*-
deba@127
     2
 *
deba@127
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@127
     4
 *
deba@127
     5
 * Copyright (C) 2003-2008
deba@127
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@127
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@127
     8
 *
deba@127
     9
 * Permission to use, modify and distribute this software is granted
deba@127
    10
 * provided that this copyright notice appears in all copies. For
deba@127
    11
 * precise terms see the accompanying LICENSE file.
deba@127
    12
 *
deba@127
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@127
    14
 * express or implied, and with no claim as to its suitability for any
deba@127
    15
 * purpose.
deba@127
    16
 *
deba@127
    17
 */
deba@127
    18
deba@127
    19
///\ingroup demos
deba@127
    20
///\file
deba@127
    21
///\brief Demonstrating graph input and output
deba@127
    22
///
deba@127
    23
/// This simple demo program gives an example of how to read and write
deba@127
    24
/// a graph and additional maps (on the nodes or the edges) from/to a
deba@127
    25
/// stream. 
deba@127
    26
///
deba@127
    27
/// \include reader_writer_demo.cc
deba@127
    28
deba@127
    29
#include <iostream>
deba@127
    30
#include <lemon/smart_graph.h>
deba@127
    31
#include <lemon/lgf_reader.h>
deba@127
    32
#include <lemon/lgf_writer.h>
deba@127
    33
#include <lemon/random.h>
deba@127
    34
deba@127
    35
deba@127
    36
using namespace lemon;
deba@127
    37
deba@127
    38
int main(int argc, const char *argv[]) {
deba@127
    39
  const int n = argc > 1 ? std::atoi(argv[1]) : 20;
deba@127
    40
  const int e = argc > 2 ? std::atoi(argv[2]) : static_cast<int>(n * log(n));
deba@127
    41
  const int m = argc > 3 ? std::atoi(argv[3]) : 100;
deba@127
    42
deba@127
    43
  SmartDigraph digraph;
deba@127
    44
deba@127
    45
  std::stringstream ss;
deba@127
    46
deba@127
    47
  try {
deba@127
    48
deba@127
    49
    typedef SmartDigraph Digraph;
deba@127
    50
    typedef Digraph::Node Node;
deba@127
    51
    typedef Digraph::Arc Arc;
deba@127
    52
    typedef Digraph::ArcIt ArcIt;
deba@127
    53
deba@127
    54
    typedef Digraph::NodeMap<int> PotentialMap;
deba@127
    55
    typedef Digraph::ArcMap<int> CapacityMap;
deba@127
    56
    typedef Digraph::ArcMap<std::string> NameMap;
deba@127
    57
deba@127
    58
    Digraph digraph;
deba@127
    59
    PotentialMap potential(digraph);
deba@127
    60
    CapacityMap capacity(digraph);
deba@127
    61
    NameMap name(digraph);
deba@127
    62
deba@127
    63
    std::vector<Node> nodes;
deba@127
    64
    for (int i = 0; i < n; ++i) {
deba@127
    65
      Node node = digraph.addNode();
deba@127
    66
      potential[node] = rnd[m];
deba@127
    67
      nodes.push_back(node);
deba@127
    68
    }
deba@127
    69
deba@127
    70
    std::vector<Arc> arcs;
deba@127
    71
    for (int i = 0; i < e; ++i) {
deba@127
    72
      int s = rnd[n];
deba@127
    73
      int t = rnd[n];
deba@127
    74
      int c = rnd[m];
deba@127
    75
      Arc arc = digraph.addArc(nodes[s], nodes[t]);
deba@127
    76
      capacity[arc] = c;
deba@127
    77
      std::ostringstream os;
deba@127
    78
      os << "arc \t" << i << std::endl;
deba@127
    79
      name[arc] = os.str();
deba@127
    80
      arcs.push_back(arc);
deba@127
    81
    }
deba@127
    82
deba@127
    83
deba@127
    84
    DigraphWriter<Digraph>(ss, digraph).
deba@127
    85
      nodeMap("potential", potential).
deba@127
    86
      arcMap("capacity", capacity).
deba@127
    87
      arcMap("name", name).
deba@127
    88
      node("source", nodes[0]).
deba@127
    89
      node("target", nodes[1]).
deba@127
    90
      arc("bottleneck", arcs[e / 2]).
deba@127
    91
      attribute("creator", "lemon library").
deba@127
    92
      run();
deba@127
    93
deba@127
    94
  } catch (DataFormatError& error) {
deba@127
    95
    std::cerr << error.what() << std::endl;
deba@127
    96
  }
deba@127
    97
deba@127
    98
  try {
deba@127
    99
deba@127
   100
    typedef SmartDigraph Digraph;
deba@127
   101
    typedef Digraph::Node Node;
deba@127
   102
    typedef Digraph::Arc Arc;
deba@127
   103
    typedef Digraph::ArcIt ArcIt;
deba@127
   104
deba@127
   105
    typedef Digraph::NodeMap<int> LabelMap;
deba@127
   106
    typedef Digraph::NodeMap<int> PotentialMap;
deba@127
   107
    typedef Digraph::ArcMap<int> CapacityMap;
deba@127
   108
    typedef Digraph::ArcMap<std::string> NameMap;
deba@127
   109
deba@127
   110
    Digraph digraph;
deba@127
   111
    LabelMap label(digraph);
deba@127
   112
    PotentialMap potential(digraph);
deba@127
   113
    CapacityMap capacity(digraph);
deba@127
   114
    NameMap name(digraph);
deba@127
   115
deba@127
   116
    Node s, t;
deba@127
   117
    Arc a;
deba@127
   118
    
deba@127
   119
    std::string creator;
deba@127
   120
deba@127
   121
    for (int i = 0; i < n; ++i) {
deba@127
   122
      Node node = digraph.addNode();
deba@127
   123
      label[node] = i;
deba@127
   124
    }
deba@127
   125
    
deba@127
   126
    DigraphReader<Digraph>(ss, digraph).
deba@127
   127
      useNodes(label).
deba@127
   128
      nodeMap("potential", potential).
deba@127
   129
      arcMap("capacity", capacity).
deba@127
   130
      arcMap("name", name).
deba@127
   131
      node("source", s).
deba@127
   132
      node("target", t).
deba@127
   133
      arc("bottleneck", a).
deba@127
   134
      attribute("creator", creator).
deba@127
   135
      run();
deba@127
   136
deba@127
   137
    DigraphWriter<Digraph>(std::cout, digraph).
deba@127
   138
      nodeMap("potential", potential).
deba@127
   139
      arcMap("capacity", capacity).
deba@127
   140
      arcMap("name", name).
deba@127
   141
      node("source", s).
deba@127
   142
      node("target", t).
deba@127
   143
      arc("bottleneck", a).
deba@127
   144
      attribute("creator", creator).
deba@127
   145
      run();
deba@127
   146
deba@127
   147
  } catch (DataFormatError& error) {
deba@127
   148
    std::cerr << error.what() << std::endl;
deba@127
   149
  }
deba@127
   150
deba@127
   151
deba@127
   152
  return 0;
deba@127
   153
}