3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
21 ///\brief Demonstrating graph input and output
23 /// This simple demo program gives an example of how to read and write
24 /// a graph and additional maps (on the nodes or the edges) from/to a
27 /// \include reader_writer_demo.cc
30 #include <lemon/smart_graph.h>
31 #include <lemon/lgf_reader.h>
32 #include <lemon/lgf_writer.h>
33 #include <lemon/random.h>
36 using namespace lemon;
38 int main(int argc, const char *argv[]) {
39 const int n = argc > 1 ? std::atoi(argv[1]) : 20;
40 const int e = argc > 2 ? std::atoi(argv[2]) : static_cast<int>(n * std::log(double(n)));
41 const int m = argc > 3 ? std::atoi(argv[3]) : 100;
49 typedef SmartDigraph Digraph;
50 typedef Digraph::Node Node;
51 typedef Digraph::Arc Arc;
52 typedef Digraph::ArcIt ArcIt;
54 typedef Digraph::NodeMap<int> PotentialMap;
55 typedef Digraph::ArcMap<int> CapacityMap;
56 typedef Digraph::ArcMap<std::string> NameMap;
59 PotentialMap potential(digraph);
60 CapacityMap capacity(digraph);
61 NameMap name(digraph);
63 std::vector<Node> nodes;
64 for (int i = 0; i < n; ++i) {
65 Node node = digraph.addNode();
66 potential[node] = rnd[m];
67 nodes.push_back(node);
70 std::vector<Arc> arcs;
71 for (int i = 0; i < e; ++i) {
75 Arc arc = digraph.addArc(nodes[s], nodes[t]);
77 std::ostringstream os;
78 os << "arc \t" << i << std::endl;
84 DigraphWriter<Digraph>(ss, digraph).
85 nodeMap("potential", potential).
86 arcMap("capacity", capacity).
88 node("source", nodes[0]).
89 node("target", nodes[1]).
90 arc("bottleneck", arcs[e / 2]).
91 attribute("creator", "lemon library").
94 } catch (DataFormatError& error) {
95 std::cerr << error.what() << std::endl;
100 typedef SmartDigraph Digraph;
101 typedef Digraph::Node Node;
102 typedef Digraph::Arc Arc;
103 typedef Digraph::ArcIt ArcIt;
105 typedef Digraph::NodeMap<int> LabelMap;
106 typedef Digraph::NodeMap<int> PotentialMap;
107 typedef Digraph::ArcMap<int> CapacityMap;
108 typedef Digraph::ArcMap<std::string> NameMap;
111 LabelMap label(digraph);
112 PotentialMap potential(digraph);
113 CapacityMap capacity(digraph);
114 NameMap name(digraph);
121 for (int i = 0; i < n; ++i) {
122 Node node = digraph.addNode();
126 DigraphReader<Digraph>(ss, digraph).
128 nodeMap("potential", potential).
129 arcMap("capacity", capacity).
130 arcMap("name", name).
133 arc("bottleneck", a).
134 attribute("creator", creator).
137 DigraphWriter<Digraph>(std::cout, digraph).
138 nodeMap("potential", potential).
139 arcMap("capacity", capacity).
140 arcMap("name", name).
143 arc("bottleneck", a).
144 attribute("creator", creator).
147 } catch (DataFormatError& error) {
148 std::cerr << error.what() << std::endl;