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