deba@127: /* -*- C++ -*- deba@127: * deba@127: * This file is a part of LEMON, a generic C++ optimization library deba@127: * deba@127: * Copyright (C) 2003-2008 deba@127: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@127: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@127: * deba@127: * Permission to use, modify and distribute this software is granted deba@127: * provided that this copyright notice appears in all copies. For deba@127: * precise terms see the accompanying LICENSE file. deba@127: * deba@127: * This software is provided "AS IS" with no warranty of any kind, deba@127: * express or implied, and with no claim as to its suitability for any deba@127: * purpose. deba@127: * deba@127: */ deba@127: deba@127: ///\ingroup demos deba@127: ///\file deba@127: ///\brief Demonstrating graph input and output deba@127: /// deba@127: /// This simple demo program gives an example of how to read and write deba@127: /// a graph and additional maps (on the nodes or the edges) from/to a deba@127: /// stream. deba@127: /// deba@127: /// \include reader_writer_demo.cc deba@127: deba@127: #include deba@127: #include deba@127: #include deba@127: #include deba@127: #include deba@127: deba@127: deba@127: using namespace lemon; deba@127: deba@127: int main(int argc, const char *argv[]) { deba@127: const int n = argc > 1 ? std::atoi(argv[1]) : 20; alpar@143: const int e = argc > 2 ? std::atoi(argv[2]) : static_cast(n * std::log(double(n))); deba@127: const int m = argc > 3 ? std::atoi(argv[3]) : 100; deba@127: deba@127: SmartDigraph digraph; deba@127: deba@127: std::stringstream ss; deba@127: deba@127: try { deba@127: deba@127: typedef SmartDigraph Digraph; deba@127: typedef Digraph::Node Node; deba@127: typedef Digraph::Arc Arc; deba@127: typedef Digraph::ArcIt ArcIt; deba@127: deba@127: typedef Digraph::NodeMap PotentialMap; deba@127: typedef Digraph::ArcMap CapacityMap; deba@127: typedef Digraph::ArcMap NameMap; deba@127: deba@127: Digraph digraph; deba@127: PotentialMap potential(digraph); deba@127: CapacityMap capacity(digraph); deba@127: NameMap name(digraph); deba@127: deba@127: std::vector nodes; deba@127: for (int i = 0; i < n; ++i) { deba@127: Node node = digraph.addNode(); deba@127: potential[node] = rnd[m]; deba@127: nodes.push_back(node); deba@127: } deba@127: deba@127: std::vector arcs; deba@127: for (int i = 0; i < e; ++i) { deba@127: int s = rnd[n]; deba@127: int t = rnd[n]; deba@127: int c = rnd[m]; deba@127: Arc arc = digraph.addArc(nodes[s], nodes[t]); deba@127: capacity[arc] = c; deba@127: std::ostringstream os; deba@127: os << "arc \t" << i << std::endl; deba@127: name[arc] = os.str(); deba@127: arcs.push_back(arc); deba@127: } deba@127: deba@127: deba@127: DigraphWriter(ss, digraph). deba@127: nodeMap("potential", potential). deba@127: arcMap("capacity", capacity). deba@127: arcMap("name", name). deba@127: node("source", nodes[0]). deba@127: node("target", nodes[1]). deba@127: arc("bottleneck", arcs[e / 2]). deba@127: attribute("creator", "lemon library"). deba@127: run(); deba@127: deba@127: } catch (DataFormatError& error) { deba@127: std::cerr << error.what() << std::endl; deba@127: } deba@127: deba@127: try { deba@127: deba@127: typedef SmartDigraph Digraph; deba@127: typedef Digraph::Node Node; deba@127: typedef Digraph::Arc Arc; deba@127: typedef Digraph::ArcIt ArcIt; deba@127: deba@127: typedef Digraph::NodeMap LabelMap; deba@127: typedef Digraph::NodeMap PotentialMap; deba@127: typedef Digraph::ArcMap CapacityMap; deba@127: typedef Digraph::ArcMap NameMap; deba@127: deba@127: Digraph digraph; deba@127: LabelMap label(digraph); deba@127: PotentialMap potential(digraph); deba@127: CapacityMap capacity(digraph); deba@127: NameMap name(digraph); deba@127: deba@127: Node s, t; deba@127: Arc a; deba@127: deba@127: std::string creator; deba@127: deba@127: for (int i = 0; i < n; ++i) { deba@127: Node node = digraph.addNode(); deba@127: label[node] = i; deba@127: } deba@127: deba@127: DigraphReader(ss, digraph). deba@127: useNodes(label). deba@127: nodeMap("potential", potential). deba@127: arcMap("capacity", capacity). deba@127: arcMap("name", name). deba@127: node("source", s). deba@127: node("target", t). deba@127: arc("bottleneck", a). deba@127: attribute("creator", creator). deba@127: run(); deba@127: deba@127: DigraphWriter(std::cout, digraph). deba@127: nodeMap("potential", potential). deba@127: arcMap("capacity", capacity). deba@127: arcMap("name", name). deba@127: node("source", s). deba@127: node("target", t). deba@127: arc("bottleneck", a). deba@127: attribute("creator", creator). deba@127: run(); deba@127: deba@127: } catch (DataFormatError& error) { deba@127: std::cerr << error.what() << std::endl; deba@127: } deba@127: deba@127: deba@127: return 0; deba@127: }