| 1 | 1 |
/* -*- C++ -*- |
| 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_GRAPH_UTILS_H |
| 20 | 20 |
#define LEMON_GRAPH_UTILS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
#include <vector> |
| 24 | 24 |
#include <map> |
| 25 | 25 |
#include <cmath> |
| 26 | 26 |
#include <algorithm> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/bits/invalid.h> |
| 29 | 29 |
#include <lemon/bits/utility.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/bits/traits.h> |
| 32 | 32 |
|
| 33 | 33 |
#include <lemon/bits/alteration_notifier.h> |
| 34 | 34 |
#include <lemon/bits/default_map.h> |
| 35 | 35 |
|
| 36 | 36 |
///\ingroup gutils |
| 37 | 37 |
///\file |
| 38 | 38 |
///\brief Graph utilities. |
| 39 | 39 |
|
| 40 | 40 |
namespace lemon {
|
| 41 | 41 |
|
| 42 | 42 |
/// \addtogroup gutils |
| 43 | 43 |
/// @{
|
| 44 | 44 |
|
| 45 | 45 |
namespace _graph_utils_bits {
|
| 46 | 46 |
template <typename Graph> |
| 47 | 47 |
struct Node { typedef typename Graph::Node type; };
|
| 48 | 48 |
|
| 49 | 49 |
template <typename Graph> |
| 50 | 50 |
struct NodeIt { typedef typename Graph::NodeIt type; };
|
| 51 | 51 |
|
| 52 | 52 |
template <typename Graph> |
| 53 | 53 |
struct Arc { typedef typename Graph::Arc type; };
|
| 54 | 54 |
|
| 55 | 55 |
template <typename Graph> |
| 56 | 56 |
struct ArcIt { typedef typename Graph::ArcIt type; };
|
| 57 | 57 |
|
| 58 | 58 |
template <typename Graph> |
| 59 | 59 |
struct Edge { typedef typename Graph::Edge type; };
|
| 60 | 60 |
|
| 61 | 61 |
template <typename Graph> |
| 62 | 62 |
struct EdgeIt { typedef typename Graph::EdgeIt type; };
|
| 63 | 63 |
|
| 64 | 64 |
template <typename Graph> |
| 65 | 65 |
struct OutArcIt { typedef typename Graph::OutArcIt type; };
|
| 66 | 66 |
|
| 67 | 67 |
template <typename Graph> |
| 68 | 68 |
struct InArcIt { typedef typename Graph::InArcIt type; };
|
| 69 | 69 |
|
| 70 | 70 |
template <typename Graph> |
| 71 | 71 |
struct IncEdgeIt { typedef typename Graph::IncEdgeIt type; };
|
| 72 | 72 |
|
| 73 | 73 |
template <typename Graph> |
| 74 | 74 |
struct BoolNodeMap {
|
| 75 | 75 |
typedef typename Graph::template NodeMap<bool> type; |
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
template <typename Graph> |
| 79 | 79 |
struct IntNodeMap {
|
| 80 | 80 |
typedef typename Graph::template NodeMap<int> type; |
| 81 | 81 |
}; |
| 82 | 82 |
|
| 83 | 83 |
template <typename Graph> |
| 84 | 84 |
struct DoubleNodeMap {
|
| 85 | 85 |
typedef typename Graph::template NodeMap<double> type; |
| 86 | 86 |
}; |
| 87 | 87 |
|
| 88 | 88 |
template <typename Graph> |
| 89 | 89 |
struct BoolArcMap {
|
| 90 | 90 |
typedef typename Graph::template ArcMap<bool> type; |
| 91 | 91 |
}; |
| 92 | 92 |
|
| 93 | 93 |
template <typename Graph> |
| 94 | 94 |
struct IntArcMap {
|
| 95 | 95 |
typedef typename Graph::template ArcMap<int> type; |
| 96 | 96 |
}; |
| 97 | 97 |
|
| 98 | 98 |
template <typename Graph> |
| 99 | 99 |
struct DoubleArcMap {
|
| 100 | 100 |
typedef typename Graph::template ArcMap<double> type; |
| 101 | 101 |
}; |
| 102 | 102 |
|
| 103 | 103 |
template <typename Graph> |
| 104 | 104 |
struct BoolEdgeMap {
|
| 105 | 105 |
typedef typename Graph::template EdgeMap<bool> type; |
| 106 | 106 |
}; |
| 107 | 107 |
|
| 108 | 108 |
template <typename Graph> |
| 109 | 109 |
struct IntEdgeMap {
|
| 110 | 110 |
typedef typename Graph::template EdgeMap<int> type; |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
template <typename Graph> |
| 114 | 114 |
struct DoubleEdgeMap {
|
| 115 | 115 |
typedef typename Graph::template EdgeMap<double> type; |
| 116 | 116 |
}; |
| 117 | 117 |
|
| 118 | 118 |
|
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 | 121 |
///Creates convenience typedefs for the digraph types and iterators |
| 122 | 122 |
|
| 123 | 123 |
///This \c \#define creates convenience typedefs for the following types |
| 124 | 124 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
| 125 | 125 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
| 126 | 126 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
| 127 | 127 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
| 128 | 128 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 129 | 129 |
Node<Digraph>::type Node; \ |
| 130 | 130 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 131 | 131 |
NodeIt<Digraph>::type NodeIt; \ |
| 132 | 132 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 133 | 133 |
Arc<Digraph>::type Arc; \ |
| 134 | 134 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 135 | 135 |
ArcIt<Digraph>::type ArcIt; \ |
| 136 | 136 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 137 | 137 |
OutArcIt<Digraph>::type OutArcIt; \ |
| 138 | 138 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 139 | 139 |
InArcIt<Digraph>::type InArcIt; \ |
| 140 | 140 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 141 | 141 |
BoolNodeMap<Digraph>::type BoolNodeMap; \ |
| 142 | 142 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 143 | 143 |
IntNodeMap<Digraph>::type IntNodeMap; \ |
| 144 | 144 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 145 | 145 |
DoubleNodeMap<Digraph>::type DoubleNodeMap; \ |
| 146 | 146 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 147 | 147 |
BoolArcMap<Digraph>::type BoolArcMap; \ |
| 148 | 148 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 149 | 149 |
IntArcMap<Digraph>::type IntArcMap; \ |
| 150 | 150 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 151 | 151 |
DoubleArcMap<Digraph>::type DoubleArcMap |
| 152 | 152 |
|
| 153 | 153 |
|
| 154 | 154 |
///Creates convenience typedefs for the graph types and iterators |
| 155 | 155 |
|
| 156 | 156 |
///This \c \#define creates the same convenience typedefs as defined |
| 157 | 157 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
| 158 | 158 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
| 159 | 159 |
///\c DoubleEdgeMap. |
| 160 | 160 |
#define GRAPH_TYPEDEFS(Graph) \ |
| 161 | 161 |
DIGRAPH_TYPEDEFS(Graph); \ |
| 162 | 162 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 163 | 163 |
Edge<Graph>::type Edge; \ |
| 164 | 164 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 165 | 165 |
EdgeIt<Graph>::type EdgeIt; \ |
| 166 | 166 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 167 |
IncEdgeIt<Graph>::type IncEdgeIt \ |
|
| 167 |
IncEdgeIt<Graph>::type IncEdgeIt; \ |
|
| 168 | 168 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 169 | 169 |
BoolEdgeMap<Graph>::type BoolEdgeMap; \ |
| 170 | 170 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 171 | 171 |
IntEdgeMap<Graph>::type IntEdgeMap; \ |
| 172 | 172 |
typedef typename ::lemon::_graph_utils_bits:: \ |
| 173 | 173 |
DoubleEdgeMap<Graph>::type DoubleEdgeMap |
| 174 | 174 |
|
| 175 | 175 |
|
| 176 | 176 |
/// \brief Function to count the items in the graph. |
| 177 | 177 |
/// |
| 178 | 178 |
/// This function counts the items (nodes, arcs etc) in the graph. |
| 179 | 179 |
/// The complexity of the function is O(n) because |
| 180 | 180 |
/// it iterates on all of the items. |
| 181 | 181 |
template <typename Graph, typename Item> |
| 182 | 182 |
inline int countItems(const Graph& g) {
|
| 183 | 183 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
| 184 | 184 |
int num = 0; |
| 185 | 185 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 186 | 186 |
++num; |
| 187 | 187 |
} |
| 188 | 188 |
return num; |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
// Node counting: |
| 192 | 192 |
|
| 193 | 193 |
namespace _graph_utils_bits {
|
| 194 | 194 |
|
| 195 | 195 |
template <typename Graph, typename Enable = void> |
| 196 | 196 |
struct CountNodesSelector {
|
| 197 | 197 |
static int count(const Graph &g) {
|
| 198 | 198 |
return countItems<Graph, typename Graph::Node>(g); |
| 199 | 199 |
} |
| 200 | 200 |
}; |
| 201 | 201 |
|
| 202 | 202 |
template <typename Graph> |
| 203 | 203 |
struct CountNodesSelector< |
| 204 | 204 |
Graph, typename |
| 205 | 205 |
enable_if<typename Graph::NodeNumTag, void>::type> |
| 206 | 206 |
{
|
| 207 | 207 |
static int count(const Graph &g) {
|
| 208 | 208 |
return g.nodeNum(); |
| 209 | 209 |
} |
| 210 | 210 |
}; |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
/// \brief Function to count the nodes in the graph. |
| 214 | 214 |
/// |
| 215 | 215 |
/// This function counts the nodes in the graph. |
| 216 | 216 |
/// The complexity of the function is O(n) but for some |
| 217 | 217 |
/// graph structures it is specialized to run in O(1). |
| 218 | 218 |
/// |
| 219 | 219 |
/// If the graph contains a \e nodeNum() member function and a |
| 220 | 220 |
/// \e NodeNumTag tag then this function calls directly the member |
| 221 | 221 |
/// function to query the cardinality of the node set. |
| 222 | 222 |
template <typename Graph> |
| 223 | 223 |
inline int countNodes(const Graph& g) {
|
| 224 | 224 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
| 225 | 225 |
} |
| 226 | 226 |
|
| 227 | 227 |
// Arc counting: |
| 228 | 228 |
|
| 229 | 229 |
namespace _graph_utils_bits {
|
| 230 | 230 |
|
| 231 | 231 |
template <typename Graph, typename Enable = void> |
| 232 | 232 |
struct CountArcsSelector {
|
| 233 | 233 |
static int count(const Graph &g) {
|
| 234 | 234 |
return countItems<Graph, typename Graph::Arc>(g); |
| 235 | 235 |
} |
| 236 | 236 |
}; |
| 237 | 237 |
|
| 238 | 238 |
template <typename Graph> |
| 239 | 239 |
struct CountArcsSelector< |
| 240 | 240 |
Graph, |
| 241 | 241 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
| 242 | 242 |
{
|
| 243 | 243 |
static int count(const Graph &g) {
|
| 244 | 244 |
return g.arcNum(); |
| 245 | 245 |
} |
| 246 | 246 |
}; |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 | 249 |
/// \brief Function to count the arcs in the graph. |
| 250 | 250 |
/// |
| 251 | 251 |
/// This function counts the arcs in the graph. |
| 252 | 252 |
/// The complexity of the function is O(e) but for some |
| 253 | 253 |
/// graph structures it is specialized to run in O(1). |
| 254 | 254 |
/// |
| 255 | 255 |
/// If the graph contains a \e arcNum() member function and a |
| 256 | 256 |
/// \e EdgeNumTag tag then this function calls directly the member |
| 257 | 257 |
/// function to query the cardinality of the arc set. |
| 258 | 258 |
template <typename Graph> |
| 259 | 259 |
inline int countArcs(const Graph& g) {
|
| 260 | 260 |
return _graph_utils_bits::CountArcsSelector<Graph>::count(g); |
| 261 | 261 |
} |
| 262 | 262 |
|
| 263 | 263 |
// Edge counting: |
| 264 | 264 |
namespace _graph_utils_bits {
|
| 265 | 265 |
|
| 266 | 266 |
template <typename Graph, typename Enable = void> |
| 267 | 267 |
struct CountEdgesSelector {
|
| 268 | 268 |
static int count(const Graph &g) {
|
| 269 | 269 |
return countItems<Graph, typename Graph::Edge>(g); |
| 270 | 270 |
} |
| 271 | 271 |
}; |
| 272 | 272 |
|
| 273 | 273 |
template <typename Graph> |
| 274 | 274 |
struct CountEdgesSelector< |
| 275 | 275 |
Graph, |
| 276 | 276 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
| 277 | 277 |
{
|
| 278 | 278 |
static int count(const Graph &g) {
|
| 279 | 279 |
return g.edgeNum(); |
| 280 | 280 |
} |
| 281 | 281 |
}; |
| 282 | 282 |
} |
| 283 | 283 |
|
| 284 | 284 |
/// \brief Function to count the edges in the graph. |
| 285 | 285 |
/// |
| 286 | 286 |
/// This function counts the edges in the graph. |
| 287 | 287 |
/// The complexity of the function is O(m) but for some |
| 288 | 288 |
/// graph structures it is specialized to run in O(1). |
| 289 | 289 |
/// |
| 290 | 290 |
/// If the graph contains a \e edgeNum() member function and a |
| 291 | 291 |
/// \e EdgeNumTag tag then this function calls directly the member |
| 292 | 292 |
/// function to query the cardinality of the edge set. |
| 293 | 293 |
template <typename Graph> |
| 294 | 294 |
inline int countEdges(const Graph& g) {
|
| 295 | 295 |
return _graph_utils_bits::CountEdgesSelector<Graph>::count(g); |
| 296 | 296 |
|
| 297 | 297 |
} |
| 298 | 298 |
|
| 299 | 299 |
|
| 300 | 300 |
template <typename Graph, typename DegIt> |
| 301 | 301 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
| 302 | 302 |
int num = 0; |
| 303 | 303 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
| 304 | 304 |
++num; |
| 305 | 305 |
} |
| 306 | 306 |
return num; |
| 307 | 307 |
} |
| 308 | 308 |
|
| 309 | 309 |
/// \brief Function to count the number of the out-arcs from node \c n. |
| 310 | 310 |
/// |
| 311 | 311 |
/// This function counts the number of the out-arcs from node \c n |
| 312 | 312 |
/// in the graph. |
| 313 | 313 |
template <typename Graph> |
| 314 | 314 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) {
|
| 315 | 315 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n); |
| 316 | 316 |
} |
| 317 | 317 |
|
| 318 | 318 |
/// \brief Function to count the number of the in-arcs to node \c n. |
| 319 | 319 |
/// |
| 320 | 320 |
/// This function counts the number of the in-arcs to node \c n |
| 321 | 321 |
/// in the graph. |
| 322 | 322 |
template <typename Graph> |
| 323 | 323 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) {
|
| 324 | 324 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n); |
| 325 | 325 |
} |
| 326 | 326 |
|
| 327 | 327 |
/// \brief Function to count the number of the inc-edges to node \c n. |
| 328 | 328 |
/// |
| 329 | 329 |
/// This function counts the number of the inc-edges to node \c n |
| 330 | 330 |
/// in the graph. |
| 331 | 331 |
template <typename Graph> |
| 332 | 332 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
|
| 333 | 333 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
| 334 | 334 |
} |
| 335 | 335 |
|
| 336 | 336 |
namespace _graph_utils_bits {
|
| 337 | 337 |
|
| 338 | 338 |
template <typename Graph, typename Enable = void> |
| 339 | 339 |
struct FindArcSelector {
|
| 340 | 340 |
typedef typename Graph::Node Node; |
| 341 | 341 |
typedef typename Graph::Arc Arc; |
| 342 | 342 |
static Arc find(const Graph &g, Node u, Node v, Arc e) {
|
| 343 | 343 |
if (e == INVALID) {
|
| 344 | 344 |
g.firstOut(e, u); |
| 345 | 345 |
} else {
|
| 346 | 346 |
g.nextOut(e); |
| 347 | 347 |
} |
| 348 | 348 |
while (e != INVALID && g.target(e) != v) {
|
| 349 | 349 |
g.nextOut(e); |
| 350 | 350 |
} |
| 351 | 351 |
return e; |
| 352 | 352 |
} |
| 353 | 353 |
}; |
| 354 | 354 |
|
| 355 | 355 |
template <typename Graph> |
| 356 | 356 |
struct FindArcSelector< |
| 357 | 357 |
Graph, |
| 358 | 358 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
| 359 | 359 |
{
|
| 360 | 360 |
typedef typename Graph::Node Node; |
| 361 | 361 |
typedef typename Graph::Arc Arc; |
| 362 | 362 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) {
|
| 363 | 363 |
return g.findArc(u, v, prev); |
| 364 | 364 |
} |
| 365 | 365 |
}; |
| 366 | 366 |
} |
| 367 | 367 |
|
| 368 | 368 |
/// \brief Finds an arc between two nodes of a graph. |
| 369 | 369 |
/// |
| 370 | 370 |
/// Finds an arc from node \c u to node \c v in graph \c g. |
| 371 | 371 |
/// |
| 372 | 372 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 373 | 373 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
| 374 | 374 |
/// the next arc from \c u to \c v after \c prev. |
| 375 | 375 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
| 376 | 376 |
/// |
| 377 | 377 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
| 378 | 378 |
///\code |
| 379 | 379 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
|
| 380 | 380 |
/// ... |
| 381 | 381 |
/// } |
| 382 | 382 |
///\endcode |
| 383 | 383 |
/// |
| 384 | 384 |
///\sa ArcLookUp |
| 385 | 385 |
///\sa AllArcLookUp |
| 386 | 386 |
///\sa DynArcLookUp |
| 387 | 387 |
///\sa ConArcIt |
| 388 | 388 |
template <typename Graph> |
| 389 | 389 |
inline typename Graph::Arc |
| 390 | 390 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 391 | 391 |
typename Graph::Arc prev = INVALID) {
|
| 392 | 392 |
return _graph_utils_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
| 393 | 393 |
} |
| 394 | 394 |
|
| 395 | 395 |
/// \brief Iterator for iterating on arcs connected the same nodes. |
| 396 | 396 |
/// |
| 397 | 397 |
/// Iterator for iterating on arcs connected the same nodes. It is |
| 398 | 398 |
/// higher level interface for the findArc() function. You can |
| 399 | 399 |
/// use it the following way: |
| 400 | 400 |
///\code |
| 401 | 401 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 402 | 402 |
/// ... |
| 403 | 403 |
/// } |
| 404 | 404 |
///\endcode |
| 405 | 405 |
/// |
| 406 | 406 |
///\sa findArc() |
| 407 | 407 |
///\sa ArcLookUp |
| 408 | 408 |
///\sa AllArcLookUp |
| 409 | 409 |
///\sa DynArcLookUp |
| 410 | 410 |
/// |
| 411 | 411 |
/// \author Balazs Dezso |
| 412 | 412 |
template <typename _Graph> |
| 413 | 413 |
class ConArcIt : public _Graph::Arc {
|
| 414 | 414 |
public: |
| 415 | 415 |
|
| 416 | 416 |
typedef _Graph Graph; |
| 417 | 417 |
typedef typename Graph::Arc Parent; |
| 418 | 418 |
|
| 419 | 419 |
typedef typename Graph::Arc Arc; |
| 420 | 420 |
typedef typename Graph::Node Node; |
| 421 | 421 |
|
| 422 | 422 |
/// \brief Constructor. |
| 423 | 423 |
/// |
| 424 | 424 |
/// Construct a new ConArcIt iterating on the arcs which |
| 425 | 425 |
/// connects the \c u and \c v node. |
| 426 | 426 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 427 | 427 |
Parent::operator=(findArc(_graph, u, v)); |
| 428 | 428 |
} |
| 429 | 429 |
|
| 430 | 430 |
/// \brief Constructor. |
| 431 | 431 |
/// |
| 432 | 432 |
/// Construct a new ConArcIt which continues the iterating from |
| 433 | 433 |
/// the \c e arc. |
| 434 | 434 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
|
| 435 | 435 |
|
| 436 | 436 |
/// \brief Increment operator. |
| 437 | 437 |
/// |
| 438 | 438 |
/// It increments the iterator and gives back the next arc. |
| 439 | 439 |
ConArcIt& operator++() {
|
| 440 | 440 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
| 441 | 441 |
_graph.target(*this), *this)); |
| 442 | 442 |
return *this; |
| 443 | 443 |
} |
| 444 | 444 |
private: |
| 445 | 445 |
const Graph& _graph; |
| 446 | 446 |
}; |
| 447 | 447 |
|
| 448 | 448 |
namespace _graph_utils_bits {
|
| 449 | 449 |
|
| 450 | 450 |
template <typename Graph, typename Enable = void> |
| 451 | 451 |
struct FindEdgeSelector {
|
| 452 | 452 |
typedef typename Graph::Node Node; |
| 453 | 453 |
typedef typename Graph::Edge Edge; |
| 454 | 454 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
| 455 | 455 |
bool b; |
| 456 | 456 |
if (u != v) {
|
| 457 | 457 |
if (e == INVALID) {
|
| 458 | 458 |
g.firstInc(e, b, u); |
| 459 | 459 |
} else {
|
| 460 | 460 |
b = g.source(e) == u; |
| 461 | 461 |
g.nextInc(e, b); |
| 462 | 462 |
} |
| 463 | 463 |
while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
|
| 464 | 464 |
g.nextInc(e, b); |
| 465 | 465 |
} |
| 466 | 466 |
} else {
|
| 467 | 467 |
if (e == INVALID) {
|
| 468 | 468 |
g.firstInc(e, b, u); |
| 469 | 469 |
} else {
|
| 470 | 470 |
b = true; |
| 471 | 471 |
g.nextInc(e, b); |
| 472 | 472 |
} |
| 473 | 473 |
while (e != INVALID && (!b || g.target(e) != v)) {
|
| 474 | 474 |
g.nextInc(e, b); |
| 475 | 475 |
} |
| 476 | 476 |
} |
| 477 | 477 |
return e; |
| 478 | 478 |
} |
| 479 | 479 |
}; |
| 480 | 480 |
|
| 481 | 481 |
template <typename Graph> |
| 482 | 482 |
struct FindEdgeSelector< |
| 483 | 483 |
Graph, |
| 484 | 484 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
| 485 | 485 |
{
|
| 486 | 486 |
typedef typename Graph::Node Node; |
| 487 | 487 |
typedef typename Graph::Edge Edge; |
| 488 | 488 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
| 489 | 489 |
return g.findEdge(u, v, prev); |
| 490 | 490 |
} |
| 491 | 491 |
}; |
| 492 | 492 |
} |
| 493 | 493 |
|
| 494 | 494 |
/// \brief Finds an edge between two nodes of a graph. |
| 495 | 495 |
/// |
| 496 | 496 |
/// Finds an edge from node \c u to node \c v in graph \c g. |
| 497 | 497 |
/// If the node \c u and node \c v is equal then each loop edge |
| 498 | 498 |
/// will be enumerated once. |
| 499 | 499 |
/// |
| 500 | 500 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 501 | 501 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
| 502 | 502 |
/// the next arc from \c u to \c v after \c prev. |
| 503 | 503 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
| 504 | 504 |
/// |
| 505 | 505 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
| 506 | 506 |
///\code |
| 507 | 507 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
| 508 | 508 |
/// e = findEdge(g,u,v,e)) {
|
| 509 | 509 |
/// ... |
| 510 | 510 |
/// } |
| 511 | 511 |
///\endcode |
| 512 | 512 |
/// |
| 513 | 513 |
///\sa ConArcIt |
| 514 | 514 |
|
| 515 | 515 |
template <typename Graph> |
| 516 | 516 |
inline typename Graph::Edge |
| 517 | 517 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 518 | 518 |
typename Graph::Edge p = INVALID) {
|
| 519 | 519 |
return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
| 520 | 520 |
} |
| 521 | 521 |
|
| 522 | 522 |
/// \brief Iterator for iterating on edges connected the same nodes. |
| 523 | 523 |
/// |
| 524 | 524 |
/// Iterator for iterating on edges connected the same nodes. It is |
| 525 | 525 |
/// higher level interface for the findEdge() function. You can |
| 526 | 526 |
/// use it the following way: |
| 527 | 527 |
///\code |
| 528 | 528 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 529 | 529 |
/// ... |
| 530 | 530 |
/// } |
| 531 | 531 |
///\endcode |
| 532 | 532 |
/// |
| 533 | 533 |
///\sa findEdge() |
| 534 | 534 |
/// |
| 535 | 535 |
/// \author Balazs Dezso |
| 536 | 536 |
template <typename _Graph> |
| 537 | 537 |
class ConEdgeIt : public _Graph::Edge {
|
| 538 | 538 |
public: |
| 539 | 539 |
|
| 540 | 540 |
typedef _Graph Graph; |
| 541 | 541 |
typedef typename Graph::Edge Parent; |
| 542 | 542 |
|
| 543 | 543 |
typedef typename Graph::Edge Edge; |
| 544 | 544 |
typedef typename Graph::Node Node; |
| 545 | 545 |
|
| 546 | 546 |
/// \brief Constructor. |
| 547 | 547 |
/// |
| 548 | 548 |
/// Construct a new ConEdgeIt iterating on the edges which |
| 549 | 549 |
/// connects the \c u and \c v node. |
| 550 | 550 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 551 | 551 |
Parent::operator=(findEdge(_graph, u, v)); |
| 552 | 552 |
} |
| 553 | 553 |
|
| 554 | 554 |
/// \brief Constructor. |
| 555 | 555 |
/// |
| 556 | 556 |
/// Construct a new ConEdgeIt which continues the iterating from |
| 557 | 557 |
/// the \c e edge. |
| 558 | 558 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
|
| 559 | 559 |
|
| 560 | 560 |
/// \brief Increment operator. |
| 561 | 561 |
/// |
| 562 | 562 |
/// It increments the iterator and gives back the next edge. |
| 563 | 563 |
ConEdgeIt& operator++() {
|
| 564 | 564 |
Parent::operator=(findEdge(_graph, _graph.source(*this), |
| 565 | 565 |
_graph.target(*this), *this)); |
| 566 | 566 |
return *this; |
| 567 | 567 |
} |
| 568 | 568 |
private: |
| 569 | 569 |
const Graph& _graph; |
| 570 | 570 |
}; |
| 571 | 571 |
|
| 572 | 572 |
namespace _graph_utils_bits {
|
| 573 | 573 |
|
| 574 | 574 |
template <typename Digraph, typename Item, typename RefMap> |
| 575 | 575 |
class MapCopyBase {
|
| 576 | 576 |
public: |
| 577 | 577 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
| 578 | 578 |
|
| 579 | 579 |
virtual ~MapCopyBase() {}
|
| 580 | 580 |
}; |
| 581 | 581 |
|
| 582 | 582 |
template <typename Digraph, typename Item, typename RefMap, |
| 583 | 583 |
typename ToMap, typename FromMap> |
| 584 | 584 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 585 | 585 |
public: |
| 586 | 586 |
|
| 587 | 587 |
MapCopy(ToMap& tmap, const FromMap& map) |
| 588 | 588 |
: _tmap(tmap), _map(map) {}
|
| 589 | 589 |
|
| 590 | 590 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 591 | 591 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 592 | 592 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 593 | 593 |
_tmap.set(refMap[it], _map[it]); |
| 594 | 594 |
} |
| 595 | 595 |
} |
| 596 | 596 |
|
| 597 | 597 |
private: |
| 598 | 598 |
ToMap& _tmap; |
| 599 | 599 |
const FromMap& _map; |
| 600 | 600 |
}; |
| 601 | 601 |
|
| 602 | 602 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
| 603 | 603 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 604 | 604 |
public: |
| 605 | 605 |
|
| 606 | 606 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
|
| 607 | 607 |
|
| 608 | 608 |
virtual void copy(const Digraph&, const RefMap& refMap) {
|
| 609 | 609 |
_it = refMap[_item]; |
| 610 | 610 |
} |
| 611 | 611 |
|
| 612 | 612 |
private: |
| 613 | 613 |
It& _it; |
| 614 | 614 |
Item _item; |
| 615 | 615 |
}; |
| 616 | 616 |
|
| 617 | 617 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
| 618 | 618 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 619 | 619 |
public: |
| 620 | 620 |
|
| 621 | 621 |
RefCopy(Ref& map) : _map(map) {}
|
| 622 | 622 |
|
| 623 | 623 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 624 | 624 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 625 | 625 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 626 | 626 |
_map.set(it, refMap[it]); |
| 627 | 627 |
} |
| 628 | 628 |
} |
| 629 | 629 |
|
| 630 | 630 |
private: |
| 631 | 631 |
Ref& _map; |
| 632 | 632 |
}; |
| 633 | 633 |
|
| 634 | 634 |
template <typename Digraph, typename Item, typename RefMap, |
| 635 | 635 |
typename CrossRef> |
| 636 | 636 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 637 | 637 |
public: |
| 638 | 638 |
|
| 639 | 639 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
| 640 | 640 |
|
| 641 | 641 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 642 | 642 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 643 | 643 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 644 | 644 |
_cmap.set(refMap[it], it); |
| 645 | 645 |
} |
| 646 | 646 |
} |
| 647 | 647 |
|
| 648 | 648 |
private: |
| 649 | 649 |
CrossRef& _cmap; |
| 650 | 650 |
}; |
| 651 | 651 |
|
| 652 | 652 |
template <typename Digraph, typename Enable = void> |
| 653 | 653 |
struct DigraphCopySelector {
|
| 654 | 654 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 655 | 655 |
static void copy(Digraph &to, const From& from, |
| 656 | 656 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 657 | 657 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 658 | 658 |
nodeRefMap[it] = to.addNode(); |
| 659 | 659 |
} |
| 660 | 660 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
| 661 | 661 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
| 662 | 662 |
nodeRefMap[from.target(it)]); |
| 663 | 663 |
} |
| 664 | 664 |
} |
| 665 | 665 |
}; |
| 666 | 666 |
|
| 667 | 667 |
template <typename Digraph> |
| 668 | 668 |
struct DigraphCopySelector< |
| 669 | 669 |
Digraph, |
| 670 | 670 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
| 671 | 671 |
{
|
| 672 | 672 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 673 | 673 |
static void copy(Digraph &to, const From& from, |
| 674 | 674 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 675 | 675 |
to.build(from, nodeRefMap, arcRefMap); |
| 676 | 676 |
} |
| 677 | 677 |
}; |
| 678 | 678 |
|
| 679 | 679 |
template <typename Graph, typename Enable = void> |
0 comments (0 inline)