| ... | ... |
@@ -981,403 +981,403 @@ |
| 981 | 981 |
template <typename Graph> |
| 982 | 982 |
struct FindArcSelector< |
| 983 | 983 |
Graph, |
| 984 | 984 |
typename enable_if<typename Graph::FindArcTag, void>::type> |
| 985 | 985 |
{
|
| 986 | 986 |
typedef typename Graph::Node Node; |
| 987 | 987 |
typedef typename Graph::Arc Arc; |
| 988 | 988 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) {
|
| 989 | 989 |
return g.findArc(u, v, prev); |
| 990 | 990 |
} |
| 991 | 991 |
}; |
| 992 | 992 |
} |
| 993 | 993 |
|
| 994 | 994 |
/// \brief Find an arc between two nodes of a digraph. |
| 995 | 995 |
/// |
| 996 | 996 |
/// This function finds an arc from node \c u to node \c v in the |
| 997 | 997 |
/// digraph \c g. |
| 998 | 998 |
/// |
| 999 | 999 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 1000 | 1000 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
| 1001 | 1001 |
/// the next arc from \c u to \c v after \c prev. |
| 1002 | 1002 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
| 1003 | 1003 |
/// |
| 1004 | 1004 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
| 1005 | 1005 |
///\code |
| 1006 | 1006 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
|
| 1007 | 1007 |
/// ... |
| 1008 | 1008 |
/// } |
| 1009 | 1009 |
///\endcode |
| 1010 | 1010 |
/// |
| 1011 | 1011 |
/// \note \ref ConArcIt provides iterator interface for the same |
| 1012 | 1012 |
/// functionality. |
| 1013 | 1013 |
/// |
| 1014 | 1014 |
///\sa ConArcIt |
| 1015 | 1015 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
| 1016 | 1016 |
template <typename Graph> |
| 1017 | 1017 |
inline typename Graph::Arc |
| 1018 | 1018 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 1019 | 1019 |
typename Graph::Arc prev = INVALID) {
|
| 1020 | 1020 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
| 1021 | 1021 |
} |
| 1022 | 1022 |
|
| 1023 | 1023 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
| 1024 | 1024 |
/// |
| 1025 | 1025 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is |
| 1026 | 1026 |
/// a higher level interface for the \ref findArc() function. You can |
| 1027 | 1027 |
/// use it the following way: |
| 1028 | 1028 |
///\code |
| 1029 | 1029 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 1030 | 1030 |
/// ... |
| 1031 | 1031 |
/// } |
| 1032 | 1032 |
///\endcode |
| 1033 | 1033 |
/// |
| 1034 | 1034 |
///\sa findArc() |
| 1035 | 1035 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
| 1036 | 1036 |
template <typename _Graph> |
| 1037 | 1037 |
class ConArcIt : public _Graph::Arc {
|
| 1038 | 1038 |
public: |
| 1039 | 1039 |
|
| 1040 | 1040 |
typedef _Graph Graph; |
| 1041 | 1041 |
typedef typename Graph::Arc Parent; |
| 1042 | 1042 |
|
| 1043 | 1043 |
typedef typename Graph::Arc Arc; |
| 1044 | 1044 |
typedef typename Graph::Node Node; |
| 1045 | 1045 |
|
| 1046 | 1046 |
/// \brief Constructor. |
| 1047 | 1047 |
/// |
| 1048 | 1048 |
/// Construct a new ConArcIt iterating on the arcs that |
| 1049 | 1049 |
/// connects nodes \c u and \c v. |
| 1050 | 1050 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 1051 | 1051 |
Parent::operator=(findArc(_graph, u, v)); |
| 1052 | 1052 |
} |
| 1053 | 1053 |
|
| 1054 | 1054 |
/// \brief Constructor. |
| 1055 | 1055 |
/// |
| 1056 | 1056 |
/// Construct a new ConArcIt that continues the iterating from arc \c a. |
| 1057 | 1057 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
|
| 1058 | 1058 |
|
| 1059 | 1059 |
/// \brief Increment operator. |
| 1060 | 1060 |
/// |
| 1061 | 1061 |
/// It increments the iterator and gives back the next arc. |
| 1062 | 1062 |
ConArcIt& operator++() {
|
| 1063 | 1063 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
| 1064 | 1064 |
_graph.target(*this), *this)); |
| 1065 | 1065 |
return *this; |
| 1066 | 1066 |
} |
| 1067 | 1067 |
private: |
| 1068 | 1068 |
const Graph& _graph; |
| 1069 | 1069 |
}; |
| 1070 | 1070 |
|
| 1071 | 1071 |
namespace _core_bits {
|
| 1072 | 1072 |
|
| 1073 | 1073 |
template <typename Graph, typename Enable = void> |
| 1074 | 1074 |
struct FindEdgeSelector {
|
| 1075 | 1075 |
typedef typename Graph::Node Node; |
| 1076 | 1076 |
typedef typename Graph::Edge Edge; |
| 1077 | 1077 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
| 1078 | 1078 |
bool b; |
| 1079 | 1079 |
if (u != v) {
|
| 1080 | 1080 |
if (e == INVALID) {
|
| 1081 | 1081 |
g.firstInc(e, b, u); |
| 1082 | 1082 |
} else {
|
| 1083 | 1083 |
b = g.u(e) == u; |
| 1084 | 1084 |
g.nextInc(e, b); |
| 1085 | 1085 |
} |
| 1086 | 1086 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
|
| 1087 | 1087 |
g.nextInc(e, b); |
| 1088 | 1088 |
} |
| 1089 | 1089 |
} else {
|
| 1090 | 1090 |
if (e == INVALID) {
|
| 1091 | 1091 |
g.firstInc(e, b, u); |
| 1092 | 1092 |
} else {
|
| 1093 | 1093 |
b = true; |
| 1094 | 1094 |
g.nextInc(e, b); |
| 1095 | 1095 |
} |
| 1096 | 1096 |
while (e != INVALID && (!b || g.v(e) != v)) {
|
| 1097 | 1097 |
g.nextInc(e, b); |
| 1098 | 1098 |
} |
| 1099 | 1099 |
} |
| 1100 | 1100 |
return e; |
| 1101 | 1101 |
} |
| 1102 | 1102 |
}; |
| 1103 | 1103 |
|
| 1104 | 1104 |
template <typename Graph> |
| 1105 | 1105 |
struct FindEdgeSelector< |
| 1106 | 1106 |
Graph, |
| 1107 | 1107 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
| 1108 | 1108 |
{
|
| 1109 | 1109 |
typedef typename Graph::Node Node; |
| 1110 | 1110 |
typedef typename Graph::Edge Edge; |
| 1111 | 1111 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
| 1112 | 1112 |
return g.findEdge(u, v, prev); |
| 1113 | 1113 |
} |
| 1114 | 1114 |
}; |
| 1115 | 1115 |
} |
| 1116 | 1116 |
|
| 1117 | 1117 |
/// \brief Find an edge between two nodes of a graph. |
| 1118 | 1118 |
/// |
| 1119 | 1119 |
/// This function finds an edge from node \c u to node \c v in graph \c g. |
| 1120 | 1120 |
/// If node \c u and node \c v is equal then each loop edge |
| 1121 | 1121 |
/// will be enumerated once. |
| 1122 | 1122 |
/// |
| 1123 | 1123 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 1124 | 1124 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for |
| 1125 | 1125 |
/// the next edge from \c u to \c v after \c prev. |
| 1126 | 1126 |
/// \return The found edge or \ref INVALID if there is no such an edge. |
| 1127 | 1127 |
/// |
| 1128 | 1128 |
/// Thus you can iterate through each edge between \c u and \c v |
| 1129 | 1129 |
/// as it follows. |
| 1130 | 1130 |
///\code |
| 1131 | 1131 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
|
| 1132 | 1132 |
/// ... |
| 1133 | 1133 |
/// } |
| 1134 | 1134 |
///\endcode |
| 1135 | 1135 |
/// |
| 1136 | 1136 |
/// \note \ref ConEdgeIt provides iterator interface for the same |
| 1137 | 1137 |
/// functionality. |
| 1138 | 1138 |
/// |
| 1139 | 1139 |
///\sa ConEdgeIt |
| 1140 | 1140 |
template <typename Graph> |
| 1141 | 1141 |
inline typename Graph::Edge |
| 1142 | 1142 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 1143 | 1143 |
typename Graph::Edge p = INVALID) {
|
| 1144 | 1144 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
| 1145 | 1145 |
} |
| 1146 | 1146 |
|
| 1147 | 1147 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes. |
| 1148 | 1148 |
/// |
| 1149 | 1149 |
/// Iterator for iterating on parallel edges connecting the same nodes. |
| 1150 | 1150 |
/// It is a higher level interface for the findEdge() function. You can |
| 1151 | 1151 |
/// use it the following way: |
| 1152 | 1152 |
///\code |
| 1153 | 1153 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
|
| 1154 | 1154 |
/// ... |
| 1155 | 1155 |
/// } |
| 1156 | 1156 |
///\endcode |
| 1157 | 1157 |
/// |
| 1158 | 1158 |
///\sa findEdge() |
| 1159 | 1159 |
template <typename _Graph> |
| 1160 | 1160 |
class ConEdgeIt : public _Graph::Edge {
|
| 1161 | 1161 |
public: |
| 1162 | 1162 |
|
| 1163 | 1163 |
typedef _Graph Graph; |
| 1164 | 1164 |
typedef typename Graph::Edge Parent; |
| 1165 | 1165 |
|
| 1166 | 1166 |
typedef typename Graph::Edge Edge; |
| 1167 | 1167 |
typedef typename Graph::Node Node; |
| 1168 | 1168 |
|
| 1169 | 1169 |
/// \brief Constructor. |
| 1170 | 1170 |
/// |
| 1171 | 1171 |
/// Construct a new ConEdgeIt iterating on the edges that |
| 1172 | 1172 |
/// connects nodes \c u and \c v. |
| 1173 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) {
|
|
| 1174 |
Parent::operator=(findEdge(_graph, u, v)); |
|
| 1173 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
|
|
| 1174 |
Parent::operator=(findEdge(_graph, _u, _v)); |
|
| 1175 | 1175 |
} |
| 1176 | 1176 |
|
| 1177 | 1177 |
/// \brief Constructor. |
| 1178 | 1178 |
/// |
| 1179 | 1179 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
| 1180 | 1180 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
|
| 1181 | 1181 |
|
| 1182 | 1182 |
/// \brief Increment operator. |
| 1183 | 1183 |
/// |
| 1184 | 1184 |
/// It increments the iterator and gives back the next edge. |
| 1185 | 1185 |
ConEdgeIt& operator++() {
|
| 1186 |
Parent::operator=(findEdge(_graph, _graph.u(*this), |
|
| 1187 |
_graph.v(*this), *this)); |
|
| 1186 |
Parent::operator=(findEdge(_graph, _u, _v, *this)); |
|
| 1188 | 1187 |
return *this; |
| 1189 | 1188 |
} |
| 1190 | 1189 |
private: |
| 1191 | 1190 |
const Graph& _graph; |
| 1191 |
Node _u, _v; |
|
| 1192 | 1192 |
}; |
| 1193 | 1193 |
|
| 1194 | 1194 |
|
| 1195 | 1195 |
///Dynamic arc look-up between given endpoints. |
| 1196 | 1196 |
|
| 1197 | 1197 |
///Using this class, you can find an arc in a digraph from a given |
| 1198 | 1198 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
| 1199 | 1199 |
///where <em>d</em> is the out-degree of the source node. |
| 1200 | 1200 |
/// |
| 1201 | 1201 |
///It is possible to find \e all parallel arcs between two nodes with |
| 1202 | 1202 |
///the \c operator() member. |
| 1203 | 1203 |
/// |
| 1204 | 1204 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
| 1205 | 1205 |
///\ref AllArcLookUp if your digraph is not changed so frequently. |
| 1206 | 1206 |
/// |
| 1207 | 1207 |
///This class uses a self-adjusting binary search tree, the Splay tree |
| 1208 | 1208 |
///of Sleator and Tarjan to guarantee the logarithmic amortized |
| 1209 | 1209 |
///time bound for arc look-ups. This class also guarantees the |
| 1210 | 1210 |
///optimal time bound in a constant factor for any distribution of |
| 1211 | 1211 |
///queries. |
| 1212 | 1212 |
/// |
| 1213 | 1213 |
///\tparam G The type of the underlying digraph. |
| 1214 | 1214 |
/// |
| 1215 | 1215 |
///\sa ArcLookUp |
| 1216 | 1216 |
///\sa AllArcLookUp |
| 1217 | 1217 |
template<class G> |
| 1218 | 1218 |
class DynArcLookUp |
| 1219 | 1219 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
| 1220 | 1220 |
{
|
| 1221 | 1221 |
public: |
| 1222 | 1222 |
typedef typename ItemSetTraits<G, typename G::Arc> |
| 1223 | 1223 |
::ItemNotifier::ObserverBase Parent; |
| 1224 | 1224 |
|
| 1225 | 1225 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
| 1226 | 1226 |
typedef G Digraph; |
| 1227 | 1227 |
|
| 1228 | 1228 |
protected: |
| 1229 | 1229 |
|
| 1230 | 1230 |
class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type {
|
| 1231 | 1231 |
public: |
| 1232 | 1232 |
|
| 1233 | 1233 |
typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent; |
| 1234 | 1234 |
|
| 1235 | 1235 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
|
| 1236 | 1236 |
|
| 1237 | 1237 |
virtual void add(const Node& node) {
|
| 1238 | 1238 |
Parent::add(node); |
| 1239 | 1239 |
Parent::set(node, INVALID); |
| 1240 | 1240 |
} |
| 1241 | 1241 |
|
| 1242 | 1242 |
virtual void add(const std::vector<Node>& nodes) {
|
| 1243 | 1243 |
Parent::add(nodes); |
| 1244 | 1244 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 1245 | 1245 |
Parent::set(nodes[i], INVALID); |
| 1246 | 1246 |
} |
| 1247 | 1247 |
} |
| 1248 | 1248 |
|
| 1249 | 1249 |
virtual void build() {
|
| 1250 | 1250 |
Parent::build(); |
| 1251 | 1251 |
Node it; |
| 1252 | 1252 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 1253 | 1253 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1254 | 1254 |
Parent::set(it, INVALID); |
| 1255 | 1255 |
} |
| 1256 | 1256 |
} |
| 1257 | 1257 |
}; |
| 1258 | 1258 |
|
| 1259 | 1259 |
const Digraph &_g; |
| 1260 | 1260 |
AutoNodeMap _head; |
| 1261 | 1261 |
typename Digraph::template ArcMap<Arc> _parent; |
| 1262 | 1262 |
typename Digraph::template ArcMap<Arc> _left; |
| 1263 | 1263 |
typename Digraph::template ArcMap<Arc> _right; |
| 1264 | 1264 |
|
| 1265 | 1265 |
class ArcLess {
|
| 1266 | 1266 |
const Digraph &g; |
| 1267 | 1267 |
public: |
| 1268 | 1268 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 1269 | 1269 |
bool operator()(Arc a,Arc b) const |
| 1270 | 1270 |
{
|
| 1271 | 1271 |
return g.target(a)<g.target(b); |
| 1272 | 1272 |
} |
| 1273 | 1273 |
}; |
| 1274 | 1274 |
|
| 1275 | 1275 |
public: |
| 1276 | 1276 |
|
| 1277 | 1277 |
///Constructor |
| 1278 | 1278 |
|
| 1279 | 1279 |
///Constructor. |
| 1280 | 1280 |
/// |
| 1281 | 1281 |
///It builds up the search database. |
| 1282 | 1282 |
DynArcLookUp(const Digraph &g) |
| 1283 | 1283 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
| 1284 | 1284 |
{
|
| 1285 | 1285 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
| 1286 | 1286 |
refresh(); |
| 1287 | 1287 |
} |
| 1288 | 1288 |
|
| 1289 | 1289 |
protected: |
| 1290 | 1290 |
|
| 1291 | 1291 |
virtual void add(const Arc& arc) {
|
| 1292 | 1292 |
insert(arc); |
| 1293 | 1293 |
} |
| 1294 | 1294 |
|
| 1295 | 1295 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 1296 | 1296 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1297 | 1297 |
insert(arcs[i]); |
| 1298 | 1298 |
} |
| 1299 | 1299 |
} |
| 1300 | 1300 |
|
| 1301 | 1301 |
virtual void erase(const Arc& arc) {
|
| 1302 | 1302 |
remove(arc); |
| 1303 | 1303 |
} |
| 1304 | 1304 |
|
| 1305 | 1305 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 1306 | 1306 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1307 | 1307 |
remove(arcs[i]); |
| 1308 | 1308 |
} |
| 1309 | 1309 |
} |
| 1310 | 1310 |
|
| 1311 | 1311 |
virtual void build() {
|
| 1312 | 1312 |
refresh(); |
| 1313 | 1313 |
} |
| 1314 | 1314 |
|
| 1315 | 1315 |
virtual void clear() {
|
| 1316 | 1316 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 1317 | 1317 |
_head.set(n, INVALID); |
| 1318 | 1318 |
} |
| 1319 | 1319 |
} |
| 1320 | 1320 |
|
| 1321 | 1321 |
void insert(Arc arc) {
|
| 1322 | 1322 |
Node s = _g.source(arc); |
| 1323 | 1323 |
Node t = _g.target(arc); |
| 1324 | 1324 |
_left.set(arc, INVALID); |
| 1325 | 1325 |
_right.set(arc, INVALID); |
| 1326 | 1326 |
|
| 1327 | 1327 |
Arc e = _head[s]; |
| 1328 | 1328 |
if (e == INVALID) {
|
| 1329 | 1329 |
_head.set(s, arc); |
| 1330 | 1330 |
_parent.set(arc, INVALID); |
| 1331 | 1331 |
return; |
| 1332 | 1332 |
} |
| 1333 | 1333 |
while (true) {
|
| 1334 | 1334 |
if (t < _g.target(e)) {
|
| 1335 | 1335 |
if (_left[e] == INVALID) {
|
| 1336 | 1336 |
_left.set(e, arc); |
| 1337 | 1337 |
_parent.set(arc, e); |
| 1338 | 1338 |
splay(arc); |
| 1339 | 1339 |
return; |
| 1340 | 1340 |
} else {
|
| 1341 | 1341 |
e = _left[e]; |
| 1342 | 1342 |
} |
| 1343 | 1343 |
} else {
|
| 1344 | 1344 |
if (_right[e] == INVALID) {
|
| 1345 | 1345 |
_right.set(e, arc); |
| 1346 | 1346 |
_parent.set(arc, e); |
| 1347 | 1347 |
splay(arc); |
| 1348 | 1348 |
return; |
| 1349 | 1349 |
} else {
|
| 1350 | 1350 |
e = _right[e]; |
| 1351 | 1351 |
} |
| 1352 | 1352 |
} |
| 1353 | 1353 |
} |
| 1354 | 1354 |
} |
| 1355 | 1355 |
|
| 1356 | 1356 |
void remove(Arc arc) {
|
| 1357 | 1357 |
if (_left[arc] == INVALID) {
|
| 1358 | 1358 |
if (_right[arc] != INVALID) {
|
| 1359 | 1359 |
_parent.set(_right[arc], _parent[arc]); |
| 1360 | 1360 |
} |
| 1361 | 1361 |
if (_parent[arc] != INVALID) {
|
| 1362 | 1362 |
if (_left[_parent[arc]] == arc) {
|
| 1363 | 1363 |
_left.set(_parent[arc], _right[arc]); |
| 1364 | 1364 |
} else {
|
| 1365 | 1365 |
_right.set(_parent[arc], _right[arc]); |
| 1366 | 1366 |
} |
| 1367 | 1367 |
} else {
|
| 1368 | 1368 |
_head.set(_g.source(arc), _right[arc]); |
| 1369 | 1369 |
} |
| 1370 | 1370 |
} else if (_right[arc] == INVALID) {
|
| 1371 | 1371 |
_parent.set(_left[arc], _parent[arc]); |
| 1372 | 1372 |
if (_parent[arc] != INVALID) {
|
| 1373 | 1373 |
if (_left[_parent[arc]] == arc) {
|
| 1374 | 1374 |
_left.set(_parent[arc], _left[arc]); |
| 1375 | 1375 |
} else {
|
| 1376 | 1376 |
_right.set(_parent[arc], _left[arc]); |
| 1377 | 1377 |
} |
| 1378 | 1378 |
} else {
|
| 1379 | 1379 |
_head.set(_g.source(arc), _left[arc]); |
| 1380 | 1380 |
} |
| 1381 | 1381 |
} else {
|
| 1382 | 1382 |
Arc e = _left[arc]; |
| 1383 | 1383 |
if (_right[e] != INVALID) {
|
0 comments (0 inline)