# HG changeset patch # User athos # Date 1120482511 0 # Node ID d99c3c84f797a98fda0091151ce1123b70e0a714 # Parent c914e7ec2b7bd4abd739d7bb637db78752a459a9 Doc. diff -r c914e7ec2b7b -r d99c3c84f797 demo/dijkstra_demo.cc --- a/demo/dijkstra_demo.cc Mon Jul 04 07:51:57 2005 +0000 +++ b/demo/dijkstra_demo.cc Mon Jul 04 13:08:31 2005 +0000 @@ -44,6 +44,11 @@ len.set(v4_t, 8); len.set(v5_t, 8); + std::cout << "This program is a simple demo of the LEMON Dijkstra class."< LengthMap; using lemon::INVALID; Graph g; - for (int i = 0; i < 3; i++) - g.addNode(); + Node s=g.addNode(); + Node v2=g.addNode(); + Node v3=g.addNode(); + Node v4=g.addNode(); + Node v5=g.addNode(); + Node t=g.addNode(); + + Edge s_v2=g.addEdge(s, v2); + Edge s_v3=g.addEdge(s, v3); + Edge v2_v4=g.addEdge(v2, v4); + Edge v2_v5=g.addEdge(v2, v5); + Edge v3_v5=g.addEdge(v3, v5); + Edge v4_t=g.addEdge(v4, t); + Edge v5_t=g.addEdge(v5, t); - for (NodeIt i(g); i!=INVALID; ++i) - for (NodeIt j(g); j!=INVALID; ++j) - if (i != j) g.addEdge(i, j); + LengthMap length(g); + + length.set(s_v2, 10); + length.set(s_v3, 10); + length.set(v2_v4, 5); + length.set(v2_v5, 8); + length.set(v3_v5, 5); + length.set(v4_t, 8); + length.set(v5_t, 8); std::cout << "Hello World!" << std::endl; std::cout << std::endl; @@ -31,4 +52,13 @@ for (EdgeIt i(g); i!=INVALID; ++i) std::cout << " (" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")"; std::cout << std::endl; + std::cout << std::endl; + + std::cout << "There is a map on the edges (length)!" << std::endl; + std::cout << std::endl; + for (EdgeIt i(g); i!=INVALID; ++i) + std::cout << "length(" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")="< reader(filename,graph); SmartGraph::EdgeMap cap(graph); reader.readEdgeMap("capacity",cap); -// reader.readNode("source",s).readNode("target",t) -// .readEdgeMap("capacity",cap).run(); reader.run(); std::cout << "Hello! We have read a graph from file " << filename<< @@ -30,50 +28,6 @@ // writer.writeNode("target", t); writer.run(); -// LemonReader reader(std::cin); - -// NodeSetReader nodeset(reader, graph); -// SmartGraph::NodeMap cost(graph); -// nodeset.readMap("cost", cost); -// SmartGraph::NodeMap mmap(graph); -// nodeset.readMap("mmap", mmap); - -// EdgeSetReader edgeset(reader, graph, nodeset); -// SmartGraph::EdgeMap description(graph); -// edgeset.readMap("description", description); - -// NodeReader nodes(reader, nodeset); -// SmartGraph::Node source; -// nodes.readNode("source", source); -// SmartGraph::Node target; -// nodes.readNode("target", target); - -// AttributeReader<> attribute(reader, "gui"); -// std::string author; -// attribute.readAttribute("author", author); -// int count; -// attribute.readAttribute("count", count); - -// PrintReader print(reader); - -// reader.run(); - - -// for (SmartGraph::NodeIt it(graph); it != INVALID; ++it) { -// std::cout << cost[it] << ' ' << mmap[it] << std::endl; -// } - -// for (SmartGraph::EdgeIt it(graph); it != INVALID; ++it) { -// std::cout << description[it] << std::endl; -// } - -// std::cout << "author: " << author << std::endl; -// std::cout << "count: " << count << std::endl; - -// std::cout << "source cost: " << cost[source] << std::endl; -// std::cout << "target cost: " << cost[target] << std::endl; - -// std::cout.flush(); } catch (DataFormatError& error) { std::cerr << error.what() << std::endl; } diff -r c914e7ec2b7b -r d99c3c84f797 demo/route.lgf --- a/demo/route.lgf Mon Jul 04 07:51:57 2005 +0000 +++ b/demo/route.lgf Mon Jul 04 13:08:31 2005 +0000 @@ -11,31 +11,33 @@ 1 408.248 577.327 0 189.239 92.5316 @edgeset - length -2 3 901.074 -8 5 270.85 -6 9 601.553 -5 9 285.022 -9 4 408.091 -3 0 719.712 -7 5 612.836 -0 4 933.353 -5 0 778.871 -5 5 0 -7 1 664.049 -5 5 0 -0 9 560.464 -4 8 352.36 -4 9 399.625 -4 1 402.171 -1 2 591.688 -3 8 182.376 -4 5 180.254 -3 1 345.283 -5 4 184.511 -6 2 1112.45 -0 1 556.624 + length id +2 3 901.074 0 +8 5 270.85 1 +6 9 601.553 2 +5 9 285.022 3 +9 4 408.091 4 +3 0 719.712 5 +7 5 612.836 6 +0 4 933.353 7 +5 0 778.871 8 +5 5 0 9 +7 1 664.049 10 +5 5 0 11 +0 9 560.464 12 +4 8 352.36 13 +4 9 399.625 14 +4 1 402.171 15 +1 2 591.688 16 +3 8 182.376 17 +4 5 180.254 18 +3 1 345.283 19 +5 4 184.511 20 +6 2 1112.45 21 +0 1 556.624 22 @nodes source 1 target 8 +@edges +@attributes @end diff -r c914e7ec2b7b -r d99c3c84f797 demo/sample.lgf --- a/demo/sample.lgf Mon Jul 04 07:51:57 2005 +0000 +++ b/demo/sample.lgf Mon Jul 04 13:08:31 2005 +0000 @@ -1,21 +1,21 @@ -@nodeset -id -5 -4 -3 -2 -1 -0 -@edgeset - id capacity -4 5 6 8 -3 5 5 8 -2 4 4 5 -1 4 3 8 -1 3 2 5 -0 2 1 10 -0 1 0 10 -@nodes +@nodeset +id coordinates_x coordinates_y +5 796.398 208.035 +4 573.002 63.002 +3 568.549 401.748 +2 277.889 68.476 +1 288.248 397.327 +0 102.239 257.532 +@edgeset + id capacity +4 5 6 8 +3 5 5 8 +2 4 4 5 +1 4 3 8 +1 3 2 5 +0 2 1 10 +0 1 0 10 +@nodes source 0 target 5 @edges diff -r c914e7ec2b7b -r d99c3c84f797 doc/quicktour.dox --- a/doc/quicktour.dox Mon Jul 04 07:51:57 2005 +0000 +++ b/doc/quicktour.dox Mon Jul 04 13:08:31 2005 +0000 @@ -42,21 +42,38 @@ file format that gives a more flexible way to store data related to network optimization. -
  1. The following code fragment shows how to fill a graph with -data. It creates a complete graph on 4 nodes. The type Listgraph is one of the -LEMON graph types: the typedefs in the beginning are for convenience and we -will suppose them later as well. +
    1. The following code shows how to build a graph from scratch +and iterate on its nodes and edges. This example also shows how to +give a map on the edges of the graph. The type Listgraph is one of +the LEMON graph types: the typedefs in the beginning are for +convenience and we will assume them later as well. -\dontinclude hello_lemon.cc -\skip ListGraph -\until addEdge +\include hello_lemon.cc -See the whole program in file \ref hello_lemon.cc in \c demo subdir of +See the whole program in file \ref hello_lemon.cc in the \c demo subdir of LEMON package. If you want to read more on the LEMON graph structures and concepts, read the page about \ref graphs "graphs". + +
    2. LEMON has an own file format for storing graphs, maps on edges/nodes and some other things. Instead of any explanation let us give a +short example file in this format: read the detailed description of the LEMON +graph file format and input-output routines here: \ref graph-io-page. + +So here is a file describing a graph of 6 nodes (0 to 5), two nodemaps +(called \c coordinates_x and \c coordinates_y), several edges, an edge map +called \c capacity and two designated nodes (called \c source and \c target). + +\include sample.lgf + +Finally let us give a simple example that reads a graph from a file and writes +it to the standard output. + +\include reader_writer_demo.cc + +See the whole program in file \ref reader_writer_demo.cc. +
    3. The following code shows how to read a graph from a stream (e.g. a file) in the DIMACS file format (find the documentation of the DIMACS file formats on the web). @@ -73,49 +90,30 @@ \ref dimacs.h "Dimacs file format reader". There you will also find the details about the output routines into files of the DIMACS format. -
    4. DIMACS formats could not give us the flexibility we needed, -so we worked out our own file format. Instead of any explanation let us give a -short example file in this format: read the detailed description of the LEMON -graph file format and input-output routines \ref graph-io-page here. - -So here is a file describing a graph of 10 nodes (0 to 9), two nodemaps -(called \c coordinates_x and \c coordinates_y), several edges, an edge map -called \c length and two designated nodes (called \c source and \c target). - -\todo Maybe a shorter example would be better here. - -\include route.lgf - -Finally let us give a simple example that reads a graph from a file and writes -it to the standard output. - -\include reader_writer_demo.cc - -See the whole program in file \ref reader_writer_demo.cc. - -\todo This is still under construction! -
  2. If you want to solve some transportation problems in a network then you will want to find shortest paths between nodes of a graph. This is usually solved using Dijkstra's algorithm. A utility that solves this is the \ref lemon::Dijkstra "LEMON Dijkstra class". The following code is a simple program using the -\ref lemon::Dijkstra "LEMON Dijkstra class" and it also shows how to define a map on the edges (the length -function): +\ref lemon::Dijkstra "LEMON Dijkstra class": it calculates the shortest path between node \c s and \c t in a graph \c g. +We omit the part reading the graph \c g and the length map \c len. \dontinclude dijkstra_demo.cc \skip ListGraph +\until Graph g +... +\skip Dijkstra algorithm \until std::cout << g.id(s) See the whole program in \ref dijkstra_demo.cc. -The first part of the code is self-explanatory: we build the graph and set the -length values of the edges. Then we instantiate a member of the Dijkstra class -and run the Dijkstra algorithm from node \c s. After this we read some of the -results. -You can do much more with the Dijkstra class, for example you can run it step -by step and gain full control of the execution. For a detailed description, see the documentation of the \ref lemon::Dijkstra "LEMON Dijkstra class". +Some explanation: after instantiating a member of the Dijkstra class +we run the Dijkstra algorithm from node \c s. After this we read some +of the results. You can do much more with the Dijkstra class, for +example you can run it step by step and gain full control of the +execution. For a detailed description, see the documentation of the +\ref lemon::Dijkstra "LEMON Dijkstra class".
  3. If you want to design a network and want to minimize the total length @@ -154,48 +152,9 @@
  4. The following code shows how to solve an LP problem using the LEMON lp interface. The code together with the comments is self-explanatory. -\code - - //A default solver is taken - LpDefault lp; - typedef LpDefault::Row Row; - typedef LpDefault::Col Col; - - - //This will be a maximization - lp.max(); - - //We add coloumns (variables) to our problem - Col x1 = lp.addCol(); - Col x2 = lp.addCol(); - Col x3 = lp.addCol(); - - //Constraints - lp.addRow(x1+x2+x3 <=100); - lp.addRow(10*x1+4*x2+5*x3<=600); - lp.addRow(2*x1+2*x2+6*x3<=300); - //Nonnegativity of the variables - lp.colLowerBound(x1, 0); - lp.colLowerBound(x2, 0); - lp.colLowerBound(x3, 0); - //Objective function - lp.setObj(10*x1+6*x2+4*x3); - - //Call the routine of the underlying LP solver - lp.solve(); - - //Print results - if (lp.primalStatus()==LpSolverBase::OPTIMAL){ - printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n", - lp.primalValue(), - lp.primal(x1), lp.primal(x2), lp.primal(x3)); - } - else{ - std::cout<<"Optimal solution not found!"< x(g); - lp.addColSet(x); - - //Nonnegativity and capacity constraints - for(EdgeIt e(g);e!=INVALID;++e) { - lp.colUpperBound(x[e],cap[e]); - lp.colLowerBound(x[e],0); - } +\dontinclude lp_maxflow_demo.cc +\skip Define a map on the edges for the variables of the LP problem +\until lp.max(); +\skip Solve with the underlying solver +\until lp.solve(); - //Flow conservation constraints for the nodes (except for 's' and 't') - for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) { - LpDefault::Expr ex; - for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e]; - for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e]; - lp.addRow(ex==0); - } - - //Objective function: the flow value entering 't' - { - LpDefault::Expr ex; - for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e]; - for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e]; - lp.setObj(ex); - } - - //Maximization - lp.max(); - - //Solve with the underlying solver - lp.solve(); - -\endcode - The complete program can be found in file \ref lp_maxflow_demo.cc. After compiling run it in the form: ./lp_maxflow_demo < sample.lgf