[Lemon-commits] Balazs Dezso: Reworking demo file

Lemon HG hg at lemon.cs.elte.hu
Wed Jun 4 12:25:35 CEST 2008


details:   http://lemon.cs.elte.hu/hg/lemon/rev/00d297da491e
changeset: 164:00d297da491e
user:      Balazs Dezso <deba [at] inf.elte.hu>
date:      Sat May 31 12:34:44 2008 +0200
description:
	Reworking demo file

diffstat:

3 files changed, 57 insertions(+), 114 deletions(-)
demo/Makefile.am |    3 -
demo/digraph.lgf |   29 +++++++++++
demo/lgf_demo.cc |  139 ++++++++++--------------------------------------------

diffs (205 lines):

diff -r c82fd9568d75 -r 00d297da491e demo/Makefile.am
--- a/demo/Makefile.am	Sat May 31 12:31:21 2008 +0200
+++ b/demo/Makefile.am	Sat May 31 12:34:44 2008 +0200
@@ -1,5 +1,6 @@
 EXTRA_DIST += \
-	demo/CMakeLists.txt
+	demo/CMakeLists.txt \
+	demo/digraph.lgf
 
 if WANT_DEMO
 
diff -r c82fd9568d75 -r 00d297da491e demo/digraph.lgf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demo/digraph.lgf	Sat May 31 12:34:44 2008 +0200
@@ -0,0 +1,29 @@
+ at nodes
+label
+0
+1
+2
+3
+4
+5
+6
+7
+ at arcs
+		label capacity
+0 	1 	0  	  16
+0 	2 	1 	  12
+0 	3 	2 	  20
+1 	2 	3 	  10
+1 	4 	4 	  10
+1 	5 	5 	  13
+2 	3 	6 	  10
+2 	4 	7 	  8
+2 	6 	8 	  8
+5 	3 	9 	  20
+3 	6 	10 	  25
+4 	7 	11 	  15
+5 	7 	12 	  15
+6 	7 	13 	  18
+ at attributes
+source 0
+target 7
diff -r c82fd9568d75 -r 00d297da491e demo/lgf_demo.cc
--- a/demo/lgf_demo.cc	Sat May 31 12:31:21 2008 +0200
+++ b/demo/lgf_demo.cc	Sat May 31 12:34:44 2008 +0200
@@ -20,11 +20,15 @@
 ///\file
 ///\brief Demonstrating graph input and output
 ///
-/// This simple demo program gives an example of how to read and write
-/// a graph and additional maps (on the nodes or the edges) from/to a
-/// stream. 
+/// This program gives an example of how to load a directed graph from
+/// an \ref lgf-format "LGF" file with the \ref lemon::DigraphReader
+/// "DigraphReader" class.
 ///
-/// \include reader_writer_demo.cc
+/// The \c "digraph.lgf" file:
+/// \include digraph.lgf
+///
+/// And the program which reads it:
+/// \include lgf_demo.cc
 
 #include <iostream>
 #include <lemon/smart_graph.h>
@@ -35,119 +39,28 @@
 
 using namespace lemon;
 
