demo/lgf_demo.cc
changeset 164 00d297da491e
parent 143 c3b45bb643b0
child 191 abc5b9d0c67e
child 192 7bf5f97d574f
     1.1 --- a/demo/lgf_demo.cc	Sat May 31 12:31:21 2008 +0200
     1.2 +++ b/demo/lgf_demo.cc	Sat May 31 12:34:44 2008 +0200
     1.3 @@ -20,11 +20,15 @@
     1.4  ///\file
     1.5  ///\brief Demonstrating graph input and output
     1.6  ///
     1.7 -/// This simple demo program gives an example of how to read and write
     1.8 -/// a graph and additional maps (on the nodes or the edges) from/to a
     1.9 -/// stream. 
    1.10 +/// This program gives an example of how to load a directed graph from
    1.11 +/// an \ref lgf-format "LGF" file with the \ref lemon::DigraphReader
    1.12 +/// "DigraphReader" class.
    1.13  ///
    1.14 -/// \include reader_writer_demo.cc
    1.15 +/// The \c "digraph.lgf" file:
    1.16 +/// \include digraph.lgf
    1.17 +///
    1.18 +/// And the program which reads it:
    1.19 +/// \include lgf_demo.cc
    1.20  
    1.21  #include <iostream>
    1.22  #include <lemon/smart_graph.h>
    1.23 @@ -35,119 +39,28 @@
    1.24  
    1.25  using namespace lemon;
    1.26  
    1.27 -int main(int argc, const char *argv[]) {
    1.28 -  const int n = argc > 1 ? std::atoi(argv[1]) : 20;
    1.29 -  const int e = argc > 2 ? std::atoi(argv[2]) : static_cast<int>(n * std::log(double(n)));
    1.30 -  const int m = argc > 3 ? std::atoi(argv[3]) : 100;
    1.31 +int main() {
    1.32 +  SmartDigraph g;
    1.33 +  SmartDigraph::ArcMap<int> cap(g);
    1.34 +  SmartDigraph::Node s, t;
    1.35  
    1.36 -  SmartDigraph digraph;
    1.37 +  digraphReader("digraph.lgf", g). // read the directeg graph into g
    1.38 +    arcMap("capacity", cap).       // read the 'capacity' arc map into cap
    1.39 +    node("source", s).             // read 'source' node to s
    1.40 +    node("target", t).             // read 'target' node to t
    1.41 +    run();
    1.42  
    1.43 -  std::stringstream ss;
    1.44 +  std::cout << "Digraph read from 'digraph.lgf'" << std::endl;
    1.45 +  std::cout << "Number of nodes: " << countNodes(g) << std::endl;
    1.46 +  std::cout << "Number of arcs: " << countArcs(g) << std::endl;
    1.47  
    1.48 -  try {
    1.49 +  std::cout << "We can write it to the standard output:" << std::endl;
    1.50  
    1.51 -    typedef SmartDigraph Digraph;
    1.52 -    typedef Digraph::Node Node;
    1.53 -    typedef Digraph::Arc Arc;
    1.54 -    typedef Digraph::ArcIt ArcIt;
    1.55 -
    1.56 -    typedef Digraph::NodeMap<int> PotentialMap;
    1.57 -    typedef Digraph::ArcMap<int> CapacityMap;
    1.58 -    typedef Digraph::ArcMap<std::string> NameMap;
    1.59 -
    1.60 -    Digraph digraph;
    1.61 -    PotentialMap potential(digraph);
    1.62 -    CapacityMap capacity(digraph);
    1.63 -    NameMap name(digraph);
    1.64 -
    1.65 -    std::vector<Node> nodes;
    1.66 -    for (int i = 0; i < n; ++i) {
    1.67 -      Node node = digraph.addNode();
    1.68 -      potential[node] = rnd[m];
    1.69 -      nodes.push_back(node);
    1.70 -    }
    1.71 -
    1.72 -    std::vector<Arc> arcs;
    1.73 -    for (int i = 0; i < e; ++i) {
    1.74 -      int s = rnd[n];
    1.75 -      int t = rnd[n];
    1.76 -      int c = rnd[m];
    1.77 -      Arc arc = digraph.addArc(nodes[s], nodes[t]);
    1.78 -      capacity[arc] = c;
    1.79 -      std::ostringstream os;
    1.80 -      os << "arc \t" << i << std::endl;
    1.81 -      name[arc] = os.str();
    1.82 -      arcs.push_back(arc);
    1.83 -    }
    1.84 -
    1.85 -
    1.86 -    DigraphWriter<Digraph>(ss, digraph).
    1.87 -      nodeMap("potential", potential).
    1.88 -      arcMap("capacity", capacity).
    1.89 -      arcMap("name", name).
    1.90 -      node("source", nodes[0]).
    1.91 -      node("target", nodes[1]).
    1.92 -      arc("bottleneck", arcs[e / 2]).
    1.93 -      attribute("creator", "lemon library").
    1.94 -      run();
    1.95 -
    1.96 -  } catch (DataFormatError& error) {
    1.97 -    std::cerr << error.what() << std::endl;
    1.98 -  }
    1.99 -
   1.100 -  try {
   1.101 -
   1.102 -    typedef SmartDigraph Digraph;
   1.103 -    typedef Digraph::Node Node;
   1.104 -    typedef Digraph::Arc Arc;
   1.105 -    typedef Digraph::ArcIt ArcIt;
   1.106 -
   1.107 -    typedef Digraph::NodeMap<int> LabelMap;
   1.108 -    typedef Digraph::NodeMap<int> PotentialMap;
   1.109 -    typedef Digraph::ArcMap<int> CapacityMap;
   1.110 -    typedef Digraph::ArcMap<std::string> NameMap;
   1.111 -
   1.112 -    Digraph digraph;
   1.113 -    LabelMap label(digraph);
   1.114 -    PotentialMap potential(digraph);
   1.115 -    CapacityMap capacity(digraph);
   1.116 -    NameMap name(digraph);
   1.117 -
   1.118 -    Node s, t;
   1.119 -    Arc a;
   1.120 -    
   1.121 -    std::string creator;
   1.122 -
   1.123 -    for (int i = 0; i < n; ++i) {
   1.124 -      Node node = digraph.addNode();
   1.125 -      label[node] = i;
   1.126 -    }
   1.127 -    
   1.128 -    DigraphReader<Digraph>(ss, digraph).
   1.129 -      useNodes(label).
   1.130 -      nodeMap("potential", potential).
   1.131 -      arcMap("capacity", capacity).
   1.132 -      arcMap("name", name).
   1.133 -      node("source", s).
   1.134 -      node("target", t).
   1.135 -      arc("bottleneck", a).
   1.136 -      attribute("creator", creator).
   1.137 -      run();
   1.138 -
   1.139 -    DigraphWriter<Digraph>(std::cout, digraph).
   1.140 -      nodeMap("potential", potential).
   1.141 -      arcMap("capacity", capacity).
   1.142 -      arcMap("name", name).
   1.143 -      node("source", s).
   1.144 -      node("target", t).
   1.145 -      arc("bottleneck", a).
   1.146 -      attribute("creator", creator).
   1.147 -      run();
   1.148 -
   1.149 -  } catch (DataFormatError& error) {
   1.150 -    std::cerr << error.what() << std::endl;
   1.151 -  }
   1.152 -
   1.153 +  digraphWriter(std::cout, g).     // write g to the standard output
   1.154 +    arcMap("capacity", cap).       // write cap into 'capacity'
   1.155 +    node("source", s).             // write s to 'source'
   1.156 +    node("target", t).             // write t to 'target'
   1.157 +    run();
   1.158  
   1.159    return 0;
   1.160  }