COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/sub_graph_adaptor_demo.cc @ 1583:2b329fd595ef

Last change on this file since 1583:2b329fd595ef was 1583:2b329fd595ef, checked in by athos, 17 years ago

Documented some more demo programs.

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