demo/sub_graph_adaptor_demo.cc
author ladanyi
Wed, 17 Aug 2005 15:26:00 +0000
changeset 1636 260ac104190f
parent 1583 2b329fd595ef
child 1641 77f6ab7ad66f
permissions -rw-r--r--
Added missing copyright headers, and corrected the file names in some of them.
     1 /* -*- C++ -*-
     2  * demo/sub_graph_adaptor_demo.cc - Part of LEMON, a generic C++ optimization
     3  * library
     4  *
     5  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  *
     8  * Permission to use, modify and distribute this software is granted
     9  * provided that this copyright notice appears in all copies. For
    10  * precise terms see the accompanying LICENSE file.
    11  *
    12  * This software is provided "AS IS" with no warranty of any kind,
    13  * express or implied, and with no claim as to its suitability for any
    14  * purpose.
    15  *
    16  */
    17 
    18 ///\ingroup demos
    19 ///\file
    20 ///\brief Computing maximum number of edge-disjoint shortest paths
    21 ///
    22 /// This program computes a maximum number of edge-disjoint shortest paths
    23 /// between nodes \c s and \c t.
    24 
    25 
    26 // Use a DIMACS max flow file as input.
    27 // sub_graph_adaptor_demo < dimacs_max_flow_file
    28 // Modified to eat lemon graph format! 
    29 
    30 
    31 #include <iostream>
    32 #include <fstream>
    33 
    34 #include <lemon/smart_graph.h>
    35 #include <lemon/dijkstra.h>
    36 #include <lemon/maps.h>
    37 #include <lemon/graph_adaptor.h>
    38 #include <lemon/dimacs.h>
    39 #include <lemon/preflow.h>
    40 #include <tight_edge_filter_map.h>
    41 
    42 #include <lemon/graph_reader.h>
    43 
    44 
    45 using namespace lemon;
    46 
    47 using std::cout;
    48 using std::endl;
    49 
    50 int main(int argc, char *argv[]) 
    51 {
    52   if(argc<2)
    53   {
    54       std::cerr << "USAGE: sub_graph_adaptor_demo input_file.lgf" << std::endl;
    55       std::cerr << "The file 'input_file.lgf' has to contain a max flow "
    56 		<< "instance in \n LEMON format "
    57 		<< "(e.g. sub_gad_input.lgf is such a file)." 
    58 		<< std::endl;
    59       return 0;
    60   }
    61 
    62 
    63   //input stream to read the graph from
    64   std::ifstream is(argv[1]);
    65 
    66   typedef SmartGraph Graph;
    67 
    68   typedef Graph::Edge Edge;
    69   typedef Graph::Node Node;
    70   typedef Graph::EdgeIt EdgeIt;
    71   typedef Graph::NodeIt NodeIt;
    72   typedef Graph::EdgeMap<int> LengthMap;
    73 
    74   Graph g;
    75   Node s, t;
    76   LengthMap length(g);
    77 
    78   //readDimacs(is, g, length, s, t);
    79 
    80 
    81   GraphReader<SmartGraph> reader(is,g);
    82   reader.readNode("source",s).readNode("target",t)
    83     .readEdgeMap("length",length).run();
    84 
    85   cout << "edges with lengths (of form id, source--length->target): " << endl;
    86   for(EdgeIt e(g); e!=INVALID; ++e) 
    87     cout << " " << g.id(e) << ", " << g.id(g.source(e)) << "--" 
    88 	 << length[e] << "->" << g.id(g.target(e)) << endl;
    89 
    90   cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
    91 
    92   typedef Dijkstra<Graph, LengthMap> Dijkstra;
    93   Dijkstra dijkstra(g, length);
    94   dijkstra.run(s);
    95 
    96   // This map returns true exactly for those edges which are 
    97   // tight w.r.t the length funcion and the potential 
    98   // given by the dijkstra algorithm.
    99   typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap> 
   100     TightEdgeFilter;
   101   TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
   102 
   103 //  ConstMap<Node, bool> const_true_map(true);
   104   // This graph contains exaclty the tight edges.
   105 // typedef SubGraphAdaptor<Graph, ConstMap<Node, bool>, TightEdgeFilter> SubGW;
   106   typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGW;
   107   SubGW gw(g, tight_edge_filter);
   108 
   109   ConstMap<Edge, int> const_1_map(1);
   110   Graph::EdgeMap<int> flow(g, 0);
   111   // Max flow between s and t in the graph of tight edges.
   112   Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> > 
   113     preflow(gw, s, t, const_1_map, flow);
   114   preflow.run();
   115 
   116   cout << "maximum number of edge-disjoint shortest paths: " 
   117        << preflow.flowValue() << endl;
   118   cout << "edges of the maximum number of edge-disjoint shortest s-t paths: " 
   119        << endl;
   120   for(EdgeIt e(g); e!=INVALID; ++e) 
   121     if (flow[e])
   122       cout << " " << g.id(e) << ", "
   123 	   << g.id(g.source(e)) << "--" 
   124 	   << length[e] << "->" << g.id(g.target(e)) << endl;
   125 }