-int main(int argc, const char *argv[]) {
-  const int n = argc > 1 ? std::atoi(argv[1]) : 20;
-  const int e = argc > 2 ? std::atoi(argv[2]) : static_cast<int>(n * std::log(double(n)));
-  const int m = argc > 3 ? std::atoi(argv[3]) : 100;
+int main() {
+  SmartDigraph g;
+  SmartDigraph::ArcMap<int> cap(g);
+  SmartDigraph::Node s, t;
 
-  SmartDigraph digraph;
+  digraphReader("digraph.lgf", g). // read the directeg graph into g
+    arcMap("capacity", cap).       // read the 'capacity' arc map into cap
+    node("source", s).             // read 'source' node to s
+    node("target", t).             // read 'target' node to t
+    run();
 
-  std::stringstream ss;
+  std::cout << "Digraph read from 'digraph.lgf'" << std::endl;
+  std::cout << "Number of nodes: " << countNodes(g) << std::endl;
+  std::cout << "Number of arcs: " << countArcs(g) << std::endl;
 
-  try {
+  std::cout << "We can write it to the standard output:" << std::endl;
 
-    typedef SmartDigraph Digraph;
-    typedef Digraph::Node Node;
-    typedef Digraph::Arc Arc;
-    typedef Digraph::ArcIt ArcIt;
-
-    typedef Digraph::NodeMap<int> PotentialMap;
-    typedef Digraph::ArcMap<int> CapacityMap;
-    typedef Digraph::ArcMap<std::string> NameMap;
-
-    Digraph digraph;
-    PotentialMap potential(digraph);
-    CapacityMap capacity(digraph);
-    NameMap name(digraph);
-
-    std::vector<Node> nodes;
-    for (int i = 0; i < n; ++i) {
-      Node node = digraph.addNode();
-      potential[node] = rnd[m];
-      nodes.push_back(node);
-    }
-
-    std::vector<Arc> arcs;
-    for (int i = 0; i < e; ++i) {
-      int s = rnd[n];
-      int t = rnd[n];
-      int c = rnd[m];
-      Arc arc = digraph.addArc(nodes[s], nodes[t]);
-      capacity[arc] = c;
-      std::ostringstream os;
-      os << "arc \t" << i << std::endl;
-      name[arc] = os.str();
-      arcs.push_back(arc);
-    }
-
-
-    DigraphWriter<Digraph>(ss, digraph).
-      nodeMap("potential", potential).
-      arcMap("capacity", capacity).
-      arcMap("name", name).
-      node("source", nodes[0]).
-      node("target", nodes[1]).
-      arc("bottleneck", arcs[e / 2]).
-      attribute("creator", "lemon library").
-      run();
-
-  } catch (DataFormatError& error) {
-    std::cerr << error.what() << std::endl;
-  }
-
-  try {
-
-    typedef SmartDigraph Digraph;
-    typedef Digraph::Node Node;
-    typedef Digraph::Arc Arc;
-    typedef Digraph::ArcIt ArcIt;
-
-    typedef Digraph::NodeMap<int> LabelMap;
-    typedef Digraph::NodeMap<int> PotentialMap;
-    typedef Digraph::ArcMap<int> CapacityMap;
-    typedef Digraph::ArcMap<std::string> NameMap;
-
-    Digraph digraph;
-    LabelMap label(digraph);
-    PotentialMap potential(digraph);
-    CapacityMap capacity(digraph);
-    NameMap name(digraph);
-
-    Node s, t;
-    Arc a;
-    
-    std::string creator;
-
-    for (int i = 0; i < n; ++i) {
-      Node node = digraph.addNode();
-      label[node] = i;
-    }
-    
-    DigraphReader<Digraph>(ss, digraph).
-      useNodes(label).
-      nodeMap("potential", potential).
-      arcMap("capacity", capacity).
-      arcMap("name", name).
-      node("source", s).
-      node("target", t).
-      arc("bottleneck", a).
-      attribute("creator", creator).
-      run();
-
-    DigraphWriter<Digraph>(std::cout, digraph).
-      nodeMap("potential", potential).
-      arcMap("capacity", capacity).
-      arcMap("name", name).
-      node("source", s).
-      node("target", t).
-      arc("bottleneck", a).
-      attribute("creator", creator).
-      run();
-
-  } catch (DataFormatError& error) {
-    std::cerr << error.what() << std::endl;
-  }
-
+  digraphWriter(std::cout, g).     // write g to the standard output
+    arcMap("capacity", cap).       // write cap into 'capacity'
+    node("source", s).             // write s to 'source'
+    node("target", t).             // write t to 'target'
+    run();
 
   return 0;
 }



More information about the Lemon-commits mailing list