COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/sub_graph_adaptor_demo.cc @ 1577:15098fb5275c

Last change on this file since 1577:15098fb5275c was 1577:15098fb5275c, checked in by athos, 19 years ago

Documentation (lp_demo,lp_maxflow) and slight changes (rest).

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