demo/lgf_demo.cc
changeset 127 1c9a9e2f7d4d
child 143 c3b45bb643b0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/lgf_demo.cc	Thu Apr 17 15:18:45 2008 +0100
     1.3 @@ -0,0 +1,153 @@
     1.4 +/* -*- C++ -*-
     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 +///\ingroup demos
    1.23 +///\file
    1.24 +///\brief Demonstrating graph input and output
    1.25 +///
    1.26 +/// This simple demo program gives an example of how to read and write
    1.27 +/// a graph and additional maps (on the nodes or the edges) from/to a
    1.28 +/// stream. 
    1.29 +///
    1.30 +/// \include reader_writer_demo.cc
    1.31 +
    1.32 +#include <iostream>
    1.33 +#include <lemon/smart_graph.h>
    1.34 +#include <lemon/lgf_reader.h>
    1.35 +#include <lemon/lgf_writer.h>
    1.36 +#include <lemon/random.h>
    1.37 +
    1.38 +
    1.39 +using namespace lemon;
    1.40 +
    1.41 +int main(int argc, const char *argv[]) {
    1.42 +  const int n = argc > 1 ? std::atoi(argv[1]) : 20;
    1.43 +  const int e = argc > 2 ? std::atoi(argv[2]) : static_cast<int>(n * log(n));
    1.44 +  const int m = argc > 3 ? std::atoi(argv[3]) : 100;
    1.45 +
    1.46 +  SmartDigraph digraph;
    1.47 +
    1.48 +  std::stringstream ss;
    1.49 +
    1.50 +  try {
    1.51 +
    1.52 +    typedef SmartDigraph Digraph;
    1.53 +    typedef Digraph::Node Node;
    1.54 +    typedef Digraph::Arc Arc;
    1.55 +    typedef Digraph::ArcIt ArcIt;
    1.56 +
    1.57 +    typedef Digraph::NodeMap<int> PotentialMap;
    1.58 +    typedef Digraph::ArcMap<int> CapacityMap;
    1.59 +    typedef Digraph::ArcMap<std::string> NameMap;
    1.60 +
    1.61 +    Digraph digraph;
    1.62 +    PotentialMap potential(digraph);
    1.63 +    CapacityMap capacity(digraph);
    1.64 +    NameMap name(digraph);
    1.65 +
    1.66 +    std::vector<Node> nodes;
    1.67 +    for (int i = 0; i < n; ++i) {
    1.68 +      Node node = digraph.addNode();
    1.69 +      potential[node] = rnd[m];
    1.70 +      nodes.push_back(node);
    1.71 +    }
    1.72 +
    1.73 +    std::vector<Arc> arcs;
    1.74 +    for (int i = 0; i < e; ++i) {
    1.75 +      int s = rnd[n];
    1.76 +      int t = rnd[n];
    1.77 +      int c = rnd[m];
    1.78 +      Arc arc = digraph.addArc(nodes[s], nodes[t]);
    1.79 +      capacity[arc] = c;
    1.80 +      std::ostringstream os;
    1.81 +      os << "arc \t" << i << std::endl;
    1.82 +      name[arc] = os.str();
    1.83 +      arcs.push_back(arc);
    1.84 +    }
    1.85 +
    1.86 +
    1.87 +    DigraphWriter<Digraph>(ss, digraph).
    1.88 +      nodeMap("potential", potential).
    1.89 +      arcMap("capacity", capacity).
    1.90 +      arcMap("name", name).
    1.91 +      node("source", nodes[0]).
    1.92 +      node("target", nodes[1]).
    1.93 +      arc("bottleneck", arcs[e / 2]).
    1.94 +      attribute("creator", "lemon library").
    1.95 +      run();
    1.96 +
    1.97 +  } catch (DataFormatError& error) {
    1.98 +    std::cerr << error.what() << std::endl;
    1.99 +  }
   1.100 +
   1.101 +  try {
   1.102 +
   1.103 +    typedef SmartDigraph Digraph;
   1.104 +    typedef Digraph::Node Node;
   1.105 +    typedef Digraph::Arc Arc;
   1.106 +    typedef Digraph::ArcIt ArcIt;
   1.107 +
   1.108 +    typedef Digraph::NodeMap<int> LabelMap;
   1.109 +    typedef Digraph::NodeMap<int> PotentialMap;
   1.110 +    typedef Digraph::ArcMap<int> CapacityMap;
   1.111 +    typedef Digraph::ArcMap<std::string> NameMap;
   1.112 +
   1.113 +    Digraph digraph;
   1.114 +    LabelMap label(digraph);
   1.115 +    PotentialMap potential(digraph);
   1.116 +    CapacityMap capacity(digraph);
   1.117 +    NameMap name(digraph);
   1.118 +
   1.119 +    Node s, t;
   1.120 +    Arc a;
   1.121 +    
   1.122 +    std::string creator;
   1.123 +
   1.124 +    for (int i = 0; i < n; ++i) {
   1.125 +      Node node = digraph.addNode();
   1.126 +      label[node] = i;
   1.127 +    }
   1.128 +    
   1.129 +    DigraphReader<Digraph>(ss, digraph).
   1.130 +      useNodes(label).
   1.131 +      nodeMap("potential", potential).
   1.132 +      arcMap("capacity", capacity).
   1.133 +      arcMap("name", name).
   1.134 +      node("source", s).
   1.135 +      node("target", t).
   1.136 +      arc("bottleneck", a).
   1.137 +      attribute("creator", creator).
   1.138 +      run();
   1.139 +
   1.140 +    DigraphWriter<Digraph>(std::cout, digraph).
   1.141 +      nodeMap("potential", potential).
   1.142 +      arcMap("capacity", capacity).
   1.143 +      arcMap("name", name).
   1.144 +      node("source", s).
   1.145 +      node("target", t).
   1.146 +      arc("bottleneck", a).
   1.147 +      attribute("creator", creator).
   1.148 +      run();
   1.149 +
   1.150 +  } catch (DataFormatError& error) {
   1.151 +    std::cerr << error.what() << std::endl;
   1.152 +  }
   1.153 +
   1.154 +
   1.155 +  return 0;
   1.156 +}