Reworking demo file
authorBalazs Dezso <deba@inf.elte.hu>
Sat, 31 May 2008 12:34:44 +0200
changeset 16400d297da491e
parent 163 c82fd9568d75
child 165 b4c336c27a03
Reworking demo file
demo/Makefile.am
demo/digraph.lgf
demo/lgf_demo.cc
     1.1 --- a/demo/Makefile.am	Sat May 31 12:31:21 2008 +0200
     1.2 +++ b/demo/Makefile.am	Sat May 31 12:34:44 2008 +0200
     1.3 @@ -1,5 +1,6 @@
     1.4  EXTRA_DIST += \
     1.5 -	demo/CMakeLists.txt
     1.6 +	demo/CMakeLists.txt \
     1.7 +	demo/digraph.lgf
     1.8  
     1.9  if WANT_DEMO
    1.10  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/demo/digraph.lgf	Sat May 31 12:34:44 2008 +0200
     2.3 @@ -0,0 +1,29 @@
     2.4 +@nodes
     2.5 +label
     2.6 +0
     2.7 +1
     2.8 +2
     2.9 +3
    2.10 +4
    2.11 +5
    2.12 +6
    2.13 +7
    2.14 +@arcs
    2.15 +		label capacity
    2.16 +0 	1 	0  	  16
    2.17 +0 	2 	1 	  12
    2.18 +0 	3 	2 	  20
    2.19 +1 	2 	3 	  10
    2.20 +1 	4 	4 	  10
    2.21 +1 	5 	5 	  13
    2.22 +2 	3 	6 	  10
    2.23 +2 	4 	7 	  8
    2.24 +2 	6 	8 	  8
    2.25 +5 	3 	9 	  20
    2.26 +3 	6 	10 	  25
    2.27 +4 	7 	11 	  15
    2.28 +5 	7 	12 	  15
    2.29 +6 	7 	13 	  18
    2.30 +@attributes
    2.31 +source 0
    2.32 +target 7
     3.1 --- a/demo/lgf_demo.cc	Sat May 31 12:31:21 2008 +0200
     3.2 +++ b/demo/lgf_demo.cc	Sat May 31 12:34:44 2008 +0200
     3.3 @@ -20,11 +20,15 @@
     3.4  ///\file
     3.5  ///\brief Demonstrating graph input and output
     3.6  ///
     3.7 -/// This simple demo program gives an example of how to read and write
     3.8 -/// a graph and additional maps (on the nodes or the edges) from/to a
     3.9 -/// stream. 
    3.10 +/// This program gives an example of how to load a directed graph from
    3.11 +/// an \ref lgf-format "LGF" file with the \ref lemon::DigraphReader
    3.12 +/// "DigraphReader" class.
    3.13  ///
    3.14 -/// \include reader_writer_demo.cc
    3.15 +/// The \c "digraph.lgf" file:
    3.16 +/// \include digraph.lgf
    3.17 +///
    3.18 +/// And the program which reads it:
    3.19 +/// \include lgf_demo.cc
    3.20  
    3.21  #include <iostream>
    3.22  #include <lemon/smart_graph.h>
    3.23 @@ -35,119 +39,28 @@
    3.24  
    3.25  using namespace lemon;
    3.26  
    3.27 -int main(int argc, const char *argv[]) {
    3.28 -  const int n = argc > 1 ? std::atoi(argv[1]) : 20;
    3.29 -  const int e = argc > 2 ? std::atoi(argv[2]) : static_cast<int>(n * std::log(double(n)));
    3.30 -  const int m = argc > 3 ? std::atoi(argv[3]) : 100;
    3.31 +int main() {
    3.32 +  SmartDigraph g;
    3.33 +  SmartDigraph::ArcMap<int> cap(g);
    3.34 +  SmartDigraph::Node s, t;
    3.35  
    3.36 -  SmartDigraph digraph;
    3.37 +  digraphReader("digraph.lgf", g). // read the directeg graph into g
    3.38 +    arcMap("capacity", cap).       // read the 'capacity' arc map into cap
    3.39 +    node("source", s).             // read 'source' node to s
    3.40 +    node("target", t).             // read 'target' node to t
    3.41 +    run();
    3.42  
    3.43 -  std::stringstream ss;
    3.44 +  std::cout << "Digraph read from 'digraph.lgf'" << std::endl;
    3.45 +  std::cout << "Number of nodes: " << countNodes(g) << std::endl;
    3.46 +  std::cout << "Number of arcs: " << countArcs(g) << std::endl;
    3.47  
    3.48 -  try {
    3.49 +  std::cout << "We can write it to the standard output:" << std::endl;
    3.50  
    3.51 -    typedef SmartDigraph Digraph;
    3.52 -    typedef Digraph::Node Node;
    3.53 -    typedef Digraph::Arc Arc;
    3.54 -    typedef Digraph::ArcIt ArcIt;
    3.55 -
    3.56 -    typedef Digraph::NodeMap<int> PotentialMap;
    3.57 -    typedef Digraph::ArcMap<int> CapacityMap;
    3.58 -    typedef Digraph::ArcMap<std::string> NameMap;
    3.59 -
    3.60 -    Digraph digraph;
    3.61 -    PotentialMap potential(digraph);
    3.62 -    CapacityMap capacity(digraph);
    3.63 -    NameMap name(digraph);
    3.64 -
    3.65 -    std::vector<Node> nodes;
    3.66 -    for (int i = 0; i < n; ++i) {
    3.67 -      Node node = digraph.addNode();
    3.68 -      potential[node] = rnd[m];
    3.69 -      nodes.push_back(node);
    3.70 -    }
    3.71 -
    3.72 -    std::vector<Arc> arcs;
    3.73 -    for (int i = 0; i < e; ++i) {
    3.74 -      int s = rnd[n];
    3.75 -      int t = rnd[n];
    3.76 -      int c = rnd[m];
    3.77 -      Arc arc = digraph.addArc(nodes[s], nodes[t]);
    3.78 -      capacity[arc] = c;
    3.79 -      std::ostringstream os;
    3.80 -      os << "arc \t" << i << std::endl;
    3.81 -      name[arc] = os.str();
    3.82 -      arcs.push_back(arc);
    3.83 -    }
    3.84 -
    3.85 -
    3.86 -    DigraphWriter<Digraph>(ss, digraph).
    3.87 -      nodeMap("potential", potential).
    3.88 -      arcMap("capacity", capacity).
    3.89 -      arcMap("name", name).
    3.90 -      node("source", nodes[0]).
    3.91 -      node("target", nodes[1]).
    3.92 -      arc("bottleneck", arcs[e / 2]).
    3.93 -      attribute("creator", "lemon library").
    3.94 -      run();
    3.95 -
    3.96 -  } catch (DataFormatError& error) {
    3.97 -    std::cerr << error.what() << std::endl;
    3.98 -  }
    3.99 -
   3.100 -  try {
   3.101 -
   3.102 -    typedef SmartDigraph Digraph;
   3.103 -    typedef Digraph::Node Node;
   3.104 -    typedef Digraph::Arc Arc;
   3.105 -    typedef Digraph::ArcIt ArcIt;
   3.106 -
   3.107 -    typedef Digraph::NodeMap<int> LabelMap;
   3.108 -    typedef Digraph::NodeMap<int> PotentialMap;
   3.109 -    typedef Digraph::ArcMap<int> CapacityMap;
   3.110 -    typedef Digraph::ArcMap<std::string> NameMap;
   3.111 -
   3.112 -    Digraph digraph;
   3.113 -    LabelMap label(digraph);
   3.114 -    PotentialMap potential(digraph);
   3.115 -    CapacityMap capacity(digraph);
   3.116 -    NameMap name(digraph);
   3.117 -
   3.118 -    Node s, t;
   3.119 -    Arc a;
   3.120 -    
   3.121 -    std::string creator;
   3.122 -
   3.123 -    for (int i = 0; i < n; ++i) {
   3.124 -      Node node = digraph.addNode();
   3.125 -      label[node] = i;
   3.126 -    }
   3.127 -    
   3.128 -    DigraphReader<Digraph>(ss, digraph).
   3.129 -      useNodes(label).
   3.130 -      nodeMap("potential", potential).
   3.131 -      arcMap("capacity", capacity).
   3.132 -      arcMap("name", name).
   3.133 -      node("source", s).
   3.134 -      node("target", t).
   3.135 -      arc("bottleneck", a).
   3.136 -      attribute("creator", creator).
   3.137 -      run();
   3.138 -
   3.139 -    DigraphWriter<Digraph>(std::cout, digraph).
   3.140 -      nodeMap("potential", potential).
   3.141 -      arcMap("capacity", capacity).
   3.142 -      arcMap("name", name).
   3.143 -      node("source", s).
   3.144 -      node("target", t).
   3.145 -      arc("bottleneck", a).
   3.146 -      attribute("creator", creator).
   3.147 -      run();
   3.148 -
   3.149 -  } catch (DataFormatError& error) {
   3.150 -    std::cerr << error.what() << std::endl;
   3.151 -  }
   3.152 -
   3.153 +  digraphWriter(std::cout, g).     // write g to the standard output
   3.154 +    arcMap("capacity", cap).       // write cap into 'capacity'
   3.155 +    node("source", s).             // write s to 'source'
   3.156 +    node("target", t).             // write t to 'target'
   3.157 +    run();
   3.158  
   3.159    return 0;
   3.160  }