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