demo/sub_graph_adaptor_demo.cc
author athos
Fri, 15 Jul 2005 16:01:55 +0000
changeset 1560 01707a8a4ca6
parent 1544 955e8e83f6b1
child 1577 15098fb5275c
permissions -rw-r--r--
Some demo programs got some interface. Most progress with lp_maxflow_demo.cc, which also got documented.
     1 // -*- c++ -*-
     2 
     3 // Use a DIMACS max flow file as input.
     4 // sub_graph_adaptor_demo < dimacs_max_flow_file
     5 // This program computes a maximum number of edge-disjoint shortest paths
     6 // between s and t.
     7 
     8 #include <iostream>
     9 #include <fstream>
    10 
    11 #include <lemon/smart_graph.h>
    12 #include <lemon/dijkstra.h>
    13 #include <lemon/maps.h>
    14 #include <lemon/graph_adaptor.h>
    15 #include <lemon/dimacs.h>
    16 #include <lemon/preflow.h>
    17 #include <tight_edge_filter_map.h>
    18 
    19 using namespace lemon;
    20 
    21 using std::cout;
    22 using std::endl;
    23 
    24 int main(int argc, char *argv[]) 
    25 {
    26   if(argc<2)
    27   {
    28       std::cerr << "USAGE: sub_graph_adaptor_demo <input_file.dim>" << std::endl;
    29       std::cerr << "The file 'input_file.dim' has to contain a max flow instance in DIMACS format (e.g. sub_graph_adaptor_demo.dim is such a file)." << std::endl;
    30       return 0;
    31   }
    32 
    33 
    34   //input stream to read the graph from
    35   std::ifstream is(argv[1]);
    36 
    37   typedef SmartGraph Graph;
    38 
    39   typedef Graph::Edge Edge;
    40   typedef Graph::Node Node;
    41   typedef Graph::EdgeIt EdgeIt;
    42   typedef Graph::NodeIt NodeIt;
    43   typedef Graph::EdgeMap<int> LengthMap;
    44 
    45   Graph g;
    46   Node s, t;
    47   LengthMap length(g);
    48 
    49   readDimacs(is, g, length, s, t);
    50 
    51   cout << "edges with lengths (of form id, source--length->target): " << endl;
    52   for(EdgeIt e(g); e!=INVALID; ++e) 
    53     cout << " " << g.id(e) << ", " << g.id(g.source(e)) << "--" 
    54 	 << length[e] << "->" << g.id(g.target(e)) << endl;
    55 
    56   cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
    57 
    58   typedef Dijkstra<Graph, LengthMap> Dijkstra;
    59   Dijkstra dijkstra(g, length);
    60   dijkstra.run(s);
    61 
    62   // This map returns true exactly for those edges which are 
    63   // tight w.r.t the length funcion and the potential 
    64   // given by the dijkstra algorithm.
    65   typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap> 
    66     TightEdgeFilter;
    67   TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
    68 
    69 //  ConstMap<Node, bool> const_true_map(true);
    70   // This graph contains exaclty the tight edges.
    71 // typedef SubGraphAdaptor<Graph, ConstMap<Node, bool>, TightEdgeFilter> SubGW;
    72   typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGW;
    73   SubGW gw(g, tight_edge_filter);
    74 
    75   ConstMap<Edge, int> const_1_map(1);
    76   Graph::EdgeMap<int> flow(g, 0);
    77   // Max flow between s and t in the graph of tight edges.
    78   Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> > 
    79     preflow(gw, s, t, const_1_map, flow);
    80   preflow.run();
    81 
    82   cout << "maximum number of edge-disjoint shortest paths: " 
    83        << preflow.flowValue() << endl;
    84   cout << "edges of the maximum number of edge-disjoint shortest s-t paths: " 
    85        << endl;
    86   for(EdgeIt e(g); e!=INVALID; ++e) 
    87     if (flow[e])
    88       cout << " " << g.id(e) << ", "
    89 	   << g.id(g.source(e)) << "--" 
    90 	   << length[e] << "->" << g.id(g.target(e)) << endl;
    91 }