1 | // -*- c++ -*- |
---|
2 | #ifndef HUGO_MINCOSTFLOWS_H |
---|
3 | #define HUGO_MINCOSTFLOWS_H |
---|
4 | |
---|
5 | ///\ingroup galgs |
---|
6 | ///\file |
---|
7 | ///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost |
---|
8 | |
---|
9 | #include <iostream> |
---|
10 | #include <dijkstra.h> |
---|
11 | #include <graph_wrapper.h> |
---|
12 | #include <maps.h> |
---|
13 | #include <vector.h> |
---|
14 | #include <for_each_macros.h> |
---|
15 | |
---|
16 | namespace hugo { |
---|
17 | |
---|
18 | /// \addtogroup galgs |
---|
19 | /// @{ |
---|
20 | |
---|
21 | ///\brief Implementation of an algorithm for finding a flow of value \c k |
---|
22 | ///(for small values of \c k) having minimal total cost between 2 nodes |
---|
23 | /// |
---|
24 | /// |
---|
25 | /// The class \ref hugo::MinCostFlows "MinCostFlows" implements |
---|
26 | /// an algorithm for finding a flow of value \c k |
---|
27 | ///(for small values of \c k) having minimal total cost |
---|
28 | /// from a given source node to a given target node in an |
---|
29 | /// edge-weighted directed graph having nonnegative integer capacities. |
---|
30 | /// The range of the length (weight) function is nonnegative reals but |
---|
31 | /// the range of capacity function is the set of nonnegative integers. |
---|
32 | /// It is not a polinomial time algorithm for counting the minimum cost |
---|
33 | /// maximal flow, since it counts the minimum cost flow for every value 0..M |
---|
34 | /// where \c M is the value of the maximal flow. |
---|
35 | /// |
---|
36 | ///\author Attila Bernath |
---|
37 | template <typename Graph, typename LengthMap, typename CapacityMap> |
---|
38 | class MinCostFlows { |
---|
39 | |
---|
40 | typedef typename LengthMap::ValueType Length; |
---|
41 | |
---|
42 | //Warning: this should be integer type |
---|
43 | typedef typename CapacityMap::ValueType Capacity; |
---|
44 | |
---|
45 | typedef typename Graph::Node Node; |
---|
46 | typedef typename Graph::NodeIt NodeIt; |
---|
47 | typedef typename Graph::Edge Edge; |
---|
48 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
49 | typedef typename Graph::template EdgeMap<int> EdgeIntMap; |
---|
50 | |
---|
51 | // typedef ConstMap<Edge,int> ConstMap; |
---|
52 | |
---|
53 | typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType; |
---|
54 | typedef typename ResGraphType::Edge ResGraphEdge; |
---|
55 | |
---|
56 | class ModLengthMap { |
---|
57 | //typedef typename ResGraphType::template NodeMap<Length> NodeMap; |
---|
58 | typedef typename Graph::template NodeMap<Length> NodeMap; |
---|
59 | const ResGraphType& G; |
---|
60 | // const EdgeIntMap& rev; |
---|
61 | const LengthMap &ol; |
---|
62 | const NodeMap &pot; |
---|
63 | public : |
---|
64 | typedef typename LengthMap::KeyType KeyType; |
---|
65 | typedef typename LengthMap::ValueType ValueType; |
---|
66 | |
---|
67 | ValueType operator[](typename ResGraphType::Edge e) const { |
---|
68 | if (G.forward(e)) |
---|
69 | return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); |
---|
70 | else |
---|
71 | return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); |
---|
72 | } |
---|
73 | |
---|
74 | ModLengthMap(const ResGraphType& _G, |
---|
75 | const LengthMap &o, const NodeMap &p) : |
---|
76 | G(_G), /*rev(_rev),*/ ol(o), pot(p){}; |
---|
77 | };//ModLengthMap |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | //Input |
---|
82 | const Graph& G; |
---|
83 | const LengthMap& length; |
---|
84 | const CapacityMap& capacity; |
---|
85 | |
---|
86 | //auxiliary variables |
---|
87 | |
---|
88 | //The value is 1 iff the edge is reversed. |
---|
89 | //If the algorithm has finished, the edges of the seeked paths are |
---|
90 | //exactly those that are reversed |
---|
91 | EdgeIntMap flow; |
---|
92 | typename Graph::template NodeMap<Length> potential; |
---|
93 | |
---|
94 | //Container to store found paths |
---|
95 | std::vector< std::vector<Edge> > paths; |
---|
96 | //typedef DirPath<Graph> DPath; |
---|
97 | //DPath paths; |
---|
98 | |
---|
99 | |
---|
100 | Length total_length; |
---|
101 | |
---|
102 | public : |
---|
103 | |
---|
104 | |
---|
105 | MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), |
---|
106 | length(_length), capacity(_cap), flow(_G), potential(_G){ } |
---|
107 | |
---|
108 | |
---|
109 | ///Runs the algorithm. |
---|
110 | |
---|
111 | ///Runs the algorithm. |
---|
112 | ///Returns k if there are at least k edge-disjoint paths from s to t. |
---|
113 | ///Otherwise it returns the number of found edge-disjoint paths from s to t. |
---|
114 | int run(Node s, Node t, int k) { |
---|
115 | |
---|
116 | //Resetting variables from previous runs |
---|
117 | total_length = 0; |
---|
118 | |
---|
119 | FOR_EACH_LOC(typename Graph::EdgeIt, e, G){ |
---|
120 | flow.set(e,0); |
---|
121 | } |
---|
122 | |
---|
123 | FOR_EACH_LOC(typename Graph::NodeIt, n, G){ |
---|
124 | //cout << potential[n]<<endl; |
---|
125 | potential.set(n,0); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | //We need a residual graph |
---|
131 | ResGraphType res_graph(G, capacity, flow); |
---|
132 | |
---|
133 | //Initialize the copy of the Dijkstra potential to zero |
---|
134 | |
---|
135 | //typename ResGraphType::template NodeMap<Length> potential(res_graph); |
---|
136 | |
---|
137 | |
---|
138 | ModLengthMap mod_length(res_graph, length, potential); |
---|
139 | |
---|
140 | Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length); |
---|
141 | |
---|
142 | int i; |
---|
143 | for (i=0; i<k; ++i){ |
---|
144 | dijkstra.run(s); |
---|
145 | if (!dijkstra.reached(t)){ |
---|
146 | //There are no k paths from s to t |
---|
147 | break; |
---|
148 | }; |
---|
149 | |
---|
150 | { |
---|
151 | //We have to copy the potential |
---|
152 | typename ResGraphType::NodeIt n; |
---|
153 | for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) { |
---|
154 | potential[n] += dijkstra.distMap()[n]; |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | //Augmenting on the sortest path |
---|
160 | Node n=t; |
---|
161 | ResGraphEdge e; |
---|
162 | while (n!=s){ |
---|
163 | e = dijkstra.pred(n); |
---|
164 | n = dijkstra.predNode(n); |
---|
165 | res_graph.augment(e,1); |
---|
166 | //Let's update the total length |
---|
167 | if (res_graph.forward(e)) |
---|
168 | total_length += length[e]; |
---|
169 | else |
---|
170 | total_length -= length[e]; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | return i; |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | ///This function gives back the total length of the found paths. |
---|
184 | ///Assumes that \c run() has been run and nothing changed since then. |
---|
185 | Length totalLength(){ |
---|
186 | return total_length; |
---|
187 | } |
---|
188 | |
---|
189 | /* |
---|
190 | ///\todo To be implemented later |
---|
191 | |
---|
192 | ///This function gives back the \c j-th path in argument p. |
---|
193 | ///Assumes that \c run() has been run and nothing changed since then. |
---|
194 | /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path. |
---|
195 | template<typename DirPath> |
---|
196 | void getPath(DirPath& p, int j){ |
---|
197 | p.clear(); |
---|
198 | typename DirPath::Builder B(p); |
---|
199 | for(typename std::vector<Edge>::iterator i=paths[j].begin(); |
---|
200 | i!=paths[j].end(); ++i ){ |
---|
201 | B.pushBack(*i); |
---|
202 | } |
---|
203 | |
---|
204 | B.commit(); |
---|
205 | } |
---|
206 | |
---|
207 | */ |
---|
208 | |
---|
209 | }; //class MinCostFlows |
---|
210 | |
---|
211 | ///@} |
---|
212 | |
---|
213 | } //namespace hugo |
---|
214 | |
---|
215 | #endif //HUGO_MINCOSTFLOW_H |
---|