demo/sub_graph_adaptor_demo.cc
author athos
Wed, 20 Jul 2005 16:05:04 +0000
changeset 1577 15098fb5275c
parent 1560 01707a8a4ca6
child 1583 2b329fd595ef
permissions -rw-r--r--
Documentation (lp_demo,lp_maxflow) and slight changes (rest).
marci@866
     1
// -*- c++ -*-
marci@866
     2
athos@1560
     3
// Use a DIMACS max flow file as input.
alpar@1401
     4
// sub_graph_adaptor_demo < dimacs_max_flow_file
marci@871
     5
// This program computes a maximum number of edge-disjoint shortest paths
marci@869
     6
// between s and t.
marci@866
     7
marci@866
     8
#include <iostream>
marci@866
     9
#include <fstream>
marci@866
    10
alpar@921
    11
#include <lemon/smart_graph.h>
alpar@921
    12
#include <lemon/dijkstra.h>
alpar@921
    13
#include <lemon/maps.h>
alpar@1401
    14
#include <lemon/graph_adaptor.h>
alpar@921
    15
#include <lemon/dimacs.h>
alpar@921
    16
#include <lemon/preflow.h>
marci@931
    17
#include <tight_edge_filter_map.h>
marci@866
    18
athos@1577
    19
athos@1577
    20
athos@1577
    21
//#include <lemon/graph_reader.h>
athos@1577
    22
//#include <lemon/graph_writer.h>
athos@1577
    23
alpar@921
    24
using namespace lemon;
marci@866
    25
marci@866
    26
using std::cout;
marci@866
    27
using std::endl;
marci@866
    28
athos@1560
    29
int main(int argc, char *argv[]) 
athos@1560
    30
{
athos@1560
    31
  if(argc<2)
athos@1560
    32
  {
athos@1577
    33
      std::cerr << "USAGE: sub_graph_adaptor_demo input_file.dim" << std::endl;
athos@1560
    34
      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;
athos@1560
    35
      return 0;
athos@1560
    36
  }
athos@1560
    37
athos@1560
    38
athos@1560
    39
  //input stream to read the graph from
athos@1560
    40
  std::ifstream is(argv[1]);
athos@1560
    41
marci@866
    42
  typedef SmartGraph Graph;
marci@866
    43
marci@866
    44
  typedef Graph::Edge Edge;
marci@866
    45
  typedef Graph::Node Node;
marci@866
    46
  typedef Graph::EdgeIt EdgeIt;
marci@866
    47
  typedef Graph::NodeIt NodeIt;
marci@866
    48
  typedef Graph::EdgeMap<int> LengthMap;
marci@866
    49
marci@866
    50
  Graph g;
marci@866
    51
  Node s, t;
marci@866
    52
  LengthMap length(g);
marci@866
    53
athos@1560
    54
  readDimacs(is, g, length, s, t);
marci@866
    55
athos@1577
    56
//     GraphWriter<SmartGraph> writer(std::cout, g);
athos@1577
    57
//     writer.writeEdgeMap("length", length);
athos@1577
    58
//     writer.writeNode("source",s);
athos@1577
    59
//     writer.writeNode("target",t);
athos@1577
    60
//     writer.run();
athos@1577
    61
athos@1577
    62
//   GraphReader<ListGraph> reader(is,g);
athos@1577
    63
//   reader.readNode("source",s).readNode("target",t)
athos@1577
    64
//     .readEdgeMap("length",length).run();
athos@1577
    65
alpar@986
    66
  cout << "edges with lengths (of form id, source--length->target): " << endl;
marci@866
    67
  for(EdgeIt e(g); e!=INVALID; ++e) 
alpar@986
    68
    cout << " " << g.id(e) << ", " << g.id(g.source(e)) << "--" 
alpar@986
    69
	 << length[e] << "->" << g.id(g.target(e)) << endl;
marci@866
    70
marci@866
    71
  cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
marci@866
    72
marci@866
    73
  typedef Dijkstra<Graph, LengthMap> Dijkstra;
marci@866
    74
  Dijkstra dijkstra(g, length);
marci@866
    75
  dijkstra.run(s);
marci@866
    76
marci@869
    77
  // This map returns true exactly for those edges which are 
marci@869
    78
  // tight w.r.t the length funcion and the potential 
marci@869
    79
  // given by the dijkstra algorithm.
marci@866
    80
  typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap> 
marci@866
    81
    TightEdgeFilter;
marci@866
    82
  TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
marci@866
    83
marci@932
    84
//  ConstMap<Node, bool> const_true_map(true);
marci@869
    85
  // This graph contains exaclty the tight edges.
alpar@1401
    86
// typedef SubGraphAdaptor<Graph, ConstMap<Node, bool>, TightEdgeFilter> SubGW;
alpar@1401
    87
  typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGW;
marci@932
    88
  SubGW gw(g, tight_edge_filter);
marci@866
    89
marci@866
    90
  ConstMap<Edge, int> const_1_map(1);
marci@866
    91
  Graph::EdgeMap<int> flow(g, 0);
marci@869
    92
  // Max flow between s and t in the graph of tight edges.
marci@866
    93
  Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> > 
marci@866
    94
    preflow(gw, s, t, const_1_map, flow);
marci@866
    95
  preflow.run();
marci@866
    96
athos@1544
    97
  cout << "maximum number of edge-disjoint shortest paths: " 
marci@931
    98
       << preflow.flowValue() << endl;
marci@866
    99
  cout << "edges of the maximum number of edge-disjoint shortest s-t paths: " 
marci@866
   100
       << endl;
marci@866
   101
  for(EdgeIt e(g); e!=INVALID; ++e) 
marci@866
   102
    if (flow[e])
marci@931
   103
      cout << " " << g.id(e) << ", "
alpar@986
   104
	   << g.id(g.source(e)) << "--" 
alpar@986
   105
	   << length[e] << "->" << g.id(g.target(e)) << endl;
marci@866
   106
}