0
4
0
... | ... |
@@ -1488,265 +1488,265 @@ |
1488 | 1488 |
/// node, then the \c reach parameter will be set to \c true. |
1489 | 1489 |
/// |
1490 | 1490 |
/// \param target The target node. |
1491 | 1491 |
/// \retval reach Indicates if the target node is reached. |
1492 | 1492 |
/// It should be initially \c false. |
1493 | 1493 |
/// |
1494 | 1494 |
/// \return The processed node. |
1495 | 1495 |
/// |
1496 | 1496 |
/// \pre The queue must not be empty. |
1497 | 1497 |
Node processNextNode(Node target, bool& reach) { |
1498 | 1498 |
Node n = _list[++_list_front]; |
1499 | 1499 |
_visitor->process(n); |
1500 | 1500 |
Arc e; |
1501 | 1501 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1502 | 1502 |
Node m = _digraph->target(e); |
1503 | 1503 |
if (!(*_reached)[m]) { |
1504 | 1504 |
_visitor->discover(e); |
1505 | 1505 |
_visitor->reach(m); |
1506 | 1506 |
_reached->set(m, true); |
1507 | 1507 |
_list[++_list_back] = m; |
1508 | 1508 |
reach = reach || (target == m); |
1509 | 1509 |
} else { |
1510 | 1510 |
_visitor->examine(e); |
1511 | 1511 |
} |
1512 | 1512 |
} |
1513 | 1513 |
return n; |
1514 | 1514 |
} |
1515 | 1515 |
|
1516 | 1516 |
/// \brief Processes the next node. |
1517 | 1517 |
/// |
1518 | 1518 |
/// Processes the next node and checks if at least one of reached |
1519 | 1519 |
/// nodes has \c true value in the \c nm node map. If one node |
1520 | 1520 |
/// with \c true value is reachable from the processed node, then the |
1521 | 1521 |
/// \c rnode parameter will be set to the first of such nodes. |
1522 | 1522 |
/// |
1523 | 1523 |
/// \param nm A \c bool (or convertible) node map that indicates the |
1524 | 1524 |
/// possible targets. |
1525 | 1525 |
/// \retval rnode The reached target node. |
1526 | 1526 |
/// It should be initially \c INVALID. |
1527 | 1527 |
/// |
1528 | 1528 |
/// \return The processed node. |
1529 | 1529 |
/// |
1530 | 1530 |
/// \pre The queue must not be empty. |
1531 | 1531 |
template <typename NM> |
1532 | 1532 |
Node processNextNode(const NM& nm, Node& rnode) { |
1533 | 1533 |
Node n = _list[++_list_front]; |
1534 | 1534 |
_visitor->process(n); |
1535 | 1535 |
Arc e; |
1536 | 1536 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1537 | 1537 |
Node m = _digraph->target(e); |
1538 | 1538 |
if (!(*_reached)[m]) { |
1539 | 1539 |
_visitor->discover(e); |
1540 | 1540 |
_visitor->reach(m); |
1541 | 1541 |
_reached->set(m, true); |
1542 | 1542 |
_list[++_list_back] = m; |
1543 | 1543 |
if (nm[m] && rnode == INVALID) rnode = m; |
1544 | 1544 |
} else { |
1545 | 1545 |
_visitor->examine(e); |
1546 | 1546 |
} |
1547 | 1547 |
} |
1548 | 1548 |
return n; |
1549 | 1549 |
} |
1550 | 1550 |
|
1551 | 1551 |
/// \brief The next node to be processed. |
1552 | 1552 |
/// |
1553 | 1553 |
/// Returns the next node to be processed or \c INVALID if the queue |
1554 | 1554 |
/// is empty. |
1555 | 1555 |
Node nextNode() const { |
1556 | 1556 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1557 | 1557 |
} |
1558 | 1558 |
|
1559 | 1559 |
/// \brief Returns \c false if there are nodes |
1560 | 1560 |
/// to be processed. |
1561 | 1561 |
/// |
1562 | 1562 |
/// Returns \c false if there are nodes |
1563 | 1563 |
/// to be processed in the queue. |
1564 | 1564 |
bool emptyQueue() const { return _list_front == _list_back; } |
1565 | 1565 |
|
1566 | 1566 |
/// \brief Returns the number of the nodes to be processed. |
1567 | 1567 |
/// |
1568 | 1568 |
/// Returns the number of the nodes to be processed in the queue. |
1569 | 1569 |
int queueSize() const { return _list_back - _list_front; } |
1570 | 1570 |
|
1571 | 1571 |
/// \brief Executes the algorithm. |
1572 | 1572 |
/// |
1573 | 1573 |
/// Executes the algorithm. |
1574 | 1574 |
/// |
1575 | 1575 |
/// This method runs the %BFS algorithm from the root node(s) |
1576 | 1576 |
/// in order to compute the shortest path to each node. |
1577 | 1577 |
/// |
1578 | 1578 |
/// The algorithm computes |
1579 | 1579 |
/// - the shortest path tree (forest), |
1580 | 1580 |
/// - the distance of each node from the root(s). |
1581 | 1581 |
/// |
1582 | 1582 |
/// \pre init() must be called and at least one root node should be added |
1583 | 1583 |
/// with addSource() before using this function. |
1584 | 1584 |
/// |
1585 | 1585 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
1586 | 1586 |
/// \code |
1587 | 1587 |
/// while ( !b.emptyQueue() ) { |
1588 | 1588 |
/// b.processNextNode(); |
1589 | 1589 |
/// } |
1590 | 1590 |
/// \endcode |
1591 | 1591 |
void start() { |
1592 | 1592 |
while ( !emptyQueue() ) processNextNode(); |
1593 | 1593 |
} |
1594 | 1594 |
|
1595 | 1595 |
/// \brief Executes the algorithm until the given target node is reached. |
1596 | 1596 |
/// |
1597 | 1597 |
/// Executes the algorithm until the given target node is reached. |
1598 | 1598 |
/// |
1599 | 1599 |
/// This method runs the %BFS algorithm from the root node(s) |
1600 | 1600 |
/// in order to compute the shortest path to \c t. |
1601 | 1601 |
/// |
1602 | 1602 |
/// The algorithm computes |
1603 | 1603 |
/// - the shortest path to \c t, |
1604 | 1604 |
/// - the distance of \c t from the root(s). |
1605 | 1605 |
/// |
1606 | 1606 |
/// \pre init() must be called and at least one root node should be |
1607 | 1607 |
/// added with addSource() before using this function. |
1608 | 1608 |
/// |
1609 | 1609 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
1610 | 1610 |
/// \code |
1611 | 1611 |
/// bool reach = false; |
1612 | 1612 |
/// while ( !b.emptyQueue() && !reach ) { |
1613 | 1613 |
/// b.processNextNode(t, reach); |
1614 | 1614 |
/// } |
1615 | 1615 |
/// \endcode |
1616 | 1616 |
void start(Node t) { |
1617 | 1617 |
bool reach = false; |
1618 | 1618 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
1619 | 1619 |
} |
1620 | 1620 |
|
1621 | 1621 |
/// \brief Executes the algorithm until a condition is met. |
1622 | 1622 |
/// |
1623 | 1623 |
/// Executes the algorithm until a condition is met. |
1624 | 1624 |
/// |
1625 | 1625 |
/// This method runs the %BFS algorithm from the root node(s) in |
1626 | 1626 |
/// order to compute the shortest path to a node \c v with |
1627 | 1627 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
1628 | 1628 |
/// |
1629 | 1629 |
/// \param nm must be a bool (or convertible) node map. The |
1630 | 1630 |
/// algorithm will stop when it reaches a node \c v with |
1631 | 1631 |
/// <tt>nm[v]</tt> true. |
1632 | 1632 |
/// |
1633 | 1633 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or |
1634 | 1634 |
/// \c INVALID if no such node was found. |
1635 | 1635 |
/// |
1636 | 1636 |
/// \pre init() must be called and at least one root node should be |
1637 | 1637 |
/// added with addSource() before using this function. |
1638 | 1638 |
/// |
1639 | 1639 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
1640 | 1640 |
/// \code |
1641 | 1641 |
/// Node rnode = INVALID; |
1642 | 1642 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
1643 | 1643 |
/// b.processNextNode(nm, rnode); |
1644 | 1644 |
/// } |
1645 | 1645 |
/// return rnode; |
1646 | 1646 |
/// \endcode |
1647 | 1647 |
template <typename NM> |
1648 | 1648 |
Node start(const NM &nm) { |
1649 | 1649 |
Node rnode = INVALID; |
1650 | 1650 |
while ( !emptyQueue() && rnode == INVALID ) { |
1651 | 1651 |
processNextNode(nm, rnode); |
1652 | 1652 |
} |
1653 | 1653 |
return rnode; |
1654 | 1654 |
} |
1655 | 1655 |
|
1656 | 1656 |
/// \brief Runs the algorithm from the given source node. |
1657 | 1657 |
/// |
1658 | 1658 |
/// This method runs the %BFS algorithm from node \c s |
1659 | 1659 |
/// in order to compute the shortest path to each node. |
1660 | 1660 |
/// |
1661 | 1661 |
/// The algorithm computes |
1662 | 1662 |
/// - the shortest path tree, |
1663 | 1663 |
/// - the distance of each node from the root. |
1664 | 1664 |
/// |
1665 | 1665 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1666 | 1666 |
///\code |
1667 | 1667 |
/// b.init(); |
1668 | 1668 |
/// b.addSource(s); |
1669 | 1669 |
/// b.start(); |
1670 | 1670 |
///\endcode |
1671 | 1671 |
void run(Node s) { |
1672 | 1672 |
init(); |
1673 | 1673 |
addSource(s); |
1674 | 1674 |
start(); |
1675 | 1675 |
} |
1676 | 1676 |
|
1677 | 1677 |
/// \brief Finds the shortest path between \c s and \c t. |
1678 | 1678 |
/// |
1679 | 1679 |
/// This method runs the %BFS algorithm from node \c s |
1680 | 1680 |
/// in order to compute the shortest path to node \c t |
1681 | 1681 |
/// (it stops searching when \c t is processed). |
1682 | 1682 |
/// |
1683 | 1683 |
/// \return \c true if \c t is reachable form \c s. |
1684 | 1684 |
/// |
1685 | 1685 |
/// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
1686 | 1686 |
/// shortcut of the following code. |
1687 | 1687 |
///\code |
1688 | 1688 |
/// b.init(); |
1689 | 1689 |
/// b.addSource(s); |
1690 | 1690 |
/// b.start(t); |
1691 | 1691 |
///\endcode |
1692 | 1692 |
bool run(Node s,Node t) { |
1693 | 1693 |
init(); |
1694 | 1694 |
addSource(s); |
1695 | 1695 |
start(t); |
1696 | 1696 |
return reached(t); |
1697 | 1697 |
} |
1698 | 1698 |
|
1699 | 1699 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1700 | 1700 |
/// |
1701 | 1701 |
/// This method runs the %BFS algorithm in order to |
1702 | 1702 |
/// compute the shortest path to each node. |
1703 | 1703 |
/// |
1704 | 1704 |
/// The algorithm computes |
1705 | 1705 |
/// - the shortest path tree (forest), |
1706 | 1706 |
/// - the distance of each node from the root(s). |
1707 | 1707 |
/// |
1708 | 1708 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1709 | 1709 |
///\code |
1710 | 1710 |
/// b.init(); |
1711 | 1711 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1712 | 1712 |
/// if (!b.reached(n)) { |
1713 | 1713 |
/// b.addSource(n); |
1714 | 1714 |
/// b.start(); |
1715 | 1715 |
/// } |
1716 | 1716 |
/// } |
1717 | 1717 |
///\endcode |
1718 | 1718 |
void run() { |
1719 | 1719 |
init(); |
1720 | 1720 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1721 | 1721 |
if (!reached(it)) { |
1722 | 1722 |
addSource(it); |
1723 | 1723 |
start(); |
1724 | 1724 |
} |
1725 | 1725 |
} |
1726 | 1726 |
} |
1727 | 1727 |
|
1728 | 1728 |
///@} |
1729 | 1729 |
|
1730 | 1730 |
/// \name Query Functions |
1731 | 1731 |
/// The results of the BFS algorithm can be obtained using these |
1732 | 1732 |
/// functions.\n |
1733 | 1733 |
/// Either \ref run(Node) "run()" or \ref start() should be called |
1734 | 1734 |
/// before using them. |
1735 | 1735 |
|
1736 | 1736 |
///@{ |
1737 | 1737 |
|
1738 | 1738 |
/// \brief Checks if a node is reached from the root(s). |
1739 | 1739 |
/// |
1740 | 1740 |
/// Returns \c true if \c v is reached from the root(s). |
1741 | 1741 |
/// |
1742 | 1742 |
/// \pre Either \ref run(Node) "run()" or \ref init() |
1743 | 1743 |
/// must be called before using this function. |
1744 |
bool reached(Node v) { return (*_reached)[v]; } |
|
1744 |
bool reached(Node v) const { return (*_reached)[v]; } |
|
1745 | 1745 |
|
1746 | 1746 |
///@} |
1747 | 1747 |
|
1748 | 1748 |
}; |
1749 | 1749 |
|
1750 | 1750 |
} //END OF NAMESPACE LEMON |
1751 | 1751 |
|
1752 | 1752 |
#endif |
... | ... |
@@ -166,590 +166,590 @@ |
166 | 166 |
|
167 | 167 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
168 | 168 |
typedef _Traits Traits; |
169 | 169 |
///The type of the digraph the algorithm runs on. |
170 | 170 |
typedef typename Traits::Digraph Digraph; |
171 | 171 |
///The type of the flow values. |
172 | 172 |
typedef typename Traits::Value Value; |
173 | 173 |
|
174 | 174 |
/// The type of the lower bound capacity map. |
175 | 175 |
typedef typename Traits::LCapMap LCapMap; |
176 | 176 |
/// The type of the upper bound capacity map. |
177 | 177 |
typedef typename Traits::UCapMap UCapMap; |
178 | 178 |
/// \brief The type of the map that stores the lower bound for |
179 | 179 |
/// the supply of the nodes. |
180 | 180 |
typedef typename Traits::DeltaMap DeltaMap; |
181 | 181 |
///The type of the flow map. |
182 | 182 |
typedef typename Traits::FlowMap FlowMap; |
183 | 183 |
|
184 | 184 |
///The type of the elevator. |
185 | 185 |
typedef typename Traits::Elevator Elevator; |
186 | 186 |
///The type of the tolerance. |
187 | 187 |
typedef typename Traits::Tolerance Tolerance; |
188 | 188 |
|
189 | 189 |
private: |
190 | 190 |
|
191 | 191 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
192 | 192 |
|
193 | 193 |
const Digraph &_g; |
194 | 194 |
int _node_num; |
195 | 195 |
|
196 | 196 |
const LCapMap *_lo; |
197 | 197 |
const UCapMap *_up; |
198 | 198 |
const DeltaMap *_delta; |
199 | 199 |
|
200 | 200 |
FlowMap *_flow; |
201 | 201 |
bool _local_flow; |
202 | 202 |
|
203 | 203 |
Elevator* _level; |
204 | 204 |
bool _local_level; |
205 | 205 |
|
206 | 206 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
207 | 207 |
ExcessMap* _excess; |
208 | 208 |
|
209 | 209 |
Tolerance _tol; |
210 | 210 |
int _el; |
211 | 211 |
|
212 | 212 |
public: |
213 | 213 |
|
214 | 214 |
typedef Circulation Create; |
215 | 215 |
|
216 | 216 |
///\name Named Template Parameters |
217 | 217 |
|
218 | 218 |
///@{ |
219 | 219 |
|
220 | 220 |
template <typename _FlowMap> |
221 | 221 |
struct SetFlowMapTraits : public Traits { |
222 | 222 |
typedef _FlowMap FlowMap; |
223 | 223 |
static FlowMap *createFlowMap(const Digraph&) { |
224 | 224 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
225 | 225 |
return 0; // ignore warnings |
226 | 226 |
} |
227 | 227 |
}; |
228 | 228 |
|
229 | 229 |
/// \brief \ref named-templ-param "Named parameter" for setting |
230 | 230 |
/// FlowMap type |
231 | 231 |
/// |
232 | 232 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
233 | 233 |
/// type. |
234 | 234 |
template <typename _FlowMap> |
235 | 235 |
struct SetFlowMap |
236 | 236 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
237 | 237 |
SetFlowMapTraits<_FlowMap> > { |
238 | 238 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
239 | 239 |
SetFlowMapTraits<_FlowMap> > Create; |
240 | 240 |
}; |
241 | 241 |
|
242 | 242 |
template <typename _Elevator> |
243 | 243 |
struct SetElevatorTraits : public Traits { |
244 | 244 |
typedef _Elevator Elevator; |
245 | 245 |
static Elevator *createElevator(const Digraph&, int) { |
246 | 246 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
247 | 247 |
return 0; // ignore warnings |
248 | 248 |
} |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
/// \brief \ref named-templ-param "Named parameter" for setting |
252 | 252 |
/// Elevator type |
253 | 253 |
/// |
254 | 254 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
255 | 255 |
/// type. If this named parameter is used, then an external |
256 | 256 |
/// elevator object must be passed to the algorithm using the |
257 | 257 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
258 | 258 |
/// \ref run() or \ref init(). |
259 | 259 |
/// \sa SetStandardElevator |
260 | 260 |
template <typename _Elevator> |
261 | 261 |
struct SetElevator |
262 | 262 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
263 | 263 |
SetElevatorTraits<_Elevator> > { |
264 | 264 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
265 | 265 |
SetElevatorTraits<_Elevator> > Create; |
266 | 266 |
}; |
267 | 267 |
|
268 | 268 |
template <typename _Elevator> |
269 | 269 |
struct SetStandardElevatorTraits : public Traits { |
270 | 270 |
typedef _Elevator Elevator; |
271 | 271 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
272 | 272 |
return new Elevator(digraph, max_level); |
273 | 273 |
} |
274 | 274 |
}; |
275 | 275 |
|
276 | 276 |
/// \brief \ref named-templ-param "Named parameter" for setting |
277 | 277 |
/// Elevator type with automatic allocation |
278 | 278 |
/// |
279 | 279 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
280 | 280 |
/// type with automatic allocation. |
281 | 281 |
/// The Elevator should have standard constructor interface to be |
282 | 282 |
/// able to automatically created by the algorithm (i.e. the |
283 | 283 |
/// digraph and the maximum level should be passed to it). |
284 | 284 |
/// However an external elevator object could also be passed to the |
285 | 285 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
286 | 286 |
/// before calling \ref run() or \ref init(). |
287 | 287 |
/// \sa SetElevator |
288 | 288 |
template <typename _Elevator> |
289 | 289 |
struct SetStandardElevator |
290 | 290 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
291 | 291 |
SetStandardElevatorTraits<_Elevator> > { |
292 | 292 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
293 | 293 |
SetStandardElevatorTraits<_Elevator> > Create; |
294 | 294 |
}; |
295 | 295 |
|
296 | 296 |
/// @} |
297 | 297 |
|
298 | 298 |
protected: |
299 | 299 |
|
300 | 300 |
Circulation() {} |
301 | 301 |
|
302 | 302 |
public: |
303 | 303 |
|
304 | 304 |
/// The constructor of the class. |
305 | 305 |
|
306 | 306 |
/// The constructor of the class. |
307 | 307 |
/// \param g The digraph the algorithm runs on. |
308 | 308 |
/// \param lo The lower bound capacity of the arcs. |
309 | 309 |
/// \param up The upper bound capacity of the arcs. |
310 | 310 |
/// \param delta The lower bound for the supply of the nodes. |
311 | 311 |
Circulation(const Digraph &g,const LCapMap &lo, |
312 | 312 |
const UCapMap &up,const DeltaMap &delta) |
313 | 313 |
: _g(g), _node_num(), |
314 | 314 |
_lo(&lo),_up(&up),_delta(&delta),_flow(0),_local_flow(false), |
315 | 315 |
_level(0), _local_level(false), _excess(0), _el() {} |
316 | 316 |
|
317 | 317 |
/// Destructor. |
318 | 318 |
~Circulation() { |
319 | 319 |
destroyStructures(); |
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
|
323 | 323 |
private: |
324 | 324 |
|
325 | 325 |
void createStructures() { |
326 | 326 |
_node_num = _el = countNodes(_g); |
327 | 327 |
|
328 | 328 |
if (!_flow) { |
329 | 329 |
_flow = Traits::createFlowMap(_g); |
330 | 330 |
_local_flow = true; |
331 | 331 |
} |
332 | 332 |
if (!_level) { |
333 | 333 |
_level = Traits::createElevator(_g, _node_num); |
334 | 334 |
_local_level = true; |
335 | 335 |
} |
336 | 336 |
if (!_excess) { |
337 | 337 |
_excess = new ExcessMap(_g); |
338 | 338 |
} |
339 | 339 |
} |
340 | 340 |
|
341 | 341 |
void destroyStructures() { |
342 | 342 |
if (_local_flow) { |
343 | 343 |
delete _flow; |
344 | 344 |
} |
345 | 345 |
if (_local_level) { |
346 | 346 |
delete _level; |
347 | 347 |
} |
348 | 348 |
if (_excess) { |
349 | 349 |
delete _excess; |
350 | 350 |
} |
351 | 351 |
} |
352 | 352 |
|
353 | 353 |
public: |
354 | 354 |
|
355 | 355 |
/// Sets the lower bound capacity map. |
356 | 356 |
|
357 | 357 |
/// Sets the lower bound capacity map. |
358 | 358 |
/// \return <tt>(*this)</tt> |
359 | 359 |
Circulation& lowerCapMap(const LCapMap& map) { |
360 | 360 |
_lo = ↦ |
361 | 361 |
return *this; |
362 | 362 |
} |
363 | 363 |
|
364 | 364 |
/// Sets the upper bound capacity map. |
365 | 365 |
|
366 | 366 |
/// Sets the upper bound capacity map. |
367 | 367 |
/// \return <tt>(*this)</tt> |
368 | 368 |
Circulation& upperCapMap(const LCapMap& map) { |
369 | 369 |
_up = ↦ |
370 | 370 |
return *this; |
371 | 371 |
} |
372 | 372 |
|
373 | 373 |
/// Sets the lower bound map for the supply of the nodes. |
374 | 374 |
|
375 | 375 |
/// Sets the lower bound map for the supply of the nodes. |
376 | 376 |
/// \return <tt>(*this)</tt> |
377 | 377 |
Circulation& deltaMap(const DeltaMap& map) { |
378 | 378 |
_delta = ↦ |
379 | 379 |
return *this; |
380 | 380 |
} |
381 | 381 |
|
382 | 382 |
/// \brief Sets the flow map. |
383 | 383 |
/// |
384 | 384 |
/// Sets the flow map. |
385 | 385 |
/// If you don't use this function before calling \ref run() or |
386 | 386 |
/// \ref init(), an instance will be allocated automatically. |
387 | 387 |
/// The destructor deallocates this automatically allocated map, |
388 | 388 |
/// of course. |
389 | 389 |
/// \return <tt>(*this)</tt> |
390 | 390 |
Circulation& flowMap(FlowMap& map) { |
391 | 391 |
if (_local_flow) { |
392 | 392 |
delete _flow; |
393 | 393 |
_local_flow = false; |
394 | 394 |
} |
395 | 395 |
_flow = ↦ |
396 | 396 |
return *this; |
397 | 397 |
} |
398 | 398 |
|
399 | 399 |
/// \brief Sets the elevator used by algorithm. |
400 | 400 |
/// |
401 | 401 |
/// Sets the elevator used by algorithm. |
402 | 402 |
/// If you don't use this function before calling \ref run() or |
403 | 403 |
/// \ref init(), an instance will be allocated automatically. |
404 | 404 |
/// The destructor deallocates this automatically allocated elevator, |
405 | 405 |
/// of course. |
406 | 406 |
/// \return <tt>(*this)</tt> |
407 | 407 |
Circulation& elevator(Elevator& elevator) { |
408 | 408 |
if (_local_level) { |
409 | 409 |
delete _level; |
410 | 410 |
_local_level = false; |
411 | 411 |
} |
412 | 412 |
_level = &elevator; |
413 | 413 |
return *this; |
414 | 414 |
} |
415 | 415 |
|
416 | 416 |
/// \brief Returns a const reference to the elevator. |
417 | 417 |
/// |
418 | 418 |
/// Returns a const reference to the elevator. |
419 | 419 |
/// |
420 | 420 |
/// \pre Either \ref run() or \ref init() must be called before |
421 | 421 |
/// using this function. |
422 |
const Elevator& elevator() { |
|
422 |
const Elevator& elevator() const { |
|
423 | 423 |
return *_level; |
424 | 424 |
} |
425 | 425 |
|
426 | 426 |
/// \brief Sets the tolerance used by algorithm. |
427 | 427 |
/// |
428 | 428 |
/// Sets the tolerance used by algorithm. |
429 | 429 |
Circulation& tolerance(const Tolerance& tolerance) const { |
430 | 430 |
_tol = tolerance; |
431 | 431 |
return *this; |
432 | 432 |
} |
433 | 433 |
|
434 | 434 |
/// \brief Returns a const reference to the tolerance. |
435 | 435 |
/// |
436 | 436 |
/// Returns a const reference to the tolerance. |
437 | 437 |
const Tolerance& tolerance() const { |
438 | 438 |
return tolerance; |
439 | 439 |
} |
440 | 440 |
|
441 | 441 |
/// \name Execution Control |
442 | 442 |
/// The simplest way to execute the algorithm is to call \ref run().\n |
443 | 443 |
/// If you need more control on the initial solution or the execution, |
444 | 444 |
/// first you have to call one of the \ref init() functions, then |
445 | 445 |
/// the \ref start() function. |
446 | 446 |
|
447 | 447 |
///@{ |
448 | 448 |
|
449 | 449 |
/// Initializes the internal data structures. |
450 | 450 |
|
451 | 451 |
/// Initializes the internal data structures and sets all flow values |
452 | 452 |
/// to the lower bound. |
453 | 453 |
void init() |
454 | 454 |
{ |
455 | 455 |
createStructures(); |
456 | 456 |
|
457 | 457 |
for(NodeIt n(_g);n!=INVALID;++n) { |
458 | 458 |
_excess->set(n, (*_delta)[n]); |
459 | 459 |
} |
460 | 460 |
|
461 | 461 |
for (ArcIt e(_g);e!=INVALID;++e) { |
462 | 462 |
_flow->set(e, (*_lo)[e]); |
463 | 463 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_flow)[e]); |
464 | 464 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_flow)[e]); |
465 | 465 |
} |
466 | 466 |
|
467 | 467 |
// global relabeling tested, but in general case it provides |
468 | 468 |
// worse performance for random digraphs |
469 | 469 |
_level->initStart(); |
470 | 470 |
for(NodeIt n(_g);n!=INVALID;++n) |
471 | 471 |
_level->initAddItem(n); |
472 | 472 |
_level->initFinish(); |
473 | 473 |
for(NodeIt n(_g);n!=INVALID;++n) |
474 | 474 |
if(_tol.positive((*_excess)[n])) |
475 | 475 |
_level->activate(n); |
476 | 476 |
} |
477 | 477 |
|
478 | 478 |
/// Initializes the internal data structures using a greedy approach. |
479 | 479 |
|
480 | 480 |
/// Initializes the internal data structures using a greedy approach |
481 | 481 |
/// to construct the initial solution. |
482 | 482 |
void greedyInit() |
483 | 483 |
{ |
484 | 484 |
createStructures(); |
485 | 485 |
|
486 | 486 |
for(NodeIt n(_g);n!=INVALID;++n) { |
487 | 487 |
_excess->set(n, (*_delta)[n]); |
488 | 488 |
} |
489 | 489 |
|
490 | 490 |
for (ArcIt e(_g);e!=INVALID;++e) { |
491 | 491 |
if (!_tol.positive((*_excess)[_g.target(e)] + (*_up)[e])) { |
492 | 492 |
_flow->set(e, (*_up)[e]); |
493 | 493 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_up)[e]); |
494 | 494 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_up)[e]); |
495 | 495 |
} else if (_tol.positive((*_excess)[_g.target(e)] + (*_lo)[e])) { |
496 | 496 |
_flow->set(e, (*_lo)[e]); |
497 | 497 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_lo)[e]); |
498 | 498 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_lo)[e]); |
499 | 499 |
} else { |
500 | 500 |
Value fc = -(*_excess)[_g.target(e)]; |
501 | 501 |
_flow->set(e, fc); |
502 | 502 |
_excess->set(_g.target(e), 0); |
503 | 503 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - fc); |
504 | 504 |
} |
505 | 505 |
} |
506 | 506 |
|
507 | 507 |
_level->initStart(); |
508 | 508 |
for(NodeIt n(_g);n!=INVALID;++n) |
509 | 509 |
_level->initAddItem(n); |
510 | 510 |
_level->initFinish(); |
511 | 511 |
for(NodeIt n(_g);n!=INVALID;++n) |
512 | 512 |
if(_tol.positive((*_excess)[n])) |
513 | 513 |
_level->activate(n); |
514 | 514 |
} |
515 | 515 |
|
516 | 516 |
///Executes the algorithm |
517 | 517 |
|
518 | 518 |
///This function executes the algorithm. |
519 | 519 |
/// |
520 | 520 |
///\return \c true if a feasible circulation is found. |
521 | 521 |
/// |
522 | 522 |
///\sa barrier() |
523 | 523 |
///\sa barrierMap() |
524 | 524 |
bool start() |
525 | 525 |
{ |
526 | 526 |
|
527 | 527 |
Node act; |
528 | 528 |
Node bact=INVALID; |
529 | 529 |
Node last_activated=INVALID; |
530 | 530 |
while((act=_level->highestActive())!=INVALID) { |
531 | 531 |
int actlevel=(*_level)[act]; |
532 | 532 |
int mlevel=_node_num; |
533 | 533 |
Value exc=(*_excess)[act]; |
534 | 534 |
|
535 | 535 |
for(OutArcIt e(_g,act);e!=INVALID; ++e) { |
536 | 536 |
Node v = _g.target(e); |
537 | 537 |
Value fc=(*_up)[e]-(*_flow)[e]; |
538 | 538 |
if(!_tol.positive(fc)) continue; |
539 | 539 |
if((*_level)[v]<actlevel) { |
540 | 540 |
if(!_tol.less(fc, exc)) { |
541 | 541 |
_flow->set(e, (*_flow)[e] + exc); |
542 | 542 |
_excess->set(v, (*_excess)[v] + exc); |
543 | 543 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
544 | 544 |
_level->activate(v); |
545 | 545 |
_excess->set(act,0); |
546 | 546 |
_level->deactivate(act); |
547 | 547 |
goto next_l; |
548 | 548 |
} |
549 | 549 |
else { |
550 | 550 |
_flow->set(e, (*_up)[e]); |
551 | 551 |
_excess->set(v, (*_excess)[v] + fc); |
552 | 552 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
553 | 553 |
_level->activate(v); |
554 | 554 |
exc-=fc; |
555 | 555 |
} |
556 | 556 |
} |
557 | 557 |
else if((*_level)[v]<mlevel) mlevel=(*_level)[v]; |
558 | 558 |
} |
559 | 559 |
for(InArcIt e(_g,act);e!=INVALID; ++e) { |
560 | 560 |
Node v = _g.source(e); |
561 | 561 |
Value fc=(*_flow)[e]-(*_lo)[e]; |
562 | 562 |
if(!_tol.positive(fc)) continue; |
563 | 563 |
if((*_level)[v]<actlevel) { |
564 | 564 |
if(!_tol.less(fc, exc)) { |
565 | 565 |
_flow->set(e, (*_flow)[e] - exc); |
566 | 566 |
_excess->set(v, (*_excess)[v] + exc); |
567 | 567 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
568 | 568 |
_level->activate(v); |
569 | 569 |
_excess->set(act,0); |
570 | 570 |
_level->deactivate(act); |
571 | 571 |
goto next_l; |
572 | 572 |
} |
573 | 573 |
else { |
574 | 574 |
_flow->set(e, (*_lo)[e]); |
575 | 575 |
_excess->set(v, (*_excess)[v] + fc); |
576 | 576 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
577 | 577 |
_level->activate(v); |
578 | 578 |
exc-=fc; |
579 | 579 |
} |
580 | 580 |
} |
581 | 581 |
else if((*_level)[v]<mlevel) mlevel=(*_level)[v]; |
582 | 582 |
} |
583 | 583 |
|
584 | 584 |
_excess->set(act, exc); |
585 | 585 |
if(!_tol.positive(exc)) _level->deactivate(act); |
586 | 586 |
else if(mlevel==_node_num) { |
587 | 587 |
_level->liftHighestActiveToTop(); |
588 | 588 |
_el = _node_num; |
589 | 589 |
return false; |
590 | 590 |
} |
591 | 591 |
else { |
592 | 592 |
_level->liftHighestActive(mlevel+1); |
593 | 593 |
if(_level->onLevel(actlevel)==0) { |
594 | 594 |
_el = actlevel; |
595 | 595 |
return false; |
596 | 596 |
} |
597 | 597 |
} |
598 | 598 |
next_l: |
599 | 599 |
; |
600 | 600 |
} |
601 | 601 |
return true; |
602 | 602 |
} |
603 | 603 |
|
604 | 604 |
/// Runs the algorithm. |
605 | 605 |
|
606 | 606 |
/// This function runs the algorithm. |
607 | 607 |
/// |
608 | 608 |
/// \return \c true if a feasible circulation is found. |
609 | 609 |
/// |
610 | 610 |
/// \note Apart from the return value, c.run() is just a shortcut of |
611 | 611 |
/// the following code. |
612 | 612 |
/// \code |
613 | 613 |
/// c.greedyInit(); |
614 | 614 |
/// c.start(); |
615 | 615 |
/// \endcode |
616 | 616 |
bool run() { |
617 | 617 |
greedyInit(); |
618 | 618 |
return start(); |
619 | 619 |
} |
620 | 620 |
|
621 | 621 |
/// @} |
622 | 622 |
|
623 | 623 |
/// \name Query Functions |
624 | 624 |
/// The results of the circulation algorithm can be obtained using |
625 | 625 |
/// these functions.\n |
626 | 626 |
/// Either \ref run() or \ref start() should be called before |
627 | 627 |
/// using them. |
628 | 628 |
|
629 | 629 |
///@{ |
630 | 630 |
|
631 | 631 |
/// \brief Returns the flow on the given arc. |
632 | 632 |
/// |
633 | 633 |
/// Returns the flow on the given arc. |
634 | 634 |
/// |
635 | 635 |
/// \pre Either \ref run() or \ref init() must be called before |
636 | 636 |
/// using this function. |
637 | 637 |
Value flow(const Arc& arc) const { |
638 | 638 |
return (*_flow)[arc]; |
639 | 639 |
} |
640 | 640 |
|
641 | 641 |
/// \brief Returns a const reference to the flow map. |
642 | 642 |
/// |
643 | 643 |
/// Returns a const reference to the arc map storing the found flow. |
644 | 644 |
/// |
645 | 645 |
/// \pre Either \ref run() or \ref init() must be called before |
646 | 646 |
/// using this function. |
647 |
const FlowMap& flowMap() { |
|
647 |
const FlowMap& flowMap() const { |
|
648 | 648 |
return *_flow; |
649 | 649 |
} |
650 | 650 |
|
651 | 651 |
/** |
652 | 652 |
\brief Returns \c true if the given node is in a barrier. |
653 | 653 |
|
654 | 654 |
Barrier is a set \e B of nodes for which |
655 | 655 |
|
656 | 656 |
\f[ \sum_{a\in\delta_{out}(B)} upper(a) - |
657 | 657 |
\sum_{a\in\delta_{in}(B)} lower(a) < \sum_{v\in B}delta(v) \f] |
658 | 658 |
|
659 | 659 |
holds. The existence of a set with this property prooves that a |
660 | 660 |
feasible circualtion cannot exist. |
661 | 661 |
|
662 | 662 |
This function returns \c true if the given node is in the found |
663 | 663 |
barrier. If a feasible circulation is found, the function |
664 | 664 |
gives back \c false for every node. |
665 | 665 |
|
666 | 666 |
\pre Either \ref run() or \ref init() must be called before |
667 | 667 |
using this function. |
668 | 668 |
|
669 | 669 |
\sa barrierMap() |
670 | 670 |
\sa checkBarrier() |
671 | 671 |
*/ |
672 |
bool barrier(const Node& node) |
|
672 |
bool barrier(const Node& node) const |
|
673 | 673 |
{ |
674 | 674 |
return (*_level)[node] >= _el; |
675 | 675 |
} |
676 | 676 |
|
677 | 677 |
/// \brief Gives back a barrier. |
678 | 678 |
/// |
679 | 679 |
/// This function sets \c bar to the characteristic vector of the |
680 | 680 |
/// found barrier. \c bar should be a \ref concepts::WriteMap "writable" |
681 | 681 |
/// node map with \c bool (or convertible) value type. |
682 | 682 |
/// |
683 | 683 |
/// If a feasible circulation is found, the function gives back an |
684 | 684 |
/// empty set, so \c bar[v] will be \c false for all nodes \c v. |
685 | 685 |
/// |
686 | 686 |
/// \note This function calls \ref barrier() for each node, |
687 | 687 |
/// so it runs in \f$O(n)\f$ time. |
688 | 688 |
/// |
689 | 689 |
/// \pre Either \ref run() or \ref init() must be called before |
690 | 690 |
/// using this function. |
691 | 691 |
/// |
692 | 692 |
/// \sa barrier() |
693 | 693 |
/// \sa checkBarrier() |
694 | 694 |
template<class BarrierMap> |
695 |
void barrierMap(BarrierMap &bar) |
|
695 |
void barrierMap(BarrierMap &bar) const |
|
696 | 696 |
{ |
697 | 697 |
for(NodeIt n(_g);n!=INVALID;++n) |
698 | 698 |
bar.set(n, (*_level)[n] >= _el); |
699 | 699 |
} |
700 | 700 |
|
701 | 701 |
/// @} |
702 | 702 |
|
703 | 703 |
/// \name Checker Functions |
704 | 704 |
/// The feasibility of the results can be checked using |
705 | 705 |
/// these functions.\n |
706 | 706 |
/// Either \ref run() or \ref start() should be called before |
707 | 707 |
/// using them. |
708 | 708 |
|
709 | 709 |
///@{ |
710 | 710 |
|
711 | 711 |
///Check if the found flow is a feasible circulation |
712 | 712 |
|
713 | 713 |
///Check if the found flow is a feasible circulation, |
714 | 714 |
/// |
715 |
bool checkFlow() { |
|
715 |
bool checkFlow() const { |
|
716 | 716 |
for(ArcIt e(_g);e!=INVALID;++e) |
717 | 717 |
if((*_flow)[e]<(*_lo)[e]||(*_flow)[e]>(*_up)[e]) return false; |
718 | 718 |
for(NodeIt n(_g);n!=INVALID;++n) |
719 | 719 |
{ |
720 | 720 |
Value dif=-(*_delta)[n]; |
721 | 721 |
for(InArcIt e(_g,n);e!=INVALID;++e) dif-=(*_flow)[e]; |
722 | 722 |
for(OutArcIt e(_g,n);e!=INVALID;++e) dif+=(*_flow)[e]; |
723 | 723 |
if(_tol.negative(dif)) return false; |
724 | 724 |
} |
725 | 725 |
return true; |
726 | 726 |
} |
727 | 727 |
|
728 | 728 |
///Check whether or not the last execution provides a barrier |
729 | 729 |
|
730 | 730 |
///Check whether or not the last execution provides a barrier. |
731 | 731 |
///\sa barrier() |
732 | 732 |
///\sa barrierMap() |
733 |
bool checkBarrier() |
|
733 |
bool checkBarrier() const |
|
734 | 734 |
{ |
735 | 735 |
Value delta=0; |
736 | 736 |
for(NodeIt n(_g);n!=INVALID;++n) |
737 | 737 |
if(barrier(n)) |
738 | 738 |
delta-=(*_delta)[n]; |
739 | 739 |
for(ArcIt e(_g);e!=INVALID;++e) |
740 | 740 |
{ |
741 | 741 |
Node s=_g.source(e); |
742 | 742 |
Node t=_g.target(e); |
743 | 743 |
if(barrier(s)&&!barrier(t)) delta+=(*_up)[e]; |
744 | 744 |
else if(barrier(t)&&!barrier(s)) delta-=(*_lo)[e]; |
745 | 745 |
} |
746 | 746 |
return _tol.negative(delta); |
747 | 747 |
} |
748 | 748 |
|
749 | 749 |
/// @} |
750 | 750 |
|
751 | 751 |
}; |
752 | 752 |
|
753 | 753 |
} |
754 | 754 |
|
755 | 755 |
#endif |
... | ... |
@@ -1372,265 +1372,265 @@ |
1372 | 1372 |
/// If you need more control on the execution, first you have to call |
1373 | 1373 |
/// \ref init(), then you can add a source node with \ref addSource() |
1374 | 1374 |
/// and perform the actual computation with \ref start(). |
1375 | 1375 |
/// This procedure can be repeated if there are nodes that have not |
1376 | 1376 |
/// been reached. |
1377 | 1377 |
|
1378 | 1378 |
/// @{ |
1379 | 1379 |
|
1380 | 1380 |
/// \brief Initializes the internal data structures. |
1381 | 1381 |
/// |
1382 | 1382 |
/// Initializes the internal data structures. |
1383 | 1383 |
void init() { |
1384 | 1384 |
create_maps(); |
1385 | 1385 |
_stack.resize(countNodes(*_digraph)); |
1386 | 1386 |
_stack_head = -1; |
1387 | 1387 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1388 | 1388 |
_reached->set(u, false); |
1389 | 1389 |
} |
1390 | 1390 |
} |
1391 | 1391 |
|
1392 | 1392 |
/// \brief Adds a new source node. |
1393 | 1393 |
/// |
1394 | 1394 |
/// Adds a new source node to the set of nodes to be processed. |
1395 | 1395 |
/// |
1396 | 1396 |
/// \pre The stack must be empty. Otherwise the algorithm gives |
1397 | 1397 |
/// wrong results. (One of the outgoing arcs of all the source nodes |
1398 | 1398 |
/// except for the last one will not be visited and distances will |
1399 | 1399 |
/// also be wrong.) |
1400 | 1400 |
void addSource(Node s) |
1401 | 1401 |
{ |
1402 | 1402 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1403 | 1403 |
if(!(*_reached)[s]) { |
1404 | 1404 |
_reached->set(s,true); |
1405 | 1405 |
_visitor->start(s); |
1406 | 1406 |
_visitor->reach(s); |
1407 | 1407 |
Arc e; |
1408 | 1408 |
_digraph->firstOut(e, s); |
1409 | 1409 |
if (e != INVALID) { |
1410 | 1410 |
_stack[++_stack_head] = e; |
1411 | 1411 |
} else { |
1412 | 1412 |
_visitor->leave(s); |
1413 | 1413 |
} |
1414 | 1414 |
} |
1415 | 1415 |
} |
1416 | 1416 |
|
1417 | 1417 |
/// \brief Processes the next arc. |
1418 | 1418 |
/// |
1419 | 1419 |
/// Processes the next arc. |
1420 | 1420 |
/// |
1421 | 1421 |
/// \return The processed arc. |
1422 | 1422 |
/// |
1423 | 1423 |
/// \pre The stack must not be empty. |
1424 | 1424 |
Arc processNextArc() { |
1425 | 1425 |
Arc e = _stack[_stack_head]; |
1426 | 1426 |
Node m = _digraph->target(e); |
1427 | 1427 |
if(!(*_reached)[m]) { |
1428 | 1428 |
_visitor->discover(e); |
1429 | 1429 |
_visitor->reach(m); |
1430 | 1430 |
_reached->set(m, true); |
1431 | 1431 |
_digraph->firstOut(_stack[++_stack_head], m); |
1432 | 1432 |
} else { |
1433 | 1433 |
_visitor->examine(e); |
1434 | 1434 |
m = _digraph->source(e); |
1435 | 1435 |
_digraph->nextOut(_stack[_stack_head]); |
1436 | 1436 |
} |
1437 | 1437 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1438 | 1438 |
_visitor->leave(m); |
1439 | 1439 |
--_stack_head; |
1440 | 1440 |
if (_stack_head >= 0) { |
1441 | 1441 |
_visitor->backtrack(_stack[_stack_head]); |
1442 | 1442 |
m = _digraph->source(_stack[_stack_head]); |
1443 | 1443 |
_digraph->nextOut(_stack[_stack_head]); |
1444 | 1444 |
} else { |
1445 | 1445 |
_visitor->stop(m); |
1446 | 1446 |
} |
1447 | 1447 |
} |
1448 | 1448 |
return e; |
1449 | 1449 |
} |
1450 | 1450 |
|
1451 | 1451 |
/// \brief Next arc to be processed. |
1452 | 1452 |
/// |
1453 | 1453 |
/// Next arc to be processed. |
1454 | 1454 |
/// |
1455 | 1455 |
/// \return The next arc to be processed or INVALID if the stack is |
1456 | 1456 |
/// empty. |
1457 | 1457 |
Arc nextArc() const { |
1458 | 1458 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1459 | 1459 |
} |
1460 | 1460 |
|
1461 | 1461 |
/// \brief Returns \c false if there are nodes |
1462 | 1462 |
/// to be processed. |
1463 | 1463 |
/// |
1464 | 1464 |
/// Returns \c false if there are nodes |
1465 | 1465 |
/// to be processed in the queue (stack). |
1466 | 1466 |
bool emptyQueue() const { return _stack_head < 0; } |
1467 | 1467 |
|
1468 | 1468 |
/// \brief Returns the number of the nodes to be processed. |
1469 | 1469 |
/// |
1470 | 1470 |
/// Returns the number of the nodes to be processed in the queue (stack). |
1471 | 1471 |
int queueSize() const { return _stack_head + 1; } |
1472 | 1472 |
|
1473 | 1473 |
/// \brief Executes the algorithm. |
1474 | 1474 |
/// |
1475 | 1475 |
/// Executes the algorithm. |
1476 | 1476 |
/// |
1477 | 1477 |
/// This method runs the %DFS algorithm from the root node |
1478 | 1478 |
/// in order to compute the %DFS path to each node. |
1479 | 1479 |
/// |
1480 | 1480 |
/// The algorithm computes |
1481 | 1481 |
/// - the %DFS tree, |
1482 | 1482 |
/// - the distance of each node from the root in the %DFS tree. |
1483 | 1483 |
/// |
1484 | 1484 |
/// \pre init() must be called and a root node should be |
1485 | 1485 |
/// added with addSource() before using this function. |
1486 | 1486 |
/// |
1487 | 1487 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
1488 | 1488 |
/// \code |
1489 | 1489 |
/// while ( !d.emptyQueue() ) { |
1490 | 1490 |
/// d.processNextArc(); |
1491 | 1491 |
/// } |
1492 | 1492 |
/// \endcode |
1493 | 1493 |
void start() { |
1494 | 1494 |
while ( !emptyQueue() ) processNextArc(); |
1495 | 1495 |
} |
1496 | 1496 |
|
1497 | 1497 |
/// \brief Executes the algorithm until the given target node is reached. |
1498 | 1498 |
/// |
1499 | 1499 |
/// Executes the algorithm until the given target node is reached. |
1500 | 1500 |
/// |
1501 | 1501 |
/// This method runs the %DFS algorithm from the root node |
1502 | 1502 |
/// in order to compute the DFS path to \c t. |
1503 | 1503 |
/// |
1504 | 1504 |
/// The algorithm computes |
1505 | 1505 |
/// - the %DFS path to \c t, |
1506 | 1506 |
/// - the distance of \c t from the root in the %DFS tree. |
1507 | 1507 |
/// |
1508 | 1508 |
/// \pre init() must be called and a root node should be added |
1509 | 1509 |
/// with addSource() before using this function. |
1510 | 1510 |
void start(Node t) { |
1511 | 1511 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t ) |
1512 | 1512 |
processNextArc(); |
1513 | 1513 |
} |
1514 | 1514 |
|
1515 | 1515 |
/// \brief Executes the algorithm until a condition is met. |
1516 | 1516 |
/// |
1517 | 1517 |
/// Executes the algorithm until a condition is met. |
1518 | 1518 |
/// |
1519 | 1519 |
/// This method runs the %DFS algorithm from the root node |
1520 | 1520 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
1521 | 1521 |
/// |
1522 | 1522 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
1523 | 1523 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
1524 | 1524 |
/// |
1525 | 1525 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
1526 | 1526 |
/// \c INVALID if no such arc was found. |
1527 | 1527 |
/// |
1528 | 1528 |
/// \pre init() must be called and a root node should be added |
1529 | 1529 |
/// with addSource() before using this function. |
1530 | 1530 |
/// |
1531 | 1531 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
1532 | 1532 |
/// not a node map. |
1533 | 1533 |
template <typename AM> |
1534 | 1534 |
Arc start(const AM &am) { |
1535 | 1535 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
1536 | 1536 |
processNextArc(); |
1537 | 1537 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1538 | 1538 |
} |
1539 | 1539 |
|
1540 | 1540 |
/// \brief Runs the algorithm from the given source node. |
1541 | 1541 |
/// |
1542 | 1542 |
/// This method runs the %DFS algorithm from node \c s. |
1543 | 1543 |
/// in order to compute the DFS path to each node. |
1544 | 1544 |
/// |
1545 | 1545 |
/// The algorithm computes |
1546 | 1546 |
/// - the %DFS tree, |
1547 | 1547 |
/// - the distance of each node from the root in the %DFS tree. |
1548 | 1548 |
/// |
1549 | 1549 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
1550 | 1550 |
///\code |
1551 | 1551 |
/// d.init(); |
1552 | 1552 |
/// d.addSource(s); |
1553 | 1553 |
/// d.start(); |
1554 | 1554 |
///\endcode |
1555 | 1555 |
void run(Node s) { |
1556 | 1556 |
init(); |
1557 | 1557 |
addSource(s); |
1558 | 1558 |
start(); |
1559 | 1559 |
} |
1560 | 1560 |
|
1561 | 1561 |
/// \brief Finds the %DFS path between \c s and \c t. |
1562 | 1562 |
|
1563 | 1563 |
/// This method runs the %DFS algorithm from node \c s |
1564 | 1564 |
/// in order to compute the DFS path to node \c t |
1565 | 1565 |
/// (it stops searching when \c t is processed). |
1566 | 1566 |
/// |
1567 | 1567 |
/// \return \c true if \c t is reachable form \c s. |
1568 | 1568 |
/// |
1569 | 1569 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1570 | 1570 |
/// just a shortcut of the following code. |
1571 | 1571 |
///\code |
1572 | 1572 |
/// d.init(); |
1573 | 1573 |
/// d.addSource(s); |
1574 | 1574 |
/// d.start(t); |
1575 | 1575 |
///\endcode |
1576 | 1576 |
bool run(Node s,Node t) { |
1577 | 1577 |
init(); |
1578 | 1578 |
addSource(s); |
1579 | 1579 |
start(t); |
1580 | 1580 |
return reached(t); |
1581 | 1581 |
} |
1582 | 1582 |
|
1583 | 1583 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1584 | 1584 |
|
1585 | 1585 |
/// This method runs the %DFS algorithm in order to |
1586 | 1586 |
/// compute the %DFS path to each node. |
1587 | 1587 |
/// |
1588 | 1588 |
/// The algorithm computes |
1589 | 1589 |
/// - the %DFS tree (forest), |
1590 | 1590 |
/// - the distance of each node from the root(s) in the %DFS tree. |
1591 | 1591 |
/// |
1592 | 1592 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1593 | 1593 |
///\code |
1594 | 1594 |
/// d.init(); |
1595 | 1595 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1596 | 1596 |
/// if (!d.reached(n)) { |
1597 | 1597 |
/// d.addSource(n); |
1598 | 1598 |
/// d.start(); |
1599 | 1599 |
/// } |
1600 | 1600 |
/// } |
1601 | 1601 |
///\endcode |
1602 | 1602 |
void run() { |
1603 | 1603 |
init(); |
1604 | 1604 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1605 | 1605 |
if (!reached(it)) { |
1606 | 1606 |
addSource(it); |
1607 | 1607 |
start(); |
1608 | 1608 |
} |
1609 | 1609 |
} |
1610 | 1610 |
} |
1611 | 1611 |
|
1612 | 1612 |
///@} |
1613 | 1613 |
|
1614 | 1614 |
/// \name Query Functions |
1615 | 1615 |
/// The results of the DFS algorithm can be obtained using these |
1616 | 1616 |
/// functions.\n |
1617 | 1617 |
/// Either \ref run(Node) "run()" or \ref start() should be called |
1618 | 1618 |
/// before using them. |
1619 | 1619 |
|
1620 | 1620 |
///@{ |
1621 | 1621 |
|
1622 | 1622 |
/// \brief Checks if a node is reached from the root(s). |
1623 | 1623 |
/// |
1624 | 1624 |
/// Returns \c true if \c v is reached from the root(s). |
1625 | 1625 |
/// |
1626 | 1626 |
/// \pre Either \ref run(Node) "run()" or \ref init() |
1627 | 1627 |
/// must be called before using this function. |
1628 |
bool reached(Node v) { return (*_reached)[v]; } |
|
1628 |
bool reached(Node v) const { return (*_reached)[v]; } |
|
1629 | 1629 |
|
1630 | 1630 |
///@} |
1631 | 1631 |
|
1632 | 1632 |
}; |
1633 | 1633 |
|
1634 | 1634 |
} //END OF NAMESPACE LEMON |
1635 | 1635 |
|
1636 | 1636 |
#endif |
... | ... |
@@ -113,513 +113,513 @@ |
113 | 113 |
template <typename _Digraph, |
114 | 114 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
115 | 115 |
typename _Traits = PreflowDefaultTraits<_Digraph, _CapacityMap> > |
116 | 116 |
#endif |
117 | 117 |
class Preflow { |
118 | 118 |
public: |
119 | 119 |
|
120 | 120 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
121 | 121 |
typedef _Traits Traits; |
122 | 122 |
///The type of the digraph the algorithm runs on. |
123 | 123 |
typedef typename Traits::Digraph Digraph; |
124 | 124 |
///The type of the capacity map. |
125 | 125 |
typedef typename Traits::CapacityMap CapacityMap; |
126 | 126 |
///The type of the flow values. |
127 | 127 |
typedef typename Traits::Value Value; |
128 | 128 |
|
129 | 129 |
///The type of the flow map. |
130 | 130 |
typedef typename Traits::FlowMap FlowMap; |
131 | 131 |
///The type of the elevator. |
132 | 132 |
typedef typename Traits::Elevator Elevator; |
133 | 133 |
///The type of the tolerance. |
134 | 134 |
typedef typename Traits::Tolerance Tolerance; |
135 | 135 |
|
136 | 136 |
private: |
137 | 137 |
|
138 | 138 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
139 | 139 |
|
140 | 140 |
const Digraph& _graph; |
141 | 141 |
const CapacityMap* _capacity; |
142 | 142 |
|
143 | 143 |
int _node_num; |
144 | 144 |
|
145 | 145 |
Node _source, _target; |
146 | 146 |
|
147 | 147 |
FlowMap* _flow; |
148 | 148 |
bool _local_flow; |
149 | 149 |
|
150 | 150 |
Elevator* _level; |
151 | 151 |
bool _local_level; |
152 | 152 |
|
153 | 153 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
154 | 154 |
ExcessMap* _excess; |
155 | 155 |
|
156 | 156 |
Tolerance _tolerance; |
157 | 157 |
|
158 | 158 |
bool _phase; |
159 | 159 |
|
160 | 160 |
|
161 | 161 |
void createStructures() { |
162 | 162 |
_node_num = countNodes(_graph); |
163 | 163 |
|
164 | 164 |
if (!_flow) { |
165 | 165 |
_flow = Traits::createFlowMap(_graph); |
166 | 166 |
_local_flow = true; |
167 | 167 |
} |
168 | 168 |
if (!_level) { |
169 | 169 |
_level = Traits::createElevator(_graph, _node_num); |
170 | 170 |
_local_level = true; |
171 | 171 |
} |
172 | 172 |
if (!_excess) { |
173 | 173 |
_excess = new ExcessMap(_graph); |
174 | 174 |
} |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
void destroyStructures() { |
178 | 178 |
if (_local_flow) { |
179 | 179 |
delete _flow; |
180 | 180 |
} |
181 | 181 |
if (_local_level) { |
182 | 182 |
delete _level; |
183 | 183 |
} |
184 | 184 |
if (_excess) { |
185 | 185 |
delete _excess; |
186 | 186 |
} |
187 | 187 |
} |
188 | 188 |
|
189 | 189 |
public: |
190 | 190 |
|
191 | 191 |
typedef Preflow Create; |
192 | 192 |
|
193 | 193 |
///\name Named Template Parameters |
194 | 194 |
|
195 | 195 |
///@{ |
196 | 196 |
|
197 | 197 |
template <typename _FlowMap> |
198 | 198 |
struct SetFlowMapTraits : public Traits { |
199 | 199 |
typedef _FlowMap FlowMap; |
200 | 200 |
static FlowMap *createFlowMap(const Digraph&) { |
201 | 201 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
202 | 202 |
return 0; // ignore warnings |
203 | 203 |
} |
204 | 204 |
}; |
205 | 205 |
|
206 | 206 |
/// \brief \ref named-templ-param "Named parameter" for setting |
207 | 207 |
/// FlowMap type |
208 | 208 |
/// |
209 | 209 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
210 | 210 |
/// type. |
211 | 211 |
template <typename _FlowMap> |
212 | 212 |
struct SetFlowMap |
213 | 213 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<_FlowMap> > { |
214 | 214 |
typedef Preflow<Digraph, CapacityMap, |
215 | 215 |
SetFlowMapTraits<_FlowMap> > Create; |
216 | 216 |
}; |
217 | 217 |
|
218 | 218 |
template <typename _Elevator> |
219 | 219 |
struct SetElevatorTraits : public Traits { |
220 | 220 |
typedef _Elevator Elevator; |
221 | 221 |
static Elevator *createElevator(const Digraph&, int) { |
222 | 222 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
223 | 223 |
return 0; // ignore warnings |
224 | 224 |
} |
225 | 225 |
}; |
226 | 226 |
|
227 | 227 |
/// \brief \ref named-templ-param "Named parameter" for setting |
228 | 228 |
/// Elevator type |
229 | 229 |
/// |
230 | 230 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
231 | 231 |
/// type. If this named parameter is used, then an external |
232 | 232 |
/// elevator object must be passed to the algorithm using the |
233 | 233 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
234 | 234 |
/// \ref run() or \ref init(). |
235 | 235 |
/// \sa SetStandardElevator |
236 | 236 |
template <typename _Elevator> |
237 | 237 |
struct SetElevator |
238 | 238 |
: public Preflow<Digraph, CapacityMap, SetElevatorTraits<_Elevator> > { |
239 | 239 |
typedef Preflow<Digraph, CapacityMap, |
240 | 240 |
SetElevatorTraits<_Elevator> > Create; |
241 | 241 |
}; |
242 | 242 |
|
243 | 243 |
template <typename _Elevator> |
244 | 244 |
struct SetStandardElevatorTraits : public Traits { |
245 | 245 |
typedef _Elevator Elevator; |
246 | 246 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
247 | 247 |
return new Elevator(digraph, max_level); |
248 | 248 |
} |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
/// \brief \ref named-templ-param "Named parameter" for setting |
252 | 252 |
/// Elevator type with automatic allocation |
253 | 253 |
/// |
254 | 254 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
255 | 255 |
/// type with automatic allocation. |
256 | 256 |
/// The Elevator should have standard constructor interface to be |
257 | 257 |
/// able to automatically created by the algorithm (i.e. the |
258 | 258 |
/// digraph and the maximum level should be passed to it). |
259 | 259 |
/// However an external elevator object could also be passed to the |
260 | 260 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
261 | 261 |
/// before calling \ref run() or \ref init(). |
262 | 262 |
/// \sa SetElevator |
263 | 263 |
template <typename _Elevator> |
264 | 264 |
struct SetStandardElevator |
265 | 265 |
: public Preflow<Digraph, CapacityMap, |
266 | 266 |
SetStandardElevatorTraits<_Elevator> > { |
267 | 267 |
typedef Preflow<Digraph, CapacityMap, |
268 | 268 |
SetStandardElevatorTraits<_Elevator> > Create; |
269 | 269 |
}; |
270 | 270 |
|
271 | 271 |
/// @} |
272 | 272 |
|
273 | 273 |
protected: |
274 | 274 |
|
275 | 275 |
Preflow() {} |
276 | 276 |
|
277 | 277 |
public: |
278 | 278 |
|
279 | 279 |
|
280 | 280 |
/// \brief The constructor of the class. |
281 | 281 |
/// |
282 | 282 |
/// The constructor of the class. |
283 | 283 |
/// \param digraph The digraph the algorithm runs on. |
284 | 284 |
/// \param capacity The capacity of the arcs. |
285 | 285 |
/// \param source The source node. |
286 | 286 |
/// \param target The target node. |
287 | 287 |
Preflow(const Digraph& digraph, const CapacityMap& capacity, |
288 | 288 |
Node source, Node target) |
289 | 289 |
: _graph(digraph), _capacity(&capacity), |
290 | 290 |
_node_num(0), _source(source), _target(target), |
291 | 291 |
_flow(0), _local_flow(false), |
292 | 292 |
_level(0), _local_level(false), |
293 | 293 |
_excess(0), _tolerance(), _phase() {} |
294 | 294 |
|
295 | 295 |
/// \brief Destructor. |
296 | 296 |
/// |
297 | 297 |
/// Destructor. |
298 | 298 |
~Preflow() { |
299 | 299 |
destroyStructures(); |
300 | 300 |
} |
301 | 301 |
|
302 | 302 |
/// \brief Sets the capacity map. |
303 | 303 |
/// |
304 | 304 |
/// Sets the capacity map. |
305 | 305 |
/// \return <tt>(*this)</tt> |
306 | 306 |
Preflow& capacityMap(const CapacityMap& map) { |
307 | 307 |
_capacity = ↦ |
308 | 308 |
return *this; |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
/// \brief Sets the flow map. |
312 | 312 |
/// |
313 | 313 |
/// Sets the flow map. |
314 | 314 |
/// If you don't use this function before calling \ref run() or |
315 | 315 |
/// \ref init(), an instance will be allocated automatically. |
316 | 316 |
/// The destructor deallocates this automatically allocated map, |
317 | 317 |
/// of course. |
318 | 318 |
/// \return <tt>(*this)</tt> |
319 | 319 |
Preflow& flowMap(FlowMap& map) { |
320 | 320 |
if (_local_flow) { |
321 | 321 |
delete _flow; |
322 | 322 |
_local_flow = false; |
323 | 323 |
} |
324 | 324 |
_flow = ↦ |
325 | 325 |
return *this; |
326 | 326 |
} |
327 | 327 |
|
328 | 328 |
/// \brief Sets the source node. |
329 | 329 |
/// |
330 | 330 |
/// Sets the source node. |
331 | 331 |
/// \return <tt>(*this)</tt> |
332 | 332 |
Preflow& source(const Node& node) { |
333 | 333 |
_source = node; |
334 | 334 |
return *this; |
335 | 335 |
} |
336 | 336 |
|
337 | 337 |
/// \brief Sets the target node. |
338 | 338 |
/// |
339 | 339 |
/// Sets the target node. |
340 | 340 |
/// \return <tt>(*this)</tt> |
341 | 341 |
Preflow& target(const Node& node) { |
342 | 342 |
_target = node; |
343 | 343 |
return *this; |
344 | 344 |
} |
345 | 345 |
|
346 | 346 |
/// \brief Sets the elevator used by algorithm. |
347 | 347 |
/// |
348 | 348 |
/// Sets the elevator used by algorithm. |
349 | 349 |
/// If you don't use this function before calling \ref run() or |
350 | 350 |
/// \ref init(), an instance will be allocated automatically. |
351 | 351 |
/// The destructor deallocates this automatically allocated elevator, |
352 | 352 |
/// of course. |
353 | 353 |
/// \return <tt>(*this)</tt> |
354 | 354 |
Preflow& elevator(Elevator& elevator) { |
355 | 355 |
if (_local_level) { |
356 | 356 |
delete _level; |
357 | 357 |
_local_level = false; |
358 | 358 |
} |
359 | 359 |
_level = &elevator; |
360 | 360 |
return *this; |
361 | 361 |
} |
362 | 362 |
|
363 | 363 |
/// \brief Returns a const reference to the elevator. |
364 | 364 |
/// |
365 | 365 |
/// Returns a const reference to the elevator. |
366 | 366 |
/// |
367 | 367 |
/// \pre Either \ref run() or \ref init() must be called before |
368 | 368 |
/// using this function. |
369 |
const Elevator& elevator() { |
|
369 |
const Elevator& elevator() const { |
|
370 | 370 |
return *_level; |
371 | 371 |
} |
372 | 372 |
|
373 | 373 |
/// \brief Sets the tolerance used by algorithm. |
374 | 374 |
/// |
375 | 375 |
/// Sets the tolerance used by algorithm. |
376 | 376 |
Preflow& tolerance(const Tolerance& tolerance) const { |
377 | 377 |
_tolerance = tolerance; |
378 | 378 |
return *this; |
379 | 379 |
} |
380 | 380 |
|
381 | 381 |
/// \brief Returns a const reference to the tolerance. |
382 | 382 |
/// |
383 | 383 |
/// Returns a const reference to the tolerance. |
384 | 384 |
const Tolerance& tolerance() const { |
385 | 385 |
return tolerance; |
386 | 386 |
} |
387 | 387 |
|
388 | 388 |
/// \name Execution Control |
389 | 389 |
/// The simplest way to execute the preflow algorithm is to use |
390 | 390 |
/// \ref run() or \ref runMinCut().\n |
391 | 391 |
/// If you need more control on the initial solution or the execution, |
392 | 392 |
/// first you have to call one of the \ref init() functions, then |
393 | 393 |
/// \ref startFirstPhase() and if you need it \ref startSecondPhase(). |
394 | 394 |
|
395 | 395 |
///@{ |
396 | 396 |
|
397 | 397 |
/// \brief Initializes the internal data structures. |
398 | 398 |
/// |
399 | 399 |
/// Initializes the internal data structures and sets the initial |
400 | 400 |
/// flow to zero on each arc. |
401 | 401 |
void init() { |
402 | 402 |
createStructures(); |
403 | 403 |
|
404 | 404 |
_phase = true; |
405 | 405 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
406 | 406 |
_excess->set(n, 0); |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
for (ArcIt e(_graph); e != INVALID; ++e) { |
410 | 410 |
_flow->set(e, 0); |
411 | 411 |
} |
412 | 412 |
|
413 | 413 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
414 | 414 |
|
415 | 415 |
_level->initStart(); |
416 | 416 |
_level->initAddItem(_target); |
417 | 417 |
|
418 | 418 |
std::vector<Node> queue; |
419 | 419 |
reached.set(_source, true); |
420 | 420 |
|
421 | 421 |
queue.push_back(_target); |
422 | 422 |
reached.set(_target, true); |
423 | 423 |
while (!queue.empty()) { |
424 | 424 |
_level->initNewLevel(); |
425 | 425 |
std::vector<Node> nqueue; |
426 | 426 |
for (int i = 0; i < int(queue.size()); ++i) { |
427 | 427 |
Node n = queue[i]; |
428 | 428 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
429 | 429 |
Node u = _graph.source(e); |
430 | 430 |
if (!reached[u] && _tolerance.positive((*_capacity)[e])) { |
431 | 431 |
reached.set(u, true); |
432 | 432 |
_level->initAddItem(u); |
433 | 433 |
nqueue.push_back(u); |
434 | 434 |
} |
435 | 435 |
} |
436 | 436 |
} |
437 | 437 |
queue.swap(nqueue); |
438 | 438 |
} |
439 | 439 |
_level->initFinish(); |
440 | 440 |
|
441 | 441 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) { |
442 | 442 |
if (_tolerance.positive((*_capacity)[e])) { |
443 | 443 |
Node u = _graph.target(e); |
444 | 444 |
if ((*_level)[u] == _level->maxLevel()) continue; |
445 | 445 |
_flow->set(e, (*_capacity)[e]); |
446 | 446 |
_excess->set(u, (*_excess)[u] + (*_capacity)[e]); |
447 | 447 |
if (u != _target && !_level->active(u)) { |
448 | 448 |
_level->activate(u); |
449 | 449 |
} |
450 | 450 |
} |
451 | 451 |
} |
452 | 452 |
} |
453 | 453 |
|
454 | 454 |
/// \brief Initializes the internal data structures using the |
455 | 455 |
/// given flow map. |
456 | 456 |
/// |
457 | 457 |
/// Initializes the internal data structures and sets the initial |
458 | 458 |
/// flow to the given \c flowMap. The \c flowMap should contain a |
459 | 459 |
/// flow or at least a preflow, i.e. at each node excluding the |
460 | 460 |
/// source node the incoming flow should greater or equal to the |
461 | 461 |
/// outgoing flow. |
462 | 462 |
/// \return \c false if the given \c flowMap is not a preflow. |
463 | 463 |
template <typename FlowMap> |
464 | 464 |
bool init(const FlowMap& flowMap) { |
465 | 465 |
createStructures(); |
466 | 466 |
|
467 | 467 |
for (ArcIt e(_graph); e != INVALID; ++e) { |
468 | 468 |
_flow->set(e, flowMap[e]); |
469 | 469 |
} |
470 | 470 |
|
471 | 471 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
472 | 472 |
Value excess = 0; |
473 | 473 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
474 | 474 |
excess += (*_flow)[e]; |
475 | 475 |
} |
476 | 476 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
477 | 477 |
excess -= (*_flow)[e]; |
478 | 478 |
} |
479 | 479 |
if (excess < 0 && n != _source) return false; |
480 | 480 |
_excess->set(n, excess); |
481 | 481 |
} |
482 | 482 |
|
483 | 483 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
484 | 484 |
|
485 | 485 |
_level->initStart(); |
486 | 486 |
_level->initAddItem(_target); |
487 | 487 |
|
488 | 488 |
std::vector<Node> queue; |
489 | 489 |
reached.set(_source, true); |
490 | 490 |
|
491 | 491 |
queue.push_back(_target); |
492 | 492 |
reached.set(_target, true); |
493 | 493 |
while (!queue.empty()) { |
494 | 494 |
_level->initNewLevel(); |
495 | 495 |
std::vector<Node> nqueue; |
496 | 496 |
for (int i = 0; i < int(queue.size()); ++i) { |
497 | 497 |
Node n = queue[i]; |
498 | 498 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
499 | 499 |
Node u = _graph.source(e); |
500 | 500 |
if (!reached[u] && |
501 | 501 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) { |
502 | 502 |
reached.set(u, true); |
503 | 503 |
_level->initAddItem(u); |
504 | 504 |
nqueue.push_back(u); |
505 | 505 |
} |
506 | 506 |
} |
507 | 507 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
508 | 508 |
Node v = _graph.target(e); |
509 | 509 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) { |
510 | 510 |
reached.set(v, true); |
511 | 511 |
_level->initAddItem(v); |
512 | 512 |
nqueue.push_back(v); |
513 | 513 |
} |
514 | 514 |
} |
515 | 515 |
} |
516 | 516 |
queue.swap(nqueue); |
517 | 517 |
} |
518 | 518 |
_level->initFinish(); |
519 | 519 |
|
520 | 520 |
for (OutArcIt e(_graph, _source); e != INVALID; ++e) { |
521 | 521 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
522 | 522 |
if (_tolerance.positive(rem)) { |
523 | 523 |
Node u = _graph.target(e); |
524 | 524 |
if ((*_level)[u] == _level->maxLevel()) continue; |
525 | 525 |
_flow->set(e, (*_capacity)[e]); |
526 | 526 |
_excess->set(u, (*_excess)[u] + rem); |
527 | 527 |
if (u != _target && !_level->active(u)) { |
528 | 528 |
_level->activate(u); |
529 | 529 |
} |
530 | 530 |
} |
531 | 531 |
} |
532 | 532 |
for (InArcIt e(_graph, _source); e != INVALID; ++e) { |
533 | 533 |
Value rem = (*_flow)[e]; |
534 | 534 |
if (_tolerance.positive(rem)) { |
535 | 535 |
Node v = _graph.source(e); |
536 | 536 |
if ((*_level)[v] == _level->maxLevel()) continue; |
537 | 537 |
_flow->set(e, 0); |
538 | 538 |
_excess->set(v, (*_excess)[v] + rem); |
539 | 539 |
if (v != _target && !_level->active(v)) { |
540 | 540 |
_level->activate(v); |
541 | 541 |
} |
542 | 542 |
} |
543 | 543 |
} |
544 | 544 |
return true; |
545 | 545 |
} |
546 | 546 |
|
547 | 547 |
/// \brief Starts the first phase of the preflow algorithm. |
548 | 548 |
/// |
549 | 549 |
/// The preflow algorithm consists of two phases, this method runs |
550 | 550 |
/// the first phase. After the first phase the maximum flow value |
551 | 551 |
/// and a minimum value cut can already be computed, although a |
552 | 552 |
/// maximum flow is not yet obtained. So after calling this method |
553 | 553 |
/// \ref flowValue() returns the value of a maximum flow and \ref |
554 | 554 |
/// minCut() returns a minimum cut. |
555 | 555 |
/// \pre One of the \ref init() functions must be called before |
556 | 556 |
/// using this function. |
557 | 557 |
void startFirstPhase() { |
558 | 558 |
_phase = true; |
559 | 559 |
|
560 | 560 |
Node n = _level->highestActive(); |
561 | 561 |
int level = _level->highestActiveLevel(); |
562 | 562 |
while (n != INVALID) { |
563 | 563 |
int num = _node_num; |
564 | 564 |
|
565 | 565 |
while (num > 0 && n != INVALID) { |
566 | 566 |
Value excess = (*_excess)[n]; |
567 | 567 |
int new_level = _level->maxLevel(); |
568 | 568 |
|
569 | 569 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
570 | 570 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
571 | 571 |
if (!_tolerance.positive(rem)) continue; |
572 | 572 |
Node v = _graph.target(e); |
573 | 573 |
if ((*_level)[v] < level) { |
574 | 574 |
if (!_level->active(v) && v != _target) { |
575 | 575 |
_level->activate(v); |
576 | 576 |
} |
577 | 577 |
if (!_tolerance.less(rem, excess)) { |
578 | 578 |
_flow->set(e, (*_flow)[e] + excess); |
579 | 579 |
_excess->set(v, (*_excess)[v] + excess); |
580 | 580 |
excess = 0; |
581 | 581 |
goto no_more_push_1; |
582 | 582 |
} else { |
583 | 583 |
excess -= rem; |
584 | 584 |
_excess->set(v, (*_excess)[v] + rem); |
585 | 585 |
_flow->set(e, (*_capacity)[e]); |
586 | 586 |
} |
587 | 587 |
} else if (new_level > (*_level)[v]) { |
588 | 588 |
new_level = (*_level)[v]; |
589 | 589 |
} |
590 | 590 |
} |
591 | 591 |
|
592 | 592 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
593 | 593 |
Value rem = (*_flow)[e]; |
594 | 594 |
if (!_tolerance.positive(rem)) continue; |
595 | 595 |
Node v = _graph.source(e); |
596 | 596 |
if ((*_level)[v] < level) { |
597 | 597 |
if (!_level->active(v) && v != _target) { |
598 | 598 |
_level->activate(v); |
599 | 599 |
} |
600 | 600 |
if (!_tolerance.less(rem, excess)) { |
601 | 601 |
_flow->set(e, (*_flow)[e] - excess); |
602 | 602 |
_excess->set(v, (*_excess)[v] + excess); |
603 | 603 |
excess = 0; |
604 | 604 |
goto no_more_push_1; |
605 | 605 |
} else { |
606 | 606 |
excess -= rem; |
607 | 607 |
_excess->set(v, (*_excess)[v] + rem); |
608 | 608 |
_flow->set(e, 0); |
609 | 609 |
} |
610 | 610 |
} else if (new_level > (*_level)[v]) { |
611 | 611 |
new_level = (*_level)[v]; |
612 | 612 |
} |
613 | 613 |
} |
614 | 614 |
|
615 | 615 |
no_more_push_1: |
616 | 616 |
|
617 | 617 |
_excess->set(n, excess); |
618 | 618 |
|
619 | 619 |
if (excess != 0) { |
620 | 620 |
if (new_level + 1 < _level->maxLevel()) { |
621 | 621 |
_level->liftHighestActive(new_level + 1); |
622 | 622 |
} else { |
623 | 623 |
_level->liftHighestActiveToTop(); |
624 | 624 |
} |
625 | 625 |
if (_level->emptyLevel(level)) { |
... | ... |
@@ -665,300 +665,300 @@ |
665 | 665 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
666 | 666 |
Value rem = (*_flow)[e]; |
667 | 667 |
if (!_tolerance.positive(rem)) continue; |
668 | 668 |
Node v = _graph.source(e); |
669 | 669 |
if ((*_level)[v] < level) { |
670 | 670 |
if (!_level->active(v) && v != _target) { |
671 | 671 |
_level->activate(v); |
672 | 672 |
} |
673 | 673 |
if (!_tolerance.less(rem, excess)) { |
674 | 674 |
_flow->set(e, (*_flow)[e] - excess); |
675 | 675 |
_excess->set(v, (*_excess)[v] + excess); |
676 | 676 |
excess = 0; |
677 | 677 |
goto no_more_push_2; |
678 | 678 |
} else { |
679 | 679 |
excess -= rem; |
680 | 680 |
_excess->set(v, (*_excess)[v] + rem); |
681 | 681 |
_flow->set(e, 0); |
682 | 682 |
} |
683 | 683 |
} else if (new_level > (*_level)[v]) { |
684 | 684 |
new_level = (*_level)[v]; |
685 | 685 |
} |
686 | 686 |
} |
687 | 687 |
|
688 | 688 |
no_more_push_2: |
689 | 689 |
|
690 | 690 |
_excess->set(n, excess); |
691 | 691 |
|
692 | 692 |
if (excess != 0) { |
693 | 693 |
if (new_level + 1 < _level->maxLevel()) { |
694 | 694 |
_level->liftActiveOn(level, new_level + 1); |
695 | 695 |
} else { |
696 | 696 |
_level->liftActiveToTop(level); |
697 | 697 |
} |
698 | 698 |
if (_level->emptyLevel(level)) { |
699 | 699 |
_level->liftToTop(level); |
700 | 700 |
} |
701 | 701 |
} else { |
702 | 702 |
_level->deactivate(n); |
703 | 703 |
} |
704 | 704 |
|
705 | 705 |
while (level >= 0 && _level->activeFree(level)) { |
706 | 706 |
--level; |
707 | 707 |
} |
708 | 708 |
if (level == -1) { |
709 | 709 |
n = _level->highestActive(); |
710 | 710 |
level = _level->highestActiveLevel(); |
711 | 711 |
} else { |
712 | 712 |
n = _level->activeOn(level); |
713 | 713 |
} |
714 | 714 |
--num; |
715 | 715 |
} |
716 | 716 |
} |
717 | 717 |
} |
718 | 718 |
|
719 | 719 |
/// \brief Starts the second phase of the preflow algorithm. |
720 | 720 |
/// |
721 | 721 |
/// The preflow algorithm consists of two phases, this method runs |
722 | 722 |
/// the second phase. After calling one of the \ref init() functions |
723 | 723 |
/// and \ref startFirstPhase() and then \ref startSecondPhase(), |
724 | 724 |
/// \ref flowMap() returns a maximum flow, \ref flowValue() returns the |
725 | 725 |
/// value of a maximum flow, \ref minCut() returns a minimum cut |
726 | 726 |
/// \pre One of the \ref init() functions and \ref startFirstPhase() |
727 | 727 |
/// must be called before using this function. |
728 | 728 |
void startSecondPhase() { |
729 | 729 |
_phase = false; |
730 | 730 |
|
731 | 731 |
typename Digraph::template NodeMap<bool> reached(_graph); |
732 | 732 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
733 | 733 |
reached.set(n, (*_level)[n] < _level->maxLevel()); |
734 | 734 |
} |
735 | 735 |
|
736 | 736 |
_level->initStart(); |
737 | 737 |
_level->initAddItem(_source); |
738 | 738 |
|
739 | 739 |
std::vector<Node> queue; |
740 | 740 |
queue.push_back(_source); |
741 | 741 |
reached.set(_source, true); |
742 | 742 |
|
743 | 743 |
while (!queue.empty()) { |
744 | 744 |
_level->initNewLevel(); |
745 | 745 |
std::vector<Node> nqueue; |
746 | 746 |
for (int i = 0; i < int(queue.size()); ++i) { |
747 | 747 |
Node n = queue[i]; |
748 | 748 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
749 | 749 |
Node v = _graph.target(e); |
750 | 750 |
if (!reached[v] && _tolerance.positive((*_flow)[e])) { |
751 | 751 |
reached.set(v, true); |
752 | 752 |
_level->initAddItem(v); |
753 | 753 |
nqueue.push_back(v); |
754 | 754 |
} |
755 | 755 |
} |
756 | 756 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
757 | 757 |
Node u = _graph.source(e); |
758 | 758 |
if (!reached[u] && |
759 | 759 |
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) { |
760 | 760 |
reached.set(u, true); |
761 | 761 |
_level->initAddItem(u); |
762 | 762 |
nqueue.push_back(u); |
763 | 763 |
} |
764 | 764 |
} |
765 | 765 |
} |
766 | 766 |
queue.swap(nqueue); |
767 | 767 |
} |
768 | 768 |
_level->initFinish(); |
769 | 769 |
|
770 | 770 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
771 | 771 |
if (!reached[n]) { |
772 | 772 |
_level->dirtyTopButOne(n); |
773 | 773 |
} else if ((*_excess)[n] > 0 && _target != n) { |
774 | 774 |
_level->activate(n); |
775 | 775 |
} |
776 | 776 |
} |
777 | 777 |
|
778 | 778 |
Node n; |
779 | 779 |
while ((n = _level->highestActive()) != INVALID) { |
780 | 780 |
Value excess = (*_excess)[n]; |
781 | 781 |
int level = _level->highestActiveLevel(); |
782 | 782 |
int new_level = _level->maxLevel(); |
783 | 783 |
|
784 | 784 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
785 | 785 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
786 | 786 |
if (!_tolerance.positive(rem)) continue; |
787 | 787 |
Node v = _graph.target(e); |
788 | 788 |
if ((*_level)[v] < level) { |
789 | 789 |
if (!_level->active(v) && v != _source) { |
790 | 790 |
_level->activate(v); |
791 | 791 |
} |
792 | 792 |
if (!_tolerance.less(rem, excess)) { |
793 | 793 |
_flow->set(e, (*_flow)[e] + excess); |
794 | 794 |
_excess->set(v, (*_excess)[v] + excess); |
795 | 795 |
excess = 0; |
796 | 796 |
goto no_more_push; |
797 | 797 |
} else { |
798 | 798 |
excess -= rem; |
799 | 799 |
_excess->set(v, (*_excess)[v] + rem); |
800 | 800 |
_flow->set(e, (*_capacity)[e]); |
801 | 801 |
} |
802 | 802 |
} else if (new_level > (*_level)[v]) { |
803 | 803 |
new_level = (*_level)[v]; |
804 | 804 |
} |
805 | 805 |
} |
806 | 806 |
|
807 | 807 |
for (InArcIt e(_graph, n); e != INVALID; ++e) { |
808 | 808 |
Value rem = (*_flow)[e]; |
809 | 809 |
if (!_tolerance.positive(rem)) continue; |
810 | 810 |
Node v = _graph.source(e); |
811 | 811 |
if ((*_level)[v] < level) { |
812 | 812 |
if (!_level->active(v) && v != _source) { |
813 | 813 |
_level->activate(v); |
814 | 814 |
} |
815 | 815 |
if (!_tolerance.less(rem, excess)) { |
816 | 816 |
_flow->set(e, (*_flow)[e] - excess); |
817 | 817 |
_excess->set(v, (*_excess)[v] + excess); |
818 | 818 |
excess = 0; |
819 | 819 |
goto no_more_push; |
820 | 820 |
} else { |
821 | 821 |
excess -= rem; |
822 | 822 |
_excess->set(v, (*_excess)[v] + rem); |
823 | 823 |
_flow->set(e, 0); |
824 | 824 |
} |
825 | 825 |
} else if (new_level > (*_level)[v]) { |
826 | 826 |
new_level = (*_level)[v]; |
827 | 827 |
} |
828 | 828 |
} |
829 | 829 |
|
830 | 830 |
no_more_push: |
831 | 831 |
|
832 | 832 |
_excess->set(n, excess); |
833 | 833 |
|
834 | 834 |
if (excess != 0) { |
835 | 835 |
if (new_level + 1 < _level->maxLevel()) { |
836 | 836 |
_level->liftHighestActive(new_level + 1); |
837 | 837 |
} else { |
838 | 838 |
// Calculation error |
839 | 839 |
_level->liftHighestActiveToTop(); |
840 | 840 |
} |
841 | 841 |
if (_level->emptyLevel(level)) { |
842 | 842 |
// Calculation error |
843 | 843 |
_level->liftToTop(level); |
844 | 844 |
} |
845 | 845 |
} else { |
846 | 846 |
_level->deactivate(n); |
847 | 847 |
} |
848 | 848 |
|
849 | 849 |
} |
850 | 850 |
} |
851 | 851 |
|
852 | 852 |
/// \brief Runs the preflow algorithm. |
853 | 853 |
/// |
854 | 854 |
/// Runs the preflow algorithm. |
855 | 855 |
/// \note pf.run() is just a shortcut of the following code. |
856 | 856 |
/// \code |
857 | 857 |
/// pf.init(); |
858 | 858 |
/// pf.startFirstPhase(); |
859 | 859 |
/// pf.startSecondPhase(); |
860 | 860 |
/// \endcode |
861 | 861 |
void run() { |
862 | 862 |
init(); |
863 | 863 |
startFirstPhase(); |
864 | 864 |
startSecondPhase(); |
865 | 865 |
} |
866 | 866 |
|
867 | 867 |
/// \brief Runs the preflow algorithm to compute the minimum cut. |
868 | 868 |
/// |
869 | 869 |
/// Runs the preflow algorithm to compute the minimum cut. |
870 | 870 |
/// \note pf.runMinCut() is just a shortcut of the following code. |
871 | 871 |
/// \code |
872 | 872 |
/// pf.init(); |
873 | 873 |
/// pf.startFirstPhase(); |
874 | 874 |
/// \endcode |
875 | 875 |
void runMinCut() { |
876 | 876 |
init(); |
877 | 877 |
startFirstPhase(); |
878 | 878 |
} |
879 | 879 |
|
880 | 880 |
/// @} |
881 | 881 |
|
882 | 882 |
/// \name Query Functions |
883 | 883 |
/// The results of the preflow algorithm can be obtained using these |
884 | 884 |
/// functions.\n |
885 | 885 |
/// Either one of the \ref run() "run*()" functions or one of the |
886 | 886 |
/// \ref startFirstPhase() "start*()" functions should be called |
887 | 887 |
/// before using them. |
888 | 888 |
|
889 | 889 |
///@{ |
890 | 890 |
|
891 | 891 |
/// \brief Returns the value of the maximum flow. |
892 | 892 |
/// |
893 | 893 |
/// Returns the value of the maximum flow by returning the excess |
894 | 894 |
/// of the target node. This value equals to the value of |
895 | 895 |
/// the maximum flow already after the first phase of the algorithm. |
896 | 896 |
/// |
897 | 897 |
/// \pre Either \ref run() or \ref init() must be called before |
898 | 898 |
/// using this function. |
899 | 899 |
Value flowValue() const { |
900 | 900 |
return (*_excess)[_target]; |
901 | 901 |
} |
902 | 902 |
|
903 | 903 |
/// \brief Returns the flow on the given arc. |
904 | 904 |
/// |
905 | 905 |
/// Returns the flow on the given arc. This method can |
906 | 906 |
/// be called after the second phase of the algorithm. |
907 | 907 |
/// |
908 | 908 |
/// \pre Either \ref run() or \ref init() must be called before |
909 | 909 |
/// using this function. |
910 | 910 |
Value flow(const Arc& arc) const { |
911 | 911 |
return (*_flow)[arc]; |
912 | 912 |
} |
913 | 913 |
|
914 | 914 |
/// \brief Returns a const reference to the flow map. |
915 | 915 |
/// |
916 | 916 |
/// Returns a const reference to the arc map storing the found flow. |
917 | 917 |
/// This method can be called after the second phase of the algorithm. |
918 | 918 |
/// |
919 | 919 |
/// \pre Either \ref run() or \ref init() must be called before |
920 | 920 |
/// using this function. |
921 |
const FlowMap& flowMap() { |
|
921 |
const FlowMap& flowMap() const { |
|
922 | 922 |
return *_flow; |
923 | 923 |
} |
924 | 924 |
|
925 | 925 |
/// \brief Returns \c true when the node is on the source side of the |
926 | 926 |
/// minimum cut. |
927 | 927 |
/// |
928 | 928 |
/// Returns true when the node is on the source side of the found |
929 | 929 |
/// minimum cut. This method can be called both after running \ref |
930 | 930 |
/// startFirstPhase() and \ref startSecondPhase(). |
931 | 931 |
/// |
932 | 932 |
/// \pre Either \ref run() or \ref init() must be called before |
933 | 933 |
/// using this function. |
934 | 934 |
bool minCut(const Node& node) const { |
935 | 935 |
return ((*_level)[node] == _level->maxLevel()) == _phase; |
936 | 936 |
} |
937 | 937 |
|
938 | 938 |
/// \brief Gives back a minimum value cut. |
939 | 939 |
/// |
940 | 940 |
/// Sets \c cutMap to the characteristic vector of a minimum value |
941 | 941 |
/// cut. \c cutMap should be a \ref concepts::WriteMap "writable" |
942 | 942 |
/// node map with \c bool (or convertible) value type. |
943 | 943 |
/// |
944 | 944 |
/// This method can be called both after running \ref startFirstPhase() |
945 | 945 |
/// and \ref startSecondPhase(). The result after the second phase |
946 | 946 |
/// could be slightly different if inexact computation is used. |
947 | 947 |
/// |
948 | 948 |
/// \note This function calls \ref minCut() for each node, so it runs in |
949 | 949 |
/// \f$O(n)\f$ time. |
950 | 950 |
/// |
951 | 951 |
/// \pre Either \ref run() or \ref init() must be called before |
952 | 952 |
/// using this function. |
953 | 953 |
template <typename CutMap> |
954 | 954 |
void minCutMap(CutMap& cutMap) const { |
955 | 955 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
956 | 956 |
cutMap.set(n, minCut(n)); |
957 | 957 |
} |
958 | 958 |
} |
959 | 959 |
|
960 | 960 |
/// @} |
961 | 961 |
}; |
962 | 962 |
} |
963 | 963 |
|
964 | 964 |
#endif |
0 comments (0 inline)