# HG changeset patch
# User Peter Kovacs <kpeter@inf.elte.hu>
# Date 1227863811 -3600
# Node ID 9b8778f1d9922a15dcf159d34c815df3c83d0a82
# Parent 624e673efa760f16a74fd941b320cc11c1890f5e
Many doc improvements for Preflow (#176)
- More precise doc for members.
- Add doc for public types.
- Removing \author comments.
- Use \tparam for template parameters.
diff --git a/lemon/preflow.h b/lemon/preflow.h
a
|
b
|
|
31 | 31 | /// \brief Default traits class of Preflow class. |
32 | 32 | /// |
33 | 33 | /// Default traits class of Preflow class. |
34 | | /// \param _Graph Digraph type. |
35 | | /// \param _CapacityMap Type of capacity map. |
36 | | template <typename _Graph, typename _CapacityMap> |
| 34 | /// \tparam _Digraph Digraph type. |
| 35 | /// \tparam _CapacityMap Capacity map type. |
| 36 | template <typename _Digraph, typename _CapacityMap> |
37 | 37 | struct PreflowDefaultTraits { |
38 | 38 | |
39 | | /// \brief The digraph type the algorithm runs on. |
40 | | typedef _Graph Digraph; |
| 39 | /// \brief The type of the digraph the algorithm runs on. |
| 40 | typedef _Digraph Digraph; |
41 | 41 | |
42 | 42 | /// \brief The type of the map that stores the arc capacities. |
43 | 43 | /// |
… |
… |
|
45 | 45 | /// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
46 | 46 | typedef _CapacityMap CapacityMap; |
47 | 47 | |
48 | | /// \brief The type of the length of the arcs. |
| 48 | /// \brief The type of the flow values. |
49 | 49 | typedef typename CapacityMap::Value Value; |
50 | 50 | |
51 | | /// \brief The map type that stores the flow values. |
| 51 | /// \brief The type of the map that stores the flow values. |
52 | 52 | /// |
53 | | /// The map type that stores the flow values. |
| 53 | /// The type of the map that stores the flow values. |
54 | 54 | /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
55 | 55 | typedef typename Digraph::template ArcMap<Value> FlowMap; |
56 | 56 | |
… |
… |
|
63 | 63 | return new FlowMap(digraph); |
64 | 64 | } |
65 | 65 | |
66 | | /// \brief The eleavator type used by Preflow algorithm. |
| 66 | /// \brief The elevator type used by Preflow algorithm. |
67 | 67 | /// |
68 | 68 | /// The elevator type used by Preflow algorithm. |
69 | 69 | /// |
… |
… |
|
73 | 73 | |
74 | 74 | /// \brief Instantiates an Elevator. |
75 | 75 | /// |
76 | | /// This function instantiates a \ref Elevator. |
| 76 | /// This function instantiates an \ref Elevator. |
77 | 77 | /// \param digraph The digraph, to which we would like to define |
78 | 78 | /// the elevator. |
79 | 79 | /// \param max_level The maximum level of the elevator. |
… |
… |
|
91 | 91 | |
92 | 92 | /// \ingroup max_flow |
93 | 93 | /// |
94 | | /// \brief %Preflow algorithms class. |
| 94 | /// \brief %Preflow algorithm class. |
95 | 95 | /// |
96 | | /// This class provides an implementation of the Goldberg's \e |
97 | | /// preflow \e algorithm producing a flow of maximum value in a |
98 | | /// digraph. The preflow algorithms are the fastest known max |
| 96 | /// This class provides an implementation of Goldberg's \e preflow |
| 97 | /// \e push-relabel \e algorithm producing a flow of maximum value in a |
| 98 | /// digraph. The preflow algorithms are the fastest known maximum |
99 | 99 | /// flow algorithms. The current implementation use a mixture of the |
100 | 100 | /// \e "highest label" and the \e "bound decrease" heuristics. |
101 | 101 | /// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$. |
102 | 102 | /// |
103 | | /// The algorithm consists from two phases. After the first phase |
104 | | /// the maximal flow value and the minimum cut can be obtained. The |
105 | | /// second phase constructs the feasible maximum flow on each arc. |
| 103 | /// The algorithm consists of two phases. After the first phase |
| 104 | /// the maximum flow value and the minimum cut is obtained. The |
| 105 | /// second phase constructs a feasible maximum flow on each arc. |
106 | 106 | /// |
107 | | /// \param _Graph The digraph type the algorithm runs on. |
108 | | /// \param _CapacityMap The flow map type. |
109 | | /// \param _Traits Traits class to set various data types used by |
| 107 | /// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 108 | /// \tparam _CapacityMap The type of the capacity map. The default map |
| 109 | /// type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>". |
| 110 | /// \tparam _Traits Traits class to set various data types used by |
110 | 111 | /// the algorithm. The default traits class is \ref |
111 | 112 | /// PreflowDefaultTraits. See \ref PreflowDefaultTraits for the |
112 | 113 | /// documentation of a %Preflow traits class. |
113 | | /// |
114 | | ///\author Jacint Szabo and Balazs Dezso |
115 | 114 | #ifdef DOXYGEN |
116 | | template <typename _Graph, typename _CapacityMap, typename _Traits> |
| 115 | template <typename _Digraph, typename _CapacityMap, typename _Traits> |
117 | 116 | #else |
118 | | template <typename _Graph, |
119 | | typename _CapacityMap = typename _Graph::template ArcMap<int>, |
120 | | typename _Traits = PreflowDefaultTraits<_Graph, _CapacityMap> > |
| 117 | template <typename _Digraph, |
| 118 | typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
| 119 | typename _Traits = PreflowDefaultTraits<_Digraph, _CapacityMap> > |
121 | 120 | #endif |
122 | 121 | class Preflow { |
123 | 122 | public: |
124 | 123 | |
| 124 | ///The traits class. |
125 | 125 | typedef _Traits Traits; |
| 126 | ///The type of the digraph the algorithm runs on. |
126 | 127 | typedef typename Traits::Digraph Digraph; |
| 128 | ///The type of the capacity map. |
127 | 129 | typedef typename Traits::CapacityMap CapacityMap; |
| 130 | ///The type of the flow values. |
128 | 131 | typedef typename Traits::Value Value; |
129 | 132 | |
| 133 | ///The type of the flow map. |
130 | 134 | typedef typename Traits::FlowMap FlowMap; |
| 135 | ///The type of the elevator. |
131 | 136 | typedef typename Traits::Elevator Elevator; |
| 137 | ///The type of the tolerance. |
132 | 138 | typedef typename Traits::Tolerance Tolerance; |
133 | 139 | |
134 | 140 | private: |
… |
… |
|
188 | 194 | |
189 | 195 | typedef Preflow Create; |
190 | 196 | |
191 | | ///\name Named template parameters |
| 197 | ///\name Named Template Parameters |
192 | 198 | |
193 | 199 | ///@{ |
194 | 200 | |
… |
… |
|
205 | 211 | /// FlowMap type |
206 | 212 | /// |
207 | 213 | /// \ref named-templ-param "Named parameter" for setting FlowMap |
208 | | /// type |
| 214 | /// type. |
209 | 215 | template <typename _FlowMap> |
210 | 216 | struct SetFlowMap |
211 | 217 | : public Preflow<Digraph, CapacityMap, SetFlowMapTraits<_FlowMap> > { |
… |
… |
|
226 | 232 | /// Elevator type |
227 | 233 | /// |
228 | 234 | /// \ref named-templ-param "Named parameter" for setting Elevator |
229 | | /// type |
| 235 | /// type. If this named parameter is used, then an external |
| 236 | /// elevator object must be passed to the algorithm using the |
| 237 | /// \ref elevator(Elevator&) "elevator()" function before calling |
| 238 | /// \ref run() or \ref init(). |
| 239 | /// \sa SetStandardElevator |
230 | 240 | template <typename _Elevator> |
231 | 241 | struct SetElevator |
232 | 242 | : public Preflow<Digraph, CapacityMap, SetElevatorTraits<_Elevator> > { |
… |
… |
|
243 | 253 | }; |
244 | 254 | |
245 | 255 | /// \brief \ref named-templ-param "Named parameter" for setting |
246 | | /// Elevator type |
| 256 | /// Elevator type with automatic allocation |
247 | 257 | /// |
248 | 258 | /// \ref named-templ-param "Named parameter" for setting Elevator |
249 | | /// type. The Elevator should be standard constructor interface, ie. |
250 | | /// the digraph and the maximum level should be passed to it. |
| 259 | /// type with automatic allocation. |
| 260 | /// The Elevator should have standard constructor interface to be |
| 261 | /// able to automatically created by the algorithm (i.e. the |
| 262 | /// digraph and the maximum level should be passed to it). |
| 263 | /// However an external elevator object could also be passed to the |
| 264 | /// algorithm with the \ref elevator(Elevator&) "elevator()" function |
| 265 | /// before calling \ref run() or \ref init(). |
| 266 | /// \sa SetElevator |
251 | 267 | template <typename _Elevator> |
252 | 268 | struct SetStandardElevator |
253 | 269 | : public Preflow<Digraph, CapacityMap, |
… |
… |
|
273 | 289 | /// \param source The source node. |
274 | 290 | /// \param target The target node. |
275 | 291 | Preflow(const Digraph& digraph, const CapacityMap& capacity, |
276 | | Node source, Node target) |
| 292 | Node source, Node target) |
277 | 293 | : _graph(digraph), _capacity(&capacity), |
278 | 294 | _node_num(0), _source(source), _target(target), |
279 | 295 | _flow(0), _local_flow(false), |
… |
… |
|
290 | 306 | /// \brief Sets the capacity map. |
291 | 307 | /// |
292 | 308 | /// Sets the capacity map. |
293 | | /// \return \c (*this) |
| 309 | /// \return <tt>(*this)</tt> |
294 | 310 | Preflow& capacityMap(const CapacityMap& map) { |
295 | 311 | _capacity = ↦ |
296 | 312 | return *this; |
… |
… |
|
299 | 315 | /// \brief Sets the flow map. |
300 | 316 | /// |
301 | 317 | /// Sets the flow map. |
302 | | /// \return \c (*this) |
| 318 | /// If you don't use this function before calling \ref run() or |
| 319 | /// \ref init(), an instance will be allocated automatically. |
| 320 | /// The destructor deallocates this automatically allocated map, |
| 321 | /// of course. |
| 322 | /// \return <tt>(*this)</tt> |
303 | 323 | Preflow& flowMap(FlowMap& map) { |
304 | 324 | if (_local_flow) { |
305 | 325 | delete _flow; |
… |
… |
|
309 | 329 | return *this; |
310 | 330 | } |
311 | 331 | |
312 | | /// \brief Returns the flow map. |
| 332 | /// \brief Sets the source node. |
313 | 333 | /// |
314 | | /// \return The flow map. |
315 | | const FlowMap& flowMap() { |
316 | | return *_flow; |
| 334 | /// Sets the source node. |
| 335 | /// \return <tt>(*this)</tt> |
| 336 | Preflow& source(const Node& node) { |
| 337 | _source = node; |
| 338 | return *this; |
| 339 | } |
| 340 | |
| 341 | /// \brief Sets the target node. |
| 342 | /// |
| 343 | /// Sets the target node. |
| 344 | /// \return <tt>(*this)</tt> |
| 345 | Preflow& target(const Node& node) { |
| 346 | _target = node; |
| 347 | return *this; |
317 | 348 | } |
318 | 349 | |
319 | 350 | /// \brief Sets the elevator. |
320 | 351 | /// |
321 | 352 | /// Sets the elevator. |
322 | | /// \return \c (*this) |
| 353 | /// If you don't use this function before calling \ref run() or |
| 354 | /// \ref init(), an instance will be allocated automatically. |
| 355 | /// The destructor deallocates this automatically allocated elevator, |
| 356 | /// of course. |
| 357 | /// \return <tt>(*this)</tt> |
323 | 358 | Preflow& elevator(Elevator& elevator) { |
324 | 359 | if (_local_level) { |
325 | 360 | delete _level; |
… |
… |
|
329 | 364 | return *this; |
330 | 365 | } |
331 | 366 | |
332 | | /// \brief Returns the elevator. |
| 367 | /// \brief Returns a const reference to the elevator. |
333 | 368 | /// |
334 | | /// \return The elevator. |
| 369 | /// Returns a const reference to the elevator. |
| 370 | /// |
| 371 | /// \pre Either \ref run() or \ref init() must be called before |
| 372 | /// using this function. |
335 | 373 | const Elevator& elevator() { |
336 | 374 | return *_level; |
337 | | } |
338 | | |
339 | | /// \brief Sets the source node. |
340 | | /// |
341 | | /// Sets the source node. |
342 | | /// \return \c (*this) |
343 | | Preflow& source(const Node& node) { |
344 | | _source = node; |
345 | | return *this; |
346 | | } |
347 | | |
348 | | /// \brief Sets the target node. |
349 | | /// |
350 | | /// Sets the target node. |
351 | | /// \return \c (*this) |
352 | | Preflow& target(const Node& node) { |
353 | | _target = node; |
354 | | return *this; |
355 | 375 | } |
356 | 376 | |
357 | 377 | /// \brief Sets the tolerance used by algorithm. |
… |
… |
|
369 | 389 | return tolerance; |
370 | 390 | } |
371 | 391 | |
372 | | /// \name Execution control The simplest way to execute the |
373 | | /// algorithm is to use one of the member functions called \c |
374 | | /// run(). |
375 | | /// \n |
376 | | /// If you need more control on initial solution or |
377 | | /// execution then you have to call one \ref init() function and then |
378 | | /// the startFirstPhase() and if you need the startSecondPhase(). |
| 392 | /// \name Execution Control |
| 393 | /// The simplest way to execute the preflow algorithm is to use |
| 394 | /// \ref run() or \ref runMinCut().\n |
| 395 | /// If you need more control on the initial solution or the execution, |
| 396 | /// first you have to call one of the \ref init() functions, then |
| 397 | /// \ref startFirstPhase() and if you need it \ref startSecondPhase(). |
379 | 398 | |
380 | 399 | ///@{ |
381 | 400 | |
382 | 401 | /// \brief Initializes the internal data structures. |
383 | 402 | /// |
384 | | /// Initializes the internal data structures. |
385 | | /// |
| 403 | /// Initializes the internal data structures and sets the initial |
| 404 | /// flow to zero on each arc. |
386 | 405 | void init() { |
387 | 406 | createStructures(); |
388 | 407 | |
… |
… |
|
440 | 459 | /// |
441 | 460 | /// Initializes the internal data structures and sets the initial |
442 | 461 | /// flow to the given \c flowMap. The \c flowMap should contain a |
443 | | /// flow or at least a preflow, ie. in each node excluding the |
444 | | /// target the incoming flow should greater or equal to the |
| 462 | /// flow or at least a preflow, i.e. at each node excluding the |
| 463 | /// source node the incoming flow should greater or equal to the |
445 | 464 | /// outgoing flow. |
446 | | /// \return %False when the given \c flowMap is not a preflow. |
| 465 | /// \return \c false if the given \c flowMap is not a preflow. |
447 | 466 | template <typename FlowMap> |
448 | 467 | bool flowInit(const FlowMap& flowMap) { |
449 | 468 | createStructures(); |
… |
… |
|
536 | 555 | /// maximum flow is not yet obtained. So after calling this method |
537 | 556 | /// \ref flowValue() returns the value of a maximum flow and \ref |
538 | 557 | /// minCut() returns a minimum cut. |
539 | | /// \pre One of the \ref init() functions should be called. |
| 558 | /// \pre One of the \ref init() functions must be called before |
| 559 | /// using this function. |
540 | 560 | void startFirstPhase() { |
541 | 561 | _phase = true; |
542 | 562 | |
… |
… |
|
702 | 722 | /// \brief Starts the second phase of the preflow algorithm. |
703 | 723 | /// |
704 | 724 | /// The preflow algorithm consists of two phases, this method runs |
705 | | /// the second phase. After calling \ref init() and \ref |
706 | | /// startFirstPhase() and then \ref startSecondPhase(), \ref |
707 | | /// flowMap() return a maximum flow, \ref flowValue() returns the |
| 725 | /// the second phase. After calling one of the \ref init() functions |
| 726 | /// and \ref startFirstPhase() and then \ref startSecondPhase(), |
| 727 | /// \ref flowMap() returns a maximum flow, \ref flowValue() returns the |
708 | 728 | /// value of a maximum flow, \ref minCut() returns a minimum cut |
709 | | /// \pre The \ref init() and startFirstPhase() functions should be |
710 | | /// called before. |
| 729 | /// \pre One of the \ref init() functions and \ref startFirstPhase() |
| 730 | /// must be called before using this function. |
711 | 731 | void startSecondPhase() { |
712 | 732 | _phase = false; |
713 | 733 | |
… |
… |
|
863 | 883 | /// @} |
864 | 884 | |
865 | 885 | /// \name Query Functions |
866 | | /// The result of the %Preflow algorithm can be obtained using these |
| 886 | /// The results of the preflow algorithm can be obtained using these |
867 | 887 | /// functions.\n |
868 | | /// Before the use of these functions, |
869 | | /// either run() or start() must be called. |
| 888 | /// Either one of the \ref run() "run*()" functions or one of the |
| 889 | /// \ref startFirstPhase() "start*()" functions should be called |
| 890 | /// before using them. |
870 | 891 | |
871 | 892 | ///@{ |
872 | 893 | |
873 | 894 | /// \brief Returns the value of the maximum flow. |
874 | 895 | /// |
875 | 896 | /// Returns the value of the maximum flow by returning the excess |
876 | | /// of the target node \c t. This value equals to the value of |
877 | | /// the maximum flow already after the first phase. |
| 897 | /// of the target node. This value equals to the value of |
| 898 | /// the maximum flow already after the first phase of the algorithm. |
| 899 | /// |
| 900 | /// \pre Either \ref run() or \ref init() must be called before |
| 901 | /// using this function. |
878 | 902 | Value flowValue() const { |
879 | 903 | return (*_excess)[_target]; |
880 | 904 | } |
881 | 905 | |
882 | | /// \brief Returns true when the node is on the source side of minimum cut. |
| 906 | /// \brief Returns the flow on the given arc. |
883 | 907 | /// |
884 | | /// Returns true when the node is on the source side of minimum |
885 | | /// cut. This method can be called both after running \ref |
| 908 | /// Returns the flow on the given arc. This method can |
| 909 | /// be called after the second phase of the algorithm. |
| 910 | /// |
| 911 | /// \pre Either \ref run() or \ref init() must be called before |
| 912 | /// using this function. |
| 913 | Value flow(const Arc& arc) const { |
| 914 | return (*_flow)[arc]; |
| 915 | } |
| 916 | |
| 917 | /// \brief Returns a const reference to the flow map. |
| 918 | /// |
| 919 | /// Returns a const reference to the arc map storing the found flow. |
| 920 | /// |
| 921 | /// \pre Either \ref run() or \ref init() must be called before |
| 922 | /// using this function. |
| 923 | const FlowMap& flowMap() { |
| 924 | return *_flow; |
| 925 | } |
| 926 | |
| 927 | /// \brief Returns \c true when the node is on the source side of the |
| 928 | /// minimum cut. |
| 929 | /// |
| 930 | /// Returns true when the node is on the source side of the found |
| 931 | /// minimum cut. This method can be called both after running \ref |
886 | 932 | /// startFirstPhase() and \ref startSecondPhase(). |
| 933 | /// |
| 934 | /// \pre Either \ref run() or \ref init() must be called before |
| 935 | /// using this function. |
887 | 936 | bool minCut(const Node& node) const { |
888 | 937 | return ((*_level)[node] == _level->maxLevel()) == _phase; |
889 | 938 | } |
890 | 939 | |
891 | | /// \brief Returns a minimum value cut. |
| 940 | /// \brief Gives back a minimum value cut. |
892 | 941 | /// |
893 | | /// Sets the \c cutMap to the characteristic vector of a minimum value |
894 | | /// cut. This method can be called both after running \ref |
895 | | /// startFirstPhase() and \ref startSecondPhase(). The result after second |
896 | | /// phase could be changed slightly if inexact computation is used. |
897 | | /// \pre The \c cutMap should be a bool-valued node-map. |
| 942 | /// Sets \c cutMap to the characteristic vector of a minimum value |
| 943 | /// cut. \c cutMap should be a \ref concepts::WriteMap "writable" |
| 944 | /// node map with \c bool (or convertible) value type. |
| 945 | /// |
| 946 | /// This method can be called both after running \ref startFirstPhase() |
| 947 | /// and \ref startSecondPhase(). The result after the second phase |
| 948 | /// could be slightly different if inexact computation is used. |
| 949 | /// |
| 950 | /// This function is just a shortcut of calling \ref minCut() for |
| 951 | /// each node, so it runs in \f$O(n)\f$ time. |
| 952 | /// |
| 953 | /// \pre Either \ref run() or \ref init() must be called before |
| 954 | /// using this function. |
898 | 955 | template <typename CutMap> |
899 | 956 | void minCutMap(CutMap& cutMap) const { |
900 | 957 | for (NodeIt n(_graph); n != INVALID; ++n) { |
… |
… |
|
902 | 959 | } |
903 | 960 | } |
904 | 961 | |
905 | | /// \brief Returns the flow on the arc. |
906 | | /// |
907 | | /// Sets the \c flowMap to the flow on the arcs. This method can |
908 | | /// be called after the second phase of algorithm. |
909 | | Value flow(const Arc& arc) const { |
910 | | return (*_flow)[arc]; |
911 | | } |
912 | | |
913 | 962 | /// @} |
914 | 963 | }; |
915 | 964 | } |