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