COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/sub_graph_adaptor_demo.cc @ 1626:e251336be488

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

Documented some more demo programs.

File size: 3.6 KB
Line 
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
24
25// Use a DIMACS max flow file as input.
26// sub_graph_adaptor_demo < dimacs_max_flow_file
27// Modified to eat lemon graph format!
28
29
30#include <iostream>
31#include <fstream>
32
33#include <lemon/smart_graph.h>
34#include <lemon/dijkstra.h>
35#include <lemon/maps.h>
36#include <lemon/graph_adaptor.h>
37#include <lemon/dimacs.h>
38#include <lemon/preflow.h>
39#include <tight_edge_filter_map.h>
40
41#include <lemon/graph_reader.h>
42
43
44using namespace lemon;
45
46using std::cout;
47using std::endl;
48
49int main(int argc, char *argv[])
50{
51  if(argc<2)
52  {
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;
58      return 0;
59  }
60
61
62  //input stream to read the graph from
63  std::ifstream is(argv[1]);
64
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
77  //readDimacs(is, g, length, s, t);
78
79
80  GraphReader<SmartGraph> reader(is,g);
81  reader.readNode("source",s).readNode("target",t)
82    .readEdgeMap("length",length).run();
83
84  cout << "edges with lengths (of form id, source--length->target): " << endl;
85  for(EdgeIt e(g); e!=INVALID; ++e)
86    cout << " " << g.id(e) << ", " << g.id(g.source(e)) << "--"
87         << length[e] << "->" << g.id(g.target(e)) << endl;
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
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.
98  typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap>
99    TightEdgeFilter;
100  TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
101
102//  ConstMap<Node, bool> const_true_map(true);
103  // This graph contains exaclty the tight edges.
104// typedef SubGraphAdaptor<Graph, ConstMap<Node, bool>, TightEdgeFilter> SubGW;
105  typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGW;
106  SubGW gw(g, tight_edge_filter);
107
108  ConstMap<Edge, int> const_1_map(1);
109  Graph::EdgeMap<int> flow(g, 0);
110  // Max flow between s and t in the graph of tight edges.
111  Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> >
112    preflow(gw, s, t, const_1_map, flow);
113  preflow.run();
114
115  cout << "maximum number of edge-disjoint shortest paths: "
116       << preflow.flowValue() << endl;
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])
121      cout << " " << g.id(e) << ", "
122           << g.id(g.source(e)) << "--"
123           << length[e] << "->" << g.id(g.target(e)) << endl;
124}
Note: See TracBrowser for help on using the repository browser.