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