1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CORE_H |
20 | 20 |
#define LEMON_CORE_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/enable_if.h> |
26 | 26 |
#include <lemon/bits/traits.h> |
27 | 27 |
|
28 | 28 |
///\file |
29 | 29 |
///\brief LEMON core utilities. |
30 | 30 |
/// |
31 | 31 |
///This header file contains core utilities for LEMON. |
32 |
///It is automatically included by all graph types, therefore it usually |
|
32 |
///It is automatically included by all graph types, therefore it usually |
|
33 | 33 |
///do not have to be included directly. |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Dummy type to make it easier to create invalid iterators. |
38 | 38 |
/// |
39 | 39 |
/// Dummy type to make it easier to create invalid iterators. |
40 | 40 |
/// See \ref INVALID for the usage. |
41 | 41 |
struct Invalid { |
42 | 42 |
public: |
43 | 43 |
bool operator==(Invalid) { return true; } |
44 | 44 |
bool operator!=(Invalid) { return false; } |
45 | 45 |
bool operator< (Invalid) { return false; } |
46 | 46 |
}; |
47 | 47 |
|
48 | 48 |
/// \brief Invalid iterators. |
49 | 49 |
/// |
50 | 50 |
/// \ref Invalid is a global type that converts to each iterator |
51 | 51 |
/// in such a way that the value of the target iterator will be invalid. |
52 | 52 |
#ifdef LEMON_ONLY_TEMPLATES |
53 | 53 |
const Invalid INVALID = Invalid(); |
54 | 54 |
#else |
55 | 55 |
extern const Invalid INVALID; |
56 | 56 |
#endif |
57 | 57 |
|
58 | 58 |
/// \addtogroup gutils |
59 | 59 |
/// @{ |
60 | 60 |
|
61 | 61 |
///Creates convenience typedefs for the digraph types and iterators |
62 | 62 |
|
63 | 63 |
///This \c \#define creates convenience typedefs for the following types |
64 | 64 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
65 | 65 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
66 | 66 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
67 | 67 |
/// |
68 | 68 |
///\note If the graph type is a dependent type, ie. the graph type depend |
69 | 69 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
70 | 70 |
///macro. |
71 | 71 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
72 | 72 |
typedef Digraph::Node Node; \ |
73 | 73 |
typedef Digraph::NodeIt NodeIt; \ |
74 | 74 |
typedef Digraph::Arc Arc; \ |
75 | 75 |
typedef Digraph::ArcIt ArcIt; \ |
76 | 76 |
typedef Digraph::InArcIt InArcIt; \ |
77 | 77 |
typedef Digraph::OutArcIt OutArcIt; \ |
78 | 78 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
79 | 79 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
80 | 80 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
... | ... |
@@ -1126,101 +1126,101 @@ |
1126 | 1126 |
/// use it the following way: |
1127 | 1127 |
///\code |
1128 | 1128 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
1129 | 1129 |
/// ... |
1130 | 1130 |
/// } |
1131 | 1131 |
///\endcode |
1132 | 1132 |
/// |
1133 | 1133 |
///\sa findEdge() |
1134 | 1134 |
template <typename _Graph> |
1135 | 1135 |
class ConEdgeIt : public _Graph::Edge { |
1136 | 1136 |
public: |
1137 | 1137 |
|
1138 | 1138 |
typedef _Graph Graph; |
1139 | 1139 |
typedef typename Graph::Edge Parent; |
1140 | 1140 |
|
1141 | 1141 |
typedef typename Graph::Edge Edge; |
1142 | 1142 |
typedef typename Graph::Node Node; |
1143 | 1143 |
|
1144 | 1144 |
/// \brief Constructor. |
1145 | 1145 |
/// |
1146 | 1146 |
/// Construct a new ConEdgeIt iterating on the edges which |
1147 | 1147 |
/// connects the \c u and \c v node. |
1148 | 1148 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) { |
1149 | 1149 |
Parent::operator=(findEdge(_graph, u, v)); |
1150 | 1150 |
} |
1151 | 1151 |
|
1152 | 1152 |
/// \brief Constructor. |
1153 | 1153 |
/// |
1154 | 1154 |
/// Construct a new ConEdgeIt which continues the iterating from |
1155 | 1155 |
/// the \c e edge. |
1156 | 1156 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
1157 | 1157 |
|
1158 | 1158 |
/// \brief Increment operator. |
1159 | 1159 |
/// |
1160 | 1160 |
/// It increments the iterator and gives back the next edge. |
1161 | 1161 |
ConEdgeIt& operator++() { |
1162 | 1162 |
Parent::operator=(findEdge(_graph, _graph.u(*this), |
1163 | 1163 |
_graph.v(*this), *this)); |
1164 | 1164 |
return *this; |
1165 | 1165 |
} |
1166 | 1166 |
private: |
1167 | 1167 |
const Graph& _graph; |
1168 | 1168 |
}; |
1169 | 1169 |
|
1170 | 1170 |
|
1171 | 1171 |
///Dynamic arc look up between given endpoints. |
1172 | 1172 |
|
1173 | 1173 |
///Using this class, you can find an arc in a digraph from a given |
1174 |
///source to a given target in amortized time <em>O(log |
|
1174 |
///source to a given target in amortized time <em>O(log</em>d<em>)</em>, |
|
1175 | 1175 |
///where <em>d</em> is the out-degree of the source node. |
1176 | 1176 |
/// |
1177 | 1177 |
///It is possible to find \e all parallel arcs between two nodes with |
1178 |
///the \c |
|
1178 |
///the \c operator() member. |
|
1179 | 1179 |
/// |
1180 | 1180 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
1181 | 1181 |
///digraph is not changed so frequently. |
1182 | 1182 |
/// |
1183 | 1183 |
///This class uses a self-adjusting binary search tree, Sleator's |
1184 | 1184 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
1185 | 1185 |
///time bound for arc lookups. This class also guarantees the |
1186 | 1186 |
///optimal time bound in a constant factor for any distribution of |
1187 | 1187 |
///queries. |
1188 | 1188 |
/// |
1189 | 1189 |
///\tparam G The type of the underlying digraph. |
1190 | 1190 |
/// |
1191 | 1191 |
///\sa ArcLookUp |
1192 | 1192 |
///\sa AllArcLookUp |
1193 | 1193 |
template<class G> |
1194 | 1194 |
class DynArcLookUp |
1195 | 1195 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
1196 | 1196 |
{ |
1197 | 1197 |
public: |
1198 | 1198 |
typedef typename ItemSetTraits<G, typename G::Arc> |
1199 | 1199 |
::ItemNotifier::ObserverBase Parent; |
1200 | 1200 |
|
1201 | 1201 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1202 | 1202 |
typedef G Digraph; |
1203 | 1203 |
|
1204 | 1204 |
protected: |
1205 | 1205 |
|
1206 | 1206 |
class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type { |
1207 | 1207 |
public: |
1208 | 1208 |
|
1209 | 1209 |
typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent; |
1210 | 1210 |
|
1211 | 1211 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {} |
1212 | 1212 |
|
1213 | 1213 |
virtual void add(const Node& node) { |
1214 | 1214 |
Parent::add(node); |
1215 | 1215 |
Parent::set(node, INVALID); |
1216 | 1216 |
} |
1217 | 1217 |
|
1218 | 1218 |
virtual void add(const std::vector<Node>& nodes) { |
1219 | 1219 |
Parent::add(nodes); |
1220 | 1220 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1221 | 1221 |
Parent::set(nodes[i], INVALID); |
1222 | 1222 |
} |
1223 | 1223 |
} |
1224 | 1224 |
|
1225 | 1225 |
virtual void build() { |
1226 | 1226 |
Parent::build(); |
... | ... |
@@ -1378,286 +1378,256 @@ |
1378 | 1378 |
_left.set(_parent[arc], e); |
1379 | 1379 |
} else { |
1380 | 1380 |
_right.set(_parent[arc], e); |
1381 | 1381 |
} |
1382 | 1382 |
} |
1383 | 1383 |
splay(s); |
1384 | 1384 |
} else { |
1385 | 1385 |
_right.set(e, _right[arc]); |
1386 | 1386 |
_parent.set(_right[arc], e); |
1387 | 1387 |
_parent.set(e, _parent[arc]); |
1388 | 1388 |
|
1389 | 1389 |
if (_parent[arc] != INVALID) { |
1390 | 1390 |
if (_left[_parent[arc]] == arc) { |
1391 | 1391 |
_left.set(_parent[arc], e); |
1392 | 1392 |
} else { |
1393 | 1393 |
_right.set(_parent[arc], e); |
1394 | 1394 |
} |
1395 | 1395 |
} else { |
1396 | 1396 |
_head.set(_g.source(arc), e); |
1397 | 1397 |
} |
1398 | 1398 |
} |
1399 | 1399 |
} |
1400 | 1400 |
} |
1401 | 1401 |
|
1402 | 1402 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1403 | 1403 |
{ |
1404 | 1404 |
int m=(a+b)/2; |
1405 | 1405 |
Arc me=v[m]; |
1406 | 1406 |
if (a < m) { |
1407 | 1407 |
Arc left = refreshRec(v,a,m-1); |
1408 | 1408 |
_left.set(me, left); |
1409 | 1409 |
_parent.set(left, me); |
1410 | 1410 |
} else { |
1411 | 1411 |
_left.set(me, INVALID); |
1412 | 1412 |
} |
1413 | 1413 |
if (m < b) { |
1414 | 1414 |
Arc right = refreshRec(v,m+1,b); |
1415 | 1415 |
_right.set(me, right); |
1416 | 1416 |
_parent.set(right, me); |
1417 | 1417 |
} else { |
1418 | 1418 |
_right.set(me, INVALID); |
1419 | 1419 |
} |
1420 | 1420 |
return me; |
1421 | 1421 |
} |
1422 | 1422 |
|
1423 | 1423 |
void refresh() { |
1424 | 1424 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1425 | 1425 |
std::vector<Arc> v; |
1426 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
|
1427 |
if(v.size()) { |
|
1426 |
for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a); |
|
1427 |
if (!v.empty()) { |
|
1428 | 1428 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1429 | 1429 |
Arc head = refreshRec(v,0,v.size()-1); |
1430 | 1430 |
_head.set(n, head); |
1431 | 1431 |
_parent.set(head, INVALID); |
1432 | 1432 |
} |
1433 | 1433 |
else _head.set(n, INVALID); |
1434 | 1434 |
} |
1435 | 1435 |
} |
1436 | 1436 |
|
1437 | 1437 |
void zig(Arc v) { |
1438 | 1438 |
Arc w = _parent[v]; |
1439 | 1439 |
_parent.set(v, _parent[w]); |
1440 | 1440 |
_parent.set(w, v); |
1441 | 1441 |
_left.set(w, _right[v]); |
1442 | 1442 |
_right.set(v, w); |
1443 | 1443 |
if (_parent[v] != INVALID) { |
1444 | 1444 |
if (_right[_parent[v]] == w) { |
1445 | 1445 |
_right.set(_parent[v], v); |
1446 | 1446 |
} else { |
1447 | 1447 |
_left.set(_parent[v], v); |
1448 | 1448 |
} |
1449 | 1449 |
} |
1450 | 1450 |
if (_left[w] != INVALID){ |
1451 | 1451 |
_parent.set(_left[w], w); |
1452 | 1452 |
} |
1453 | 1453 |
} |
1454 | 1454 |
|
1455 | 1455 |
void zag(Arc v) { |
1456 | 1456 |
Arc w = _parent[v]; |
1457 | 1457 |
_parent.set(v, _parent[w]); |
1458 | 1458 |
_parent.set(w, v); |
1459 | 1459 |
_right.set(w, _left[v]); |
1460 | 1460 |
_left.set(v, w); |
1461 | 1461 |
if (_parent[v] != INVALID){ |
1462 | 1462 |
if (_left[_parent[v]] == w) { |
1463 | 1463 |
_left.set(_parent[v], v); |
1464 | 1464 |
} else { |
1465 | 1465 |
_right.set(_parent[v], v); |
1466 | 1466 |
} |
1467 | 1467 |
} |
1468 | 1468 |
if (_right[w] != INVALID){ |
1469 | 1469 |
_parent.set(_right[w], w); |
1470 | 1470 |
} |
1471 | 1471 |
} |
1472 | 1472 |
|
1473 | 1473 |
void splay(Arc v) { |
1474 | 1474 |
while (_parent[v] != INVALID) { |
1475 | 1475 |
if (v == _left[_parent[v]]) { |
1476 | 1476 |
if (_parent[_parent[v]] == INVALID) { |
1477 | 1477 |
zig(v); |
1478 | 1478 |
} else { |
1479 | 1479 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1480 | 1480 |
zig(_parent[v]); |
1481 | 1481 |
zig(v); |
1482 | 1482 |
} else { |
1483 | 1483 |
zig(v); |
1484 | 1484 |
zag(v); |
1485 | 1485 |
} |
1486 | 1486 |
} |
1487 | 1487 |
} else { |
1488 | 1488 |
if (_parent[_parent[v]] == INVALID) { |
1489 | 1489 |
zag(v); |
1490 | 1490 |
} else { |
1491 | 1491 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1492 | 1492 |
zag(v); |
1493 | 1493 |
zig(v); |
1494 | 1494 |
} else { |
1495 | 1495 |
zag(_parent[v]); |
1496 | 1496 |
zag(v); |
1497 | 1497 |
} |
1498 | 1498 |
} |
1499 | 1499 |
} |
1500 | 1500 |
} |
1501 | 1501 |
_head[_g.source(v)] = v; |
1502 | 1502 |
} |
1503 | 1503 |
|
1504 | 1504 |
|
1505 | 1505 |
public: |
1506 | 1506 |
|
1507 | 1507 |
///Find an arc between two nodes. |
1508 | 1508 |
|
1509 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
1510 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
1509 |
///Find an arc between two nodes. |
|
1511 | 1510 |
///\param s The source node |
1512 | 1511 |
///\param t The target node |
1513 |
///\return An arc from \c s to \c t if there exists, |
|
1514 |
///\ref INVALID otherwise. |
|
1515 |
Arc operator()(Node s, Node t) const |
|
1516 |
{ |
|
1517 |
Arc a = _head[s]; |
|
1518 |
if (a == INVALID) return INVALID; |
|
1519 |
while (true) { |
|
1520 |
if (_g.target(a) == t) { |
|
1512 |
///\param p The previous arc between \c s and \c t. It it is INVALID or |
|
1513 |
///not given, the operator finds the first appropriate arc. |
|
1514 |
///\return An arc from \c s to \c t after \c p or |
|
1515 |
///\ref INVALID if there is no more. |
|
1516 |
/// |
|
1517 |
///For example, you can count the number of arcs from \c u to \c v in the |
|
1518 |
///following way. |
|
1519 |
///\code |
|
1520 |
///DynArcLookUp<ListDigraph> ae(g); |
|
1521 |
///... |
|
1522 |
///int n=0; |
|
1523 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
|
1524 |
///\endcode |
|
1525 |
/// |
|
1526 |
///Finding the arcs take at most <em>O(</em>log<em>d)</em> |
|
1527 |
///amortized time, specifically, the time complexity of the lookups |
|
1528 |
///is equal to the optimal search tree implementation for the |
|
1529 |
///current query distribution in a constant factor. |
|
1530 |
/// |
|
1531 |
///\note This is a dynamic data structure, therefore the data |
|
1532 |
///structure is updated after each graph alteration. However, |
|
1533 |
///theoretically this data structure is faster than \c ArcLookUp |
|
1534 |
///or AllEdgeLookup, but it often provides worse performance than |
|
1535 |
///them. |
|
1536 |
/// |
|
1537 |
Arc operator()(Node s, Node t, Arc p = INVALID) const { |
|
1538 |
if (p == INVALID) { |
|
1539 |
Arc a = _head[s]; |
|
1540 |
if (a == INVALID) return INVALID; |
|
1541 |
Arc r = INVALID; |
|
1542 |
while (true) { |
|
1543 |
if (_g.target(a) < t) { |
|
1544 |
if (_right[a] == INVALID) { |
|
1545 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1546 |
return r; |
|
1547 |
} else { |
|
1548 |
a = _right[a]; |
|
1549 |
} |
|
1550 |
} else { |
|
1551 |
if (_g.target(a) == t) { |
|
1552 |
r = a; |
|
1553 |
} |
|
1554 |
if (_left[a] == INVALID) { |
|
1555 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1556 |
return r; |
|
1557 |
} else { |
|
1558 |
a = _left[a]; |
|
1559 |
} |
|
1560 |
} |
|
1561 |
} |
|
1562 |
} else { |
|
1563 |
Arc a = p; |
|
1564 |
if (_right[a] != INVALID) { |
|
1565 |
a = _right[a]; |
|
1566 |
while (_left[a] != INVALID) { |
|
1567 |
a = _left[a]; |
|
1568 |
} |
|
1521 | 1569 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1522 |
return a; |
|
1523 |
} else if (t < _g.target(a)) { |
|
1524 |
if (_left[a] == INVALID) { |
|
1525 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1570 |
} else { |
|
1571 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
|
1572 |
a = _parent[a]; |
|
1573 |
} |
|
1574 |
if (_parent[a] == INVALID) { |
|
1526 | 1575 |
return INVALID; |
1527 | 1576 |
} else { |
1528 |
a = _left[a]; |
|
1529 |
} |
|
1530 |
} else { |
|
1531 |
if (_right[a] == INVALID) { |
|
1577 |
a = _parent[a]; |
|
1532 | 1578 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1533 |
return INVALID; |
|
1534 |
} else { |
|
1535 |
a = _right[a]; |
|
1536 | 1579 |
} |
1537 | 1580 |
} |
1581 |
if (_g.target(a) == t) return a; |
|
1582 |
else return INVALID; |
|
1538 | 1583 |
} |
1539 | 1584 |
} |
1540 | 1585 |
|
1541 |
///Find the first arc between two nodes. |
|
1542 |
|
|
1543 |
///Find the first arc between two nodes in time |
|
1544 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
|
1545 |
/// outgoing arcs of \c s. |
|
1546 |
///\param s The source node |
|
1547 |
///\param t The target node |
|
1548 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
|
1549 |
/// otherwise. |
|
1550 |
Arc findFirst(Node s, Node t) const |
|
1551 |
{ |
|
1552 |
Arc a = _head[s]; |
|
1553 |
if (a == INVALID) return INVALID; |
|
1554 |
Arc r = INVALID; |
|
1555 |
while (true) { |
|
1556 |
if (_g.target(a) < t) { |
|
1557 |
if (_right[a] == INVALID) { |
|
1558 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1559 |
return r; |
|
1560 |
} else { |
|
1561 |
a = _right[a]; |
|
1562 |
} |
|
1563 |
} else { |
|
1564 |
if (_g.target(a) == t) { |
|
1565 |
r = a; |
|
1566 |
} |
|
1567 |
if (_left[a] == INVALID) { |
|
1568 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1569 |
return r; |
|
1570 |
} else { |
|
1571 |
a = _left[a]; |
|
1572 |
} |
|
1573 |
} |
|
1574 |
} |
|
1575 |
} |
|
1576 |
|
|
1577 |
///Find the next arc between two nodes. |
|
1578 |
|
|
1579 |
///Find the next arc between two nodes in time |
|
1580 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
|
1581 |
/// outgoing arcs of \c s. |
|
1582 |
///\param s The source node |
|
1583 |
///\param t The target node |
|
1584 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
|
1585 |
/// otherwise. |
|
1586 |
|
|
1587 |
///\note If \c e is not the result of the previous \c findFirst() |
|
1588 |
///operation then the amorized time bound can not be guaranteed. |
|
1589 |
#ifdef DOXYGEN |
|
1590 |
Arc findNext(Node s, Node t, Arc a) const |
|
1591 |
#else |
|
1592 |
Arc findNext(Node, Node t, Arc a) const |
|
1593 |
#endif |
|
1594 |
{ |
|
1595 |
if (_right[a] != INVALID) { |
|
1596 |
a = _right[a]; |
|
1597 |
while (_left[a] != INVALID) { |
|
1598 |
a = _left[a]; |
|
1599 |
} |
|
1600 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1601 |
} else { |
|
1602 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
|
1603 |
a = _parent[a]; |
|
1604 |
} |
|
1605 |
if (_parent[a] == INVALID) { |
|
1606 |
return INVALID; |
|
1607 |
} else { |
|
1608 |
a = _parent[a]; |
|
1609 |
const_cast<DynArcLookUp&>(*this).splay(a); |
|
1610 |
} |
|
1611 |
} |
|
1612 |
if (_g.target(a) == t) return a; |
|
1613 |
else return INVALID; |
|
1614 |
} |
|
1615 |
|
|
1616 | 1586 |
}; |
1617 | 1587 |
|
1618 | 1588 |
///Fast arc look up between given endpoints. |
1619 | 1589 |
|
1620 | 1590 |
///Using this class, you can find an arc in a digraph from a given |
1621 | 1591 |
///source to a given target in time <em>O(log d)</em>, |
1622 | 1592 |
///where <em>d</em> is the out-degree of the source node. |
1623 | 1593 |
/// |
1624 | 1594 |
///It is not possible to find \e all parallel arcs between two nodes. |
1625 | 1595 |
///Use \ref AllArcLookUp for this purpose. |
1626 | 1596 |
/// |
1627 | 1597 |
///\warning This class is static, so you should refresh() (or at least |
1628 | 1598 |
///refresh(Node)) this data structure |
1629 | 1599 |
///whenever the digraph changes. This is a time consuming (superlinearly |
1630 | 1600 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
1631 | 1601 |
/// |
1632 | 1602 |
///\tparam G The type of the underlying digraph. |
1633 | 1603 |
/// |
1634 | 1604 |
///\sa DynArcLookUp |
1635 | 1605 |
///\sa AllArcLookUp |
1636 | 1606 |
template<class G> |
1637 | 1607 |
class ArcLookUp |
1638 | 1608 |
{ |
1639 | 1609 |
public: |
1640 | 1610 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1641 | 1611 |
typedef G Digraph; |
1642 | 1612 |
|
1643 | 1613 |
protected: |
1644 | 1614 |
const Digraph &_g; |
1645 | 1615 |
typename Digraph::template NodeMap<Arc> _head; |
1646 | 1616 |
typename Digraph::template ArcMap<Arc> _left; |
1647 | 1617 |
typename Digraph::template ArcMap<Arc> _right; |
1648 | 1618 |
|
1649 | 1619 |
class ArcLess { |
1650 | 1620 |
const Digraph &g; |
1651 | 1621 |
public: |
1652 | 1622 |
ArcLess(const Digraph &_g) : g(_g) {} |
1653 | 1623 |
bool operator()(Arc a,Arc b) const |
1654 | 1624 |
{ |
1655 | 1625 |
return g.target(a)<g.target(b); |
1656 | 1626 |
} |
1657 | 1627 |
}; |
1658 | 1628 |
|
1659 | 1629 |
public: |
1660 | 1630 |
|
1661 | 1631 |
///Constructor |
1662 | 1632 |
|
1663 | 1633 |
///Constructor. |
0 comments (0 inline)