1.1 --- a/src/work/bfs_iterator.hh Sat Apr 03 14:22:33 2004 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,836 +0,0 @@
1.4 -#ifndef BFS_ITERATOR_HH
1.5 -#define BFS_ITERATOR_HH
1.6 -
1.7 -#include <queue>
1.8 -#include <stack>
1.9 -#include <utility>
1.10 -#include <graph_wrapper.h>
1.11 -
1.12 -namespace hugo {
1.13 -
1.14 - template <typename Graph>
1.15 - struct bfs {
1.16 - typedef typename Graph::NodeIt NodeIt;
1.17 - typedef typename Graph::EdgeIt EdgeIt;
1.18 - typedef typename Graph::EachNodeIt EachNodeIt;
1.19 - typedef typename Graph::OutEdgeIt OutEdgeIt;
1.20 - Graph& G;
1.21 - NodeIt s;
1.22 - typename Graph::NodeMap<bool> reached;
1.23 - typename Graph::NodeMap<EdgeIt> pred;
1.24 - typename Graph::NodeMap<int> dist;
1.25 - std::queue<NodeIt> bfs_queue;
1.26 - bfs(Graph& _G, NodeIt _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) {
1.27 - bfs_queue.push(s);
1.28 - for(EachNodeIt i=G.template first<EachNodeIt>(); i.valid(); ++i)
1.29 - reached.set(i, false);
1.30 - reached.set(s, true);
1.31 - dist.set(s, 0);
1.32 - }
1.33 -
1.34 - void run() {
1.35 - while (!bfs_queue.empty()) {
1.36 - NodeIt v=bfs_queue.front();
1.37 - OutEdgeIt e=G.template first<OutEdgeIt>(v);
1.38 - bfs_queue.pop();
1.39 - for( ; e.valid(); ++e) {
1.40 - NodeIt w=G.bNode(e);
1.41 - std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl;
1.42 - if (!reached.get(w)) {
1.43 - std::cout << G.id(w) << " is newly reached :-)" << std::endl;
1.44 - bfs_queue.push(w);
1.45 - dist.set(w, dist.get(v)+1);
1.46 - pred.set(w, e);
1.47 - reached.set(w, true);
1.48 - } else {
1.49 - std::cout << G.id(w) << " is already reached" << std::endl;
1.50 - }
1.51 - }
1.52 - }
1.53 - }
1.54 - };
1.55 -
1.56 - template <typename Graph>
1.57 - struct bfs_visitor {
1.58 - typedef typename Graph::NodeIt NodeIt;
1.59 - typedef typename Graph::EdgeIt EdgeIt;
1.60 - typedef typename Graph::OutEdgeIt OutEdgeIt;
1.61 - Graph& G;
1.62 - bfs_visitor(Graph& _G) : G(_G) { }
1.63 - void at_previously_reached(OutEdgeIt& e) {
1.64 - //NodeIt v=G.aNode(e);
1.65 - NodeIt w=G.bNode(e);
1.66 - std::cout << G.id(w) << " is already reached" << std::endl;
1.67 - }
1.68 - void at_newly_reached(OutEdgeIt& e) {
1.69 - //NodeIt v=G.aNode(e);
1.70 - NodeIt w=G.bNode(e);
1.71 - std::cout << G.id(w) << " is newly reached :-)" << std::endl;
1.72 - }
1.73 - };
1.74 -
1.75 - template <typename Graph, typename ReachedMap, typename visitor_type>
1.76 - struct bfs_iterator {
1.77 - typedef typename Graph::NodeIt NodeIt;
1.78 - typedef typename Graph::EdgeIt EdgeIt;
1.79 - typedef typename Graph::OutEdgeIt OutEdgeIt;
1.80 - Graph& G;
1.81 - std::queue<OutEdgeIt>& bfs_queue;
1.82 - ReachedMap& reached;
1.83 - visitor_type& visitor;
1.84 - void process() {
1.85 - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.86 - if (bfs_queue.empty()) return;
1.87 - OutEdgeIt e=bfs_queue.front();
1.88 - //NodeIt v=G.aNode(e);
1.89 - NodeIt w=G.bNode(e);
1.90 - if (!reached.get(w)) {
1.91 - visitor.at_newly_reached(e);
1.92 - bfs_queue.push(G.template first<OutEdgeIt>(w));
1.93 - reached.set(w, true);
1.94 - } else {
1.95 - visitor.at_previously_reached(e);
1.96 - }
1.97 - }
1.98 - bfs_iterator(Graph& _G, std::queue<OutEdgeIt>& _bfs_queue, ReachedMap& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) {
1.99 - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.100 - valid();
1.101 - }
1.102 - bfs_iterator<Graph, ReachedMap, visitor_type>& operator++() {
1.103 - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.104 - //if (bfs_queue.empty()) return *this;
1.105 - if (!valid()) return *this;
1.106 - ++(bfs_queue.front());
1.107 - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.108 - valid();
1.109 - return *this;
1.110 - }
1.111 - //void next() {
1.112 - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.113 - // if (bfs_queue.empty()) return;
1.114 - // ++(bfs_queue.front());
1.115 - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.116 - //}
1.117 - bool valid() {
1.118 - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.119 - if (bfs_queue.empty()) return false; else return true;
1.120 - }
1.121 - //bool finished() {
1.122 - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.123 - // if (bfs_queue.empty()) return true; else return false;
1.124 - //}
1.125 - operator EdgeIt () { return bfs_queue.front(); }
1.126 -
1.127 - };
1.128 -
1.129 - template <typename Graph, typename ReachedMap>
1.130 - struct bfs_iterator1 {
1.131 - typedef typename Graph::NodeIt NodeIt;
1.132 - typedef typename Graph::EdgeIt EdgeIt;
1.133 - typedef typename Graph::OutEdgeIt OutEdgeIt;
1.134 - Graph& G;
1.135 - std::queue<OutEdgeIt>& bfs_queue;
1.136 - ReachedMap& reached;
1.137 - bool _newly_reached;
1.138 - bfs_iterator1(Graph& _G, std::queue<OutEdgeIt>& _bfs_queue, ReachedMap& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) {
1.139 - valid();
1.140 - if (!bfs_queue.empty() && bfs_queue.front().valid()) {
1.141 - OutEdgeIt e=bfs_queue.front();
1.142 - NodeIt w=G.bNode(e);
1.143 - if (!reached.get(w)) {
1.144 - bfs_queue.push(G.template first<OutEdgeIt>(w));
1.145 - reached.set(w, true);
1.146 - _newly_reached=true;
1.147 - } else {
1.148 - _newly_reached=false;
1.149 - }
1.150 - }
1.151 - }
1.152 - bfs_iterator1<Graph, ReachedMap>& operator++() {
1.153 - if (!valid()) return *this;
1.154 - ++(bfs_queue.front());
1.155 - valid();
1.156 - if (!bfs_queue.empty() && bfs_queue.front().valid()) {
1.157 - OutEdgeIt e=bfs_queue.front();
1.158 - NodeIt w=G.bNode(e);
1.159 - if (!reached.get(w)) {
1.160 - bfs_queue.push(G.template first<OutEdgeIt>(w));
1.161 - reached.set(w, true);
1.162 - _newly_reached=true;
1.163 - } else {
1.164 - _newly_reached=false;
1.165 - }
1.166 - }
1.167 - return *this;
1.168 - }
1.169 - bool valid() {
1.170 - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
1.171 - if (bfs_queue.empty()) return false; else return true;
1.172 - }
1.173 - operator OutEdgeIt() { return bfs_queue.front(); }
1.174 - //ize
1.175 - bool newly_reached() { return _newly_reached; }
1.176 -
1.177 - };
1.178 -
1.179 - template <typename Graph, typename OutEdgeIt, typename ReachedMap>
1.180 - struct BfsIterator {
1.181 - typedef typename Graph::NodeIt NodeIt;
1.182 - Graph& G;
1.183 - std::queue<OutEdgeIt>& bfs_queue;
1.184 - ReachedMap& reached;
1.185 - bool b_node_newly_reached;
1.186 - OutEdgeIt actual_edge;
1.187 - BfsIterator(Graph& _G,
1.188 - std::queue<OutEdgeIt>& _bfs_queue,
1.189 - ReachedMap& _reached) :
1.190 - G(_G), bfs_queue(_bfs_queue), reached(_reached) {
1.191 - actual_edge=bfs_queue.front();
1.192 - if (actual_edge.valid()) {
1.193 - NodeIt w=G.bNode(actual_edge);
1.194 - if (!reached.get(w)) {
1.195 - bfs_queue.push(G.firstOutEdge(w));
1.196 - reached.set(w, true);
1.197 - b_node_newly_reached=true;
1.198 - } else {
1.199 - b_node_newly_reached=false;
1.200 - }
1.201 - }
1.202 - }
1.203 - BfsIterator<Graph, OutEdgeIt, ReachedMap>&
1.204 - operator++() {
1.205 - if (bfs_queue.front().valid()) {
1.206 - ++(bfs_queue.front());
1.207 - actual_edge=bfs_queue.front();
1.208 - if (actual_edge.valid()) {
1.209 - NodeIt w=G.bNode(actual_edge);
1.210 - if (!reached.get(w)) {
1.211 - bfs_queue.push(G.firstOutEdge(w));
1.212 - reached.set(w, true);
1.213 - b_node_newly_reached=true;
1.214 - } else {
1.215 - b_node_newly_reached=false;
1.216 - }
1.217 - }
1.218 - } else {
1.219 - bfs_queue.pop();
1.220 - actual_edge=bfs_queue.front();
1.221 - if (actual_edge.valid()) {
1.222 - NodeIt w=G.bNode(actual_edge);
1.223 - if (!reached.get(w)) {
1.224 - bfs_queue.push(G.firstOutEdge(w));
1.225 - reached.set(w, true);
1.226 - b_node_newly_reached=true;
1.227 - } else {
1.228 - b_node_newly_reached=false;
1.229 - }
1.230 - }
1.231 - }
1.232 - return *this;
1.233 - }
1.234 - bool finished() { return bfs_queue.empty(); }
1.235 - operator OutEdgeIt () { return actual_edge; }
1.236 - bool bNodeIsNewlyReached() { return b_node_newly_reached; }
1.237 - bool aNodeIsExamined() { return !(actual_edge.valid()); }
1.238 - };
1.239 -
1.240 -
1.241 - template <typename Graph, typename OutEdgeIt, typename ReachedMap>
1.242 - struct DfsIterator {
1.243 - typedef typename Graph::NodeIt NodeIt;
1.244 - Graph& G;
1.245 - std::stack<OutEdgeIt>& bfs_queue;
1.246 - ReachedMap& reached;
1.247 - bool b_node_newly_reached;
1.248 - OutEdgeIt actual_edge;
1.249 - DfsIterator(Graph& _G,
1.250 - std::stack<OutEdgeIt>& _bfs_queue,
1.251 - ReachedMap& _reached) :
1.252 - G(_G), bfs_queue(_bfs_queue), reached(_reached) {
1.253 - actual_edge=bfs_queue.top();
1.254 - if (actual_edge.valid()) {
1.255 - NodeIt w=G.bNode(actual_edge);
1.256 - if (!reached.get(w)) {
1.257 - bfs_queue.push(G.firstOutEdge(w));
1.258 - reached.set(w, true);
1.259 - b_node_newly_reached=true;
1.260 - } else {
1.261 - ++(bfs_queue.top());
1.262 - b_node_newly_reached=false;
1.263 - }
1.264 - } else {
1.265 - bfs_queue.pop();
1.266 - }
1.267 - }
1.268 - DfsIterator<Graph, OutEdgeIt, ReachedMap>&
1.269 - operator++() {
1.270 - actual_edge=bfs_queue.top();
1.271 - if (actual_edge.valid()) {
1.272 - NodeIt w=G.bNode(actual_edge);
1.273 - if (!reached.get(w)) {
1.274 - bfs_queue.push(G.firstOutEdge(w));
1.275 - reached.set(w, true);
1.276 - b_node_newly_reached=true;
1.277 - } else {
1.278 - ++(bfs_queue.top());
1.279 - b_node_newly_reached=false;
1.280 - }
1.281 - } else {
1.282 - bfs_queue.pop();
1.283 - }
1.284 - return *this;
1.285 - }
1.286 - bool finished() { return bfs_queue.empty(); }
1.287 - operator OutEdgeIt () { return actual_edge; }
1.288 - bool bNodeIsNewlyReached() { return b_node_newly_reached; }
1.289 - bool aNodeIsExamined() { return !(actual_edge.valid()); }
1.290 - };
1.291 -
1.292 - template <typename Graph, typename OutEdgeIt, typename ReachedMap>
1.293 - struct BfsIterator1 {
1.294 - typedef typename Graph::NodeIt NodeIt;
1.295 - Graph& G;
1.296 - std::queue<OutEdgeIt>& bfs_queue;
1.297 - ReachedMap& reached;
1.298 - bool b_node_newly_reached;
1.299 - OutEdgeIt actual_edge;
1.300 - BfsIterator1(Graph& _G,
1.301 - std::queue<OutEdgeIt>& _bfs_queue,
1.302 - ReachedMap& _reached) :
1.303 - G(_G), bfs_queue(_bfs_queue), reached(_reached) {
1.304 - actual_edge=bfs_queue.front();
1.305 - if (actual_edge.valid()) {
1.306 - NodeIt w=G.bNode(actual_edge);
1.307 - if (!reached.get(w)) {
1.308 - bfs_queue.push(OutEdgeIt(G, w));
1.309 - reached.set(w, true);
1.310 - b_node_newly_reached=true;
1.311 - } else {
1.312 - b_node_newly_reached=false;
1.313 - }
1.314 - }
1.315 - }
1.316 - void next() {
1.317 - if (bfs_queue.front().valid()) {
1.318 - ++(bfs_queue.front());
1.319 - actual_edge=bfs_queue.front();
1.320 - if (actual_edge.valid()) {
1.321 - NodeIt w=G.bNode(actual_edge);
1.322 - if (!reached.get(w)) {
1.323 - bfs_queue.push(OutEdgeIt(G, w));
1.324 - reached.set(w, true);
1.325 - b_node_newly_reached=true;
1.326 - } else {
1.327 - b_node_newly_reached=false;
1.328 - }
1.329 - }
1.330 - } else {
1.331 - bfs_queue.pop();
1.332 - actual_edge=bfs_queue.front();
1.333 - if (actual_edge.valid()) {
1.334 - NodeIt w=G.bNode(actual_edge);
1.335 - if (!reached.get(w)) {
1.336 - bfs_queue.push(OutEdgeIt(G, w));
1.337 - reached.set(w, true);
1.338 - b_node_newly_reached=true;
1.339 - } else {
1.340 - b_node_newly_reached=false;
1.341 - }
1.342 - }
1.343 - }
1.344 - //return *this;
1.345 - }
1.346 - bool finished() { return bfs_queue.empty(); }
1.347 - operator OutEdgeIt () { return actual_edge; }
1.348 - bool bNodeIsNewlyReached() { return b_node_newly_reached; }
1.349 - bool aNodeIsExamined() { return !(actual_edge.valid()); }
1.350 - };
1.351 -
1.352 -
1.353 - template <typename Graph, typename OutEdgeIt, typename ReachedMap>
1.354 - struct DfsIterator1 {
1.355 - typedef typename Graph::NodeIt NodeIt;
1.356 - Graph& G;
1.357 - std::stack<OutEdgeIt>& bfs_queue;
1.358 - ReachedMap& reached;
1.359 - bool b_node_newly_reached;
1.360 - OutEdgeIt actual_edge;
1.361 - DfsIterator1(Graph& _G,
1.362 - std::stack<OutEdgeIt>& _bfs_queue,
1.363 - ReachedMap& _reached) :
1.364 - G(_G), bfs_queue(_bfs_queue), reached(_reached) {
1.365 - //actual_edge=bfs_queue.top();
1.366 - //if (actual_edge.valid()) {
1.367 - // NodeIt w=G.bNode(actual_edge);
1.368 - //if (!reached.get(w)) {
1.369 - // bfs_queue.push(OutEdgeIt(G, w));
1.370 - // reached.set(w, true);
1.371 - // b_node_newly_reached=true;
1.372 - //} else {
1.373 - // ++(bfs_queue.top());
1.374 - // b_node_newly_reached=false;
1.375 - //}
1.376 - //} else {
1.377 - // bfs_queue.pop();
1.378 - //}
1.379 - }
1.380 - void next() {
1.381 - actual_edge=bfs_queue.top();
1.382 - if (actual_edge.valid()) {
1.383 - NodeIt w=G.bNode(actual_edge);
1.384 - if (!reached.get(w)) {
1.385 - bfs_queue.push(OutEdgeIt(G, w));
1.386 - reached.set(w, true);
1.387 - b_node_newly_reached=true;
1.388 - } else {
1.389 - ++(bfs_queue.top());
1.390 - b_node_newly_reached=false;
1.391 - }
1.392 - } else {
1.393 - bfs_queue.pop();
1.394 - }
1.395 - //return *this;
1.396 - }
1.397 - bool finished() { return bfs_queue.empty(); }
1.398 - operator OutEdgeIt () { return actual_edge; }
1.399 - bool bNodeIsNewlyReached() { return b_node_newly_reached; }
1.400 - bool aNodeIsLeaved() { return !(actual_edge.valid()); }
1.401 - };
1.402 -
1.403 - template <typename Graph, typename OutEdgeIt, typename ReachedMap>
1.404 - class BfsIterator2 {
1.405 - typedef typename Graph::NodeIt NodeIt;
1.406 - const Graph& G;
1.407 - std::queue<OutEdgeIt> bfs_queue;
1.408 - ReachedMap reached;
1.409 - bool b_node_newly_reached;
1.410 - OutEdgeIt actual_edge;
1.411 - public:
1.412 - BfsIterator2(const Graph& _G) : G(_G), reached(G, false) { }
1.413 - void pushAndSetReached(NodeIt s) {
1.414 - reached.set(s, true);
1.415 - if (bfs_queue.empty()) {
1.416 - bfs_queue.push(G.template first<OutEdgeIt>(s));
1.417 - actual_edge=bfs_queue.front();
1.418 - if (actual_edge.valid()) {
1.419 - NodeIt w=G.bNode(actual_edge);
1.420 - if (!reached.get(w)) {
1.421 - bfs_queue.push(G.template first<OutEdgeIt>(w));
1.422 - reached.set(w, true);
1.423 - b_node_newly_reached=true;
1.424 - } else {
1.425 - b_node_newly_reached=false;
1.426 - }
1.427 - } //else {
1.428 - //}
1.429 - } else {
1.430 - bfs_queue.push(G.template first<OutEdgeIt>(s));
1.431 - }
1.432 - }
1.433 - BfsIterator2<Graph, OutEdgeIt, ReachedMap>&
1.434 - operator++() {
1.435 - if (bfs_queue.front().valid()) {
1.436 - ++(bfs_queue.front());
1.437 - actual_edge=bfs_queue.front();
1.438 - if (actual_edge.valid()) {
1.439 - NodeIt w=G.bNode(actual_edge);
1.440 - if (!reached.get(w)) {
1.441 - bfs_queue.push(G.template first<OutEdgeIt>(w));
1.442 - reached.set(w, true);
1.443 - b_node_newly_reached=true;
1.444 - } else {
1.445 - b_node_newly_reached=false;
1.446 - }
1.447 - }
1.448 - } else {
1.449 - bfs_queue.pop();
1.450 - if (!bfs_queue.empty()) {
1.451 - actual_edge=bfs_queue.front();
1.452 - if (actual_edge.valid()) {
1.453 - NodeIt w=G.bNode(actual_edge);
1.454 - if (!reached.get(w)) {
1.455 - bfs_queue.push(G.template first<OutEdgeIt>(w));
1.456 - reached.set(w, true);
1.457 - b_node_newly_reached=true;
1.458 - } else {
1.459 - b_node_newly_reached=false;
1.460 - }
1.461 - }
1.462 - }
1.463 - }
1.464 - return *this;
1.465 - }
1.466 - bool finished() const { return bfs_queue.empty(); }
1.467 - operator OutEdgeIt () const { return actual_edge; }
1.468 - bool isBNodeNewlyReached() const { return b_node_newly_reached; }
1.469 - bool isANodeExamined() const { return !(actual_edge.valid()); }
1.470 - const ReachedMap& getReachedMap() const { return reached; }
1.471 - const std::queue<OutEdgeIt>& getBfsQueue() const { return bfs_queue; }
1.472 - };
1.473 -
1.474 -
1.475 - template <typename Graph, typename OutEdgeIt, typename ReachedMap>
1.476 - class BfsIterator3 {
1.477 - typedef typename Graph::NodeIt NodeIt;
1.478 - const Graph& G;
1.479 - std::queue< std::pair<NodeIt, OutEdgeIt> > bfs_queue;
1.480 - ReachedMap reached;
1.481 - bool b_node_newly_reached;
1.482 - OutEdgeIt actual_edge;
1.483 - public:
1.484 - BfsIterator3(const Graph& _G) : G(_G), reached(G, false) { }
1.485 - void pushAndSetReached(NodeIt s) {
1.486 - reached.set(s, true);
1.487 - if (bfs_queue.empty()) {
1.488 - bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(s, G.template first<OutEdgeIt>(s)));
1.489 - actual_edge=bfs_queue.front().second;
1.490 - if (actual_edge.valid()) {
1.491 - NodeIt w=G.bNode(actual_edge);
1.492 - if (!reached.get(w)) {
1.493 - bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
1.494 - reached.set(w, true);
1.495 - b_node_newly_reached=true;
1.496 - } else {
1.497 - b_node_newly_reached=false;
1.498 - }
1.499 - } //else {
1.500 - //}
1.501 - } else {
1.502 - bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(s, G.template first<OutEdgeIt>(s)));
1.503 - }
1.504 - }
1.505 - BfsIterator3<Graph, OutEdgeIt, ReachedMap>&
1.506 - operator++() {
1.507 - if (bfs_queue.front().second.valid()) {
1.508 - ++(bfs_queue.front().second);
1.509 - actual_edge=bfs_queue.front().second;
1.510 - if (actual_edge.valid()) {
1.511 - NodeIt w=G.bNode(actual_edge);
1.512 - if (!reached.get(w)) {
1.513 - bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
1.514 - reached.set(w, true);
1.515 - b_node_newly_reached=true;
1.516 - } else {
1.517 - b_node_newly_reached=false;
1.518 - }
1.519 - }
1.520 - } else {
1.521 - bfs_queue.pop();
1.522 - if (!bfs_queue.empty()) {
1.523 - actual_edge=bfs_queue.front().second;
1.524 - if (actual_edge.valid()) {
1.525 - NodeIt w=G.bNode(actual_edge);
1.526 - if (!reached.get(w)) {
1.527 - bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
1.528 - reached.set(w, true);
1.529 - b_node_newly_reached=true;
1.530 - } else {
1.531 - b_node_newly_reached=false;
1.532 - }
1.533 - }
1.534 - }
1.535 - }
1.536 - return *this;
1.537 - }
1.538 - bool finished() const { return bfs_queue.empty(); }
1.539 - operator OutEdgeIt () const { return actual_edge; }
1.540 - bool isBNodeNewlyReached() const { return b_node_newly_reached; }
1.541 - bool isANodeExamined() const { return !(actual_edge.valid()); }
1.542 - NodeIt aNode() const { return bfs_queue.front().first; }
1.543 - NodeIt bNode() const { return G.bNode(actual_edge); }
1.544 - const ReachedMap& getReachedMap() const { return reached; }
1.545 - //const std::queue< std::pair<NodeIt, OutEdgeIt> >& getBfsQueue() const { return bfs_queue; }
1.546 - };
1.547 -
1.548 -
1.549 - template <typename Graph, typename OutEdgeIt,
1.550 - typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
1.551 - class BfsIterator4 {
1.552 - typedef typename Graph::NodeIt NodeIt;
1.553 - const Graph& G;
1.554 - std::queue<NodeIt> bfs_queue;
1.555 - ReachedMap& reached;
1.556 - bool b_node_newly_reached;
1.557 - OutEdgeIt actual_edge;
1.558 - bool own_reached_map;
1.559 - public:
1.560 - BfsIterator4(const Graph& _G, ReachedMap& _reached) :
1.561 - G(_G), reached(_reached),
1.562 - own_reached_map(false) { }
1.563 - BfsIterator4(const Graph& _G) :
1.564 - G(_G), reached(*(new ReachedMap(G /*, false*/))),
1.565 - own_reached_map(true) { }
1.566 - ~BfsIterator4() { if (own_reached_map) delete &reached; }
1.567 - void pushAndSetReached(NodeIt s) {
1.568 - //std::cout << "mimi" << &reached << std::endl;
1.569 - reached.set(s, true);
1.570 - //std::cout << "mumus" << std::endl;
1.571 - if (bfs_queue.empty()) {
1.572 - //std::cout << "bibi1" << std::endl;
1.573 - bfs_queue.push(s);
1.574 - //std::cout << "zizi" << std::endl;
1.575 - G.getFirst(actual_edge, s);
1.576 - //std::cout << "kiki" << std::endl;
1.577 - if (G.valid(actual_edge)/*.valid()*/) {
1.578 - NodeIt w=G.bNode(actual_edge);
1.579 - if (!reached.get(w)) {
1.580 - bfs_queue.push(w);
1.581 - reached.set(w, true);
1.582 - b_node_newly_reached=true;
1.583 - } else {
1.584 - b_node_newly_reached=false;
1.585 - }
1.586 - }
1.587 - } else {
1.588 - //std::cout << "bibi2" << std::endl;
1.589 - bfs_queue.push(s);
1.590 - }
1.591 - }
1.592 - BfsIterator4<Graph, OutEdgeIt, ReachedMap>&
1.593 - operator++() {
1.594 - if (G.valid(actual_edge)/*.valid()*/) {
1.595 - /*++*/G.next(actual_edge);
1.596 - if (G.valid(actual_edge)/*.valid()*/) {
1.597 - NodeIt w=G.bNode(actual_edge);
1.598 - if (!reached.get(w)) {
1.599 - bfs_queue.push(w);
1.600 - reached.set(w, true);
1.601 - b_node_newly_reached=true;
1.602 - } else {
1.603 - b_node_newly_reached=false;
1.604 - }
1.605 - }
1.606 - } else {
1.607 - bfs_queue.pop();
1.608 - if (!bfs_queue.empty()) {
1.609 - G.getFirst(actual_edge, bfs_queue.front());
1.610 - if (G.valid(actual_edge)/*.valid()*/) {
1.611 - NodeIt w=G.bNode(actual_edge);
1.612 - if (!reached.get(w)) {
1.613 - bfs_queue.push(w);
1.614 - reached.set(w, true);
1.615 - b_node_newly_reached=true;
1.616 - } else {
1.617 - b_node_newly_reached=false;
1.618 - }
1.619 - }
1.620 - }
1.621 - }
1.622 - return *this;
1.623 - }
1.624 - bool finished() const { return bfs_queue.empty(); }
1.625 - operator OutEdgeIt () const { return actual_edge; }
1.626 - bool isBNodeNewlyReached() const { return b_node_newly_reached; }
1.627 - bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
1.628 - NodeIt aNode() const { return bfs_queue.front(); }
1.629 - NodeIt bNode() const { return G.bNode(actual_edge); }
1.630 - const ReachedMap& getReachedMap() const { return reached; }
1.631 - const std::queue<NodeIt>& getBfsQueue() const { return bfs_queue; }
1.632 - };
1.633 -
1.634 -
1.635 - template <typename GraphWrapper, /*typename OutEdgeIt,*/
1.636 - typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
1.637 - class BfsIterator5 {
1.638 - typedef typename GraphWrapper::NodeIt NodeIt;
1.639 - typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
1.640 - GraphWrapper G;
1.641 - std::queue<NodeIt> bfs_queue;
1.642 - ReachedMap& reached;
1.643 - bool b_node_newly_reached;
1.644 - OutEdgeIt actual_edge;
1.645 - bool own_reached_map;
1.646 - public:
1.647 - BfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) :
1.648 - G(_G), reached(_reached),
1.649 - own_reached_map(false) { }
1.650 - BfsIterator5(const GraphWrapper& _G) :
1.651 - G(_G), reached(*(new ReachedMap(G /*, false*/))),
1.652 - own_reached_map(true) { }
1.653 -// BfsIterator5(const typename GraphWrapper::BaseGraph& _G,
1.654 -// ReachedMap& _reached) :
1.655 -// G(_G), reached(_reached),
1.656 -// own_reached_map(false) { }
1.657 -// BfsIterator5(const typename GraphWrapper::BaseGraph& _G) :
1.658 -// G(_G), reached(*(new ReachedMap(G /*, false*/))),
1.659 -// own_reached_map(true) { }
1.660 - ~BfsIterator5() { if (own_reached_map) delete &reached; }
1.661 - void pushAndSetReached(NodeIt s) {
1.662 - reached.set(s, true);
1.663 - if (bfs_queue.empty()) {
1.664 - bfs_queue.push(s);
1.665 - G.getFirst(actual_edge, s);
1.666 - if (G.valid(actual_edge)/*.valid()*/) {
1.667 - NodeIt w=G.bNode(actual_edge);
1.668 - if (!reached.get(w)) {
1.669 - bfs_queue.push(w);
1.670 - reached.set(w, true);
1.671 - b_node_newly_reached=true;
1.672 - } else {
1.673 - b_node_newly_reached=false;
1.674 - }
1.675 - }
1.676 - } else {
1.677 - bfs_queue.push(s);
1.678 - }
1.679 - }
1.680 - BfsIterator5<GraphWrapper, /*OutEdgeIt,*/ ReachedMap>&
1.681 - operator++() {
1.682 - if (G.valid(actual_edge)/*.valid()*/) {
1.683 - /*++*/G.next(actual_edge);
1.684 - if (G.valid(actual_edge)/*.valid()*/) {
1.685 - NodeIt w=G.bNode(actual_edge);
1.686 - if (!reached.get(w)) {
1.687 - bfs_queue.push(w);
1.688 - reached.set(w, true);
1.689 - b_node_newly_reached=true;
1.690 - } else {
1.691 - b_node_newly_reached=false;
1.692 - }
1.693 - }
1.694 - } else {
1.695 - bfs_queue.pop();
1.696 - if (!bfs_queue.empty()) {
1.697 - G.getFirst(actual_edge, bfs_queue.front());
1.698 - if (G.valid(actual_edge)/*.valid()*/) {
1.699 - NodeIt w=G.bNode(actual_edge);
1.700 - if (!reached.get(w)) {
1.701 - bfs_queue.push(w);
1.702 - reached.set(w, true);
1.703 - b_node_newly_reached=true;
1.704 - } else {
1.705 - b_node_newly_reached=false;
1.706 - }
1.707 - }
1.708 - }
1.709 - }
1.710 - return *this;
1.711 - }
1.712 - bool finished() const { return bfs_queue.empty(); }
1.713 - operator OutEdgeIt () const { return actual_edge; }
1.714 - bool isBNodeNewlyReached() const { return b_node_newly_reached; }
1.715 - bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
1.716 - NodeIt aNode() const { return bfs_queue.front(); }
1.717 - NodeIt bNode() const { return G.bNode(actual_edge); }
1.718 - const ReachedMap& getReachedMap() const { return reached; }
1.719 - const std::queue<NodeIt>& getBfsQueue() const { return bfs_queue; }
1.720 - };
1.721 -
1.722 - template <typename Graph, typename OutEdgeIt,
1.723 - typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
1.724 - class DfsIterator4 {
1.725 - typedef typename Graph::NodeIt NodeIt;
1.726 - const Graph& G;
1.727 - std::stack<OutEdgeIt> dfs_stack;
1.728 - bool b_node_newly_reached;
1.729 - OutEdgeIt actual_edge;
1.730 - NodeIt actual_node;
1.731 - ReachedMap& reached;
1.732 - bool own_reached_map;
1.733 - public:
1.734 - DfsIterator4(const Graph& _G, ReachedMap& _reached) :
1.735 - G(_G), reached(_reached),
1.736 - own_reached_map(false) { }
1.737 - DfsIterator4(const Graph& _G) :
1.738 - G(_G), reached(*(new ReachedMap(G /*, false*/))),
1.739 - own_reached_map(true) { }
1.740 - ~DfsIterator4() { if (own_reached_map) delete &reached; }
1.741 - void pushAndSetReached(NodeIt s) {
1.742 - actual_node=s;
1.743 - reached.set(s, true);
1.744 - dfs_stack.push(G.template first<OutEdgeIt>(s));
1.745 - }
1.746 - DfsIterator4<Graph, OutEdgeIt, ReachedMap>&
1.747 - operator++() {
1.748 - actual_edge=dfs_stack.top();
1.749 - //actual_node=G.aNode(actual_edge);
1.750 - if (G.valid(actual_edge)/*.valid()*/) {
1.751 - NodeIt w=G.bNode(actual_edge);
1.752 - actual_node=w;
1.753 - if (!reached.get(w)) {
1.754 - dfs_stack.push(G.template first<OutEdgeIt>(w));
1.755 - reached.set(w, true);
1.756 - b_node_newly_reached=true;
1.757 - } else {
1.758 - actual_node=G.aNode(actual_edge);
1.759 - /*++*/G.next(dfs_stack.top());
1.760 - b_node_newly_reached=false;
1.761 - }
1.762 - } else {
1.763 - //actual_node=G.aNode(dfs_stack.top());
1.764 - dfs_stack.pop();
1.765 - }
1.766 - return *this;
1.767 - }
1.768 - bool finished() const { return dfs_stack.empty(); }
1.769 - operator OutEdgeIt () const { return actual_edge; }
1.770 - bool isBNodeNewlyReached() const { return b_node_newly_reached; }
1.771 - bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
1.772 - NodeIt aNode() const { return actual_node; /*FIXME*/}
1.773 - NodeIt bNode() const { return G.bNode(actual_edge); }
1.774 - const ReachedMap& getReachedMap() const { return reached; }
1.775 - const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
1.776 - };
1.777 -
1.778 - template <typename GraphWrapper, /*typename OutEdgeIt,*/
1.779 - typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
1.780 - class DfsIterator5 {
1.781 - typedef typename GraphWrapper::NodeIt NodeIt;
1.782 - typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
1.783 - GraphWrapper G;
1.784 - std::stack<OutEdgeIt> dfs_stack;
1.785 - bool b_node_newly_reached;
1.786 - OutEdgeIt actual_edge;
1.787 - NodeIt actual_node;
1.788 - ReachedMap& reached;
1.789 - bool own_reached_map;
1.790 - public:
1.791 - DfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) :
1.792 - G(_G), reached(_reached),
1.793 - own_reached_map(false) { }
1.794 - DfsIterator5(const GraphWrapper& _G) :
1.795 - G(_G), reached(*(new ReachedMap(G /*, false*/))),
1.796 - own_reached_map(true) { }
1.797 - ~DfsIterator5() { if (own_reached_map) delete &reached; }
1.798 - void pushAndSetReached(NodeIt s) {
1.799 - actual_node=s;
1.800 - reached.set(s, true);
1.801 - dfs_stack.push(G.template first<OutEdgeIt>(s));
1.802 - }
1.803 - DfsIterator5<GraphWrapper, /*OutEdgeIt,*/ ReachedMap>&
1.804 - operator++() {
1.805 - actual_edge=dfs_stack.top();
1.806 - //actual_node=G.aNode(actual_edge);
1.807 - if (G.valid(actual_edge)/*.valid()*/) {
1.808 - NodeIt w=G.bNode(actual_edge);
1.809 - actual_node=w;
1.810 - if (!reached.get(w)) {
1.811 - dfs_stack.push(G.template first<OutEdgeIt>(w));
1.812 - reached.set(w, true);
1.813 - b_node_newly_reached=true;
1.814 - } else {
1.815 - actual_node=G.aNode(actual_edge);
1.816 - /*++*/G.next(dfs_stack.top());
1.817 - b_node_newly_reached=false;
1.818 - }
1.819 - } else {
1.820 - //actual_node=G.aNode(dfs_stack.top());
1.821 - dfs_stack.pop();
1.822 - }
1.823 - return *this;
1.824 - }
1.825 - bool finished() const { return dfs_stack.empty(); }
1.826 - operator OutEdgeIt () const { return actual_edge; }
1.827 - bool isBNodeNewlyReached() const { return b_node_newly_reached; }
1.828 - bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
1.829 - NodeIt aNode() const { return actual_node; /*FIXME*/}
1.830 - NodeIt bNode() const { return G.bNode(actual_edge); }
1.831 - const ReachedMap& getReachedMap() const { return reached; }
1.832 - const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
1.833 - };
1.834 -
1.835 -
1.836 -
1.837 -} // namespace hugo
1.838 -
1.839 -#endif //BFS_ITERATOR_HH
2.1 --- a/src/work/edmonds_karp.hh Sat Apr 03 14:22:33 2004 +0000
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,677 +0,0 @@
2.4 -#ifndef EDMONDS_KARP_HH
2.5 -#define EDMONDS_KARP_HH
2.6 -
2.7 -#include <algorithm>
2.8 -#include <list>
2.9 -#include <iterator>
2.10 -
2.11 -#include <bfs_iterator.hh>
2.12 -//#include <time_measure.h>
2.13 -
2.14 -namespace hugo {
2.15 -
2.16 - template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
2.17 - class ResGraph {
2.18 - public:
2.19 - typedef typename Graph::NodeIt NodeIt;
2.20 - typedef typename Graph::EachNodeIt EachNodeIt;
2.21 - private:
2.22 - typedef typename Graph::SymEdgeIt OldSymEdgeIt;
2.23 - const Graph& G;
2.24 - FlowMap& flow;
2.25 - const CapacityMap& capacity;
2.26 - public:
2.27 - ResGraph(const Graph& _G, FlowMap& _flow,
2.28 - const CapacityMap& _capacity) :
2.29 - G(_G), flow(_flow), capacity(_capacity) { }
2.30 -
2.31 - class EdgeIt;
2.32 - class OutEdgeIt;
2.33 - friend class EdgeIt;
2.34 - friend class OutEdgeIt;
2.35 -
2.36 - class EdgeIt {
2.37 - friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
2.38 - protected:
2.39 - const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
2.40 - OldSymEdgeIt sym;
2.41 - public:
2.42 - EdgeIt() { }
2.43 - //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
2.44 - Number free() const {
2.45 - if (resG->G.aNode(sym)==resG->G.tail(sym)) {
2.46 - return (resG->capacity.get(sym)-resG->flow.get(sym));
2.47 - } else {
2.48 - return (resG->flow.get(sym));
2.49 - }
2.50 - }
2.51 - bool valid() const { return sym.valid(); }
2.52 - void augment(Number a) const {
2.53 - if (resG->G.aNode(sym)==resG->G.tail(sym)) {
2.54 - resG->flow.set(sym, resG->flow.get(sym)+a);
2.55 - //resG->flow[sym]+=a;
2.56 - } else {
2.57 - resG->flow.set(sym, resG->flow.get(sym)-a);
2.58 - //resG->flow[sym]-=a;
2.59 - }
2.60 - }
2.61 - };
2.62 -
2.63 - class OutEdgeIt : public EdgeIt {
2.64 - friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
2.65 - public:
2.66 - OutEdgeIt() { }
2.67 - //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
2.68 - private:
2.69 - OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
2.70 - resG=&_resG;
2.71 - sym=resG->G.template first<OldSymEdgeIt>(v);
2.72 - while( sym.valid() && !(free()>0) ) { ++sym; }
2.73 - }
2.74 - public:
2.75 - OutEdgeIt& operator++() {
2.76 - ++sym;
2.77 - while( sym.valid() && !(free()>0) ) { ++sym; }
2.78 - return *this;
2.79 - }
2.80 - };
2.81 -
2.82 - void getFirst(OutEdgeIt& e, NodeIt v) const {
2.83 - e=OutEdgeIt(*this, v);
2.84 - }
2.85 - void getFirst(EachNodeIt& v) const { G.getFirst(v); }
2.86 -
2.87 - template< typename It >
2.88 - It first() const {
2.89 - It e;
2.90 - getFirst(e);
2.91 - return e;
2.92 - }
2.93 -
2.94 - template< typename It >
2.95 - It first(NodeIt v) const {
2.96 - It e;
2.97 - getFirst(e, v);
2.98 - return e;
2.99 - }
2.100 -
2.101 - NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
2.102 - NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
2.103 -
2.104 - NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
2.105 - NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
2.106 -
2.107 - int id(NodeIt v) const { return G.id(v); }
2.108 -
2.109 - template <typename S>
2.110 - class NodeMap {
2.111 - typename Graph::NodeMap<S> node_map;
2.112 - public:
2.113 - NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
2.114 - NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
2.115 - void set(NodeIt nit, S a) { node_map.set(nit, a); }
2.116 - S get(NodeIt nit) const { return node_map.get(nit); }
2.117 - S& operator[](NodeIt nit) { return node_map[nit]; }
2.118 - const S& operator[](NodeIt nit) const { return node_map[nit]; }
2.119 - };
2.120 -
2.121 - };
2.122 -
2.123 -
2.124 - template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
2.125 - class ResGraph2 {
2.126 - public:
2.127 - typedef typename Graph::NodeIt NodeIt;
2.128 - typedef typename Graph::EachNodeIt EachNodeIt;
2.129 - private:
2.130 - //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
2.131 - typedef typename Graph::OutEdgeIt OldOutEdgeIt;
2.132 - typedef typename Graph::InEdgeIt OldInEdgeIt;
2.133 -
2.134 - const Graph& G;
2.135 - FlowMap& flow;
2.136 - const CapacityMap& capacity;
2.137 - public:
2.138 - ResGraph2(const Graph& _G, FlowMap& _flow,
2.139 - const CapacityMap& _capacity) :
2.140 - G(_G), flow(_flow), capacity(_capacity) { }
2.141 -
2.142 - class EdgeIt;
2.143 - class OutEdgeIt;
2.144 - friend class EdgeIt;
2.145 - friend class OutEdgeIt;
2.146 -
2.147 - class EdgeIt {
2.148 - friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
2.149 - protected:
2.150 - const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
2.151 - //OldSymEdgeIt sym;
2.152 - OldOutEdgeIt out;
2.153 - OldInEdgeIt in;
2.154 - bool out_or_in; //true, iff out
2.155 - public:
2.156 - EdgeIt() : out_or_in(true) { }
2.157 - Number free() const {
2.158 - if (out_or_in) {
2.159 - return (resG->capacity.get(out)-resG->flow.get(out));
2.160 - } else {
2.161 - return (resG->flow.get(in));
2.162 - }
2.163 - }
2.164 - bool valid() const {
2.165 - return out_or_in && out.valid() || in.valid(); }
2.166 - void augment(Number a) const {
2.167 - if (out_or_in) {
2.168 - resG->flow.set(out, resG->flow.get(out)+a);
2.169 - } else {
2.170 - resG->flow.set(in, resG->flow.get(in)-a);
2.171 - }
2.172 - }
2.173 - };
2.174 -
2.175 - class OutEdgeIt : public EdgeIt {
2.176 - friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
2.177 - public:
2.178 - OutEdgeIt() { }
2.179 - private:
2.180 - OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
2.181 - resG=&_resG;
2.182 - out=resG->G.template first<OldOutEdgeIt>(v);
2.183 - while( out.valid() && !(free()>0) ) { ++out; }
2.184 - if (!out.valid()) {
2.185 - out_or_in=0;
2.186 - in=resG->G.template first<OldInEdgeIt>(v);
2.187 - while( in.valid() && !(free()>0) ) { ++in; }
2.188 - }
2.189 - }
2.190 - public:
2.191 - OutEdgeIt& operator++() {
2.192 - if (out_or_in) {
2.193 - NodeIt v=resG->G.aNode(out);
2.194 - ++out;
2.195 - while( out.valid() && !(free()>0) ) { ++out; }
2.196 - if (!out.valid()) {
2.197 - out_or_in=0;
2.198 - in=resG->G.template first<OldInEdgeIt>(v);
2.199 - while( in.valid() && !(free()>0) ) { ++in; }
2.200 - }
2.201 - } else {
2.202 - ++in;
2.203 - while( in.valid() && !(free()>0) ) { ++in; }
2.204 - }
2.205 - return *this;
2.206 - }
2.207 - };
2.208 -
2.209 - void getFirst(OutEdgeIt& e, NodeIt v) const {
2.210 - e=OutEdgeIt(*this, v);
2.211 - }
2.212 - void getFirst(EachNodeIt& v) const { G.getFirst(v); }
2.213 -
2.214 - template< typename It >
2.215 - It first() const {
2.216 - It e;
2.217 - getFirst(e);
2.218 - return e;
2.219 - }
2.220 -
2.221 - template< typename It >
2.222 - It first(NodeIt v) const {
2.223 - It e;
2.224 - getFirst(e, v);
2.225 - return e;
2.226 - }
2.227 -
2.228 - NodeIt tail(EdgeIt e) const {
2.229 - return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
2.230 - NodeIt head(EdgeIt e) const {
2.231 - return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
2.232 -
2.233 - NodeIt aNode(OutEdgeIt e) const {
2.234 - return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
2.235 - NodeIt bNode(OutEdgeIt e) const {
2.236 - return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
2.237 -
2.238 - int id(NodeIt v) const { return G.id(v); }
2.239 -
2.240 - template <typename S>
2.241 - class NodeMap {
2.242 - typename Graph::NodeMap<S> node_map;
2.243 - public:
2.244 - NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
2.245 - NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
2.246 - void set(NodeIt nit, S a) { node_map.set(nit, a); }
2.247 - S get(NodeIt nit) const { return node_map.get(nit); }
2.248 - };
2.249 - };
2.250 -
2.251 -
2.252 - template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
2.253 - class MaxFlow {
2.254 - public:
2.255 - typedef typename Graph::NodeIt NodeIt;
2.256 - typedef typename Graph::EdgeIt EdgeIt;
2.257 - typedef typename Graph::EachEdgeIt EachEdgeIt;
2.258 - typedef typename Graph::OutEdgeIt OutEdgeIt;
2.259 - typedef typename Graph::InEdgeIt InEdgeIt;
2.260 -
2.261 - private:
2.262 - const Graph* G;
2.263 - NodeIt s;
2.264 - NodeIt t;
2.265 - FlowMap* flow;
2.266 - const CapacityMap* capacity;
2.267 - typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
2.268 - typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
2.269 - typedef typename AugGraph::EdgeIt AugEdgeIt;
2.270 -
2.271 - //AugGraph res_graph;
2.272 - //typedef typename AugGraph::NodeMap<bool> ReachedMap;
2.273 - //typename AugGraph::NodeMap<AugEdgeIt> pred;
2.274 - //typename AugGraph::NodeMap<Number> free;
2.275 - public:
2.276 - MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
2.277 - G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //,
2.278 - //res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
2.279 - { }
2.280 - bool augmentOnShortestPath() {
2.281 - AugGraph res_graph(*G, *flow, *capacity);
2.282 - bool _augment=false;
2.283 -
2.284 - typedef typename AugGraph::NodeMap<bool> ReachedMap;
2.285 - BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph);
2.286 - res_bfs.pushAndSetReached(s);
2.287 -
2.288 - typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
2.289 - //filled up with invalid iterators
2.290 - //pred.set(s, AugEdgeIt());
2.291 -
2.292 - typename AugGraph::NodeMap<Number> free(res_graph);
2.293 -
2.294 - //searching for augmenting path
2.295 - while ( !res_bfs.finished() ) {
2.296 - AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
2.297 - if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
2.298 - NodeIt v=res_graph.tail(e);
2.299 - NodeIt w=res_graph.head(e);
2.300 - pred.set(w, e);
2.301 - if (res_graph.valid(pred.get(v))) {
2.302 - free.set(w, std::min(free.get(v), res_graph.free(e)));
2.303 - } else {
2.304 - free.set(w, res_graph.free(e));
2.305 - }
2.306 - if (res_graph.head(e)==t) { _augment=true; break; }
2.307 - }
2.308 -
2.309 - ++res_bfs;
2.310 - } //end of searching augmenting path
2.311 -
2.312 - if (_augment) {
2.313 - NodeIt n=t;
2.314 - Number augment_value=free.get(t);
2.315 - while (res_graph.valid(pred.get(n))) {
2.316 - AugEdgeIt e=pred.get(n);
2.317 - res_graph.augment(e, augment_value);
2.318 - //e.augment(augment_value);
2.319 - n=res_graph.tail(e);
2.320 - }
2.321 - }
2.322 -
2.323 - return _augment;
2.324 - }
2.325 -
2.326 - template<typename MutableGraph> bool augmentOnBlockingFlow() {
2.327 - bool _augment=false;
2.328 -
2.329 - AugGraph res_graph(*G, *flow, *capacity);
2.330 -
2.331 - typedef typename AugGraph::NodeMap<bool> ReachedMap;
2.332 - BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
2.333 -
2.334 - bfs.pushAndSetReached(s);
2.335 - typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
2.336 - while ( !bfs.finished() ) {
2.337 - AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
2.338 - if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
2.339 - dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
2.340 - }
2.341 -
2.342 - ++bfs;
2.343 - } //computing distances from s in the residual graph
2.344 -
2.345 - MutableGraph F;
2.346 - typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph);
2.347 - for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
2.348 - res_graph_to_F.set(n, F.addNode());
2.349 - }
2.350 -
2.351 - typename MutableGraph::NodeIt sF=res_graph_to_F.get(s);
2.352 - typename MutableGraph::NodeIt tF=res_graph_to_F.get(t);
2.353 -
2.354 - typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F);
2.355 - typename MutableGraph::EdgeMap<Number> residual_capacity(F);
2.356 -
2.357 - //Making F to the graph containing the edges of the residual graph
2.358 - //which are in some shortest paths
2.359 - for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
2.360 - if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
2.361 - typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
2.362 - original_edge.update();
2.363 - original_edge.set(f, e);
2.364 - residual_capacity.update();
2.365 - residual_capacity.set(f, res_graph.free(e));
2.366 - }
2.367 - }
2.368 -
2.369 - bool __augment=true;
2.370 -
2.371 - while (__augment) {
2.372 - __augment=false;
2.373 - //computing blocking flow with dfs
2.374 - typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
2.375 - DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
2.376 - typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators
2.377 - typename MutableGraph::NodeMap<Number> free(F);
2.378 -
2.379 - dfs.pushAndSetReached(sF);
2.380 - while (!dfs.finished()) {
2.381 - ++dfs;
2.382 - if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
2.383 - if (dfs.isBNodeNewlyReached()) {
2.384 -// std::cout << "OutEdgeIt: " << dfs;
2.385 -// std::cout << " aNode: " << F.aNode(dfs);
2.386 -// std::cout << " bNode: " << F.bNode(dfs) << " ";
2.387 -
2.388 - typename MutableGraph::NodeIt v=F.aNode(dfs);
2.389 - typename MutableGraph::NodeIt w=F.bNode(dfs);
2.390 - pred.set(w, dfs);
2.391 - if (F.valid(pred.get(v))) {
2.392 - free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
2.393 - } else {
2.394 - free.set(w, residual_capacity.get(dfs));
2.395 - }
2.396 - if (w==tF) {
2.397 - //std::cout << "AUGMENTATION"<<std::endl;
2.398 - __augment=true;
2.399 - _augment=true;
2.400 - break;
2.401 - }
2.402 -
2.403 - } else {
2.404 - F.erase(typename MutableGraph::OutEdgeIt(dfs));
2.405 - }
2.406 - }
2.407 - }
2.408 -
2.409 - if (__augment) {
2.410 - typename MutableGraph::NodeIt n=tF;
2.411 - Number augment_value=free.get(tF);
2.412 - while (F.valid(pred.get(n))) {
2.413 - typename MutableGraph::EdgeIt e=pred.get(n);
2.414 - res_graph.augment(original_edge.get(e), augment_value);
2.415 - //original_edge.get(e).augment(augment_value);
2.416 - n=F.tail(e);
2.417 - if (residual_capacity.get(e)==augment_value)
2.418 - F.erase(e);
2.419 - else
2.420 - residual_capacity.set(e, residual_capacity.get(e)-augment_value);
2.421 - }
2.422 - }
2.423 -
2.424 - }
2.425 -
2.426 - return _augment;
2.427 - }
2.428 - bool augmentOnBlockingFlow2() {
2.429 - bool _augment=false;
2.430 -
2.431 - //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
2.432 - typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
2.433 - typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
2.434 - typedef typename EAugGraph::EdgeIt EAugEdgeIt;
2.435 -
2.436 - EAugGraph res_graph(*G, *flow, *capacity);
2.437 -
2.438 - //std::cout << "meg jo1" << std::endl;
2.439 -
2.440 - //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
2.441 - BfsIterator4<
2.442 - ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
2.443 - ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,
2.444 - ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
2.445 -
2.446 - //std::cout << "meg jo2" << std::endl;
2.447 -
2.448 - bfs.pushAndSetReached(s);
2.449 - //std::cout << "meg jo2.5" << std::endl;
2.450 -
2.451 - //typename EAugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
2.452 - typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
2.453 - NodeMap<int>& dist=res_graph.dist;
2.454 - //std::cout << "meg jo2.6" << std::endl;
2.455 -
2.456 - while ( !bfs.finished() ) {
2.457 - ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
2.458 -// EAugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
2.459 - //if (res_graph.valid(e)) {
2.460 - // std::cout<<"a:"<<res_graph.tail(e)<<"b:"<<res_graph.head(e)<<std::endl;
2.461 - //}
2.462 - if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
2.463 - dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
2.464 - }
2.465 -
2.466 - ++bfs;
2.467 - } //computing distances from s in the residual graph
2.468 -
2.469 -
2.470 - //std::cout << "meg jo3" << std::endl;
2.471 -
2.472 -// typedef typename EAugGraph::EachNodeIt EAugEachNodeIt;
2.473 -// for(EAugEachNodeIt n=res_graph.template first<EAugEachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
2.474 -// std::cout << "dist: " << dist.get(n) << std::endl;
2.475 -// }
2.476 -
2.477 - bool __augment=true;
2.478 -
2.479 - while (__augment) {
2.480 -// std::cout << "new iteration"<< std::endl;
2.481 -
2.482 - __augment=false;
2.483 - //computing blocking flow with dfs
2.484 - typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
2.485 - DfsIterator4< EAugGraph, EAugOutEdgeIt, BlockingReachedMap >
2.486 - dfs(res_graph);
2.487 - typename EAugGraph::NodeMap<EAugEdgeIt> pred(res_graph); //invalid iterators
2.488 - typename EAugGraph::NodeMap<Number> free(res_graph);
2.489 -
2.490 - dfs.pushAndSetReached(s);
2.491 - while (!dfs.finished()) {
2.492 - ++dfs;
2.493 - if (res_graph.valid(EAugOutEdgeIt(dfs))) {
2.494 - if (dfs.isBNodeNewlyReached()) {
2.495 -// std::cout << "OutEdgeIt: " << dfs;
2.496 -// std::cout << " aNode: " << res_graph.aNode(dfs);
2.497 -// std::cout << " res cap: " << EAugOutEdgeIt(dfs).free();
2.498 -// std::cout << " bNode: " << res_graph.bNode(dfs) << " ";
2.499 -
2.500 - typename EAugGraph::NodeIt v=res_graph.aNode(dfs);
2.501 - typename EAugGraph::NodeIt w=res_graph.bNode(dfs);
2.502 -
2.503 - pred.set(w, EAugOutEdgeIt(dfs));
2.504 -
2.505 - //std::cout << EAugOutEdgeIt(dfs).free() << std::endl;
2.506 - if (res_graph.valid(pred.get(v))) {
2.507 - free.set(w, std::min(free.get(v), res_graph.free(/*EAugOutEdgeIt*/(dfs))));
2.508 - } else {
2.509 - free.set(w, res_graph.free(/*EAugOutEdgeIt*/(dfs)));
2.510 - }
2.511 -
2.512 - if (w==t) {
2.513 -// std::cout << "t is reached, AUGMENTATION"<<std::endl;
2.514 - __augment=true;
2.515 - _augment=true;
2.516 - break;
2.517 - }
2.518 - } else {
2.519 -// std::cout << "<<DELETE ";
2.520 -// std::cout << " aNode: " << res_graph.aNode(dfs);
2.521 -// std::cout << " res cap: " << EAugOutEdgeIt(dfs).free();
2.522 -// std::cout << " bNode: " << res_graph.bNode(dfs) << " ";
2.523 -// std::cout << "DELETE>> ";
2.524 -
2.525 - res_graph.erase(dfs);
2.526 - }
2.527 - }
2.528 -
2.529 - }
2.530 -
2.531 - if (__augment) {
2.532 - typename EAugGraph::NodeIt n=t;
2.533 - Number augment_value=free.get(t);
2.534 -// std::cout << "av:" << augment_value << std::endl;
2.535 - while (res_graph.valid(pred.get(n))) {
2.536 - EAugEdgeIt e=pred.get(n);
2.537 - res_graph.augment(e, augment_value);
2.538 - //e.augment(augment_value);
2.539 - n=res_graph.tail(e);
2.540 - if (res_graph.free(e)==0)
2.541 - res_graph.erase(e);
2.542 - }
2.543 - }
2.544 -
2.545 - }
2.546 -
2.547 - return _augment;
2.548 - }
2.549 - void run() {
2.550 - //int num_of_augmentations=0;
2.551 - while (augmentOnShortestPath()) {
2.552 - //while (augmentOnBlockingFlow<MutableGraph>()) {
2.553 - //std::cout << ++num_of_augmentations << " ";
2.554 - //std::cout<<std::endl;
2.555 - }
2.556 - }
2.557 - template<typename MutableGraph> void run() {
2.558 - //int num_of_augmentations=0;
2.559 - //while (augmentOnShortestPath()) {
2.560 - while (augmentOnBlockingFlow<MutableGraph>()) {
2.561 - //std::cout << ++num_of_augmentations << " ";
2.562 - //std::cout<<std::endl;
2.563 - }
2.564 - }
2.565 - Number flowValue() {
2.566 - Number a=0;
2.567 - OutEdgeIt e;
2.568 - for(G->getFirst(e, s); G->valid(e); G->next(e)) {
2.569 - a+=flow->get(e);
2.570 - }
2.571 - return a;
2.572 - }
2.573 - };
2.574 -
2.575 -
2.576 -// template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
2.577 -// class MaxFlow2 {
2.578 -// public:
2.579 -// typedef typename Graph::NodeIt NodeIt;
2.580 -// typedef typename Graph::EdgeIt EdgeIt;
2.581 -// typedef typename Graph::EachEdgeIt EachEdgeIt;
2.582 -// typedef typename Graph::OutEdgeIt OutEdgeIt;
2.583 -// typedef typename Graph::InEdgeIt InEdgeIt;
2.584 -// private:
2.585 -// const Graph& G;
2.586 -// std::list<NodeIt>& S;
2.587 -// std::list<NodeIt>& T;
2.588 -// FlowMap& flow;
2.589 -// const CapacityMap& capacity;
2.590 -// typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
2.591 -// typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
2.592 -// typedef typename AugGraph::EdgeIt AugEdgeIt;
2.593 -// typename Graph::NodeMap<bool> SMap;
2.594 -// typename Graph::NodeMap<bool> TMap;
2.595 -// public:
2.596 -// MaxFlow2(const Graph& _G, std::list<NodeIt>& _S, std::list<NodeIt>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) {
2.597 -// for(typename std::list<NodeIt>::const_iterator i=S.begin();
2.598 -// i!=S.end(); ++i) {
2.599 -// SMap.set(*i, true);
2.600 -// }
2.601 -// for (typename std::list<NodeIt>::const_iterator i=T.begin();
2.602 -// i!=T.end(); ++i) {
2.603 -// TMap.set(*i, true);
2.604 -// }
2.605 -// }
2.606 -// bool augment() {
2.607 -// AugGraph res_graph(G, flow, capacity);
2.608 -// bool _augment=false;
2.609 -// NodeIt reached_t_node;
2.610 -
2.611 -// typedef typename AugGraph::NodeMap<bool> ReachedMap;
2.612 -// BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
2.613 -// for(typename std::list<NodeIt>::const_iterator i=S.begin();
2.614 -// i!=S.end(); ++i) {
2.615 -// res_bfs.pushAndSetReached(*i);
2.616 -// }
2.617 -// //res_bfs.pushAndSetReached(s);
2.618 -
2.619 -// typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
2.620 -// //filled up with invalid iterators
2.621 -
2.622 -// typename AugGraph::NodeMap<Number> free(res_graph);
2.623 -
2.624 -// //searching for augmenting path
2.625 -// while ( !res_bfs.finished() ) {
2.626 -// AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
2.627 -// if (e.valid() && res_bfs.isBNodeNewlyReached()) {
2.628 -// NodeIt v=res_graph.tail(e);
2.629 -// NodeIt w=res_graph.head(e);
2.630 -// pred.set(w, e);
2.631 -// if (pred.get(v).valid()) {
2.632 -// free.set(w, std::min(free.get(v), e.free()));
2.633 -// } else {
2.634 -// free.set(w, e.free());
2.635 -// }
2.636 -// if (TMap.get(res_graph.head(e))) {
2.637 -// _augment=true;
2.638 -// reached_t_node=res_graph.head(e);
2.639 -// break;
2.640 -// }
2.641 -// }
2.642 -
2.643 -// ++res_bfs;
2.644 -// } //end of searching augmenting path
2.645 -
2.646 -// if (_augment) {
2.647 -// NodeIt n=reached_t_node;
2.648 -// Number augment_value=free.get(reached_t_node);
2.649 -// while (pred.get(n).valid()) {
2.650 -// AugEdgeIt e=pred.get(n);
2.651 -// e.augment(augment_value);
2.652 -// n=res_graph.tail(e);
2.653 -// }
2.654 -// }
2.655 -
2.656 -// return _augment;
2.657 -// }
2.658 -// void run() {
2.659 -// while (augment()) { }
2.660 -// }
2.661 -// Number flowValue() {
2.662 -// Number a=0;
2.663 -// for(typename std::list<NodeIt>::const_iterator i=S.begin();
2.664 -// i!=S.end(); ++i) {
2.665 -// for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
2.666 -// a+=flow.get(e);
2.667 -// }
2.668 -// for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
2.669 -// a-=flow.get(e);
2.670 -// }
2.671 -// }
2.672 -// return a;
2.673 -// }
2.674 -// };
2.675 -
2.676 -
2.677 -
2.678 -} // namespace hugo
2.679 -
2.680 -#endif //EDMONDS_KARP_HH
3.1 --- a/src/work/list_graph.hh Sat Apr 03 14:22:33 2004 +0000
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,552 +0,0 @@
3.4 -#ifndef LIST_GRAPH_HH
3.5 -#define LIST_GRAPH_HH
3.6 -
3.7 -#include <iostream>
3.8 -#include <vector>
3.9 -
3.10 -namespace hugo {
3.11 -
3.12 - template <typename It>
3.13 - int count(It it) {
3.14 - int i=0;
3.15 - for( ; it.valid(); ++it) { ++i; }
3.16 - return i;
3.17 - }
3.18 -
3.19 - class ListGraph {
3.20 -
3.21 - class node_item;
3.22 - class edge_item;
3.23 -
3.24 - public:
3.25 -
3.26 - class NodeIt;
3.27 - class EachNodeIt;
3.28 - class EdgeIt;
3.29 - class EachEdgeIt;
3.30 - class OutEdgeIt;
3.31 - class InEdgeIt;
3.32 - class SymEdgeIt;
3.33 - template <typename T> class NodeMap;
3.34 - template <typename T> class EdgeMap;
3.35 -
3.36 - private:
3.37 -
3.38 - template <typename T> friend class NodeMap;
3.39 - template <typename T> friend class EdgeMap;
3.40 -
3.41 - template <typename T>
3.42 - class NodeMap {
3.43 - const ListGraph& G;
3.44 - std::vector<T> container;
3.45 - public:
3.46 - typedef T ValueType;
3.47 - typedef NodeIt KeyType;
3.48 - NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { }
3.49 - NodeMap(const ListGraph& _G, T a) :
3.50 - G(_G), container(G.node_id, a) { }
3.51 - void set(NodeIt n, T a) { container[/*G.id(n)*/n.node->id]=a; }
3.52 - T get(NodeIt n) const { return container[/*G.id(n)*/n.node->id]; }
3.53 - T& operator[](NodeIt n) { return container[/*G.id(n)*/n.node->id]; }
3.54 - const T& operator[](NodeIt n) const { return container[/*G.id(n)*/n.node->id]; }
3.55 - void update() { container.resize(G.node_id); }
3.56 - void update(T a) { container.resize(G.node_id, a); }
3.57 - };
3.58 -
3.59 - template <typename T>
3.60 - class EdgeMap {
3.61 - const ListGraph& G;
3.62 - std::vector<T> container;
3.63 - public:
3.64 - typedef T ValueType;
3.65 - typedef EdgeIt KeyType;
3.66 - EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { }
3.67 - EdgeMap(const ListGraph& _G, T a) :
3.68 - G(_G), container(G.edge_id, a) { }
3.69 - void set(EdgeIt e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
3.70 - T get(EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; }
3.71 - T& operator[](EdgeIt e) { return container[/*G.id(e)*/e.edge->id]; }
3.72 - const T& operator[](EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; }
3.73 - void update() { container.resize(G.edge_id); }
3.74 - void update(T a) { container.resize(G.edge_id, a); }
3.75 - };
3.76 -
3.77 - int node_id;
3.78 - int edge_id;
3.79 - int _node_num;
3.80 - int _edge_num;
3.81 -
3.82 - node_item* _first_node;
3.83 - node_item* _last_node;
3.84 -
3.85 - class node_item {
3.86 - friend class ListGraph;
3.87 - template <typename T> friend class NodeMap;
3.88 -
3.89 - friend class NodeIt;
3.90 - friend class EachNodeIt;
3.91 - friend class EdgeIt;
3.92 - friend class EachEdgeIt;
3.93 - friend class OutEdgeIt;
3.94 - friend class InEdgeIt;
3.95 - friend class SymEdgeIt;
3.96 - friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
3.97 - friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
3.98 - //ListGraph* G;
3.99 - int id;
3.100 - edge_item* _first_out_edge;
3.101 - edge_item* _last_out_edge;
3.102 - edge_item* _first_in_edge;
3.103 - edge_item* _last_in_edge;
3.104 - node_item* _next_node;
3.105 - node_item* _prev_node;
3.106 - public:
3.107 - node_item() { }
3.108 - };
3.109 -
3.110 - class edge_item {
3.111 - friend class ListGraph;
3.112 - template <typename T> friend class EdgeMap;
3.113 -
3.114 - friend class NodeIt;
3.115 - friend class EachNodeIt;
3.116 - friend class EdgeIt;
3.117 - friend class EachEdgeIt;
3.118 - friend class OutEdgeIt;
3.119 - friend class InEdgeIt;
3.120 - friend class SymEdgeIt;
3.121 - friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
3.122 - //ListGraph* G;
3.123 - int id;
3.124 - node_item* _tail;
3.125 - node_item* _head;
3.126 - edge_item* _next_out;
3.127 - edge_item* _prev_out;
3.128 - edge_item* _next_in;
3.129 - edge_item* _prev_in;
3.130 - public:
3.131 - edge_item() { }
3.132 - };
3.133 -
3.134 - node_item* _add_node() {
3.135 - node_item* p=new node_item;
3.136 - p->id=node_id++;
3.137 - p->_first_out_edge=0;
3.138 - p->_last_out_edge=0;
3.139 - p->_first_in_edge=0;
3.140 - p->_last_in_edge=0;
3.141 - p->_prev_node=_last_node;
3.142 - p->_next_node=0;
3.143 - if (_last_node) _last_node->_next_node=p;
3.144 - _last_node=p;
3.145 - if (!_first_node) _first_node=p;
3.146 -
3.147 - ++_node_num;
3.148 - return p;
3.149 - }
3.150 -
3.151 - edge_item* _add_edge(node_item* _tail, node_item* _head) {
3.152 - edge_item* e=new edge_item;
3.153 - e->id=edge_id++;
3.154 - e->_tail=_tail;
3.155 - e->_head=_head;
3.156 -
3.157 - e->_prev_out=_tail->_last_out_edge;
3.158 - if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
3.159 - _tail->_last_out_edge=e;
3.160 - if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
3.161 - e->_next_out=0;
3.162 -
3.163 - e->_prev_in=_head->_last_in_edge;
3.164 - if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
3.165 - _head->_last_in_edge=e;
3.166 - if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
3.167 - e->_next_in=0;
3.168 -
3.169 - ++_edge_num;
3.170 - return e;
3.171 - }
3.172 -
3.173 - //deletes a node which has no out edge and no in edge
3.174 - void _delete_node(node_item* v) {
3.175 - if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
3.176 - _last_node=v->_prev_node;
3.177 - if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
3.178 - _first_node=v->_next_node;
3.179 -
3.180 - delete v;
3.181 - --_node_num;
3.182 - }
3.183 -
3.184 - void _delete_edge(edge_item* e) {
3.185 - if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
3.186 - (e->_tail)->_last_out_edge=e->_prev_out;
3.187 - if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
3.188 - (e->_tail)->_first_out_edge=e->_next_out;
3.189 - if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
3.190 - (e->_head)->_last_in_edge=e->_prev_in;
3.191 - if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
3.192 - (e->_head)->_first_in_edge=e->_next_in;
3.193 -
3.194 - delete e;
3.195 - --_edge_num;
3.196 - }
3.197 -
3.198 - void _set_tail(edge_item* e, node_item* _tail) {
3.199 - if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
3.200 - (e->_tail)->_last_out_edge=e->_prev_out;
3.201 - if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
3.202 - (e->_tail)->_first_out_edge=e->_next_out;
3.203 -
3.204 - e->_tail=_tail;
3.205 -
3.206 - e->_prev_out=_tail->_last_out_edge;
3.207 - if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
3.208 - _tail->_last_out_edge=e;
3.209 - if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
3.210 - e->_next_out=0;
3.211 - }
3.212 -
3.213 - void _set_head(edge_item* e, node_item* _head) {
3.214 - if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
3.215 - (e->_head)->_last_in_edge=e->_prev_in;
3.216 - if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
3.217 - (e->_head)->_first_in_edge=e->_next_in;
3.218 -
3.219 - e->_head=_head;
3.220 -
3.221 - e->_prev_in=_head->_last_in_edge;
3.222 - if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
3.223 - _head->_last_in_edge=e;
3.224 - if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
3.225 - e->_next_in=0;
3.226 - }
3.227 -
3.228 - public:
3.229 -
3.230 - /* default constructor */
3.231 -
3.232 - ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
3.233 -
3.234 - ~ListGraph() {
3.235 - while (first<EachNodeIt>().valid()) erase(first<EachNodeIt>());
3.236 - }
3.237 -
3.238 - int nodeNum() const { return _node_num; }
3.239 - int edgeNum() const { return _edge_num; }
3.240 -
3.241 - /* functions to construct iterators from the graph, or from each other */
3.242 -
3.243 - //EachNodeIt firstNode() const { return EachNodeIt(*this); }
3.244 - //EachEdgeIt firstEdge() const { return EachEdgeIt(*this); }
3.245 -
3.246 - //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); }
3.247 - //InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); }
3.248 - //SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); }
3.249 - NodeIt tail(EdgeIt e) const { return e.tailNode(); }
3.250 - NodeIt head(EdgeIt e) const { return e.headNode(); }
3.251 -
3.252 - NodeIt aNode(const OutEdgeIt& e) const { return e.aNode(); }
3.253 - NodeIt aNode(const InEdgeIt& e) const { return e.aNode(); }
3.254 - NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); }
3.255 -
3.256 - NodeIt bNode(const OutEdgeIt& e) const { return e.bNode(); }
3.257 - NodeIt bNode(const InEdgeIt& e) const { return e.bNode(); }
3.258 - NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); }
3.259 -
3.260 - //NodeIt invalid_node() { return NodeIt(); }
3.261 - //EdgeIt invalid_edge() { return EdgeIt(); }
3.262 - //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
3.263 - //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
3.264 - //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
3.265 -
3.266 - /* same methods in other style */
3.267 - /* for experimental purpose */
3.268 -
3.269 - EachNodeIt& getFirst(EachNodeIt& v) const {
3.270 - v=EachNodeIt(*this); return v; }
3.271 - EachEdgeIt& getFirst(EachEdgeIt& e) const {
3.272 - e=EachEdgeIt(*this); return e; }
3.273 - OutEdgeIt& getFirst(OutEdgeIt& e, NodeIt v) const {
3.274 - e=OutEdgeIt(v); return e; }
3.275 - InEdgeIt& getFirst(InEdgeIt& e, NodeIt v) const {
3.276 - e=InEdgeIt(v); return e; }
3.277 - SymEdgeIt& getFirst(SymEdgeIt& e, NodeIt v) const {
3.278 - e=SymEdgeIt(v); return e; }
3.279 - //void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); }
3.280 - //void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); }
3.281 -
3.282 - //void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); }
3.283 - //void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); }
3.284 - //void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); }
3.285 - //void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); }
3.286 - //void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); }
3.287 - //void getBNode(NodeIt& n, const SymEdgeIt& e) const { n=e.bNode(); }
3.288 - //void get_invalid(NodeIt& n) { n=NodeIt(); }
3.289 - //void get_invalid(EdgeIt& e) { e=EdgeIt(); }
3.290 - //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
3.291 - //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
3.292 - //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
3.293 -
3.294 - template< typename It >
3.295 - It first() const {
3.296 - It e;
3.297 - getFirst(e);
3.298 - return e;
3.299 - }
3.300 -
3.301 - template< typename It >
3.302 - It first(NodeIt v) const {
3.303 - It e;
3.304 - getFirst(e, v);
3.305 - return e;
3.306 - }
3.307 -
3.308 - bool valid(NodeIt n) const { return n.valid(); }
3.309 - bool valid(EdgeIt e) const { return e.valid(); }
3.310 -
3.311 - template <typename It> It getNext(It it) const {
3.312 - It tmp(it); return next(tmp); }
3.313 - template <typename It> It& next(It& it) const { return ++it; }
3.314 -
3.315 -
3.316 - /* for getting id's of graph objects */
3.317 - /* these are important for the implementation of property vectors */
3.318 -
3.319 - int id(NodeIt v) const { return v.node->id; }
3.320 - int id(EdgeIt e) const { return e.edge->id; }
3.321 -
3.322 - /* adding nodes and edges */
3.323 -
3.324 - NodeIt addNode() { return NodeIt(_add_node()); }
3.325 - EdgeIt addEdge(NodeIt u, NodeIt v) {
3.326 - return EdgeIt(_add_edge(u.node, v.node));
3.327 - }
3.328 -
3.329 - void erase(NodeIt i) {
3.330 - while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
3.331 - while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
3.332 - _delete_node(i.node);
3.333 - }
3.334 -
3.335 - void erase(EdgeIt e) { _delete_edge(e.edge); }
3.336 -
3.337 - void clear() {
3.338 - while (first<EachNodeIt>().valid()) erase(first<EachNodeIt>());
3.339 - }
3.340 -
3.341 - void setTail(EdgeIt e, NodeIt tail) {
3.342 - _set_tail(e.edge, tail.node);
3.343 - }
3.344 -
3.345 - void setHead(EdgeIt e, NodeIt head) {
3.346 - _set_head(e.edge, head.node);
3.347 - }
3.348 -
3.349 - /* stream operations, for testing purpose */
3.350 -
3.351 - friend std::ostream& operator<<(std::ostream& os, const NodeIt& i) {
3.352 - os << i.node->id; return os;
3.353 - }
3.354 - friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i) {
3.355 - os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
3.356 - return os;
3.357 - }
3.358 -
3.359 - class NodeIt {
3.360 - friend class ListGraph;
3.361 - template <typename T> friend class NodeMap;
3.362 -
3.363 - friend class EdgeIt;
3.364 - friend class OutEdgeIt;
3.365 - friend class InEdgeIt;
3.366 - friend class SymEdgeIt;
3.367 - //public: //FIXME: It is required by op= of EachNodeIt
3.368 - protected:
3.369 - node_item* node;
3.370 - protected:
3.371 - friend int ListGraph::id(NodeIt v) const;
3.372 - public:
3.373 - NodeIt() : node(0) { }
3.374 - NodeIt(node_item* _node) : node(_node) { }
3.375 - bool valid() const { return (node!=0); }
3.376 - //void makeInvalid() { node=0; }
3.377 - friend bool operator==(const NodeIt& u, const NodeIt& v) {
3.378 - return v.node==u.node;
3.379 - }
3.380 - friend bool operator!=(const NodeIt& u, const NodeIt& v) {
3.381 - return v.node!=u.node;
3.382 - }
3.383 - friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
3.384 - };
3.385 -
3.386 - class EachNodeIt : public NodeIt {
3.387 - friend class ListGraph;
3.388 - //protected:
3.389 - public: //for everybody but marci
3.390 - EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { }
3.391 - public:
3.392 - EachNodeIt() : NodeIt() { }
3.393 - EachNodeIt(node_item* v) : NodeIt(v) { }
3.394 - EachNodeIt& operator++() { node=node->_next_node; return *this; }
3.395 - //FIXME::
3.396 - // EachNodeIt& operator=(const NodeIt& e)
3.397 - // { node=e.node; return *this; }
3.398 - };
3.399 -
3.400 - class EdgeIt {
3.401 - friend class ListGraph;
3.402 - template <typename T> friend class EdgeMap;
3.403 -
3.404 - friend class NodeIt;
3.405 - friend class EachNodeIt;
3.406 - protected:
3.407 - edge_item* edge;
3.408 - friend int ListGraph::id(EdgeIt e) const;
3.409 - public:
3.410 - EdgeIt() : edge(0) { }
3.411 - //EdgeIt() { }
3.412 - EdgeIt(edge_item* _edge) : edge(_edge) { }
3.413 - bool valid() const { return (edge!=0); }
3.414 - //void makeInvalid() { edge=0; }
3.415 - friend bool operator==(const EdgeIt& u, const EdgeIt& v) {
3.416 - return v.edge==u.edge;
3.417 - }
3.418 - friend bool operator!=(const EdgeIt& u, const EdgeIt& v) {
3.419 - return v.edge!=u.edge;
3.420 - }
3.421 - protected:
3.422 - NodeIt tailNode() const { return NodeIt(edge->_tail); }
3.423 - NodeIt headNode() const { return NodeIt(edge->_head); }
3.424 - public:
3.425 - friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
3.426 - };
3.427 -
3.428 - class EachEdgeIt : public EdgeIt {
3.429 - friend class ListGraph;
3.430 - //protected:
3.431 - public: //for alpar
3.432 - EachEdgeIt(const ListGraph& G) {
3.433 - node_item* v=G._first_node;
3.434 - if (v) edge=v->_first_out_edge; else edge=0;
3.435 - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
3.436 - }
3.437 - public:
3.438 - EachEdgeIt() : EdgeIt() { }
3.439 - EachEdgeIt(edge_item* _e) : EdgeIt(_e) { }
3.440 - EachEdgeIt& operator++() {
3.441 - node_item* v=edge->_tail;
3.442 - edge=edge->_next_out;
3.443 - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
3.444 - return *this;
3.445 - }
3.446 - };
3.447 -
3.448 - class OutEdgeIt : public EdgeIt {
3.449 - friend class ListGraph;
3.450 - //node_item* v;
3.451 - //protected:
3.452 - public: //for alpar
3.453 - OutEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
3.454 - public:
3.455 - OutEdgeIt() : EdgeIt()/*, v(0)*/ { }
3.456 - OutEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
3.457 - OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
3.458 - protected:
3.459 - NodeIt aNode() const { return NodeIt(edge->_tail); }
3.460 - NodeIt bNode() const { return NodeIt(edge->_head); }
3.461 - };
3.462 -
3.463 - class InEdgeIt : public EdgeIt {
3.464 - friend class ListGraph;
3.465 - //node_item* v;
3.466 - //protected:
3.467 - public: //for alpar
3.468 - InEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
3.469 - public:
3.470 - InEdgeIt() : EdgeIt()/*, v(0)*/ { }
3.471 - InEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
3.472 - InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
3.473 - protected:
3.474 - NodeIt aNode() const { return NodeIt(edge->_head); }
3.475 - NodeIt bNode() const { return NodeIt(edge->_tail); }
3.476 - };
3.477 -
3.478 - class SymEdgeIt : public EdgeIt {
3.479 - friend class ListGraph;
3.480 - bool out_or_in; //1 iff out, 0 iff in
3.481 - //node_item* v;
3.482 - //protected:
3.483 - public: //for alpar
3.484 - SymEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ {
3.485 - out_or_in=1;
3.486 - edge=_v.node->_first_out_edge;
3.487 - if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
3.488 - }
3.489 - public:
3.490 - SymEdgeIt() : EdgeIt() /*, v(0)*/ { }
3.491 - SymEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ {
3.492 - out_or_in=1;
3.493 - edge=_v.node->_first_out_edge;
3.494 - if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
3.495 - }
3.496 - SymEdgeIt& operator++() {
3.497 - if (out_or_in) {
3.498 - node_item* v=edge->_tail;
3.499 - edge=edge->_next_out;
3.500 - if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
3.501 - } else {
3.502 - edge=edge->_next_in;
3.503 - }
3.504 - return *this;
3.505 - }
3.506 - protected:
3.507 - NodeIt aNode() const {
3.508 - return (out_or_in) ? NodeIt(edge->_tail) : NodeIt(edge->_head); }
3.509 - NodeIt bNode() const {
3.510 - return (out_or_in) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
3.511 - };
3.512 -
3.513 - };
3.514 -
3.515 -// template< typename T >
3.516 -// T ListGraph::first() const {
3.517 -// std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
3.518 -// return T();
3.519 -// }
3.520 -
3.521 -// template<>
3.522 -// ListGraph::EachNodeIt ListGraph::first<ListGraph::EachNodeIt>() const {
3.523 -// return firstNode();
3.524 -// }
3.525 -
3.526 -// template<>
3.527 -// ListGraph::EachEdgeIt ListGraph::first<ListGraph::EachEdgeIt>() const {
3.528 -// return firstEdge();
3.529 -// }
3.530 -
3.531 -// template< typename T >
3.532 -// T ListGraph::first(ListGraph::NodeIt v) const {
3.533 -// std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::NodeIt);" << std::endl;
3.534 -// return T();
3.535 -// }
3.536 -
3.537 -// template<>
3.538 -// ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::NodeIt v) const {
3.539 -// return firstOutEdge(v);
3.540 -// }
3.541 -
3.542 -// template<>
3.543 -// ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::NodeIt v) const {
3.544 -// return firstInEdge(v);
3.545 -// }
3.546 -
3.547 -// template<>
3.548 -// ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::NodeIt v) const {
3.549 -// return firstSymEdge(v);
3.550 -// }
3.551 -
3.552 -
3.553 -} //namespace hugo
3.554 -
3.555 -#endif //LIST_GRAPH_HH
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/src/work/marci/oldies/bfs_iterator.hh Sat Apr 03 14:41:31 2004 +0000
4.3 @@ -0,0 +1,836 @@
4.4 +#ifndef BFS_ITERATOR_HH
4.5 +#define BFS_ITERATOR_HH
4.6 +
4.7 +#include <queue>
4.8 +#include <stack>
4.9 +#include <utility>
4.10 +#include <graph_wrapper.h>
4.11 +
4.12 +namespace hugo {
4.13 +
4.14 + template <typename Graph>
4.15 + struct bfs {
4.16 + typedef typename Graph::NodeIt NodeIt;
4.17 + typedef typename Graph::EdgeIt EdgeIt;
4.18 + typedef typename Graph::EachNodeIt EachNodeIt;
4.19 + typedef typename Graph::OutEdgeIt OutEdgeIt;
4.20 + Graph& G;
4.21 + NodeIt s;
4.22 + typename Graph::NodeMap<bool> reached;
4.23 + typename Graph::NodeMap<EdgeIt> pred;
4.24 + typename Graph::NodeMap<int> dist;
4.25 + std::queue<NodeIt> bfs_queue;
4.26 + bfs(Graph& _G, NodeIt _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) {
4.27 + bfs_queue.push(s);
4.28 + for(EachNodeIt i=G.template first<EachNodeIt>(); i.valid(); ++i)
4.29 + reached.set(i, false);
4.30 + reached.set(s, true);
4.31 + dist.set(s, 0);
4.32 + }
4.33 +
4.34 + void run() {
4.35 + while (!bfs_queue.empty()) {
4.36 + NodeIt v=bfs_queue.front();
4.37 + OutEdgeIt e=G.template first<OutEdgeIt>(v);
4.38 + bfs_queue.pop();
4.39 + for( ; e.valid(); ++e) {
4.40 + NodeIt w=G.bNode(e);
4.41 + std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl;
4.42 + if (!reached.get(w)) {
4.43 + std::cout << G.id(w) << " is newly reached :-)" << std::endl;
4.44 + bfs_queue.push(w);
4.45 + dist.set(w, dist.get(v)+1);
4.46 + pred.set(w, e);
4.47 + reached.set(w, true);
4.48 + } else {
4.49 + std::cout << G.id(w) << " is already reached" << std::endl;
4.50 + }
4.51 + }
4.52 + }
4.53 + }
4.54 + };
4.55 +
4.56 + template <typename Graph>
4.57 + struct bfs_visitor {
4.58 + typedef typename Graph::NodeIt NodeIt;
4.59 + typedef typename Graph::EdgeIt EdgeIt;
4.60 + typedef typename Graph::OutEdgeIt OutEdgeIt;
4.61 + Graph& G;
4.62 + bfs_visitor(Graph& _G) : G(_G) { }
4.63 + void at_previously_reached(OutEdgeIt& e) {
4.64 + //NodeIt v=G.aNode(e);
4.65 + NodeIt w=G.bNode(e);
4.66 + std::cout << G.id(w) << " is already reached" << std::endl;
4.67 + }
4.68 + void at_newly_reached(OutEdgeIt& e) {
4.69 + //NodeIt v=G.aNode(e);
4.70 + NodeIt w=G.bNode(e);
4.71 + std::cout << G.id(w) << " is newly reached :-)" << std::endl;
4.72 + }
4.73 + };
4.74 +
4.75 + template <typename Graph, typename ReachedMap, typename visitor_type>
4.76 + struct bfs_iterator {
4.77 + typedef typename Graph::NodeIt NodeIt;
4.78 + typedef typename Graph::EdgeIt EdgeIt;
4.79 + typedef typename Graph::OutEdgeIt OutEdgeIt;
4.80 + Graph& G;
4.81 + std::queue<OutEdgeIt>& bfs_queue;
4.82 + ReachedMap& reached;
4.83 + visitor_type& visitor;
4.84 + void process() {
4.85 + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.86 + if (bfs_queue.empty()) return;
4.87 + OutEdgeIt e=bfs_queue.front();
4.88 + //NodeIt v=G.aNode(e);
4.89 + NodeIt w=G.bNode(e);
4.90 + if (!reached.get(w)) {
4.91 + visitor.at_newly_reached(e);
4.92 + bfs_queue.push(G.template first<OutEdgeIt>(w));
4.93 + reached.set(w, true);
4.94 + } else {
4.95 + visitor.at_previously_reached(e);
4.96 + }
4.97 + }
4.98 + bfs_iterator(Graph& _G, std::queue<OutEdgeIt>& _bfs_queue, ReachedMap& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) {
4.99 + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.100 + valid();
4.101 + }
4.102 + bfs_iterator<Graph, ReachedMap, visitor_type>& operator++() {
4.103 + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.104 + //if (bfs_queue.empty()) return *this;
4.105 + if (!valid()) return *this;
4.106 + ++(bfs_queue.front());
4.107 + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.108 + valid();
4.109 + return *this;
4.110 + }
4.111 + //void next() {
4.112 + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.113 + // if (bfs_queue.empty()) return;
4.114 + // ++(bfs_queue.front());
4.115 + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.116 + //}
4.117 + bool valid() {
4.118 + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.119 + if (bfs_queue.empty()) return false; else return true;
4.120 + }
4.121 + //bool finished() {
4.122 + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.123 + // if (bfs_queue.empty()) return true; else return false;
4.124 + //}
4.125 + operator EdgeIt () { return bfs_queue.front(); }
4.126 +
4.127 + };
4.128 +
4.129 + template <typename Graph, typename ReachedMap>
4.130 + struct bfs_iterator1 {
4.131 + typedef typename Graph::NodeIt NodeIt;
4.132 + typedef typename Graph::EdgeIt EdgeIt;
4.133 + typedef typename Graph::OutEdgeIt OutEdgeIt;
4.134 + Graph& G;
4.135 + std::queue<OutEdgeIt>& bfs_queue;
4.136 + ReachedMap& reached;
4.137 + bool _newly_reached;
4.138 + bfs_iterator1(Graph& _G, std::queue<OutEdgeIt>& _bfs_queue, ReachedMap& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) {
4.139 + valid();
4.140 + if (!bfs_queue.empty() && bfs_queue.front().valid()) {
4.141 + OutEdgeIt e=bfs_queue.front();
4.142 + NodeIt w=G.bNode(e);
4.143 + if (!reached.get(w)) {
4.144 + bfs_queue.push(G.template first<OutEdgeIt>(w));
4.145 + reached.set(w, true);
4.146 + _newly_reached=true;
4.147 + } else {
4.148 + _newly_reached=false;
4.149 + }
4.150 + }
4.151 + }
4.152 + bfs_iterator1<Graph, ReachedMap>& operator++() {
4.153 + if (!valid()) return *this;
4.154 + ++(bfs_queue.front());
4.155 + valid();
4.156 + if (!bfs_queue.empty() && bfs_queue.front().valid()) {
4.157 + OutEdgeIt e=bfs_queue.front();
4.158 + NodeIt w=G.bNode(e);
4.159 + if (!reached.get(w)) {
4.160 + bfs_queue.push(G.template first<OutEdgeIt>(w));
4.161 + reached.set(w, true);
4.162 + _newly_reached=true;
4.163 + } else {
4.164 + _newly_reached=false;
4.165 + }
4.166 + }
4.167 + return *this;
4.168 + }
4.169 + bool valid() {
4.170 + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
4.171 + if (bfs_queue.empty()) return false; else return true;
4.172 + }
4.173 + operator OutEdgeIt() { return bfs_queue.front(); }
4.174 + //ize
4.175 + bool newly_reached() { return _newly_reached; }
4.176 +
4.177 + };
4.178 +
4.179 + template <typename Graph, typename OutEdgeIt, typename ReachedMap>
4.180 + struct BfsIterator {
4.181 + typedef typename Graph::NodeIt NodeIt;
4.182 + Graph& G;
4.183 + std::queue<OutEdgeIt>& bfs_queue;
4.184 + ReachedMap& reached;
4.185 + bool b_node_newly_reached;
4.186 + OutEdgeIt actual_edge;
4.187 + BfsIterator(Graph& _G,
4.188 + std::queue<OutEdgeIt>& _bfs_queue,
4.189 + ReachedMap& _reached) :
4.190 + G(_G), bfs_queue(_bfs_queue), reached(_reached) {
4.191 + actual_edge=bfs_queue.front();
4.192 + if (actual_edge.valid()) {
4.193 + NodeIt w=G.bNode(actual_edge);
4.194 + if (!reached.get(w)) {
4.195 + bfs_queue.push(G.firstOutEdge(w));
4.196 + reached.set(w, true);
4.197 + b_node_newly_reached=true;
4.198 + } else {
4.199 + b_node_newly_reached=false;
4.200 + }
4.201 + }
4.202 + }
4.203 + BfsIterator<Graph, OutEdgeIt, ReachedMap>&
4.204 + operator++() {
4.205 + if (bfs_queue.front().valid()) {
4.206 + ++(bfs_queue.front());
4.207 + actual_edge=bfs_queue.front();
4.208 + if (actual_edge.valid()) {
4.209 + NodeIt w=G.bNode(actual_edge);
4.210 + if (!reached.get(w)) {
4.211 + bfs_queue.push(G.firstOutEdge(w));
4.212 + reached.set(w, true);
4.213 + b_node_newly_reached=true;
4.214 + } else {
4.215 + b_node_newly_reached=false;
4.216 + }
4.217 + }
4.218 + } else {
4.219 + bfs_queue.pop();
4.220 + actual_edge=bfs_queue.front();
4.221 + if (actual_edge.valid()) {
4.222 + NodeIt w=G.bNode(actual_edge);
4.223 + if (!reached.get(w)) {
4.224 + bfs_queue.push(G.firstOutEdge(w));
4.225 + reached.set(w, true);
4.226 + b_node_newly_reached=true;
4.227 + } else {
4.228 + b_node_newly_reached=false;
4.229 + }
4.230 + }
4.231 + }
4.232 + return *this;
4.233 + }
4.234 + bool finished() { return bfs_queue.empty(); }
4.235 + operator OutEdgeIt () { return actual_edge; }
4.236 + bool bNodeIsNewlyReached() { return b_node_newly_reached; }
4.237 + bool aNodeIsExamined() { return !(actual_edge.valid()); }
4.238 + };
4.239 +
4.240 +
4.241 + template <typename Graph, typename OutEdgeIt, typename ReachedMap>
4.242 + struct DfsIterator {
4.243 + typedef typename Graph::NodeIt NodeIt;
4.244 + Graph& G;
4.245 + std::stack<OutEdgeIt>& bfs_queue;
4.246 + ReachedMap& reached;
4.247 + bool b_node_newly_reached;
4.248 + OutEdgeIt actual_edge;
4.249 + DfsIterator(Graph& _G,
4.250 + std::stack<OutEdgeIt>& _bfs_queue,
4.251 + ReachedMap& _reached) :
4.252 + G(_G), bfs_queue(_bfs_queue), reached(_reached) {
4.253 + actual_edge=bfs_queue.top();
4.254 + if (actual_edge.valid()) {
4.255 + NodeIt w=G.bNode(actual_edge);
4.256 + if (!reached.get(w)) {
4.257 + bfs_queue.push(G.firstOutEdge(w));
4.258 + reached.set(w, true);
4.259 + b_node_newly_reached=true;
4.260 + } else {
4.261 + ++(bfs_queue.top());
4.262 + b_node_newly_reached=false;
4.263 + }
4.264 + } else {
4.265 + bfs_queue.pop();
4.266 + }
4.267 + }
4.268 + DfsIterator<Graph, OutEdgeIt, ReachedMap>&
4.269 + operator++() {
4.270 + actual_edge=bfs_queue.top();
4.271 + if (actual_edge.valid()) {
4.272 + NodeIt w=G.bNode(actual_edge);
4.273 + if (!reached.get(w)) {
4.274 + bfs_queue.push(G.firstOutEdge(w));
4.275 + reached.set(w, true);
4.276 + b_node_newly_reached=true;
4.277 + } else {
4.278 + ++(bfs_queue.top());
4.279 + b_node_newly_reached=false;
4.280 + }
4.281 + } else {
4.282 + bfs_queue.pop();
4.283 + }
4.284 + return *this;
4.285 + }
4.286 + bool finished() { return bfs_queue.empty(); }
4.287 + operator OutEdgeIt () { return actual_edge; }
4.288 + bool bNodeIsNewlyReached() { return b_node_newly_reached; }
4.289 + bool aNodeIsExamined() { return !(actual_edge.valid()); }
4.290 + };
4.291 +
4.292 + template <typename Graph, typename OutEdgeIt, typename ReachedMap>
4.293 + struct BfsIterator1 {
4.294 + typedef typename Graph::NodeIt NodeIt;
4.295 + Graph& G;
4.296 + std::queue<OutEdgeIt>& bfs_queue;
4.297 + ReachedMap& reached;
4.298 + bool b_node_newly_reached;
4.299 + OutEdgeIt actual_edge;
4.300 + BfsIterator1(Graph& _G,
4.301 + std::queue<OutEdgeIt>& _bfs_queue,
4.302 + ReachedMap& _reached) :
4.303 + G(_G), bfs_queue(_bfs_queue), reached(_reached) {
4.304 + actual_edge=bfs_queue.front();
4.305 + if (actual_edge.valid()) {
4.306 + NodeIt w=G.bNode(actual_edge);
4.307 + if (!reached.get(w)) {
4.308 + bfs_queue.push(OutEdgeIt(G, w));
4.309 + reached.set(w, true);
4.310 + b_node_newly_reached=true;
4.311 + } else {
4.312 + b_node_newly_reached=false;
4.313 + }
4.314 + }
4.315 + }
4.316 + void next() {
4.317 + if (bfs_queue.front().valid()) {
4.318 + ++(bfs_queue.front());
4.319 + actual_edge=bfs_queue.front();
4.320 + if (actual_edge.valid()) {
4.321 + NodeIt w=G.bNode(actual_edge);
4.322 + if (!reached.get(w)) {
4.323 + bfs_queue.push(OutEdgeIt(G, w));
4.324 + reached.set(w, true);
4.325 + b_node_newly_reached=true;
4.326 + } else {
4.327 + b_node_newly_reached=false;
4.328 + }
4.329 + }
4.330 + } else {
4.331 + bfs_queue.pop();
4.332 + actual_edge=bfs_queue.front();
4.333 + if (actual_edge.valid()) {
4.334 + NodeIt w=G.bNode(actual_edge);
4.335 + if (!reached.get(w)) {
4.336 + bfs_queue.push(OutEdgeIt(G, w));
4.337 + reached.set(w, true);
4.338 + b_node_newly_reached=true;
4.339 + } else {
4.340 + b_node_newly_reached=false;
4.341 + }
4.342 + }
4.343 + }
4.344 + //return *this;
4.345 + }
4.346 + bool finished() { return bfs_queue.empty(); }
4.347 + operator OutEdgeIt () { return actual_edge; }
4.348 + bool bNodeIsNewlyReached() { return b_node_newly_reached; }
4.349 + bool aNodeIsExamined() { return !(actual_edge.valid()); }
4.350 + };
4.351 +
4.352 +
4.353 + template <typename Graph, typename OutEdgeIt, typename ReachedMap>
4.354 + struct DfsIterator1 {
4.355 + typedef typename Graph::NodeIt NodeIt;
4.356 + Graph& G;
4.357 + std::stack<OutEdgeIt>& bfs_queue;
4.358 + ReachedMap& reached;
4.359 + bool b_node_newly_reached;
4.360 + OutEdgeIt actual_edge;
4.361 + DfsIterator1(Graph& _G,
4.362 + std::stack<OutEdgeIt>& _bfs_queue,
4.363 + ReachedMap& _reached) :
4.364 + G(_G), bfs_queue(_bfs_queue), reached(_reached) {
4.365 + //actual_edge=bfs_queue.top();
4.366 + //if (actual_edge.valid()) {
4.367 + // NodeIt w=G.bNode(actual_edge);
4.368 + //if (!reached.get(w)) {
4.369 + // bfs_queue.push(OutEdgeIt(G, w));
4.370 + // reached.set(w, true);
4.371 + // b_node_newly_reached=true;
4.372 + //} else {
4.373 + // ++(bfs_queue.top());
4.374 + // b_node_newly_reached=false;
4.375 + //}
4.376 + //} else {
4.377 + // bfs_queue.pop();
4.378 + //}
4.379 + }
4.380 + void next() {
4.381 + actual_edge=bfs_queue.top();
4.382 + if (actual_edge.valid()) {
4.383 + NodeIt w=G.bNode(actual_edge);
4.384 + if (!reached.get(w)) {
4.385 + bfs_queue.push(OutEdgeIt(G, w));
4.386 + reached.set(w, true);
4.387 + b_node_newly_reached=true;
4.388 + } else {
4.389 + ++(bfs_queue.top());
4.390 + b_node_newly_reached=false;
4.391 + }
4.392 + } else {
4.393 + bfs_queue.pop();
4.394 + }
4.395 + //return *this;
4.396 + }
4.397 + bool finished() { return bfs_queue.empty(); }
4.398 + operator OutEdgeIt () { return actual_edge; }
4.399 + bool bNodeIsNewlyReached() { return b_node_newly_reached; }
4.400 + bool aNodeIsLeaved() { return !(actual_edge.valid()); }
4.401 + };
4.402 +
4.403 + template <typename Graph, typename OutEdgeIt, typename ReachedMap>
4.404 + class BfsIterator2 {
4.405 + typedef typename Graph::NodeIt NodeIt;
4.406 + const Graph& G;
4.407 + std::queue<OutEdgeIt> bfs_queue;
4.408 + ReachedMap reached;
4.409 + bool b_node_newly_reached;
4.410 + OutEdgeIt actual_edge;
4.411 + public:
4.412 + BfsIterator2(const Graph& _G) : G(_G), reached(G, false) { }
4.413 + void pushAndSetReached(NodeIt s) {
4.414 + reached.set(s, true);
4.415 + if (bfs_queue.empty()) {
4.416 + bfs_queue.push(G.template first<OutEdgeIt>(s));
4.417 + actual_edge=bfs_queue.front();
4.418 + if (actual_edge.valid()) {
4.419 + NodeIt w=G.bNode(actual_edge);
4.420 + if (!reached.get(w)) {
4.421 + bfs_queue.push(G.template first<OutEdgeIt>(w));
4.422 + reached.set(w, true);
4.423 + b_node_newly_reached=true;
4.424 + } else {
4.425 + b_node_newly_reached=false;
4.426 + }
4.427 + } //else {
4.428 + //}
4.429 + } else {
4.430 + bfs_queue.push(G.template first<OutEdgeIt>(s));
4.431 + }
4.432 + }
4.433 + BfsIterator2<Graph, OutEdgeIt, ReachedMap>&
4.434 + operator++() {
4.435 + if (bfs_queue.front().valid()) {
4.436 + ++(bfs_queue.front());
4.437 + actual_edge=bfs_queue.front();
4.438 + if (actual_edge.valid()) {
4.439 + NodeIt w=G.bNode(actual_edge);
4.440 + if (!reached.get(w)) {
4.441 + bfs_queue.push(G.template first<OutEdgeIt>(w));
4.442 + reached.set(w, true);
4.443 + b_node_newly_reached=true;
4.444 + } else {
4.445 + b_node_newly_reached=false;
4.446 + }
4.447 + }
4.448 + } else {
4.449 + bfs_queue.pop();
4.450 + if (!bfs_queue.empty()) {
4.451 + actual_edge=bfs_queue.front();
4.452 + if (actual_edge.valid()) {
4.453 + NodeIt w=G.bNode(actual_edge);
4.454 + if (!reached.get(w)) {
4.455 + bfs_queue.push(G.template first<OutEdgeIt>(w));
4.456 + reached.set(w, true);
4.457 + b_node_newly_reached=true;
4.458 + } else {
4.459 + b_node_newly_reached=false;
4.460 + }
4.461 + }
4.462 + }
4.463 + }
4.464 + return *this;
4.465 + }
4.466 + bool finished() const { return bfs_queue.empty(); }
4.467 + operator OutEdgeIt () const { return actual_edge; }
4.468 + bool isBNodeNewlyReached() const { return b_node_newly_reached; }
4.469 + bool isANodeExamined() const { return !(actual_edge.valid()); }
4.470 + const ReachedMap& getReachedMap() const { return reached; }
4.471 + const std::queue<OutEdgeIt>& getBfsQueue() const { return bfs_queue; }
4.472 + };
4.473 +
4.474 +
4.475 + template <typename Graph, typename OutEdgeIt, typename ReachedMap>
4.476 + class BfsIterator3 {
4.477 + typedef typename Graph::NodeIt NodeIt;
4.478 + const Graph& G;
4.479 + std::queue< std::pair<NodeIt, OutEdgeIt> > bfs_queue;
4.480 + ReachedMap reached;
4.481 + bool b_node_newly_reached;
4.482 + OutEdgeIt actual_edge;
4.483 + public:
4.484 + BfsIterator3(const Graph& _G) : G(_G), reached(G, false) { }
4.485 + void pushAndSetReached(NodeIt s) {
4.486 + reached.set(s, true);
4.487 + if (bfs_queue.empty()) {
4.488 + bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(s, G.template first<OutEdgeIt>(s)));
4.489 + actual_edge=bfs_queue.front().second;
4.490 + if (actual_edge.valid()) {
4.491 + NodeIt w=G.bNode(actual_edge);
4.492 + if (!reached.get(w)) {
4.493 + bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
4.494 + reached.set(w, true);
4.495 + b_node_newly_reached=true;
4.496 + } else {
4.497 + b_node_newly_reached=false;
4.498 + }
4.499 + } //else {
4.500 + //}
4.501 + } else {
4.502 + bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(s, G.template first<OutEdgeIt>(s)));
4.503 + }
4.504 + }
4.505 + BfsIterator3<Graph, OutEdgeIt, ReachedMap>&
4.506 + operator++() {
4.507 + if (bfs_queue.front().second.valid()) {
4.508 + ++(bfs_queue.front().second);
4.509 + actual_edge=bfs_queue.front().second;
4.510 + if (actual_edge.valid()) {
4.511 + NodeIt w=G.bNode(actual_edge);
4.512 + if (!reached.get(w)) {
4.513 + bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
4.514 + reached.set(w, true);
4.515 + b_node_newly_reached=true;
4.516 + } else {
4.517 + b_node_newly_reached=false;
4.518 + }
4.519 + }
4.520 + } else {
4.521 + bfs_queue.pop();
4.522 + if (!bfs_queue.empty()) {
4.523 + actual_edge=bfs_queue.front().second;
4.524 + if (actual_edge.valid()) {
4.525 + NodeIt w=G.bNode(actual_edge);
4.526 + if (!reached.get(w)) {
4.527 + bfs_queue.push(std::pair<NodeIt, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
4.528 + reached.set(w, true);
4.529 + b_node_newly_reached=true;
4.530 + } else {
4.531 + b_node_newly_reached=false;
4.532 + }
4.533 + }
4.534 + }
4.535 + }
4.536 + return *this;
4.537 + }
4.538 + bool finished() const { return bfs_queue.empty(); }
4.539 + operator OutEdgeIt () const { return actual_edge; }
4.540 + bool isBNodeNewlyReached() const { return b_node_newly_reached; }
4.541 + bool isANodeExamined() const { return !(actual_edge.valid()); }
4.542 + NodeIt aNode() const { return bfs_queue.front().first; }
4.543 + NodeIt bNode() const { return G.bNode(actual_edge); }
4.544 + const ReachedMap& getReachedMap() const { return reached; }
4.545 + //const std::queue< std::pair<NodeIt, OutEdgeIt> >& getBfsQueue() const { return bfs_queue; }
4.546 + };
4.547 +
4.548 +
4.549 + template <typename Graph, typename OutEdgeIt,
4.550 + typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
4.551 + class BfsIterator4 {
4.552 + typedef typename Graph::NodeIt NodeIt;
4.553 + const Graph& G;
4.554 + std::queue<NodeIt> bfs_queue;
4.555 + ReachedMap& reached;
4.556 + bool b_node_newly_reached;
4.557 + OutEdgeIt actual_edge;
4.558 + bool own_reached_map;
4.559 + public:
4.560 + BfsIterator4(const Graph& _G, ReachedMap& _reached) :
4.561 + G(_G), reached(_reached),
4.562 + own_reached_map(false) { }
4.563 + BfsIterator4(const Graph& _G) :
4.564 + G(_G), reached(*(new ReachedMap(G /*, false*/))),
4.565 + own_reached_map(true) { }
4.566 + ~BfsIterator4() { if (own_reached_map) delete &reached; }
4.567 + void pushAndSetReached(NodeIt s) {
4.568 + //std::cout << "mimi" << &reached << std::endl;
4.569 + reached.set(s, true);
4.570 + //std::cout << "mumus" << std::endl;
4.571 + if (bfs_queue.empty()) {
4.572 + //std::cout << "bibi1" << std::endl;
4.573 + bfs_queue.push(s);
4.574 + //std::cout << "zizi" << std::endl;
4.575 + G.getFirst(actual_edge, s);
4.576 + //std::cout << "kiki" << std::endl;
4.577 + if (G.valid(actual_edge)/*.valid()*/) {
4.578 + NodeIt w=G.bNode(actual_edge);
4.579 + if (!reached.get(w)) {
4.580 + bfs_queue.push(w);
4.581 + reached.set(w, true);
4.582 + b_node_newly_reached=true;
4.583 + } else {
4.584 + b_node_newly_reached=false;
4.585 + }
4.586 + }
4.587 + } else {
4.588 + //std::cout << "bibi2" << std::endl;
4.589 + bfs_queue.push(s);
4.590 + }
4.591 + }
4.592 + BfsIterator4<Graph, OutEdgeIt, ReachedMap>&
4.593 + operator++() {
4.594 + if (G.valid(actual_edge)/*.valid()*/) {
4.595 + /*++*/G.next(actual_edge);
4.596 + if (G.valid(actual_edge)/*.valid()*/) {
4.597 + NodeIt w=G.bNode(actual_edge);
4.598 + if (!reached.get(w)) {
4.599 + bfs_queue.push(w);
4.600 + reached.set(w, true);
4.601 + b_node_newly_reached=true;
4.602 + } else {
4.603 + b_node_newly_reached=false;
4.604 + }
4.605 + }
4.606 + } else {
4.607 + bfs_queue.pop();
4.608 + if (!bfs_queue.empty()) {
4.609 + G.getFirst(actual_edge, bfs_queue.front());
4.610 + if (G.valid(actual_edge)/*.valid()*/) {
4.611 + NodeIt w=G.bNode(actual_edge);
4.612 + if (!reached.get(w)) {
4.613 + bfs_queue.push(w);
4.614 + reached.set(w, true);
4.615 + b_node_newly_reached=true;
4.616 + } else {
4.617 + b_node_newly_reached=false;
4.618 + }
4.619 + }
4.620 + }
4.621 + }
4.622 + return *this;
4.623 + }
4.624 + bool finished() const { return bfs_queue.empty(); }
4.625 + operator OutEdgeIt () const { return actual_edge; }
4.626 + bool isBNodeNewlyReached() const { return b_node_newly_reached; }
4.627 + bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
4.628 + NodeIt aNode() const { return bfs_queue.front(); }
4.629 + NodeIt bNode() const { return G.bNode(actual_edge); }
4.630 + const ReachedMap& getReachedMap() const { return reached; }
4.631 + const std::queue<NodeIt>& getBfsQueue() const { return bfs_queue; }
4.632 + };
4.633 +
4.634 +
4.635 + template <typename GraphWrapper, /*typename OutEdgeIt,*/
4.636 + typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
4.637 + class BfsIterator5 {
4.638 + typedef typename GraphWrapper::NodeIt NodeIt;
4.639 + typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
4.640 + GraphWrapper G;
4.641 + std::queue<NodeIt> bfs_queue;
4.642 + ReachedMap& reached;
4.643 + bool b_node_newly_reached;
4.644 + OutEdgeIt actual_edge;
4.645 + bool own_reached_map;
4.646 + public:
4.647 + BfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) :
4.648 + G(_G), reached(_reached),
4.649 + own_reached_map(false) { }
4.650 + BfsIterator5(const GraphWrapper& _G) :
4.651 + G(_G), reached(*(new ReachedMap(G /*, false*/))),
4.652 + own_reached_map(true) { }
4.653 +// BfsIterator5(const typename GraphWrapper::BaseGraph& _G,
4.654 +// ReachedMap& _reached) :
4.655 +// G(_G), reached(_reached),
4.656 +// own_reached_map(false) { }
4.657 +// BfsIterator5(const typename GraphWrapper::BaseGraph& _G) :
4.658 +// G(_G), reached(*(new ReachedMap(G /*, false*/))),
4.659 +// own_reached_map(true) { }
4.660 + ~BfsIterator5() { if (own_reached_map) delete &reached; }
4.661 + void pushAndSetReached(NodeIt s) {
4.662 + reached.set(s, true);
4.663 + if (bfs_queue.empty()) {
4.664 + bfs_queue.push(s);
4.665 + G.getFirst(actual_edge, s);
4.666 + if (G.valid(actual_edge)/*.valid()*/) {
4.667 + NodeIt w=G.bNode(actual_edge);
4.668 + if (!reached.get(w)) {
4.669 + bfs_queue.push(w);
4.670 + reached.set(w, true);
4.671 + b_node_newly_reached=true;
4.672 + } else {
4.673 + b_node_newly_reached=false;
4.674 + }
4.675 + }
4.676 + } else {
4.677 + bfs_queue.push(s);
4.678 + }
4.679 + }
4.680 + BfsIterator5<GraphWrapper, /*OutEdgeIt,*/ ReachedMap>&
4.681 + operator++() {
4.682 + if (G.valid(actual_edge)/*.valid()*/) {
4.683 + /*++*/G.next(actual_edge);
4.684 + if (G.valid(actual_edge)/*.valid()*/) {
4.685 + NodeIt w=G.bNode(actual_edge);
4.686 + if (!reached.get(w)) {
4.687 + bfs_queue.push(w);
4.688 + reached.set(w, true);
4.689 + b_node_newly_reached=true;
4.690 + } else {
4.691 + b_node_newly_reached=false;
4.692 + }
4.693 + }
4.694 + } else {
4.695 + bfs_queue.pop();
4.696 + if (!bfs_queue.empty()) {
4.697 + G.getFirst(actual_edge, bfs_queue.front());
4.698 + if (G.valid(actual_edge)/*.valid()*/) {
4.699 + NodeIt w=G.bNode(actual_edge);
4.700 + if (!reached.get(w)) {
4.701 + bfs_queue.push(w);
4.702 + reached.set(w, true);
4.703 + b_node_newly_reached=true;
4.704 + } else {
4.705 + b_node_newly_reached=false;
4.706 + }
4.707 + }
4.708 + }
4.709 + }
4.710 + return *this;
4.711 + }
4.712 + bool finished() const { return bfs_queue.empty(); }
4.713 + operator OutEdgeIt () const { return actual_edge; }
4.714 + bool isBNodeNewlyReached() const { return b_node_newly_reached; }
4.715 + bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
4.716 + NodeIt aNode() const { return bfs_queue.front(); }
4.717 + NodeIt bNode() const { return G.bNode(actual_edge); }
4.718 + const ReachedMap& getReachedMap() const { return reached; }
4.719 + const std::queue<NodeIt>& getBfsQueue() const { return bfs_queue; }
4.720 + };
4.721 +
4.722 + template <typename Graph, typename OutEdgeIt,
4.723 + typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
4.724 + class DfsIterator4 {
4.725 + typedef typename Graph::NodeIt NodeIt;
4.726 + const Graph& G;
4.727 + std::stack<OutEdgeIt> dfs_stack;
4.728 + bool b_node_newly_reached;
4.729 + OutEdgeIt actual_edge;
4.730 + NodeIt actual_node;
4.731 + ReachedMap& reached;
4.732 + bool own_reached_map;
4.733 + public:
4.734 + DfsIterator4(const Graph& _G, ReachedMap& _reached) :
4.735 + G(_G), reached(_reached),
4.736 + own_reached_map(false) { }
4.737 + DfsIterator4(const Graph& _G) :
4.738 + G(_G), reached(*(new ReachedMap(G /*, false*/))),
4.739 + own_reached_map(true) { }
4.740 + ~DfsIterator4() { if (own_reached_map) delete &reached; }
4.741 + void pushAndSetReached(NodeIt s) {
4.742 + actual_node=s;
4.743 + reached.set(s, true);
4.744 + dfs_stack.push(G.template first<OutEdgeIt>(s));
4.745 + }
4.746 + DfsIterator4<Graph, OutEdgeIt, ReachedMap>&
4.747 + operator++() {
4.748 + actual_edge=dfs_stack.top();
4.749 + //actual_node=G.aNode(actual_edge);
4.750 + if (G.valid(actual_edge)/*.valid()*/) {
4.751 + NodeIt w=G.bNode(actual_edge);
4.752 + actual_node=w;
4.753 + if (!reached.get(w)) {
4.754 + dfs_stack.push(G.template first<OutEdgeIt>(w));
4.755 + reached.set(w, true);
4.756 + b_node_newly_reached=true;
4.757 + } else {
4.758 + actual_node=G.aNode(actual_edge);
4.759 + /*++*/G.next(dfs_stack.top());
4.760 + b_node_newly_reached=false;
4.761 + }
4.762 + } else {
4.763 + //actual_node=G.aNode(dfs_stack.top());
4.764 + dfs_stack.pop();
4.765 + }
4.766 + return *this;
4.767 + }
4.768 + bool finished() const { return dfs_stack.empty(); }
4.769 + operator OutEdgeIt () const { return actual_edge; }
4.770 + bool isBNodeNewlyReached() const { return b_node_newly_reached; }
4.771 + bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
4.772 + NodeIt aNode() const { return actual_node; /*FIXME*/}
4.773 + NodeIt bNode() const { return G.bNode(actual_edge); }
4.774 + const ReachedMap& getReachedMap() const { return reached; }
4.775 + const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
4.776 + };
4.777 +
4.778 + template <typename GraphWrapper, /*typename OutEdgeIt,*/
4.779 + typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
4.780 + class DfsIterator5 {
4.781 + typedef typename GraphWrapper::NodeIt NodeIt;
4.782 + typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
4.783 + GraphWrapper G;
4.784 + std::stack<OutEdgeIt> dfs_stack;
4.785 + bool b_node_newly_reached;
4.786 + OutEdgeIt actual_edge;
4.787 + NodeIt actual_node;
4.788 + ReachedMap& reached;
4.789 + bool own_reached_map;
4.790 + public:
4.791 + DfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) :
4.792 + G(_G), reached(_reached),
4.793 + own_reached_map(false) { }
4.794 + DfsIterator5(const GraphWrapper& _G) :
4.795 + G(_G), reached(*(new ReachedMap(G /*, false*/))),
4.796 + own_reached_map(true) { }
4.797 + ~DfsIterator5() { if (own_reached_map) delete &reached; }
4.798 + void pushAndSetReached(NodeIt s) {
4.799 + actual_node=s;
4.800 + reached.set(s, true);
4.801 + dfs_stack.push(G.template first<OutEdgeIt>(s));
4.802 + }
4.803 + DfsIterator5<GraphWrapper, /*OutEdgeIt,*/ ReachedMap>&
4.804 + operator++() {
4.805 + actual_edge=dfs_stack.top();
4.806 + //actual_node=G.aNode(actual_edge);
4.807 + if (G.valid(actual_edge)/*.valid()*/) {
4.808 + NodeIt w=G.bNode(actual_edge);
4.809 + actual_node=w;
4.810 + if (!reached.get(w)) {
4.811 + dfs_stack.push(G.template first<OutEdgeIt>(w));
4.812 + reached.set(w, true);
4.813 + b_node_newly_reached=true;
4.814 + } else {
4.815 + actual_node=G.aNode(actual_edge);
4.816 + /*++*/G.next(dfs_stack.top());
4.817 + b_node_newly_reached=false;
4.818 + }
4.819 + } else {
4.820 + //actual_node=G.aNode(dfs_stack.top());
4.821 + dfs_stack.pop();
4.822 + }
4.823 + return *this;
4.824 + }
4.825 + bool finished() const { return dfs_stack.empty(); }
4.826 + operator OutEdgeIt () const { return actual_edge; }
4.827 + bool isBNodeNewlyReached() const { return b_node_newly_reached; }
4.828 + bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
4.829 + NodeIt aNode() const { return actual_node; /*FIXME*/}
4.830 + NodeIt bNode() const { return G.bNode(actual_edge); }
4.831 + const ReachedMap& getReachedMap() const { return reached; }
4.832 + const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
4.833 + };
4.834 +
4.835 +
4.836 +
4.837 +} // namespace hugo
4.838 +
4.839 +#endif //BFS_ITERATOR_HH
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/src/work/marci/oldies/edmonds_karp.hh Sat Apr 03 14:41:31 2004 +0000
5.3 @@ -0,0 +1,677 @@
5.4 +#ifndef EDMONDS_KARP_HH
5.5 +#define EDMONDS_KARP_HH
5.6 +
5.7 +#include <algorithm>
5.8 +#include <list>
5.9 +#include <iterator>
5.10 +
5.11 +#include <bfs_iterator.hh>
5.12 +//#include <time_measure.h>
5.13 +
5.14 +namespace hugo {
5.15 +
5.16 + template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
5.17 + class ResGraph {
5.18 + public:
5.19 + typedef typename Graph::NodeIt NodeIt;
5.20 + typedef typename Graph::EachNodeIt EachNodeIt;
5.21 + private:
5.22 + typedef typename Graph::SymEdgeIt OldSymEdgeIt;
5.23 + const Graph& G;
5.24 + FlowMap& flow;
5.25 + const CapacityMap& capacity;
5.26 + public:
5.27 + ResGraph(const Graph& _G, FlowMap& _flow,
5.28 + const CapacityMap& _capacity) :
5.29 + G(_G), flow(_flow), capacity(_capacity) { }
5.30 +
5.31 + class EdgeIt;
5.32 + class OutEdgeIt;
5.33 + friend class EdgeIt;
5.34 + friend class OutEdgeIt;
5.35 +
5.36 + class EdgeIt {
5.37 + friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
5.38 + protected:
5.39 + const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
5.40 + OldSymEdgeIt sym;
5.41 + public:
5.42 + EdgeIt() { }
5.43 + //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
5.44 + Number free() const {
5.45 + if (resG->G.aNode(sym)==resG->G.tail(sym)) {
5.46 + return (resG->capacity.get(sym)-resG->flow.get(sym));
5.47 + } else {
5.48 + return (resG->flow.get(sym));
5.49 + }
5.50 + }
5.51 + bool valid() const { return sym.valid(); }
5.52 + void augment(Number a) const {
5.53 + if (resG->G.aNode(sym)==resG->G.tail(sym)) {
5.54 + resG->flow.set(sym, resG->flow.get(sym)+a);
5.55 + //resG->flow[sym]+=a;
5.56 + } else {
5.57 + resG->flow.set(sym, resG->flow.get(sym)-a);
5.58 + //resG->flow[sym]-=a;
5.59 + }
5.60 + }
5.61 + };
5.62 +
5.63 + class OutEdgeIt : public EdgeIt {
5.64 + friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
5.65 + public:
5.66 + OutEdgeIt() { }
5.67 + //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
5.68 + private:
5.69 + OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
5.70 + resG=&_resG;
5.71 + sym=resG->G.template first<OldSymEdgeIt>(v);
5.72 + while( sym.valid() && !(free()>0) ) { ++sym; }
5.73 + }
5.74 + public:
5.75 + OutEdgeIt& operator++() {
5.76 + ++sym;
5.77 + while( sym.valid() && !(free()>0) ) { ++sym; }
5.78 + return *this;
5.79 + }
5.80 + };
5.81 +
5.82 + void getFirst(OutEdgeIt& e, NodeIt v) const {
5.83 + e=OutEdgeIt(*this, v);
5.84 + }
5.85 + void getFirst(EachNodeIt& v) const { G.getFirst(v); }
5.86 +
5.87 + template< typename It >
5.88 + It first() const {
5.89 + It e;
5.90 + getFirst(e);
5.91 + return e;
5.92 + }
5.93 +
5.94 + template< typename It >
5.95 + It first(NodeIt v) const {
5.96 + It e;
5.97 + getFirst(e, v);
5.98 + return e;
5.99 + }
5.100 +
5.101 + NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
5.102 + NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
5.103 +
5.104 + NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
5.105 + NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
5.106 +
5.107 + int id(NodeIt v) const { return G.id(v); }
5.108 +
5.109 + template <typename S>
5.110 + class NodeMap {
5.111 + typename Graph::NodeMap<S> node_map;
5.112 + public:
5.113 + NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
5.114 + NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
5.115 + void set(NodeIt nit, S a) { node_map.set(nit, a); }
5.116 + S get(NodeIt nit) const { return node_map.get(nit); }
5.117 + S& operator[](NodeIt nit) { return node_map[nit]; }
5.118 + const S& operator[](NodeIt nit) const { return node_map[nit]; }
5.119 + };
5.120 +
5.121 + };
5.122 +
5.123 +
5.124 + template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
5.125 + class ResGraph2 {
5.126 + public:
5.127 + typedef typename Graph::NodeIt NodeIt;
5.128 + typedef typename Graph::EachNodeIt EachNodeIt;
5.129 + private:
5.130 + //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
5.131 + typedef typename Graph::OutEdgeIt OldOutEdgeIt;
5.132 + typedef typename Graph::InEdgeIt OldInEdgeIt;
5.133 +
5.134 + const Graph& G;
5.135 + FlowMap& flow;
5.136 + const CapacityMap& capacity;
5.137 + public:
5.138 + ResGraph2(const Graph& _G, FlowMap& _flow,
5.139 + const CapacityMap& _capacity) :
5.140 + G(_G), flow(_flow), capacity(_capacity) { }
5.141 +
5.142 + class EdgeIt;
5.143 + class OutEdgeIt;
5.144 + friend class EdgeIt;
5.145 + friend class OutEdgeIt;
5.146 +
5.147 + class EdgeIt {
5.148 + friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
5.149 + protected:
5.150 + const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
5.151 + //OldSymEdgeIt sym;
5.152 + OldOutEdgeIt out;
5.153 + OldInEdgeIt in;
5.154 + bool out_or_in; //true, iff out
5.155 + public:
5.156 + EdgeIt() : out_or_in(true) { }
5.157 + Number free() const {
5.158 + if (out_or_in) {
5.159 + return (resG->capacity.get(out)-resG->flow.get(out));
5.160 + } else {
5.161 + return (resG->flow.get(in));
5.162 + }
5.163 + }
5.164 + bool valid() const {
5.165 + return out_or_in && out.valid() || in.valid(); }
5.166 + void augment(Number a) const {
5.167 + if (out_or_in) {
5.168 + resG->flow.set(out, resG->flow.get(out)+a);
5.169 + } else {
5.170 + resG->flow.set(in, resG->flow.get(in)-a);
5.171 + }
5.172 + }
5.173 + };
5.174 +
5.175 + class OutEdgeIt : public EdgeIt {
5.176 + friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
5.177 + public:
5.178 + OutEdgeIt() { }
5.179 + private:
5.180 + OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
5.181 + resG=&_resG;
5.182 + out=resG->G.template first<OldOutEdgeIt>(v);
5.183 + while( out.valid() && !(free()>0) ) { ++out; }
5.184 + if (!out.valid()) {
5.185 + out_or_in=0;
5.186 + in=resG->G.template first<OldInEdgeIt>(v);
5.187 + while( in.valid() && !(free()>0) ) { ++in; }
5.188 + }
5.189 + }
5.190 + public:
5.191 + OutEdgeIt& operator++() {
5.192 + if (out_or_in) {
5.193 + NodeIt v=resG->G.aNode(out);
5.194 + ++out;
5.195 + while( out.valid() && !(free()>0) ) { ++out; }
5.196 + if (!out.valid()) {
5.197 + out_or_in=0;
5.198 + in=resG->G.template first<OldInEdgeIt>(v);
5.199 + while( in.valid() && !(free()>0) ) { ++in; }
5.200 + }
5.201 + } else {
5.202 + ++in;
5.203 + while( in.valid() && !(free()>0) ) { ++in; }
5.204 + }
5.205 + return *this;
5.206 + }
5.207 + };
5.208 +
5.209 + void getFirst(OutEdgeIt& e, NodeIt v) const {
5.210 + e=OutEdgeIt(*this, v);
5.211 + }
5.212 + void getFirst(EachNodeIt& v) const { G.getFirst(v); }
5.213 +
5.214 + template< typename It >
5.215 + It first() const {
5.216 + It e;
5.217 + getFirst(e);
5.218 + return e;
5.219 + }
5.220 +
5.221 + template< typename It >
5.222 + It first(NodeIt v) const {
5.223 + It e;
5.224 + getFirst(e, v);
5.225 + return e;
5.226 + }
5.227 +
5.228 + NodeIt tail(EdgeIt e) const {
5.229 + return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
5.230 + NodeIt head(EdgeIt e) const {
5.231 + return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
5.232 +
5.233 + NodeIt aNode(OutEdgeIt e) const {
5.234 + return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
5.235 + NodeIt bNode(OutEdgeIt e) const {
5.236 + return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
5.237 +
5.238 + int id(NodeIt v) const { return G.id(v); }
5.239 +
5.240 + template <typename S>
5.241 + class NodeMap {
5.242 + typename Graph::NodeMap<S> node_map;
5.243 + public:
5.244 + NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
5.245 + NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
5.246 + void set(NodeIt nit, S a) { node_map.set(nit, a); }
5.247 + S get(NodeIt nit) const { return node_map.get(nit); }
5.248 + };
5.249 + };
5.250 +
5.251 +
5.252 + template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
5.253 + class MaxFlow {
5.254 + public:
5.255 + typedef typename Graph::NodeIt NodeIt;
5.256 + typedef typename Graph::EdgeIt EdgeIt;
5.257 + typedef typename Graph::EachEdgeIt EachEdgeIt;
5.258 + typedef typename Graph::OutEdgeIt OutEdgeIt;
5.259 + typedef typename Graph::InEdgeIt InEdgeIt;
5.260 +
5.261 + private:
5.262 + const Graph* G;
5.263 + NodeIt s;
5.264 + NodeIt t;
5.265 + FlowMap* flow;
5.266 + const CapacityMap* capacity;
5.267 + typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
5.268 + typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
5.269 + typedef typename AugGraph::EdgeIt AugEdgeIt;
5.270 +
5.271 + //AugGraph res_graph;
5.272 + //typedef typename AugGraph::NodeMap<bool> ReachedMap;
5.273 + //typename AugGraph::NodeMap<AugEdgeIt> pred;
5.274 + //typename AugGraph::NodeMap<Number> free;
5.275 + public:
5.276 + MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
5.277 + G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //,
5.278 + //res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
5.279 + { }
5.280 + bool augmentOnShortestPath() {
5.281 + AugGraph res_graph(*G, *flow, *capacity);
5.282 + bool _augment=false;
5.283 +
5.284 + typedef typename AugGraph::NodeMap<bool> ReachedMap;
5.285 + BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph);
5.286 + res_bfs.pushAndSetReached(s);
5.287 +
5.288 + typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
5.289 + //filled up with invalid iterators
5.290 + //pred.set(s, AugEdgeIt());
5.291 +
5.292 + typename AugGraph::NodeMap<Number> free(res_graph);
5.293 +
5.294 + //searching for augmenting path
5.295 + while ( !res_bfs.finished() ) {
5.296 + AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
5.297 + if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) {
5.298 + NodeIt v=res_graph.tail(e);
5.299 + NodeIt w=res_graph.head(e);
5.300 + pred.set(w, e);
5.301 + if (res_graph.valid(pred.get(v))) {
5.302 + free.set(w, std::min(free.get(v), res_graph.free(e)));
5.303 + } else {
5.304 + free.set(w, res_graph.free(e));
5.305 + }
5.306 + if (res_graph.head(e)==t) { _augment=true; break; }
5.307 + }
5.308 +
5.309 + ++res_bfs;
5.310 + } //end of searching augmenting path
5.311 +
5.312 + if (_augment) {
5.313 + NodeIt n=t;
5.314 + Number augment_value=free.get(t);
5.315 + while (res_graph.valid(pred.get(n))) {
5.316 + AugEdgeIt e=pred.get(n);
5.317 + res_graph.augment(e, augment_value);
5.318 + //e.augment(augment_value);
5.319 + n=res_graph.tail(e);
5.320 + }
5.321 + }
5.322 +
5.323 + return _augment;
5.324 + }
5.325 +
5.326 + template<typename MutableGraph> bool augmentOnBlockingFlow() {
5.327 + bool _augment=false;
5.328 +
5.329 + AugGraph res_graph(*G, *flow, *capacity);
5.330 +
5.331 + typedef typename AugGraph::NodeMap<bool> ReachedMap;
5.332 + BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph);
5.333 +
5.334 + bfs.pushAndSetReached(s);
5.335 + typename AugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
5.336 + while ( !bfs.finished() ) {
5.337 + AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
5.338 + if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
5.339 + dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
5.340 + }
5.341 +
5.342 + ++bfs;
5.343 + } //computing distances from s in the residual graph
5.344 +
5.345 + MutableGraph F;
5.346 + typename AugGraph::NodeMap<NodeIt> res_graph_to_F(res_graph);
5.347 + for(typename AugGraph::EachNodeIt n=res_graph.template first<typename AugGraph::EachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
5.348 + res_graph_to_F.set(n, F.addNode());
5.349 + }
5.350 +
5.351 + typename MutableGraph::NodeIt sF=res_graph_to_F.get(s);
5.352 + typename MutableGraph::NodeIt tF=res_graph_to_F.get(t);
5.353 +
5.354 + typename MutableGraph::EdgeMap<AugEdgeIt> original_edge(F);
5.355 + typename MutableGraph::EdgeMap<Number> residual_capacity(F);
5.356 +
5.357 + //Making F to the graph containing the edges of the residual graph
5.358 + //which are in some shortest paths
5.359 + for(typename AugGraph::EachEdgeIt e=res_graph.template first<typename AugGraph::EachEdgeIt>(); res_graph.valid(e); res_graph.next(e)) {
5.360 + if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) {
5.361 + typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e)));
5.362 + original_edge.update();
5.363 + original_edge.set(f, e);
5.364 + residual_capacity.update();
5.365 + residual_capacity.set(f, res_graph.free(e));
5.366 + }
5.367 + }
5.368 +
5.369 + bool __augment=true;
5.370 +
5.371 + while (__augment) {
5.372 + __augment=false;
5.373 + //computing blocking flow with dfs
5.374 + typedef typename MutableGraph::NodeMap<bool> BlockingReachedMap;
5.375 + DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F);
5.376 + typename MutableGraph::NodeMap<EdgeIt> pred(F); //invalid iterators
5.377 + typename MutableGraph::NodeMap<Number> free(F);
5.378 +
5.379 + dfs.pushAndSetReached(sF);
5.380 + while (!dfs.finished()) {
5.381 + ++dfs;
5.382 + if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) {
5.383 + if (dfs.isBNodeNewlyReached()) {
5.384 +// std::cout << "OutEdgeIt: " << dfs;
5.385 +// std::cout << " aNode: " << F.aNode(dfs);
5.386 +// std::cout << " bNode: " << F.bNode(dfs) << " ";
5.387 +
5.388 + typename MutableGraph::NodeIt v=F.aNode(dfs);
5.389 + typename MutableGraph::NodeIt w=F.bNode(dfs);
5.390 + pred.set(w, dfs);
5.391 + if (F.valid(pred.get(v))) {
5.392 + free.set(w, std::min(free.get(v), residual_capacity.get(dfs)));
5.393 + } else {
5.394 + free.set(w, residual_capacity.get(dfs));
5.395 + }
5.396 + if (w==tF) {
5.397 + //std::cout << "AUGMENTATION"<<std::endl;
5.398 + __augment=true;
5.399 + _augment=true;
5.400 + break;
5.401 + }
5.402 +
5.403 + } else {
5.404 + F.erase(typename MutableGraph::OutEdgeIt(dfs));
5.405 + }
5.406 + }
5.407 + }
5.408 +
5.409 + if (__augment) {
5.410 + typename MutableGraph::NodeIt n=tF;
5.411 + Number augment_value=free.get(tF);
5.412 + while (F.valid(pred.get(n))) {
5.413 + typename MutableGraph::EdgeIt e=pred.get(n);
5.414 + res_graph.augment(original_edge.get(e), augment_value);
5.415 + //original_edge.get(e).augment(augment_value);
5.416 + n=F.tail(e);
5.417 + if (residual_capacity.get(e)==augment_value)
5.418 + F.erase(e);
5.419 + else
5.420 + residual_capacity.set(e, residual_capacity.get(e)-augment_value);
5.421 + }
5.422 + }
5.423 +
5.424 + }
5.425 +
5.426 + return _augment;
5.427 + }
5.428 + bool augmentOnBlockingFlow2() {
5.429 + bool _augment=false;
5.430 +
5.431 + //typedef ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> EAugGraph;
5.432 + typedef FilterGraphWrapper< ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap> > EAugGraph;
5.433 + typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt;
5.434 + typedef typename EAugGraph::EdgeIt EAugEdgeIt;
5.435 +
5.436 + EAugGraph res_graph(*G, *flow, *capacity);
5.437 +
5.438 + //std::cout << "meg jo1" << std::endl;
5.439 +
5.440 + //typedef typename EAugGraph::NodeMap<bool> ReachedMap;
5.441 + BfsIterator4<
5.442 + ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>,
5.443 + ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt,
5.444 + ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::NodeMap<bool> > bfs(res_graph);
5.445 +
5.446 + //std::cout << "meg jo2" << std::endl;
5.447 +
5.448 + bfs.pushAndSetReached(s);
5.449 + //std::cout << "meg jo2.5" << std::endl;
5.450 +
5.451 + //typename EAugGraph::NodeMap<int> dist(res_graph); //filled up with 0's
5.452 + typename ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::
5.453 + NodeMap<int>& dist=res_graph.dist;
5.454 + //std::cout << "meg jo2.6" << std::endl;
5.455 +
5.456 + while ( !bfs.finished() ) {
5.457 + ErasingResGraphWrapper<Graph, Number, FlowMap, CapacityMap>::OutEdgeIt e=bfs;
5.458 +// EAugOutEdgeIt e=/*AugOutEdgeIt*/(bfs);
5.459 + //if (res_graph.valid(e)) {
5.460 + // std::cout<<"a:"<<res_graph.tail(e)<<"b:"<<res_graph.head(e)<<std::endl;
5.461 + //}
5.462 + if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
5.463 + dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1);
5.464 + }
5.465 +
5.466 + ++bfs;
5.467 + } //computing distances from s in the residual graph
5.468 +
5.469 +
5.470 + //std::cout << "meg jo3" << std::endl;
5.471 +
5.472 +// typedef typename EAugGraph::EachNodeIt EAugEachNodeIt;
5.473 +// for(EAugEachNodeIt n=res_graph.template first<EAugEachNodeIt>(); res_graph.valid(n); res_graph.next(n)) {
5.474 +// std::cout << "dist: " << dist.get(n) << std::endl;
5.475 +// }
5.476 +
5.477 + bool __augment=true;
5.478 +
5.479 + while (__augment) {
5.480 +// std::cout << "new iteration"<< std::endl;
5.481 +
5.482 + __augment=false;
5.483 + //computing blocking flow with dfs
5.484 + typedef typename EAugGraph::NodeMap<bool> BlockingReachedMap;
5.485 + DfsIterator4< EAugGraph, EAugOutEdgeIt, BlockingReachedMap >
5.486 + dfs(res_graph);
5.487 + typename EAugGraph::NodeMap<EAugEdgeIt> pred(res_graph); //invalid iterators
5.488 + typename EAugGraph::NodeMap<Number> free(res_graph);
5.489 +
5.490 + dfs.pushAndSetReached(s);
5.491 + while (!dfs.finished()) {
5.492 + ++dfs;
5.493 + if (res_graph.valid(EAugOutEdgeIt(dfs))) {
5.494 + if (dfs.isBNodeNewlyReached()) {
5.495 +// std::cout << "OutEdgeIt: " << dfs;
5.496 +// std::cout << " aNode: " << res_graph.aNode(dfs);
5.497 +// std::cout << " res cap: " << EAugOutEdgeIt(dfs).free();
5.498 +// std::cout << " bNode: " << res_graph.bNode(dfs) << " ";
5.499 +
5.500 + typename EAugGraph::NodeIt v=res_graph.aNode(dfs);
5.501 + typename EAugGraph::NodeIt w=res_graph.bNode(dfs);
5.502 +
5.503 + pred.set(w, EAugOutEdgeIt(dfs));
5.504 +
5.505 + //std::cout << EAugOutEdgeIt(dfs).free() << std::endl;
5.506 + if (res_graph.valid(pred.get(v))) {
5.507 + free.set(w, std::min(free.get(v), res_graph.free(/*EAugOutEdgeIt*/(dfs))));
5.508 + } else {
5.509 + free.set(w, res_graph.free(/*EAugOutEdgeIt*/(dfs)));
5.510 + }
5.511 +
5.512 + if (w==t) {
5.513 +// std::cout << "t is reached, AUGMENTATION"<<std::endl;
5.514 + __augment=true;
5.515 + _augment=true;
5.516 + break;
5.517 + }
5.518 + } else {
5.519 +// std::cout << "<<DELETE ";
5.520 +// std::cout << " aNode: " << res_graph.aNode(dfs);
5.521 +// std::cout << " res cap: " << EAugOutEdgeIt(dfs).free();
5.522 +// std::cout << " bNode: " << res_graph.bNode(dfs) << " ";
5.523 +// std::cout << "DELETE>> ";
5.524 +
5.525 + res_graph.erase(dfs);
5.526 + }
5.527 + }
5.528 +
5.529 + }
5.530 +
5.531 + if (__augment) {
5.532 + typename EAugGraph::NodeIt n=t;
5.533 + Number augment_value=free.get(t);
5.534 +// std::cout << "av:" << augment_value << std::endl;
5.535 + while (res_graph.valid(pred.get(n))) {
5.536 + EAugEdgeIt e=pred.get(n);
5.537 + res_graph.augment(e, augment_value);
5.538 + //e.augment(augment_value);
5.539 + n=res_graph.tail(e);
5.540 + if (res_graph.free(e)==0)
5.541 + res_graph.erase(e);
5.542 + }
5.543 + }
5.544 +
5.545 + }
5.546 +
5.547 + return _augment;
5.548 + }
5.549 + void run() {
5.550 + //int num_of_augmentations=0;
5.551 + while (augmentOnShortestPath()) {
5.552 + //while (augmentOnBlockingFlow<MutableGraph>()) {
5.553 + //std::cout << ++num_of_augmentations << " ";
5.554 + //std::cout<<std::endl;
5.555 + }
5.556 + }
5.557 + template<typename MutableGraph> void run() {
5.558 + //int num_of_augmentations=0;
5.559 + //while (augmentOnShortestPath()) {
5.560 + while (augmentOnBlockingFlow<MutableGraph>()) {
5.561 + //std::cout << ++num_of_augmentations << " ";
5.562 + //std::cout<<std::endl;
5.563 + }
5.564 + }
5.565 + Number flowValue() {
5.566 + Number a=0;
5.567 + OutEdgeIt e;
5.568 + for(G->getFirst(e, s); G->valid(e); G->next(e)) {
5.569 + a+=flow->get(e);
5.570 + }
5.571 + return a;
5.572 + }
5.573 + };
5.574 +
5.575 +
5.576 +// template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
5.577 +// class MaxFlow2 {
5.578 +// public:
5.579 +// typedef typename Graph::NodeIt NodeIt;
5.580 +// typedef typename Graph::EdgeIt EdgeIt;
5.581 +// typedef typename Graph::EachEdgeIt EachEdgeIt;
5.582 +// typedef typename Graph::OutEdgeIt OutEdgeIt;
5.583 +// typedef typename Graph::InEdgeIt InEdgeIt;
5.584 +// private:
5.585 +// const Graph& G;
5.586 +// std::list<NodeIt>& S;
5.587 +// std::list<NodeIt>& T;
5.588 +// FlowMap& flow;
5.589 +// const CapacityMap& capacity;
5.590 +// typedef ResGraphWrapper<Graph, Number, FlowMap, CapacityMap > AugGraph;
5.591 +// typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
5.592 +// typedef typename AugGraph::EdgeIt AugEdgeIt;
5.593 +// typename Graph::NodeMap<bool> SMap;
5.594 +// typename Graph::NodeMap<bool> TMap;
5.595 +// public:
5.596 +// MaxFlow2(const Graph& _G, std::list<NodeIt>& _S, std::list<NodeIt>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) {
5.597 +// for(typename std::list<NodeIt>::const_iterator i=S.begin();
5.598 +// i!=S.end(); ++i) {
5.599 +// SMap.set(*i, true);
5.600 +// }
5.601 +// for (typename std::list<NodeIt>::const_iterator i=T.begin();
5.602 +// i!=T.end(); ++i) {
5.603 +// TMap.set(*i, true);
5.604 +// }
5.605 +// }
5.606 +// bool augment() {
5.607 +// AugGraph res_graph(G, flow, capacity);
5.608 +// bool _augment=false;
5.609 +// NodeIt reached_t_node;
5.610 +
5.611 +// typedef typename AugGraph::NodeMap<bool> ReachedMap;
5.612 +// BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
5.613 +// for(typename std::list<NodeIt>::const_iterator i=S.begin();
5.614 +// i!=S.end(); ++i) {
5.615 +// res_bfs.pushAndSetReached(*i);
5.616 +// }
5.617 +// //res_bfs.pushAndSetReached(s);
5.618 +
5.619 +// typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
5.620 +// //filled up with invalid iterators
5.621 +
5.622 +// typename AugGraph::NodeMap<Number> free(res_graph);
5.623 +
5.624 +// //searching for augmenting path
5.625 +// while ( !res_bfs.finished() ) {
5.626 +// AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs);
5.627 +// if (e.valid() && res_bfs.isBNodeNewlyReached()) {
5.628 +// NodeIt v=res_graph.tail(e);
5.629 +// NodeIt w=res_graph.head(e);
5.630 +// pred.set(w, e);
5.631 +// if (pred.get(v).valid()) {
5.632 +// free.set(w, std::min(free.get(v), e.free()));
5.633 +// } else {
5.634 +// free.set(w, e.free());
5.635 +// }
5.636 +// if (TMap.get(res_graph.head(e))) {
5.637 +// _augment=true;
5.638 +// reached_t_node=res_graph.head(e);
5.639 +// break;
5.640 +// }
5.641 +// }
5.642 +
5.643 +// ++res_bfs;
5.644 +// } //end of searching augmenting path
5.645 +
5.646 +// if (_augment) {
5.647 +// NodeIt n=reached_t_node;
5.648 +// Number augment_value=free.get(reached_t_node);
5.649 +// while (pred.get(n).valid()) {
5.650 +// AugEdgeIt e=pred.get(n);
5.651 +// e.augment(augment_value);
5.652 +// n=res_graph.tail(e);
5.653 +// }
5.654 +// }
5.655 +
5.656 +// return _augment;
5.657 +// }
5.658 +// void run() {
5.659 +// while (augment()) { }
5.660 +// }
5.661 +// Number flowValue() {
5.662 +// Number a=0;
5.663 +// for(typename std::list<NodeIt>::const_iterator i=S.begin();
5.664 +// i!=S.end(); ++i) {
5.665 +// for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
5.666 +// a+=flow.get(e);
5.667 +// }
5.668 +// for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
5.669 +// a-=flow.get(e);
5.670 +// }
5.671 +// }
5.672 +// return a;
5.673 +// }
5.674 +// };
5.675 +
5.676 +
5.677 +
5.678 +} // namespace hugo
5.679 +
5.680 +#endif //EDMONDS_KARP_HH
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/src/work/marci/oldies/list_graph.hh Sat Apr 03 14:41:31 2004 +0000
6.3 @@ -0,0 +1,552 @@
6.4 +#ifndef LIST_GRAPH_HH
6.5 +#define LIST_GRAPH_HH
6.6 +
6.7 +#include <iostream>
6.8 +#include <vector>
6.9 +
6.10 +namespace hugo {
6.11 +
6.12 + template <typename It>
6.13 + int count(It it) {
6.14 + int i=0;
6.15 + for( ; it.valid(); ++it) { ++i; }
6.16 + return i;
6.17 + }
6.18 +
6.19 + class ListGraph {
6.20 +
6.21 + class node_item;
6.22 + class edge_item;
6.23 +
6.24 + public:
6.25 +
6.26 + class NodeIt;
6.27 + class EachNodeIt;
6.28 + class EdgeIt;
6.29 + class EachEdgeIt;
6.30 + class OutEdgeIt;
6.31 + class InEdgeIt;
6.32 + class SymEdgeIt;
6.33 + template <typename T> class NodeMap;
6.34 + template <typename T> class EdgeMap;
6.35 +
6.36 + private:
6.37 +
6.38 + template <typename T> friend class NodeMap;
6.39 + template <typename T> friend class EdgeMap;
6.40 +
6.41 + template <typename T>
6.42 + class NodeMap {
6.43 + const ListGraph& G;
6.44 + std::vector<T> container;
6.45 + public:
6.46 + typedef T ValueType;
6.47 + typedef NodeIt KeyType;
6.48 + NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { }
6.49 + NodeMap(const ListGraph& _G, T a) :
6.50 + G(_G), container(G.node_id, a) { }
6.51 + void set(NodeIt n, T a) { container[/*G.id(n)*/n.node->id]=a; }
6.52 + T get(NodeIt n) const { return container[/*G.id(n)*/n.node->id]; }
6.53 + T& operator[](NodeIt n) { return container[/*G.id(n)*/n.node->id]; }
6.54 + const T& operator[](NodeIt n) const { return container[/*G.id(n)*/n.node->id]; }
6.55 + void update() { container.resize(G.node_id); }
6.56 + void update(T a) { container.resize(G.node_id, a); }
6.57 + };
6.58 +
6.59 + template <typename T>
6.60 + class EdgeMap {
6.61 + const ListGraph& G;
6.62 + std::vector<T> container;
6.63 + public:
6.64 + typedef T ValueType;
6.65 + typedef EdgeIt KeyType;
6.66 + EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { }
6.67 + EdgeMap(const ListGraph& _G, T a) :
6.68 + G(_G), container(G.edge_id, a) { }
6.69 + void set(EdgeIt e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
6.70 + T get(EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; }
6.71 + T& operator[](EdgeIt e) { return container[/*G.id(e)*/e.edge->id]; }
6.72 + const T& operator[](EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; }
6.73 + void update() { container.resize(G.edge_id); }
6.74 + void update(T a) { container.resize(G.edge_id, a); }
6.75 + };
6.76 +
6.77 + int node_id;
6.78 + int edge_id;
6.79 + int _node_num;
6.80 + int _edge_num;
6.81 +
6.82 + node_item* _first_node;
6.83 + node_item* _last_node;
6.84 +
6.85 + class node_item {
6.86 + friend class ListGraph;
6.87 + template <typename T> friend class NodeMap;
6.88 +
6.89 + friend class NodeIt;
6.90 + friend class EachNodeIt;
6.91 + friend class EdgeIt;
6.92 + friend class EachEdgeIt;
6.93 + friend class OutEdgeIt;
6.94 + friend class InEdgeIt;
6.95 + friend class SymEdgeIt;
6.96 + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
6.97 + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
6.98 + //ListGraph* G;
6.99 + int id;
6.100 + edge_item* _first_out_edge;
6.101 + edge_item* _last_out_edge;
6.102 + edge_item* _first_in_edge;
6.103 + edge_item* _last_in_edge;
6.104 + node_item* _next_node;
6.105 + node_item* _prev_node;
6.106 + public:
6.107 + node_item() { }
6.108 + };
6.109 +
6.110 + class edge_item {
6.111 + friend class ListGraph;
6.112 + template <typename T> friend class EdgeMap;
6.113 +
6.114 + friend class NodeIt;
6.115 + friend class EachNodeIt;
6.116 + friend class EdgeIt;
6.117 + friend class EachEdgeIt;
6.118 + friend class OutEdgeIt;
6.119 + friend class InEdgeIt;
6.120 + friend class SymEdgeIt;
6.121 + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
6.122 + //ListGraph* G;
6.123 + int id;
6.124 + node_item* _tail;
6.125 + node_item* _head;
6.126 + edge_item* _next_out;
6.127 + edge_item* _prev_out;
6.128 + edge_item* _next_in;
6.129 + edge_item* _prev_in;
6.130 + public:
6.131 + edge_item() { }
6.132 + };
6.133 +
6.134 + node_item* _add_node() {
6.135 + node_item* p=new node_item;
6.136 + p->id=node_id++;
6.137 + p->_first_out_edge=0;
6.138 + p->_last_out_edge=0;
6.139 + p->_first_in_edge=0;
6.140 + p->_last_in_edge=0;
6.141 + p->_prev_node=_last_node;
6.142 + p->_next_node=0;
6.143 + if (_last_node) _last_node->_next_node=p;
6.144 + _last_node=p;
6.145 + if (!_first_node) _first_node=p;
6.146 +
6.147 + ++_node_num;
6.148 + return p;
6.149 + }
6.150 +
6.151 + edge_item* _add_edge(node_item* _tail, node_item* _head) {
6.152 + edge_item* e=new edge_item;
6.153 + e->id=edge_id++;
6.154 + e->_tail=_tail;
6.155 + e->_head=_head;
6.156 +
6.157 + e->_prev_out=_tail->_last_out_edge;
6.158 + if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
6.159 + _tail->_last_out_edge=e;
6.160 + if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
6.161 + e->_next_out=0;
6.162 +
6.163 + e->_prev_in=_head->_last_in_edge;
6.164 + if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
6.165 + _head->_last_in_edge=e;
6.166 + if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
6.167 + e->_next_in=0;
6.168 +
6.169 + ++_edge_num;
6.170 + return e;
6.171 + }
6.172 +
6.173 + //deletes a node which has no out edge and no in edge
6.174 + void _delete_node(node_item* v) {
6.175 + if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else
6.176 + _last_node=v->_prev_node;
6.177 + if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else
6.178 + _first_node=v->_next_node;
6.179 +
6.180 + delete v;
6.181 + --_node_num;
6.182 + }
6.183 +
6.184 + void _delete_edge(edge_item* e) {
6.185 + if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
6.186 + (e->_tail)->_last_out_edge=e->_prev_out;
6.187 + if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
6.188 + (e->_tail)->_first_out_edge=e->_next_out;
6.189 + if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
6.190 + (e->_head)->_last_in_edge=e->_prev_in;
6.191 + if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
6.192 + (e->_head)->_first_in_edge=e->_next_in;
6.193 +
6.194 + delete e;
6.195 + --_edge_num;
6.196 + }
6.197 +
6.198 + void _set_tail(edge_item* e, node_item* _tail) {
6.199 + if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else
6.200 + (e->_tail)->_last_out_edge=e->_prev_out;
6.201 + if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else
6.202 + (e->_tail)->_first_out_edge=e->_next_out;
6.203 +
6.204 + e->_tail=_tail;
6.205 +
6.206 + e->_prev_out=_tail->_last_out_edge;
6.207 + if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
6.208 + _tail->_last_out_edge=e;
6.209 + if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
6.210 + e->_next_out=0;
6.211 + }
6.212 +
6.213 + void _set_head(edge_item* e, node_item* _head) {
6.214 + if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else
6.215 + (e->_head)->_last_in_edge=e->_prev_in;
6.216 + if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else
6.217 + (e->_head)->_first_in_edge=e->_next_in;
6.218 +
6.219 + e->_head=_head;
6.220 +
6.221 + e->_prev_in=_head->_last_in_edge;
6.222 + if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
6.223 + _head->_last_in_edge=e;
6.224 + if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
6.225 + e->_next_in=0;
6.226 + }
6.227 +
6.228 + public:
6.229 +
6.230 + /* default constructor */
6.231 +
6.232 + ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
6.233 +
6.234 + ~ListGraph() {
6.235 + while (first<EachNodeIt>().valid()) erase(first<EachNodeIt>());
6.236 + }
6.237 +
6.238 + int nodeNum() const { return _node_num; }
6.239 + int edgeNum() const { return _edge_num; }
6.240 +
6.241 + /* functions to construct iterators from the graph, or from each other */
6.242 +
6.243 + //EachNodeIt firstNode() const { return EachNodeIt(*this); }
6.244 + //EachEdgeIt firstEdge() const { return EachEdgeIt(*this); }
6.245 +
6.246 + //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); }
6.247 + //InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); }
6.248 + //SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); }
6.249 + NodeIt tail(EdgeIt e) const { return e.tailNode(); }
6.250 + NodeIt head(EdgeIt e) const { return e.headNode(); }
6.251 +
6.252 + NodeIt aNode(const OutEdgeIt& e) const { return e.aNode(); }
6.253 + NodeIt aNode(const InEdgeIt& e) const { return e.aNode(); }
6.254 + NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); }
6.255 +
6.256 + NodeIt bNode(const OutEdgeIt& e) const { return e.bNode(); }
6.257 + NodeIt bNode(const InEdgeIt& e) const { return e.bNode(); }
6.258 + NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); }
6.259 +
6.260 + //NodeIt invalid_node() { return NodeIt(); }
6.261 + //EdgeIt invalid_edge() { return EdgeIt(); }
6.262 + //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
6.263 + //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
6.264 + //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
6.265 +
6.266 + /* same methods in other style */
6.267 + /* for experimental purpose */
6.268 +
6.269 + EachNodeIt& getFirst(EachNodeIt& v) const {
6.270 + v=EachNodeIt(*this); return v; }
6.271 + EachEdgeIt& getFirst(EachEdgeIt& e) const {
6.272 + e=EachEdgeIt(*this); return e; }
6.273 + OutEdgeIt& getFirst(OutEdgeIt& e, NodeIt v) const {
6.274 + e=OutEdgeIt(v); return e; }
6.275 + InEdgeIt& getFirst(InEdgeIt& e, NodeIt v) const {
6.276 + e=InEdgeIt(v); return e; }
6.277 + SymEdgeIt& getFirst(SymEdgeIt& e, NodeIt v) const {
6.278 + e=SymEdgeIt(v); return e; }
6.279 + //void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); }
6.280 + //void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); }
6.281 +
6.282 + //void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); }
6.283 + //void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); }
6.284 + //void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); }
6.285 + //void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); }
6.286 + //void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); }
6.287 + //void getBNode(NodeIt& n, const SymEdgeIt& e) const { n=e.bNode(); }
6.288 + //void get_invalid(NodeIt& n) { n=NodeIt(); }
6.289 + //void get_invalid(EdgeIt& e) { e=EdgeIt(); }
6.290 + //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
6.291 + //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
6.292 + //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
6.293 +
6.294 + template< typename It >
6.295 + It first() const {
6.296 + It e;
6.297 + getFirst(e);
6.298 + return e;
6.299 + }
6.300 +
6.301 + template< typename It >
6.302 + It first(NodeIt v) const {
6.303 + It e;
6.304 + getFirst(e, v);
6.305 + return e;
6.306 + }
6.307 +
6.308 + bool valid(NodeIt n) const { return n.valid(); }
6.309 + bool valid(EdgeIt e) const { return e.valid(); }
6.310 +
6.311 + template <typename It> It getNext(It it) const {
6.312 + It tmp(it); return next(tmp); }
6.313 + template <typename It> It& next(It& it) const { return ++it; }
6.314 +
6.315 +
6.316 + /* for getting id's of graph objects */
6.317 + /* these are important for the implementation of property vectors */
6.318 +
6.319 + int id(NodeIt v) const { return v.node->id; }
6.320 + int id(EdgeIt e) const { return e.edge->id; }
6.321 +
6.322 + /* adding nodes and edges */
6.323 +
6.324 + NodeIt addNode() { return NodeIt(_add_node()); }
6.325 + EdgeIt addEdge(NodeIt u, NodeIt v) {
6.326 + return EdgeIt(_add_edge(u.node, v.node));
6.327 + }
6.328 +
6.329 + void erase(NodeIt i) {
6.330 + while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
6.331 + while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
6.332 + _delete_node(i.node);
6.333 + }
6.334 +
6.335 + void erase(EdgeIt e) { _delete_edge(e.edge); }
6.336 +
6.337 + void clear() {
6.338 + while (first<EachNodeIt>().valid()) erase(first<EachNodeIt>());
6.339 + }
6.340 +
6.341 + void setTail(EdgeIt e, NodeIt tail) {
6.342 + _set_tail(e.edge, tail.node);
6.343 + }
6.344 +
6.345 + void setHead(EdgeIt e, NodeIt head) {
6.346 + _set_head(e.edge, head.node);
6.347 + }
6.348 +
6.349 + /* stream operations, for testing purpose */
6.350 +
6.351 + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i) {
6.352 + os << i.node->id; return os;
6.353 + }
6.354 + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i) {
6.355 + os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
6.356 + return os;
6.357 + }
6.358 +
6.359 + class NodeIt {
6.360 + friend class ListGraph;
6.361 + template <typename T> friend class NodeMap;
6.362 +
6.363 + friend class EdgeIt;
6.364 + friend class OutEdgeIt;
6.365 + friend class InEdgeIt;
6.366 + friend class SymEdgeIt;
6.367 + //public: //FIXME: It is required by op= of EachNodeIt
6.368 + protected:
6.369 + node_item* node;
6.370 + protected:
6.371 + friend int ListGraph::id(NodeIt v) const;
6.372 + public:
6.373 + NodeIt() : node(0) { }
6.374 + NodeIt(node_item* _node) : node(_node) { }
6.375 + bool valid() const { return (node!=0); }
6.376 + //void makeInvalid() { node=0; }
6.377 + friend bool operator==(const NodeIt& u, const NodeIt& v) {
6.378 + return v.node==u.node;
6.379 + }
6.380 + friend bool operator!=(const NodeIt& u, const NodeIt& v) {
6.381 + return v.node!=u.node;
6.382 + }
6.383 + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i);
6.384 + };
6.385 +
6.386 + class EachNodeIt : public NodeIt {
6.387 + friend class ListGraph;
6.388 + //protected:
6.389 + public: //for everybody but marci
6.390 + EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { }
6.391 + public:
6.392 + EachNodeIt() : NodeIt() { }
6.393 + EachNodeIt(node_item* v) : NodeIt(v) { }
6.394 + EachNodeIt& operator++() { node=node->_next_node; return *this; }
6.395 + //FIXME::
6.396 + // EachNodeIt& operator=(const NodeIt& e)
6.397 + // { node=e.node; return *this; }
6.398 + };
6.399 +
6.400 + class EdgeIt {
6.401 + friend class ListGraph;
6.402 + template <typename T> friend class EdgeMap;
6.403 +
6.404 + friend class NodeIt;
6.405 + friend class EachNodeIt;
6.406 + protected:
6.407 + edge_item* edge;
6.408 + friend int ListGraph::id(EdgeIt e) const;
6.409 + public:
6.410 + EdgeIt() : edge(0) { }
6.411 + //EdgeIt() { }
6.412 + EdgeIt(edge_item* _edge) : edge(_edge) { }
6.413 + bool valid() const { return (edge!=0); }
6.414 + //void makeInvalid() { edge=0; }
6.415 + friend bool operator==(const EdgeIt& u, const EdgeIt& v) {
6.416 + return v.edge==u.edge;
6.417 + }
6.418 + friend bool operator!=(const EdgeIt& u, const EdgeIt& v) {
6.419 + return v.edge!=u.edge;
6.420 + }
6.421 + protected:
6.422 + NodeIt tailNode() const { return NodeIt(edge->_tail); }
6.423 + NodeIt headNode() const { return NodeIt(edge->_head); }
6.424 + public:
6.425 + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i);
6.426 + };
6.427 +
6.428 + class EachEdgeIt : public EdgeIt {
6.429 + friend class ListGraph;
6.430 + //protected:
6.431 + public: //for alpar
6.432 + EachEdgeIt(const ListGraph& G) {
6.433 + node_item* v=G._first_node;
6.434 + if (v) edge=v->_first_out_edge; else edge=0;
6.435 + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
6.436 + }
6.437 + public:
6.438 + EachEdgeIt() : EdgeIt() { }
6.439 + EachEdgeIt(edge_item* _e) : EdgeIt(_e) { }
6.440 + EachEdgeIt& operator++() {
6.441 + node_item* v=edge->_tail;
6.442 + edge=edge->_next_out;
6.443 + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
6.444 + return *this;
6.445 + }
6.446 + };
6.447 +
6.448 + class OutEdgeIt : public EdgeIt {
6.449 + friend class ListGraph;
6.450 + //node_item* v;
6.451 + //protected:
6.452 + public: //for alpar
6.453 + OutEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
6.454 + public:
6.455 + OutEdgeIt() : EdgeIt()/*, v(0)*/ { }
6.456 + OutEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
6.457 + OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
6.458 + protected:
6.459 + NodeIt aNode() const { return NodeIt(edge->_tail); }
6.460 + NodeIt bNode() const { return NodeIt(edge->_head); }
6.461 + };
6.462 +
6.463 + class InEdgeIt : public EdgeIt {
6.464 + friend class ListGraph;
6.465 + //node_item* v;
6.466 + //protected:
6.467 + public: //for alpar
6.468 + InEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
6.469 + public:
6.470 + InEdgeIt() : EdgeIt()/*, v(0)*/ { }
6.471 + InEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
6.472 + InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
6.473 + protected:
6.474 + NodeIt aNode() const { return NodeIt(edge->_head); }
6.475 + NodeIt bNode() const { return NodeIt(edge->_tail); }
6.476 + };
6.477 +
6.478 + class SymEdgeIt : public EdgeIt {
6.479 + friend class ListGraph;
6.480 + bool out_or_in; //1 iff out, 0 iff in
6.481 + //node_item* v;
6.482 + //protected:
6.483 + public: //for alpar
6.484 + SymEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ {
6.485 + out_or_in=1;
6.486 + edge=_v.node->_first_out_edge;
6.487 + if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
6.488 + }
6.489 + public:
6.490 + SymEdgeIt() : EdgeIt() /*, v(0)*/ { }
6.491 + SymEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ {
6.492 + out_or_in=1;
6.493 + edge=_v.node->_first_out_edge;
6.494 + if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
6.495 + }
6.496 + SymEdgeIt& operator++() {
6.497 + if (out_or_in) {
6.498 + node_item* v=edge->_tail;
6.499 + edge=edge->_next_out;
6.500 + if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
6.501 + } else {
6.502 + edge=edge->_next_in;
6.503 + }
6.504 + return *this;
6.505 + }
6.506 + protected:
6.507 + NodeIt aNode() const {
6.508 + return (out_or_in) ? NodeIt(edge->_tail) : NodeIt(edge->_head); }
6.509 + NodeIt bNode() const {
6.510 + return (out_or_in) ? NodeIt(edge->_head) : NodeIt(edge->_tail); }
6.511 + };
6.512 +
6.513 + };
6.514 +
6.515 +// template< typename T >
6.516 +// T ListGraph::first() const {
6.517 +// std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>();" << std::endl;
6.518 +// return T();
6.519 +// }
6.520 +
6.521 +// template<>
6.522 +// ListGraph::EachNodeIt ListGraph::first<ListGraph::EachNodeIt>() const {
6.523 +// return firstNode();
6.524 +// }
6.525 +
6.526 +// template<>
6.527 +// ListGraph::EachEdgeIt ListGraph::first<ListGraph::EachEdgeIt>() const {
6.528 +// return firstEdge();
6.529 +// }
6.530 +
6.531 +// template< typename T >
6.532 +// T ListGraph::first(ListGraph::NodeIt v) const {
6.533 +// std::cerr << "Invalid use of template<typemane T> T ListGraph::first<T>(ListGRaph::NodeIt);" << std::endl;
6.534 +// return T();
6.535 +// }
6.536 +
6.537 +// template<>
6.538 +// ListGraph::OutEdgeIt ListGraph::first<ListGraph::OutEdgeIt>(const ListGraph::NodeIt v) const {
6.539 +// return firstOutEdge(v);
6.540 +// }
6.541 +
6.542 +// template<>
6.543 +// ListGraph::InEdgeIt ListGraph::first<ListGraph::InEdgeIt>(const ListGraph::NodeIt v) const {
6.544 +// return firstInEdge(v);
6.545 +// }
6.546 +
6.547 +// template<>
6.548 +// ListGraph::SymEdgeIt ListGraph::first<ListGraph::SymEdgeIt>(const ListGraph::NodeIt v) const {
6.549 +// return firstSymEdge(v);
6.550 +// }
6.551 +
6.552 +
6.553 +} //namespace hugo
6.554 +
6.555 +#endif //LIST_GRAPH_HH
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/src/work/marci/oldies/marci_bfs.hh Sat Apr 03 14:41:31 2004 +0000
7.3 @@ -0,0 +1,176 @@
7.4 +#ifndef MARCI_BFS_HH
7.5 +#define MARCI_BFS_HH
7.6 +
7.7 +#include <queue>
7.8 +
7.9 +#include <marci_property_vector.hh>
7.10 +
7.11 +namespace hugo {
7.12 +
7.13 + template <typename graph_type>
7.14 + struct bfs {
7.15 + typedef typename graph_type::node_iterator node_iterator;
7.16 + typedef typename graph_type::edge_iterator edge_iterator;
7.17 + typedef typename graph_type::each_node_iterator each_node_iterator;
7.18 + typedef typename graph_type::out_edge_iterator out_edge_iterator;
7.19 + graph_type& G;
7.20 + node_iterator s;
7.21 + node_property_vector<graph_type, bool> reached;
7.22 + node_property_vector<graph_type, edge_iterator> pred;
7.23 + node_property_vector<graph_type, int> dist;
7.24 + std::queue<node_iterator> bfs_queue;
7.25 + bfs(graph_type& _G, node_iterator _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) {
7.26 + bfs_queue.push(s);
7.27 + for(each_node_iterator i=G.first_node(); i.valid(); ++i)
7.28 + reached.put(i, false);
7.29 + reached.put(s, true);
7.30 + dist.put(s, 0);
7.31 + }
7.32 +
7.33 + void run() {
7.34 + while (!bfs_queue.empty()) {
7.35 + node_iterator v=bfs_queue.front();
7.36 + out_edge_iterator e=G.first_out_edge(v);
7.37 + bfs_queue.pop();
7.38 + for( ; e.valid(); ++e) {
7.39 + node_iterator w=G.head(e);
7.40 + std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl;
7.41 + if (!reached.get(w)) {
7.42 + std::cout << G.id(w) << " is newly reached :-)" << std::endl;
7.43 + bfs_queue.push(w);
7.44 + dist.put(w, dist.get(v)+1);
7.45 + pred.put(w, e);
7.46 + reached.put(w, true);
7.47 + } else {
7.48 + std::cout << G.id(w) << " is already reached" << std::endl;
7.49 + }
7.50 + }
7.51 + }
7.52 + }
7.53 + };
7.54 +
7.55 + template <typename graph_type>
7.56 + struct bfs_visitor {
7.57 + typedef typename graph_type::node_iterator node_iterator;
7.58 + typedef typename graph_type::edge_iterator edge_iterator;
7.59 + typedef typename graph_type::out_edge_iterator out_edge_iterator;
7.60 + graph_type& G;
7.61 + bfs_visitor(graph_type& _G) : G(_G) { }
7.62 + void at_previously_reached(out_edge_iterator& e) {
7.63 + //node_iterator v=G.tail(e);
7.64 + node_iterator w=G.head(e);
7.65 + std::cout << G.id(w) << " is already reached" << std::endl;
7.66 + }
7.67 + void at_newly_reached(out_edge_iterator& e) {
7.68 + //node_iterator v=G.tail(e);
7.69 + node_iterator w=G.head(e);
7.70 + std::cout << G.id(w) << " is newly reached :-)" << std::endl;
7.71 + }
7.72 + };
7.73 +
7.74 + template <typename graph_type, typename reached_type, typename visitor_type>
7.75 + struct bfs_iterator {
7.76 + typedef typename graph_type::node_iterator node_iterator;
7.77 + typedef typename graph_type::edge_iterator edge_iterator;
7.78 + typedef typename graph_type::out_edge_iterator out_edge_iterator;
7.79 + graph_type& G;
7.80 + std::queue<out_edge_iterator>& bfs_queue;
7.81 + reached_type& reached;
7.82 + visitor_type& visitor;
7.83 + void process() {
7.84 + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.85 + if (bfs_queue.empty()) return;
7.86 + out_edge_iterator e=bfs_queue.front();
7.87 + //node_iterator v=G.tail(e);
7.88 + node_iterator w=G.head(e);
7.89 + if (!reached.get(w)) {
7.90 + visitor.at_newly_reached(e);
7.91 + bfs_queue.push(G.first_out_edge(w));
7.92 + reached.put(w, true);
7.93 + } else {
7.94 + visitor.at_previously_reached(e);
7.95 + }
7.96 + }
7.97 + bfs_iterator(graph_type& _G, std::queue<out_edge_iterator>& _bfs_queue, reached_type& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) {
7.98 + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.99 + valid();
7.100 + }
7.101 + bfs_iterator<graph_type, reached_type, visitor_type>& operator++() {
7.102 + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.103 + //if (bfs_queue.empty()) return *this;
7.104 + if (!valid()) return *this;
7.105 + ++(bfs_queue.front());
7.106 + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.107 + valid();
7.108 + return *this;
7.109 + }
7.110 + //void next() {
7.111 + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.112 + // if (bfs_queue.empty()) return;
7.113 + // ++(bfs_queue.front());
7.114 + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.115 + //}
7.116 + bool valid() {
7.117 + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.118 + if (bfs_queue.empty()) return false; else return true;
7.119 + }
7.120 + //bool finished() {
7.121 + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.122 + // if (bfs_queue.empty()) return true; else return false;
7.123 + //}
7.124 + operator edge_iterator () { return bfs_queue.front(); }
7.125 +
7.126 + };
7.127 +
7.128 + template <typename graph_type, typename reached_type>
7.129 + struct bfs_iterator1 {
7.130 + typedef typename graph_type::node_iterator node_iterator;
7.131 + typedef typename graph_type::edge_iterator edge_iterator;
7.132 + typedef typename graph_type::out_edge_iterator out_edge_iterator;
7.133 + graph_type& G;
7.134 + std::queue<out_edge_iterator>& bfs_queue;
7.135 + reached_type& reached;
7.136 + bool _newly_reached;
7.137 + bfs_iterator1(graph_type& _G, std::queue<out_edge_iterator>& _bfs_queue, reached_type& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) {
7.138 + valid();
7.139 + if (!bfs_queue.empty() && bfs_queue.front().valid()) {
7.140 + out_edge_iterator e=bfs_queue.front();
7.141 + node_iterator w=G.head(e);
7.142 + if (!reached.get(w)) {
7.143 + bfs_queue.push(G.first_out_edge(w));
7.144 + reached.put(w, true);
7.145 + _newly_reached=true;
7.146 + } else {
7.147 + _newly_reached=false;
7.148 + }
7.149 + }
7.150 + }
7.151 + bfs_iterator1<graph_type, reached_type>& operator++() {
7.152 + if (!valid()) return *this;
7.153 + ++(bfs_queue.front());
7.154 + valid();
7.155 + if (!bfs_queue.empty() && bfs_queue.front().valid()) {
7.156 + out_edge_iterator e=bfs_queue.front();
7.157 + node_iterator w=G.head(e);
7.158 + if (!reached.get(w)) {
7.159 + bfs_queue.push(G.first_out_edge(w));
7.160 + reached.put(w, true);
7.161 + _newly_reached=true;
7.162 + } else {
7.163 + _newly_reached=false;
7.164 + }
7.165 + }
7.166 + return *this;
7.167 + }
7.168 + bool valid() {
7.169 + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
7.170 + if (bfs_queue.empty()) return false; else return true;
7.171 + }
7.172 + operator edge_iterator () { return bfs_queue.front(); }
7.173 + bool newly_reached() { return _newly_reached; }
7.174 +
7.175 + };
7.176 +
7.177 +} // namespace hugo
7.178 +
7.179 +#endif //MARCI_BFS_HH
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/src/work/marci/oldies/marci_graph_concept.txt Sat Apr 03 14:41:31 2004 +0000
8.3 @@ -0,0 +1,217 @@
8.4 +ETIK-OL-NOLIB-NEGRES graph concept-ek.
8.5 +
8.6 + Ebben a dokumentacioban graph concept tervek es azok megvalositasarol irok.
8.7 +A felsorolt rutinok, osztalyok egyaltalan nem kikristalyosodottak, 1-1 elemi
8.8 +operacio elvegzesere gyakran tobb mod is rendelkezesre all. A tervezesi fazisban pont annak kell kiderulnie, hogy milyen metodusok tavolithatok el, s milyen
8.9 +ujakra van szukseg.
8.10 +
8.11 + Megvalositottunk egy graph osztalyt mely listaban tarolja a pontokat,
8.12 +az 1 pontbol kiindulo eleket, s az 1 pontba bemeno eleket. Konstrualni lehet
8.13 +ures grafot, hozzaadni pontokat, eleket. Az incidenciat node_iteratorok-kal
8.14 +ill. edge_iteratorokkal lehet megfigyelni. Adott tovabba 1 template osztaly,
8.15 +a graf pontjaihoz vagy eleihez tetszoleges tipusu property hozzarendelesere,
8.16 +a jelen megvalositas ezeket vektorben tarolja. Fontos azonban, hogy ezen
8.17 +property_vector csak azokra a graf-objektumokra ervenyes, melyek
8.18 +letrehozasanak pillanataban a grafhoz tartoznak.
8.19 +
8.20 +marci_bfs.hh //bfs, tejesen kiserleti
8.21 +marci_graph_demo.cc //peldaprogi a lisas graf hasznalatahoz
8.22 +marci_list_graph.hh //list_graph megvalositas
8.23 +marci_max_flow.hh //folyam, kiserleti
8.24 +marci_property_vector.hh //property vector megvalosites indexelt grafokhoz
8.25 +graf es iterator tipusok:
8.26 +
8.27 +class list_graph;
8.28 +
8.29 +class node_iterator;
8.30 +trivialis node iterator, csak cimezni lehet vele, pl property vectort
8.31 +
8.32 +class each_node_iterator;
8.33 +node iterator a graf pontjainak bejarasara, node_iterator-ra konvertalhato
8.34 +
8.35 +class edge_iterator;
8.36 +trivialis edge iterator, csak cimezni lehet vele, pl property vectort
8.37 +
8.38 +class each_edge_iterator;
8.39 +edge iterator a graf osszes elenek bejarasara
8.40 +
8.41 +class out_edge_iterator;
8.42 +edge iterator 1 pont ki eleinek bejarasara, edge_iterator-ra konvertalhato
8.43 +
8.44 +class in_edge_iterator;
8.45 +edge iterator 1 pont be eleinek bejarasara, edge_iterator-ra konvertalhato
8.46 +
8.47 +class sym_edge_iterator;
8.48 +edge iterator 1 pont be es ki eleinek bejarasara, edge_iterator-ra
8.49 +konvertalhato
8.50 +
8.51 +default constructor:
8.52 +
8.53 +list_graph();
8.54 +
8.55 +A graf osztaly fobb publikus metodusai, az alapveto hasznalathoz:
8.56 +Hasonlo funkciok megvalosithatok 1 kesobb leirt modon, illetve
8.57 +ezek kozul nehany az iteratorok metodusaival, megis azt javasolnam, hogy az
8.58 +iteratorok metodusait ne hasznaljuk. Miert? Azt szeretnenk, ha 1 ponthalmazon
8.59 +van 2 graf, es csak az elhalmazhoz keszitunk uj iteratorokat, akkor pl 1 pont
8.60 +out-edge-iteratora megkaphato legyen a grafbol es a node_iteratorbol. Ezert
8.61 +out_edge_iterator(const node_iterator&) hasznalata nem javasolt,
8.62 +esetleg majd szamuzzuk a concept-bol, s akkor nem nesz baj.
8.63 +
8.64 +each_node_iterator first_node();
8.65 +each_edge_iterator first_edge();
8.66 +out_edge_iterator first_out_edge(const node_iterator&);
8.67 +in_edge_iterator first_in_edge(const node_iterator&);
8.68 +sym_edge_iterator first_sym_edge(const node_iterator&);
8.69 +
8.70 +node_iterator tail(const edge_iterator&);
8.71 +node_iterator head(const edge_iterator&);
8.72 +
8.73 +node_iterator a_node(const out_edge_iterator&);
8.74 +node_iterator a_node(const in_edge_iterator&);
8.75 +node_iterator a_node(const sym_edge_iterator&);
8.76 +//az out, in or sym edge iterator rogzitett pontjara ad 1 node_iterator-t
8.77 +
8.78 +node_iterator b_node(const out_edge_iterator&);
8.79 +node_iterator b_node(const in_edge_iterator&);
8.80 +node_iterator b_node(const sym_edge_iterator&);
8.81 +//az out, in or sym edge iterator nem rogzitett pontjara ad 1 node_iterator-t
8.82 +
8.83 +//node_iterator invalid_node();
8.84 +//edge_iterator invalid_edge();
8.85 +//out_edge_iterator invalid_out_edge();
8.86 +//in_edge_iterator invalid_in_edge();
8.87 +//sym_edge_iterator invalid_sym_edge();
8.88 +
8.89 +//az iteratorok ures konstruktorai meghatarozatlan
8.90 +tartalmu konstruktort adnak vissza, ezekkel a matodusokkal
8.91 +lehet ervenytelent csinalni.
8.92 +Lehet hogy ezt az ures konstruktorral kellene, tessek vitatkozni.
8.93 +
8.94 +Kiserleti cellal ugyanezen fv-ek mas stilusu megvalositasai:
8.95 +
8.96 +void get_first(each_node_iterator&);
8.97 +void get_first(each_edge_iterator&);
8.98 +void get_first(out_edge_iterator&, const node_iterator&);
8.99 +void get_first(in_edge_iterator&, const node_iterator&);
8.100 +void get_first(sym_edge_iterator&, const node_iterator&);
8.101 +
8.102 +void get_tail(node_iterator&, const edge_iterator&);
8.103 +void get_head(node_iterator&, const edge_iterator&);
8.104 +
8.105 +void get_a_node(node_iterator&, const out_edge_iterator&);
8.106 +void get_a_node(node_iterator&, const in_edge_iterator&);
8.107 +void get_a_node(node_iterator&, const sym_edge_iterator&);
8.108 +
8.109 +void get_b_node(node_iterator&, const out_edge_iterator&);
8.110 +void get_b_node(node_iterator&, const in_edge_iterator&);
8.111 +void get_b_node(node_iterator&, const sym_edge_iterator&);
8.112 +
8.113 +//void get_invalid(node_iterator&);
8.114 +//void get_invalid(edge_iterator&);
8.115 +//void get_invalid(out_edge_iterator&);
8.116 +//void get_invalid(in_edge_iterator&);
8.117 +//void get_invalid(sym_edge_iterator&);
8.118 +
8.119 +Pontok azonositasara de meginkabb property vectorokhoz:
8.120 +
8.121 +int id(const node_iterator&);
8.122 +int id(const edge_iterator&);
8.123 +
8.124 +Pontok es elek hozzaadasanak metodusai:
8.125 +
8.126 +node_iterator add_node();
8.127 +edge_iterator add_edge(const node_iterator&, const node_iterator&);
8.128 +
8.129 +Hogy konnyebb legyen a progikat tesztelni, nehany stream utasitas:
8.130 +ezek nem a list_graph metodusai
8.131 +
8.132 +friend std::ostream& operator<<(std::ostream&, const node_iterator&);
8.133 +friend std::ostream& operator<<(std::ostream&, const edge_iterator&);
8.134 +
8.135 +node_iterator metodusai:
8.136 +node_iterator();
8.137 +bool valid();
8.138 +void make_invalid();
8.139 +ezek nem tagfuggvenyek:
8.140 +friend bool operator==(const node_iterator&, const node_iterator&);
8.141 +friend bool operator!=(const node_iterator& u, const node_iterator& v);
8.142 +
8.143 +each_node_iterator metodusai:
8.144 +ez publikusan szarmazik a node_iterator-bol, tehat a fentiek is.
8.145 +each_node_iterator();
8.146 +each_node_iterator& operator++();
8.147 +
8.148 +edge_iterator metodusai:
8.149 +edge_iterator();
8.150 +bool valid();
8.151 +void make_invalid();
8.152 +ezek nem tagfvek:
8.153 +friend bool operator==(const edge_iterator&, const edge_iterator&);
8.154 +friend bool operator!=(const edge_iterator&, const edge_iterator&);
8.155 +ujra tagfv-ek.
8.156 +//node_iterator tail_node() const; nem javasolt
8.157 +//node_iterator head_node() const; nem javasolt
8.158 +
8.159 +each_edge_iterator metodusai:
8.160 +edge_iterator-bol szarmazik
8.161 +each_edge_iterator();
8.162 +each_edge_iterator& operator++();
8.163 +
8.164 +out_edge_iterator metodusai:
8.165 +edge_iterator-bol szarmazik
8.166 +out_edge_iterator();
8.167 +//out_edge_iterator(const node_iterator&); nem javasolt
8.168 +out_edge_iterator& operator++();
8.169 +//node_iterator a_node() const; nem javasolt
8.170 +//node_iterator b_node() const;
8.171 +
8.172 +
8.173 +in_edge_iterator metodusai:
8.174 +edge_iterator-bol szarmazik
8.175 +in_edge_iterator();
8.176 +//in_edge_iterator(const node_iterator&); nem javasolt
8.177 +in_edge_iterator& operator++();
8.178 +//node_iterator a_node() const; nem javasolt
8.179 +//node_iterator b_node() const;
8.180 +
8.181 +
8.182 +sym_edge_iterator metodusai:
8.183 +edge_iterator-bol szarmazik
8.184 +sym_edge_iterator();
8.185 +//sym_edge_iterator(const node_iterator&); nem javasolt
8.186 +sym_edge_iterator& operator++();
8.187 +//node_iterator a_node() const; nem javasolt
8.188 +//node_iterator b_node() const;
8.189 +
8.190 +Node propery array-okrol:
8.191 +
8.192 +template <typename graph_type, typename T>
8.193 +class node_property_vector;
8.194 +
8.195 +metodusok:
8.196 +
8.197 +node_property_vector(graph_type&);
8.198 +void put(graph_type::node_iterator, const T&);
8.199 +T get(graph_type::node_iterator);
8.200 +
8.201 +Ugyanez edge_property_array-okkal
8.202 +
8.203 +template <typename graph_type, typename T>
8.204 +class edge_property_vector;
8.205 +
8.206 +edge_property_vector(graph_type&);
8.207 +void put(graph_type::edge_iterator, const T&);
8.208 +get(graph_type::edge_iterator);
8.209 +
8.210 + Ennyi nem javasolas utan, meg nehany szo.
8.211 + Alparral ugy gondoltuk, hogy az iterator 1 olyan egyszeru objetum legyen
8.212 +csak, mellyel, ha ervenyes, akkor lehet tovabblepni 1 pont vagy ellistaban.
8.213 +Az hogy valamilyen pont-iteratorbeol el-iteratort csinalunk, igenis legyen a
8.214 +graf objektum feladata, hiszen igy lehet csinelni ugyanazon a ponthalmazon
8.215 +tobb grafot ugyanazon pont-iteratorokkal.
8.216 + Sokkal komolyabb kerdesek merultek fel azzal kapcsolatban, hogy hogyan adjuk
8.217 +at a propertyket az algoritmusoknak, algoritmus-objektumoknak.
8.218 +Errol majd kesobb.
8.219 +
8.220 +marci@cs.elte.hu
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/src/work/marci/oldies/marci_graph_demo.cc Sat Apr 03 14:41:31 2004 +0000
9.3 @@ -0,0 +1,270 @@
9.4 +#include <iostream>
9.5 +#include <vector>
9.6 +#include <string>
9.7 +
9.8 +#include <list_graph.h>
9.9 +#include <bfs_iterator.h>
9.10 +#include <edmonds_karp.h>
9.11 +
9.12 +using namespace hugo;
9.13 +
9.14 +int main (int, char*[])
9.15 +{
9.16 + typedef ListGraph::Node Node;
9.17 + typedef ListGraph::Edge Edge;
9.18 + typedef ListGraph::NodeIt NodeIt;
9.19 + typedef ListGraph::EdgeIt EdgeIt;
9.20 + typedef ListGraph::OutEdgeIt OutEdgeIt;
9.21 + typedef ListGraph::InEdgeIt InEdgeIt;
9.22 + typedef ListGraph::SymEdgeIt SymEdgeIt;
9.23 + ListGraph G;
9.24 + std::vector<Node> vector_of_Nodes;
9.25 + for(int i=0; i!=8; ++i) vector_of_Nodes.push_back(G.addNode());
9.26 + for(int i=0; i!=8; ++i)
9.27 + for(int j=0; j!=8; ++j)
9.28 + if ((i<j)&&(i+j)%3) G.addEdge(vector_of_Nodes[i], vector_of_Nodes[j]);
9.29 +
9.30 + std::cout << "We construct a directed graph on the node set {0,1,2,...,7}," <<std::endl << "i-->j is arc iff i<j and (i+j)%3." << std::endl;
9.31 + std::cout << "number of nodes: " << count(G.first<NodeIt>()) << std::endl;
9.32 +
9.33 + for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
9.34 + std::cout << "node " << G.id(i) << std::endl;
9.35 + std::cout << " outdegree (OutEdgeIt): " << count(G.first<OutEdgeIt>(i)) << " ";
9.36 + for(OutEdgeIt j=G.first<OutEdgeIt>(i); G.valid(j); G.next(j)) {
9.37 + std::cout << "(" << G.id(G.tail(j)) << "--" << G.id(j) << "->" << G.id(G.head(j)) << ") ";
9.38 + }
9.39 + std::cout << std::endl;
9.40 +
9.41 + std::cout<< " ";
9.42 + for(OutEdgeIt j=G.first<OutEdgeIt>(i); G.valid(j); G.next(j)) {
9.43 + std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; }
9.44 + std::cout<<std::endl;
9.45 +
9.46 + std::cout << " indegree: (InEdgeIt) " << count(G.first<InEdgeIt>(i)) << " ";
9.47 + for(InEdgeIt j=G.first<InEdgeIt>(i); G.valid(j); G.next(j)) {
9.48 + std::cout << j << " "; }
9.49 + std::cout << std::endl;
9.50 +
9.51 + std::cout<< " ";
9.52 + for(InEdgeIt j=G.first<InEdgeIt>(i); G.valid(j); G.next(j)) {
9.53 + std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; }
9.54 + std::cout<<std::endl;
9.55 +
9.56 + std::cout << " degree: (SymEdgeIt) " << count(G.first<SymEdgeIt>(i)) << " ";
9.57 + for(SymEdgeIt j=G.first<SymEdgeIt>(i); G.valid(j); G.next(j)) {
9.58 + std::cout << j << " "; }
9.59 + std::cout<<std::endl;
9.60 +
9.61 + std::cout<< " ";
9.62 + for(SymEdgeIt j=G.first<SymEdgeIt>(i); G.valid(j); G.next(j)) {
9.63 + std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; }
9.64 + std::cout<<std::endl;
9.65 + }
9.66 +
9.67 + std::cout << "all edges: ";
9.68 + for(EdgeIt i=G.first<EdgeIt>(); G.valid(i); G.next(i)) {
9.69 + std::cout << i << " ";
9.70 + }
9.71 + std::cout << std::endl;
9.72 +
9.73 + std::cout << "node property array test" << std::endl;
9.74 + ListGraph::NodeMap<int> my_property_vector(G);
9.75 + NodeIt v;
9.76 + G.first(v);
9.77 + my_property_vector.set(v, 42);
9.78 + my_property_vector.set(G.next(G.first<NodeIt>()), 314);
9.79 + my_property_vector.set(G.next(G.next(G.first<NodeIt>())), 1956);
9.80 + my_property_vector.set(vector_of_Nodes[3], 1989);
9.81 + my_property_vector.set(vector_of_Nodes[4], 2003);
9.82 + my_property_vector.set(vector_of_Nodes[7], 1978);
9.83 + std::cout << "some node property values..." << std::endl;
9.84 + for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
9.85 + std::cout << my_property_vector.get(i) << std::endl;
9.86 + }
9.87 + int _i=1;
9.88 + int _ii=1;
9.89 + ListGraph::EdgeMap<int> my_edge_property(G);
9.90 + for(EdgeIt i=G.first<EdgeIt>(); G.valid(i); G.next(i)) {
9.91 + my_edge_property.set(i, _i);
9.92 + _i*=_ii; ++_ii;
9.93 + }
9.94 +
9.95 + std::cout << "node and edge property values on the tails and heads of edges..." << std::endl;
9.96 + for(EdgeIt j=G.first<EdgeIt>(); G.valid(j); G.next(j)) {
9.97 + std::cout << my_property_vector.get(G.tail(j)) << "--" << my_edge_property.get(j) << "-->" << my_property_vector.get(G.head(j)) << " ";
9.98 + }
9.99 + std::cout << std::endl;
9.100 +/*
9.101 + std::cout << "bfs from the first node" << std::endl;
9.102 + bfs<ListGraph> bfs_test(G, G.first<NodeIt>());
9.103 + bfs_test.run();
9.104 + std::cout << "reached: ";
9.105 + for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
9.106 + std::cout << bfs_test.reached.get(i) << " ";
9.107 + }
9.108 + std::cout<<std::endl;
9.109 + std::cout << "dist: ";
9.110 + for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
9.111 + std::cout << bfs_test.dist.get(i) << " ";
9.112 + }
9.113 + std::cout<<std::endl;
9.114 +*/
9.115 +
9.116 + std::cout << "augmenting path flow algorithm test..." << std::endl;
9.117 + ListGraph flowG;
9.118 +
9.119 + Node s=flowG.addNode();
9.120 + Node v1=flowG.addNode();
9.121 + Node v2=flowG.addNode();
9.122 + Node v3=flowG.addNode();
9.123 + Node v4=flowG.addNode();
9.124 + Node t=flowG.addNode();
9.125 +
9.126 + ListGraph::NodeMap<std::string> node_name(flowG);
9.127 + node_name.set(s, "s");
9.128 + node_name.set(v1, "v1");
9.129 + node_name.set(v2, "v2");
9.130 + node_name.set(v3, "v3");
9.131 + node_name.set(v4, "v4");
9.132 + node_name.set(t, "t");
9.133 +
9.134 + Edge s_v1=flowG.addEdge(s, v1);
9.135 + Edge s_v2=flowG.addEdge(s, v2);
9.136 + Edge v1_v2=flowG.addEdge(v1, v2);
9.137 + Edge v2_v1=flowG.addEdge(v2, v1);
9.138 + Edge v1_v3=flowG.addEdge(v1, v3);
9.139 + Edge v3_v2=flowG.addEdge(v3, v2);
9.140 + Edge v2_v4=flowG.addEdge(v2, v4);
9.141 + Edge v4_v3=flowG.addEdge(v4, v3);
9.142 + Edge v3_t=flowG.addEdge(v3, t);
9.143 + Edge v4_t=flowG.addEdge(v4, t);
9.144 +
9.145 + ListGraph::EdgeMap<int> cap(flowG);
9.146 +
9.147 + cap.set(s_v1, 16);
9.148 + cap.set(s_v2, 13);
9.149 + cap.set(v1_v2, 10);
9.150 + cap.set(v2_v1, 4);
9.151 + cap.set(v1_v3, 12);
9.152 + cap.set(v3_v2, 9);
9.153 + cap.set(v2_v4, 14);
9.154 + cap.set(v4_v3, 7);
9.155 + cap.set(v3_t, 20);
9.156 + cap.set(v4_t, 4);
9.157 +
9.158 + std::cout << "on directed graph graph" << std::endl; //<< flowG;
9.159 + std::cout << "names and capacity values" << std::endl;
9.160 + for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) {
9.161 + std::cout << node_name.get(i) << ": ";
9.162 + std::cout << "out edges: ";
9.163 + for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j))
9.164 + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
9.165 + std::cout << "in edges: ";
9.166 + for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j))
9.167 + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
9.168 + std::cout << std::endl;
9.169 + }
9.170 +
9.171 + //flowG.deleteEdge(s_v1);
9.172 + //flowG.deleteEdge(s_v2);
9.173 + //flowG.deleteEdge(v1_v2);
9.174 + //flowG.deleteEdge(v1_v3);
9.175 +
9.176 +
9.177 + //flowG.setTail(v3_t, v2);
9.178 + //flowG.setHead(v3_t, s);
9.179 +/*
9.180 + for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) {
9.181 + std::cout << node_name.get(i) << ": ";
9.182 + std::cout << "out edges: ";
9.183 + for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j))
9.184 + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
9.185 + std::cout << "in edges: ";
9.186 + for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j))
9.187 + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
9.188 + std::cout << std::endl;
9.189 + }
9.190 +
9.191 + for(EdgeIt e=flowG.first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
9.192 + std::cout << node_name.get(flowG.tail(e)) << "-"<< cap.get(e) << "->" << node_name.get(flowG.head(e)) << " ";
9.193 + }
9.194 +*/
9.195 + /*
9.196 + while (flowG.valid(flowG.first<EdgeIt>())) {
9.197 + flowG.deleteEdge(flowG.first<EdgeIt>());
9.198 + for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) {
9.199 + std::cout << node_name.get(i) << ": ";
9.200 + std::cout << "out edges: ";
9.201 + for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j))
9.202 + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
9.203 + std::cout << "in edges: ";
9.204 + for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j))
9.205 + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
9.206 + std::cout << std::endl;
9.207 + }
9.208 + }
9.209 +
9.210 + while (flowG.valid(flowG.first<NodeIt>())) {
9.211 + flowG.deleteNode(flowG.first<NodeIt>());
9.212 + for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) {
9.213 + std::cout << node_name.get(i) << ": ";
9.214 + std::cout << "out edges: ";
9.215 + for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j))
9.216 + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
9.217 + std::cout << "in edges: ";
9.218 + for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j))
9.219 + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
9.220 + std::cout << std::endl;
9.221 + }
9.222 + }
9.223 + */
9.224 +
9.225 + //std::cout << std::endl;
9.226 +
9.227 +
9.228 + {
9.229 + ListGraph::EdgeMap<int> flow(flowG, 0);
9.230 + MaxFlow<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(flowG, s, t, flow, cap);
9.231 + /*
9.232 + max_flow_test.augmentOnBlockingFlow<ListGraph>();
9.233 + for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
9.234 + std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
9.235 + }
9.236 + std::cout<<std::endl;
9.237 + max_flow_test.augmentOnBlockingFlow<ListGraph>();
9.238 + for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
9.239 + std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
9.240 + }
9.241 + std::cout<<std::endl;*/
9.242 + //max_flow_test.run();
9.243 +
9.244 + //std::cout << "maximum flow: "<< std::endl;
9.245 + while (max_flow_test.augmentOnShortestPath()) {
9.246 + for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
9.247 + std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
9.248 + }
9.249 + std::cout<<std::endl;
9.250 + }
9.251 + std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
9.252 + }
9.253 +/*
9.254 + {
9.255 + std::list<Node> S;
9.256 + S.push_back(s); S.push_back(v3);
9.257 + std::list<Node> T;
9.258 + T.push_back(t);
9.259 +
9.260 + ListGraph::EdgeMap<int> flow(flowG, 0);
9.261 + MaxFlow2<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(flowG, S, T, flow, cap);
9.262 + max_flow_test.run();
9.263 +
9.264 + std::cout << "maximum flow: "<< std::endl;
9.265 + for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
9.266 + std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
9.267 + }
9.268 + std::cout<<std::endl;
9.269 + std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
9.270 + }
9.271 +*/
9.272 + return 0;
9.273 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/src/work/marci/oldies/marci_graph_traits.hh Sat Apr 03 14:41:31 2004 +0000
10.3 @@ -0,0 +1,19 @@
10.4 +#ifndef MARCI_GRAPH_TRAITS_HH
10.5 +#define MARCI_GRAPH_TRAITS_HH
10.6 +
10.7 +namespace hugo {
10.8 +
10.9 + template <typename graph_type>
10.10 + struct graph_traits {
10.11 + typedef typename graph_type::node_iterator node_iterator;
10.12 + typedef typename graph_type::edge_iterator edge_iterator;
10.13 + typedef typename graph_type::each_node_iterator each_node_iterator;
10.14 + typedef typename graph_type::each_edge_iterator each_edge_iterator;
10.15 + typedef typename graph_type::out_edge_iterator out_edge_iterator;
10.16 + typedef typename graph_type::in_edge_iterator in_edge_iterator;
10.17 + typedef typename graph_type::sym_edge_iterator sym_edge_iterator;
10.18 + };
10.19 +
10.20 +} // namespace hugo
10.21 +
10.22 +#endif //MARCI_GRAPH_TRAITS_HH
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/src/work/marci/oldies/marci_list_graph.hh Sat Apr 03 14:41:31 2004 +0000
11.3 @@ -0,0 +1,343 @@
11.4 +#ifndef MARCI_LIST_GRAPH_HH
11.5 +#define MARCI_LIST_GRAPH_HH
11.6 +
11.7 +#include <iostream>
11.8 +
11.9 +namespace hugo {
11.10 +
11.11 + class list_graph {
11.12 + class node_item;
11.13 + class edge_item;
11.14 + public:
11.15 + class node_iterator;
11.16 + class each_node_iterator;
11.17 + class edge_iterator;
11.18 + class each_edge_iterator;
11.19 + class out_edge_iterator;
11.20 + class in_edge_iterator;
11.21 + class sym_edge_iterator;
11.22 + private:
11.23 + int node_id;
11.24 + int edge_id;
11.25 + int _node_num;
11.26 + int _edge_num;
11.27 +
11.28 + node_item* _first_node;
11.29 + node_item* _last_node;
11.30 +
11.31 + class node_item {
11.32 + friend class list_graph;
11.33 + friend class node_iterator;
11.34 + friend class each_node_iterator;
11.35 + friend class edge_iterator;
11.36 + friend class each_edge_iterator;
11.37 + friend class out_edge_iterator;
11.38 + friend class in_edge_iterator;
11.39 + friend class sym_edge_iterator;
11.40 + friend std::ostream& operator<<(std::ostream& os, const node_iterator& i);
11.41 + friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i);
11.42 + list_graph* G;
11.43 + int id;
11.44 + edge_item* _first_out_edge;
11.45 + edge_item* _last_out_edge;
11.46 + edge_item* _first_in_edge;
11.47 + edge_item* _last_in_edge;
11.48 + node_item* _next_node;
11.49 + node_item* _prev_node;
11.50 + public:
11.51 + node_item() { }
11.52 + };
11.53 +
11.54 + class edge_item {
11.55 + friend class list_graph;
11.56 + friend class node_iterator;
11.57 + friend class each_node_iterator;
11.58 + friend class edge_iterator;
11.59 + friend class each_edge_iterator;
11.60 + friend class out_edge_iterator;
11.61 + friend class in_edge_iterator;
11.62 + friend class sym_edge_iterator;
11.63 + friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i);
11.64 + list_graph* G;
11.65 + int id;
11.66 + node_item* _tail;
11.67 + node_item* _head;
11.68 + edge_item* _next_out;
11.69 + edge_item* _prev_out;
11.70 + edge_item* _next_in;
11.71 + edge_item* _prev_in;
11.72 + public:
11.73 + edge_item() { }
11.74 + };
11.75 +
11.76 + node_item* _add_node() {
11.77 + node_item* p=new node_item;
11.78 + p->id=node_id++;
11.79 + p->_first_out_edge=0;
11.80 + p->_last_out_edge=0;
11.81 + p->_first_in_edge=0;
11.82 + p->_last_in_edge=0;
11.83 + p->_prev_node=_last_node;
11.84 + p->_next_node=0;
11.85 + if (_last_node) _last_node->_next_node=p;
11.86 + _last_node=p;
11.87 + if (!_first_node) _first_node=p;
11.88 + ++_node_num;
11.89 + return p;
11.90 + }
11.91 +
11.92 + edge_item* _add_edge(node_item* _tail, node_item* _head) {
11.93 + edge_item* e=new edge_item;
11.94 + e->id=edge_id++;
11.95 + e->_tail=_tail;
11.96 + e->_head=_head;
11.97 +
11.98 + e->_prev_out=_tail->_last_out_edge;
11.99 + if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
11.100 + _tail->_last_out_edge=e;
11.101 + if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
11.102 +
11.103 + e->_prev_in=_head->_last_in_edge;
11.104 + if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
11.105 + _head->_last_in_edge=e;
11.106 + if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
11.107 + ++_edge_num;
11.108 + return e;
11.109 + }
11.110 +
11.111 + public:
11.112 +
11.113 + /* default constructor */
11.114 +
11.115 + list_graph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
11.116 +
11.117 + int node_num() { return _node_num; }
11.118 + int edge_num() { return _edge_num; }
11.119 +
11.120 + /* functions to construct iterators from the graph, or from each other */
11.121 +
11.122 + each_node_iterator first_node() { return each_node_iterator(_first_node); }
11.123 + each_edge_iterator first_edge() {
11.124 + node_item* v=_first_node;
11.125 + edge_item* edge=v->_first_out_edge;
11.126 + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
11.127 + return each_edge_iterator(v, edge);
11.128 + }
11.129 +
11.130 + out_edge_iterator first_out_edge(const node_iterator& v) {
11.131 + return out_edge_iterator(v);
11.132 + }
11.133 + in_edge_iterator first_in_edge(const node_iterator& v) {
11.134 + return in_edge_iterator(v);
11.135 + }
11.136 + sym_edge_iterator first_sym_edge(const node_iterator& v) {
11.137 + return sym_edge_iterator(v);
11.138 + }
11.139 + node_iterator tail(const edge_iterator& e) { return e.tail_node(); }
11.140 + node_iterator head(const edge_iterator& e) { return e.head_node(); }
11.141 +
11.142 + node_iterator a_node(const out_edge_iterator& e) { return e.a_node(); }
11.143 + node_iterator a_node(const in_edge_iterator& e) { return e.a_node(); }
11.144 + node_iterator a_node(const sym_edge_iterator& e) { return e.a_node(); }
11.145 +
11.146 + node_iterator b_node(const out_edge_iterator& e) { return e.b_node(); }
11.147 + node_iterator b_node(const in_edge_iterator& e) { return e.b_node(); }
11.148 + node_iterator b_node(const sym_edge_iterator& e) { return e.b_node(); }
11.149 +
11.150 + //node_iterator invalid_node() { return node_iterator(); }
11.151 + //edge_iterator invalid_edge() { return edge_iterator(); }
11.152 + //out_edge_iterator invalid_out_edge() { return out_edge_iterator(); }
11.153 + //in_edge_iterator invalid_in_edge() { return in_edge_iterator(); }
11.154 + //sym_edge_iterator invalid_sym_edge() { return sym_edge_iterator(); }
11.155 +
11.156 + /* same methods in other style */
11.157 + /* for experimental purpose */
11.158 +
11.159 + void get_first(each_node_iterator& v) { v=each_node_iterator(_first_node); }
11.160 + void get_first(each_edge_iterator& e) { e=first_edge(); }
11.161 + void get_first(out_edge_iterator& e, const node_iterator& v) {
11.162 + e=out_edge_iterator(v);
11.163 + }
11.164 + void get_first(in_edge_iterator& e, const node_iterator& v) {
11.165 + e=in_edge_iterator(v);
11.166 + }
11.167 + void get_first(sym_edge_iterator& e, const node_iterator& v) {
11.168 + e=sym_edge_iterator(v);
11.169 + }
11.170 + void get_tail(node_iterator& n, const edge_iterator& e) { n=tail(e); }
11.171 + void get_head(node_iterator& n, const edge_iterator& e) { n=head(e); }
11.172 +
11.173 + void get_a_node(node_iterator& n, const out_edge_iterator& e) { n=e.a_node(); }
11.174 + void get_a_node(node_iterator& n, const in_edge_iterator& e) { n=e.a_node(); }
11.175 + void get_a_node(node_iterator& n, const sym_edge_iterator& e) { n=e.a_node(); }
11.176 + void get_b_node(node_iterator& n, const out_edge_iterator& e) { n=e.b_node(); }
11.177 + void get_b_node(node_iterator& n, const in_edge_iterator& e) { n=e.b_node(); }
11.178 + void get_b_node(node_iterator& n, const sym_edge_iterator& e) { n=e.b_node(); }
11.179 + //void get_invalid(node_iterator& n) { n=node_iterator(); }
11.180 + //void get_invalid(edge_iterator& e) { e=edge_iterator(); }
11.181 + //void get_invalid(out_edge_iterator& e) { e=out_edge_iterator(); }
11.182 + //void get_invalid(in_edge_iterator& e) { e=in_edge_iterator(); }
11.183 + //void get_invalid(sym_edge_iterator& e) { e=sym_edge_iterator(); }
11.184 +
11.185 +
11.186 + /* for getting id's of graph objects */
11.187 + /* these are important for the implementation of property vectors */
11.188 +
11.189 + int id(const node_iterator& v) { return v.node->id; }
11.190 + int id(const edge_iterator& e) { return e.edge->id; }
11.191 +
11.192 + /* adding nodes and edges */
11.193 +
11.194 + node_iterator add_node() { return node_iterator(_add_node()); }
11.195 + edge_iterator add_edge(const node_iterator& u, const node_iterator& v) {
11.196 + return edge_iterator(_add_edge(u.node, v.node));
11.197 + }
11.198 +
11.199 + /* stream operations, for testing purpose */
11.200 +
11.201 + friend std::ostream& operator<<(std::ostream& os, const node_iterator& i) {
11.202 + os << i.node->id; return os;
11.203 + }
11.204 + friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i) {
11.205 + os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
11.206 + return os;
11.207 + }
11.208 +
11.209 + class node_iterator {
11.210 + friend class list_graph;
11.211 +
11.212 + friend class edge_iterator;
11.213 + friend class out_edge_iterator;
11.214 + friend class in_edge_iterator;
11.215 + friend class sym_edge_iterator;
11.216 + protected:
11.217 + node_item* node;
11.218 + friend int list_graph::id(const node_iterator& v);
11.219 + public:
11.220 + node_iterator() : node(0) { }
11.221 + node_iterator(node_item* _node) : node(_node) { }
11.222 + bool valid() { return (node!=0); }
11.223 + void make_invalid() { node=0; }
11.224 + friend bool operator==(const node_iterator& u, const node_iterator& v) {
11.225 + return v.node==u.node;
11.226 + }
11.227 + friend bool operator!=(const node_iterator& u, const node_iterator& v) {
11.228 + return v.node!=u.node;
11.229 + }
11.230 + friend std::ostream& operator<<(std::ostream& os, const node_iterator& i);
11.231 + };
11.232 +
11.233 + class each_node_iterator : public node_iterator {
11.234 + friend class list_graph;
11.235 + public:
11.236 + each_node_iterator() : node_iterator() { }
11.237 + each_node_iterator(node_item* v) : node_iterator(v) { }
11.238 + each_node_iterator& operator++() { node=node->_next_node; return *this; }
11.239 + };
11.240 +
11.241 + class edge_iterator {
11.242 + friend class list_graph;
11.243 +
11.244 + friend class node_iterator;
11.245 + friend class each_node_iterator;
11.246 + protected:
11.247 + edge_item* edge;
11.248 + friend int list_graph::id(const edge_iterator& e);
11.249 + public:
11.250 + edge_iterator() : edge(0) { }
11.251 + edge_iterator(edge_item* _edge) : edge(_edge) { }
11.252 + bool valid() { return (edge!=0); }
11.253 + void make_invalid() { edge=0; }
11.254 + friend bool operator==(const edge_iterator& u, const edge_iterator& v) {
11.255 + return v.edge==u.edge;
11.256 + }
11.257 + friend bool operator!=(const edge_iterator& u, const edge_iterator& v) {
11.258 + return v.edge!=u.edge;
11.259 + }
11.260 + protected:
11.261 + node_iterator tail_node() const { return node_iterator(edge->_tail); }
11.262 + node_iterator head_node() const { return node_iterator(edge->_head); }
11.263 + public:
11.264 + friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i);
11.265 + };
11.266 +
11.267 + class each_edge_iterator : public edge_iterator {
11.268 + friend class list_graph;
11.269 + node_item* v;
11.270 + public:
11.271 + each_edge_iterator() : edge_iterator(), v(0) { }
11.272 + each_edge_iterator(node_item* _v, edge_item* _e) : edge_iterator(_e), v(_v) { }
11.273 + each_edge_iterator& operator++() {
11.274 + edge=edge->_next_out;
11.275 + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
11.276 + return *this;
11.277 + }
11.278 + };
11.279 +
11.280 + class out_edge_iterator : public edge_iterator {
11.281 + friend class list_graph;
11.282 + node_item* v;
11.283 + public:
11.284 + out_edge_iterator() : edge_iterator(), v(0) { }
11.285 + protected:
11.286 + out_edge_iterator(const node_iterator& _v) : v(_v.node) { edge=v->_first_out_edge; }
11.287 + public:
11.288 + out_edge_iterator& operator++() { edge=edge->_next_out; return *this; }
11.289 + protected:
11.290 + node_iterator a_node() const { return node_iterator(v); }
11.291 + node_iterator b_node() const {
11.292 + return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); }
11.293 + };
11.294 +
11.295 + class in_edge_iterator : public edge_iterator {
11.296 + friend class list_graph;
11.297 + node_item* v;
11.298 + public:
11.299 + in_edge_iterator() : edge_iterator(), v(0) { }
11.300 + protected:
11.301 + in_edge_iterator(const node_iterator& _v) : v(_v.node) {
11.302 + edge=v->_first_in_edge;
11.303 + }
11.304 + public:
11.305 + in_edge_iterator& operator++() { edge=edge->_next_in; return *this; }
11.306 + protected:
11.307 + node_iterator a_node() const { return node_iterator(v); }
11.308 + node_iterator b_node() const {
11.309 + return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); }
11.310 + };
11.311 +
11.312 + class sym_edge_iterator : public edge_iterator {
11.313 + friend class list_graph;
11.314 + bool out_or_in; //1 iff out, 0 iff in
11.315 + node_item* v;
11.316 + public:
11.317 + sym_edge_iterator() : edge_iterator(), v(0) { }
11.318 + protected:
11.319 + sym_edge_iterator(const node_iterator& _v) : v(_v.node) {
11.320 + out_or_in=1;
11.321 + edge=v->_first_out_edge;
11.322 + if (!edge) { edge=v->_first_in_edge; out_or_in=0; }
11.323 + }
11.324 + public:
11.325 + sym_edge_iterator& operator++() {
11.326 + if (out_or_in) {
11.327 + edge=edge->_next_out;
11.328 + if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
11.329 + } else {
11.330 + edge=edge->_next_in;
11.331 + }
11.332 + return *this;
11.333 + }
11.334 + protected:
11.335 + node_iterator a_node() const { return node_iterator(v); }
11.336 + node_iterator b_node() const {
11.337 + return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); }
11.338 + };
11.339 +
11.340 + };
11.341 +
11.342 +
11.343 +
11.344 +} //namespace hugo
11.345 +
11.346 +#endif //MARCI_LIST_GRAPH_HH
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/src/work/marci/oldies/marci_makefile Sat Apr 03 14:41:31 2004 +0000
12.3 @@ -0,0 +1,5 @@
12.4 +CXXFLAGS = -Wall -ansi -I.
12.5 +CXX = g++-3.0
12.6 +
12.7 +marci_graph_demo: marci_graph_demo.cc marci_list_graph.hh marci_property_vector.hh marci_bfs.hh marci_max_flow.hh
12.8 + $(CXX) $(CXXFLAGS) marci_graph_demo.cc -o marci_graph_demo
12.9 \ No newline at end of file
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/src/work/marci/oldies/marci_max_flow.hh Sat Apr 03 14:41:31 2004 +0000
13.3 @@ -0,0 +1,183 @@
13.4 +#ifndef MARCI_MAX_FLOW_HH
13.5 +#define MARCI_MAX_FLOW_HH
13.6 +
13.7 +#include <algorithm>
13.8 +
13.9 +#include <marci_property_vector.hh>
13.10 +#include <marci_bfs.hh>
13.11 +
13.12 +namespace hugo {
13.13 +
13.14 + template<typename graph_type, typename T>
13.15 + class res_graph_type {
13.16 + typedef typename graph_type::node_iterator node_iterator;
13.17 + typedef typename graph_type::each_node_iterator each_node_iterator;
13.18 + typedef typename graph_type::sym_edge_iterator old_sym_edge_iterator;
13.19 + graph_type& G;
13.20 + edge_property_vector<graph_type, T>& flow;
13.21 + edge_property_vector<graph_type, T>& capacity;
13.22 + public:
13.23 + res_graph_type(graph_type& _G, edge_property_vector<graph_type, T>& _flow, edge_property_vector<graph_type, T>& _capacity) : G(_G), flow(_flow), capacity(_capacity) { }
13.24 +
13.25 + class edge_iterator {
13.26 + friend class res_graph_type<graph_type, T>;
13.27 + protected:
13.28 + res_graph_type<graph_type, T>* resG;
13.29 + old_sym_edge_iterator sym;
13.30 + public:
13.31 + edge_iterator() { }
13.32 + //bool is_free() {
13.33 + //if (resG->G.a_node(sym)==resG->G.tail(sym)) {
13.34 + // return (resG->flow.get(sym)<resG->capacity.get(sym));
13.35 + //} else {
13.36 + // return (resG->flow.get(sym)>0);
13.37 + //}
13.38 + //}
13.39 + T free() {
13.40 + if (resG->G.a_node(sym)==resG->G.tail(sym)) {
13.41 + return (resG->capacity.get(sym)-resG->flow.get(sym));
13.42 + } else {
13.43 + return (resG->flow.get(sym));
13.44 + }
13.45 + }
13.46 + bool valid() { return sym.valid(); }
13.47 + void make_invalid() { sym.make_invalid(); }
13.48 + void augment(T a) {
13.49 + if (resG->G.a_node(sym)==resG->G.tail(sym)) {
13.50 + resG->flow.put(sym, resG->flow.get(sym)+a);
13.51 + } else {
13.52 + resG->flow.put(sym, resG->flow.get(sym)-a);
13.53 + }
13.54 + }
13.55 + };
13.56 +
13.57 + class out_edge_iterator : public edge_iterator {
13.58 + public:
13.59 + out_edge_iterator() { }
13.60 + out_edge_iterator(res_graph_type<graph_type, T>& _resG, const node_iterator& v) {
13.61 + resG=&_resG;
13.62 + sym=resG->G.first_sym_edge(v);
13.63 + while( sym.valid() && !(free()>0) ) { ++sym; }
13.64 + }
13.65 + out_edge_iterator& operator++() {
13.66 + ++sym;
13.67 + while( sym.valid() && !(free()>0) ) { ++sym; }
13.68 + return *this;
13.69 + }
13.70 + };
13.71 +
13.72 + out_edge_iterator first_out_edge(const node_iterator& v) {
13.73 + return out_edge_iterator(*this, v);
13.74 + }
13.75 +
13.76 + each_node_iterator first_node() {
13.77 + return G.first_node();
13.78 + }
13.79 +
13.80 + node_iterator tail(const edge_iterator& e) { return G.a_node(e.sym); }
13.81 + node_iterator head(const edge_iterator& e) { return G.b_node(e.sym); }
13.82 +
13.83 + int id(const node_iterator& v) { return G.id(v); }
13.84 +
13.85 + //node_iterator invalid_node() { return G.invalid_node(); }
13.86 + //res_edge_it invalid_edge() { res_edge_it n; n.sym=G.invalid_sym_edge(); return n; }
13.87 + };
13.88 +
13.89 + template <typename graph_type, typename T>
13.90 + struct max_flow_type {
13.91 + typedef typename graph_type::node_iterator node_iterator;
13.92 + typedef typename graph_type::edge_iterator edge_iterator;
13.93 + typedef typename graph_type::each_node_iterator each_node_iterator;
13.94 + typedef typename graph_type::out_edge_iterator out_edge_iterator;
13.95 + typedef typename graph_type::in_edge_iterator in_edge_iterator;
13.96 + graph_type& G;
13.97 + node_iterator s;
13.98 + node_iterator t;
13.99 + edge_property_vector<graph_type, T> flow;
13.100 + edge_property_vector<graph_type, T>& capacity;
13.101 +
13.102 + max_flow_type(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector<graph_type, T>& _capacity) : G(_G), s(_s), t(_t), flow(_G), capacity(_capacity) {
13.103 + for(each_node_iterator i=G.first_node(); i.valid(); ++i)
13.104 + for(out_edge_iterator j=G.first_out_edge(i); j.valid(); ++j)
13.105 + flow.put(j, 0);
13.106 + }
13.107 + void run() {
13.108 + typedef res_graph_type<graph_type, T> aug_graph_type;
13.109 + aug_graph_type res_graph(G, flow, capacity);
13.110 +
13.111 + bool augment;
13.112 + do {
13.113 + augment=false;
13.114 +
13.115 + typedef std::queue<aug_graph_type::out_edge_iterator> bfs_queue_type;
13.116 + bfs_queue_type bfs_queue;
13.117 + bfs_queue.push(res_graph.first_out_edge(s));
13.118 +
13.119 + typedef node_property_vector<aug_graph_type, bool> reached_type;
13.120 + reached_type reached(res_graph, false);
13.121 + reached.put(s, true);
13.122 +
13.123 + bfs_iterator1< aug_graph_type, reached_type >
13.124 + res_bfs(res_graph, bfs_queue, reached);
13.125 +
13.126 + typedef node_property_vector<aug_graph_type, aug_graph_type::edge_iterator> pred_type;
13.127 + pred_type pred(res_graph);
13.128 + aug_graph_type::edge_iterator a;
13.129 + a.make_invalid();
13.130 + pred.put(s, a);
13.131 +
13.132 + typedef node_property_vector<aug_graph_type, int> free_type;
13.133 + free_type free(res_graph);
13.134 +
13.135 + //searching for augmenting path
13.136 + while ( res_bfs.valid() ) {
13.137 + //std::cout<<"KULSO ciklus itt jar: "<<G.id(res_graph.tail(res_bfs))<<"->"<<G.id(res_graph.head(res_bfs))<<std::endl;
13.138 + if (res_bfs.newly_reached()) {
13.139 + aug_graph_type::edge_iterator e;
13.140 + e=res_bfs;
13.141 + node_iterator v=res_graph.tail(e);
13.142 + node_iterator w=res_graph.head(e);
13.143 + //std::cout<<G.id(v)<<"->"<<G.id(w)<<", "<<G.id(w)<<" is newly reached";
13.144 + pred.put(w, e);
13.145 + if (pred.get(v).valid()) {
13.146 + free.put(w, std::min(free.get(v), e.free()));
13.147 + //std::cout <<" nem elso csucs: ";
13.148 + //std::cout <<"szabad kap eddig: "<< free.get(w) << " ";
13.149 + } else {
13.150 + free.put(w, e.free());
13.151 + //std::cout <<" elso csucs: ";
13.152 + //std::cout <<"szabad kap eddig: "<< free.get(w) << " ";
13.153 + }
13.154 + //std::cout<<std::endl;
13.155 + }
13.156 +
13.157 + if (res_graph.head(res_bfs)==t) break;
13.158 + ++res_bfs;
13.159 + }
13.160 + if (reached.get(t)) {
13.161 + augment=true;
13.162 + node_iterator n=t;
13.163 + T augment_value=free.get(t);
13.164 + std::cout<<"augmentation: ";
13.165 + while (pred.get(n).valid()) {
13.166 + aug_graph_type::edge_iterator e=pred.get(n);
13.167 + e.augment(augment_value);
13.168 + std::cout<<"("<<res_graph.tail(e)<< "->"<<res_graph.head(e)<<") ";
13.169 + n=res_graph.tail(e);
13.170 + }
13.171 + std::cout<<std::endl;
13.172 + }
13.173 +
13.174 + std::cout << "actual flow: "<< std::endl;
13.175 + for(typename graph_type::each_edge_iterator e=G.first_edge(); e.valid(); ++e) {
13.176 + std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
13.177 + }
13.178 + std::cout<<std::endl;
13.179 +
13.180 + } while (augment);
13.181 + }
13.182 + };
13.183 +
13.184 +} // namespace hugo
13.185 +
13.186 +#endif //MARCI_MAX_FLOW_HH
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/src/work/marci/oldies/marci_property_vector.hh Sat Apr 03 14:41:31 2004 +0000
14.3 @@ -0,0 +1,57 @@
14.4 +#ifndef MARCI_PROPERTY_VECTOR_HH
14.5 +#define MARCI_PROPERTY_VECTOR_HH
14.6 +
14.7 +#include <vector>
14.8 +
14.9 +namespace hugo {
14.10 +
14.11 + template <typename iterator>
14.12 + int number_of(iterator _it) {
14.13 + int i=0;
14.14 + for( ; _it.valid(); ++_it) { ++i; }
14.15 + return i;
14.16 + }
14.17 +
14.18 + template <typename graph_type, typename T>
14.19 + class node_property_vector {
14.20 + typedef typename graph_type::node_iterator node_iterator;
14.21 + typedef typename graph_type::each_node_iterator each_node_iterator;
14.22 + graph_type& G;
14.23 + std::vector<T> container;
14.24 + public:
14.25 + node_property_vector(graph_type& _G) : G(_G) {
14.26 + int i=0;
14.27 + for(each_node_iterator it=G.first_node(); it.valid(); ++it) ++i;
14.28 + container.resize(i);
14.29 + }
14.30 + node_property_vector(graph_type& _G, T a) : G(_G) {
14.31 + for(each_node_iterator it=G.first_node(); it.valid(); ++it) { container.push_back(a); }
14.32 + }
14.33 + void put(node_iterator nit, const T& a) { container[G.id(nit)]=a; }
14.34 + T get(node_iterator nit) { return container[G.id(nit)]; }
14.35 + };
14.36 +
14.37 + template <typename graph_type, typename T>
14.38 + class edge_property_vector {
14.39 + typedef typename graph_type::edge_iterator edge_iterator;
14.40 + typedef typename graph_type::each_edge_iterator each_edge_iterator;
14.41 + graph_type& G;
14.42 + std::vector<T> container;
14.43 + public:
14.44 + edge_property_vector(graph_type& _G) : G(_G) {
14.45 + int i=0;
14.46 + for(each_edge_iterator it=G.first_edge(); it.valid(); ++it) ++i;
14.47 + container.resize(i);
14.48 + }
14.49 + edge_property_vector(graph_type& _G, T a) : G(_G) {
14.50 + for(each_edge_iterator it=G.first_edge(); it.valid(); ++it) {
14.51 + container.push_back(a);
14.52 + }
14.53 + }
14.54 + void put(edge_iterator eit, const T& a) { container[G.id(eit)]=a; }
14.55 + T get(edge_iterator eit) { return container[G.id(eit)]; }
14.56 + };
14.57 +
14.58 +} // namespace hugo
14.59 +
14.60 +#endif //MARCI_PROPERTY_VECTOR_HH
15.1 --- a/src/work/marci_bfs.hh Sat Apr 03 14:22:33 2004 +0000
15.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
15.3 @@ -1,176 +0,0 @@
15.4 -#ifndef MARCI_BFS_HH
15.5 -#define MARCI_BFS_HH
15.6 -
15.7 -#include <queue>
15.8 -
15.9 -#include <marci_property_vector.hh>
15.10 -
15.11 -namespace hugo {
15.12 -
15.13 - template <typename graph_type>
15.14 - struct bfs {
15.15 - typedef typename graph_type::node_iterator node_iterator;
15.16 - typedef typename graph_type::edge_iterator edge_iterator;
15.17 - typedef typename graph_type::each_node_iterator each_node_iterator;
15.18 - typedef typename graph_type::out_edge_iterator out_edge_iterator;
15.19 - graph_type& G;
15.20 - node_iterator s;
15.21 - node_property_vector<graph_type, bool> reached;
15.22 - node_property_vector<graph_type, edge_iterator> pred;
15.23 - node_property_vector<graph_type, int> dist;
15.24 - std::queue<node_iterator> bfs_queue;
15.25 - bfs(graph_type& _G, node_iterator _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) {
15.26 - bfs_queue.push(s);
15.27 - for(each_node_iterator i=G.first_node(); i.valid(); ++i)
15.28 - reached.put(i, false);
15.29 - reached.put(s, true);
15.30 - dist.put(s, 0);
15.31 - }
15.32 -
15.33 - void run() {
15.34 - while (!bfs_queue.empty()) {
15.35 - node_iterator v=bfs_queue.front();
15.36 - out_edge_iterator e=G.first_out_edge(v);
15.37 - bfs_queue.pop();
15.38 - for( ; e.valid(); ++e) {
15.39 - node_iterator w=G.head(e);
15.40 - std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl;
15.41 - if (!reached.get(w)) {
15.42 - std::cout << G.id(w) << " is newly reached :-)" << std::endl;
15.43 - bfs_queue.push(w);
15.44 - dist.put(w, dist.get(v)+1);
15.45 - pred.put(w, e);
15.46 - reached.put(w, true);
15.47 - } else {
15.48 - std::cout << G.id(w) << " is already reached" << std::endl;
15.49 - }
15.50 - }
15.51 - }
15.52 - }
15.53 - };
15.54 -
15.55 - template <typename graph_type>
15.56 - struct bfs_visitor {
15.57 - typedef typename graph_type::node_iterator node_iterator;
15.58 - typedef typename graph_type::edge_iterator edge_iterator;
15.59 - typedef typename graph_type::out_edge_iterator out_edge_iterator;
15.60 - graph_type& G;
15.61 - bfs_visitor(graph_type& _G) : G(_G) { }
15.62 - void at_previously_reached(out_edge_iterator& e) {
15.63 - //node_iterator v=G.tail(e);
15.64 - node_iterator w=G.head(e);
15.65 - std::cout << G.id(w) << " is already reached" << std::endl;
15.66 - }
15.67 - void at_newly_reached(out_edge_iterator& e) {
15.68 - //node_iterator v=G.tail(e);
15.69 - node_iterator w=G.head(e);
15.70 - std::cout << G.id(w) << " is newly reached :-)" << std::endl;
15.71 - }
15.72 - };
15.73 -
15.74 - template <typename graph_type, typename reached_type, typename visitor_type>
15.75 - struct bfs_iterator {
15.76 - typedef typename graph_type::node_iterator node_iterator;
15.77 - typedef typename graph_type::edge_iterator edge_iterator;
15.78 - typedef typename graph_type::out_edge_iterator out_edge_iterator;
15.79 - graph_type& G;
15.80 - std::queue<out_edge_iterator>& bfs_queue;
15.81 - reached_type& reached;
15.82 - visitor_type& visitor;
15.83 - void process() {
15.84 - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.85 - if (bfs_queue.empty()) return;
15.86 - out_edge_iterator e=bfs_queue.front();
15.87 - //node_iterator v=G.tail(e);
15.88 - node_iterator w=G.head(e);
15.89 - if (!reached.get(w)) {
15.90 - visitor.at_newly_reached(e);
15.91 - bfs_queue.push(G.first_out_edge(w));
15.92 - reached.put(w, true);
15.93 - } else {
15.94 - visitor.at_previously_reached(e);
15.95 - }
15.96 - }
15.97 - bfs_iterator(graph_type& _G, std::queue<out_edge_iterator>& _bfs_queue, reached_type& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) {
15.98 - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.99 - valid();
15.100 - }
15.101 - bfs_iterator<graph_type, reached_type, visitor_type>& operator++() {
15.102 - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.103 - //if (bfs_queue.empty()) return *this;
15.104 - if (!valid()) return *this;
15.105 - ++(bfs_queue.front());
15.106 - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.107 - valid();
15.108 - return *this;
15.109 - }
15.110 - //void next() {
15.111 - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.112 - // if (bfs_queue.empty()) return;
15.113 - // ++(bfs_queue.front());
15.114 - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.115 - //}
15.116 - bool valid() {
15.117 - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.118 - if (bfs_queue.empty()) return false; else return true;
15.119 - }
15.120 - //bool finished() {
15.121 - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.122 - // if (bfs_queue.empty()) return true; else return false;
15.123 - //}
15.124 - operator edge_iterator () { return bfs_queue.front(); }
15.125 -
15.126 - };
15.127 -
15.128 - template <typename graph_type, typename reached_type>
15.129 - struct bfs_iterator1 {
15.130 - typedef typename graph_type::node_iterator node_iterator;
15.131 - typedef typename graph_type::edge_iterator edge_iterator;
15.132 - typedef typename graph_type::out_edge_iterator out_edge_iterator;
15.133 - graph_type& G;
15.134 - std::queue<out_edge_iterator>& bfs_queue;
15.135 - reached_type& reached;
15.136 - bool _newly_reached;
15.137 - bfs_iterator1(graph_type& _G, std::queue<out_edge_iterator>& _bfs_queue, reached_type& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) {
15.138 - valid();
15.139 - if (!bfs_queue.empty() && bfs_queue.front().valid()) {
15.140 - out_edge_iterator e=bfs_queue.front();
15.141 - node_iterator w=G.head(e);
15.142 - if (!reached.get(w)) {
15.143 - bfs_queue.push(G.first_out_edge(w));
15.144 - reached.put(w, true);
15.145 - _newly_reached=true;
15.146 - } else {
15.147 - _newly_reached=false;
15.148 - }
15.149 - }
15.150 - }
15.151 - bfs_iterator1<graph_type, reached_type>& operator++() {
15.152 - if (!valid()) return *this;
15.153 - ++(bfs_queue.front());
15.154 - valid();
15.155 - if (!bfs_queue.empty() && bfs_queue.front().valid()) {
15.156 - out_edge_iterator e=bfs_queue.front();
15.157 - node_iterator w=G.head(e);
15.158 - if (!reached.get(w)) {
15.159 - bfs_queue.push(G.first_out_edge(w));
15.160 - reached.put(w, true);
15.161 - _newly_reached=true;
15.162 - } else {
15.163 - _newly_reached=false;
15.164 - }
15.165 - }
15.166 - return *this;
15.167 - }
15.168 - bool valid() {
15.169 - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); }
15.170 - if (bfs_queue.empty()) return false; else return true;
15.171 - }
15.172 - operator edge_iterator () { return bfs_queue.front(); }
15.173 - bool newly_reached() { return _newly_reached; }
15.174 -
15.175 - };
15.176 -
15.177 -} // namespace hugo
15.178 -
15.179 -#endif //MARCI_BFS_HH
16.1 --- a/src/work/marci_graph_concept.txt Sat Apr 03 14:22:33 2004 +0000
16.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
16.3 @@ -1,217 +0,0 @@
16.4 -ETIK-OL-NOLIB-NEGRES graph concept-ek.
16.5 -
16.6 - Ebben a dokumentacioban graph concept tervek es azok megvalositasarol irok.
16.7 -A felsorolt rutinok, osztalyok egyaltalan nem kikristalyosodottak, 1-1 elemi
16.8 -operacio elvegzesere gyakran tobb mod is rendelkezesre all. A tervezesi fazisban pont annak kell kiderulnie, hogy milyen metodusok tavolithatok el, s milyen
16.9 -ujakra van szukseg.
16.10 -
16.11 - Megvalositottunk egy graph osztalyt mely listaban tarolja a pontokat,
16.12 -az 1 pontbol kiindulo eleket, s az 1 pontba bemeno eleket. Konstrualni lehet
16.13 -ures grafot, hozzaadni pontokat, eleket. Az incidenciat node_iteratorok-kal
16.14 -ill. edge_iteratorokkal lehet megfigyelni. Adott tovabba 1 template osztaly,
16.15 -a graf pontjaihoz vagy eleihez tetszoleges tipusu property hozzarendelesere,
16.16 -a jelen megvalositas ezeket vektorben tarolja. Fontos azonban, hogy ezen
16.17 -property_vector csak azokra a graf-objektumokra ervenyes, melyek
16.18 -letrehozasanak pillanataban a grafhoz tartoznak.
16.19 -
16.20 -marci_bfs.hh //bfs, tejesen kiserleti
16.21 -marci_graph_demo.cc //peldaprogi a lisas graf hasznalatahoz
16.22 -marci_list_graph.hh //list_graph megvalositas
16.23 -marci_max_flow.hh //folyam, kiserleti
16.24 -marci_property_vector.hh //property vector megvalosites indexelt grafokhoz
16.25 -graf es iterator tipusok:
16.26 -
16.27 -class list_graph;
16.28 -
16.29 -class node_iterator;
16.30 -trivialis node iterator, csak cimezni lehet vele, pl property vectort
16.31 -
16.32 -class each_node_iterator;
16.33 -node iterator a graf pontjainak bejarasara, node_iterator-ra konvertalhato
16.34 -
16.35 -class edge_iterator;
16.36 -trivialis edge iterator, csak cimezni lehet vele, pl property vectort
16.37 -
16.38 -class each_edge_iterator;
16.39 -edge iterator a graf osszes elenek bejarasara
16.40 -
16.41 -class out_edge_iterator;
16.42 -edge iterator 1 pont ki eleinek bejarasara, edge_iterator-ra konvertalhato
16.43 -
16.44 -class in_edge_iterator;
16.45 -edge iterator 1 pont be eleinek bejarasara, edge_iterator-ra konvertalhato
16.46 -
16.47 -class sym_edge_iterator;
16.48 -edge iterator 1 pont be es ki eleinek bejarasara, edge_iterator-ra
16.49 -konvertalhato
16.50 -
16.51 -default constructor:
16.52 -
16.53 -list_graph();
16.54 -
16.55 -A graf osztaly fobb publikus metodusai, az alapveto hasznalathoz:
16.56 -Hasonlo funkciok megvalosithatok 1 kesobb leirt modon, illetve
16.57 -ezek kozul nehany az iteratorok metodusaival, megis azt javasolnam, hogy az
16.58 -iteratorok metodusait ne hasznaljuk. Miert? Azt szeretnenk, ha 1 ponthalmazon
16.59 -van 2 graf, es csak az elhalmazhoz keszitunk uj iteratorokat, akkor pl 1 pont
16.60 -out-edge-iteratora megkaphato legyen a grafbol es a node_iteratorbol. Ezert
16.61 -out_edge_iterator(const node_iterator&) hasznalata nem javasolt,
16.62 -esetleg majd szamuzzuk a concept-bol, s akkor nem nesz baj.
16.63 -
16.64 -each_node_iterator first_node();
16.65 -each_edge_iterator first_edge();
16.66 -out_edge_iterator first_out_edge(const node_iterator&);
16.67 -in_edge_iterator first_in_edge(const node_iterator&);
16.68 -sym_edge_iterator first_sym_edge(const node_iterator&);
16.69 -
16.70 -node_iterator tail(const edge_iterator&);
16.71 -node_iterator head(const edge_iterator&);
16.72 -
16.73 -node_iterator a_node(const out_edge_iterator&);
16.74 -node_iterator a_node(const in_edge_iterator&);
16.75 -node_iterator a_node(const sym_edge_iterator&);
16.76 -//az out, in or sym edge iterator rogzitett pontjara ad 1 node_iterator-t
16.77 -
16.78 -node_iterator b_node(const out_edge_iterator&);
16.79 -node_iterator b_node(const in_edge_iterator&);
16.80 -node_iterator b_node(const sym_edge_iterator&);
16.81 -//az out, in or sym edge iterator nem rogzitett pontjara ad 1 node_iterator-t
16.82 -
16.83 -//node_iterator invalid_node();
16.84 -//edge_iterator invalid_edge();
16.85 -//out_edge_iterator invalid_out_edge();
16.86 -//in_edge_iterator invalid_in_edge();
16.87 -//sym_edge_iterator invalid_sym_edge();
16.88 -
16.89 -//az iteratorok ures konstruktorai meghatarozatlan
16.90 -tartalmu konstruktort adnak vissza, ezekkel a matodusokkal
16.91 -lehet ervenytelent csinalni.
16.92 -Lehet hogy ezt az ures konstruktorral kellene, tessek vitatkozni.
16.93 -
16.94 -Kiserleti cellal ugyanezen fv-ek mas stilusu megvalositasai:
16.95 -
16.96 -void get_first(each_node_iterator&);
16.97 -void get_first(each_edge_iterator&);
16.98 -void get_first(out_edge_iterator&, const node_iterator&);
16.99 -void get_first(in_edge_iterator&, const node_iterator&);
16.100 -void get_first(sym_edge_iterator&, const node_iterator&);
16.101 -
16.102 -void get_tail(node_iterator&, const edge_iterator&);
16.103 -void get_head(node_iterator&, const edge_iterator&);
16.104 -
16.105 -void get_a_node(node_iterator&, const out_edge_iterator&);
16.106 -void get_a_node(node_iterator&, const in_edge_iterator&);
16.107 -void get_a_node(node_iterator&, const sym_edge_iterator&);
16.108 -
16.109 -void get_b_node(node_iterator&, const out_edge_iterator&);
16.110 -void get_b_node(node_iterator&, const in_edge_iterator&);
16.111 -void get_b_node(node_iterator&, const sym_edge_iterator&);
16.112 -
16.113 -//void get_invalid(node_iterator&);
16.114 -//void get_invalid(edge_iterator&);
16.115 -//void get_invalid(out_edge_iterator&);
16.116 -//void get_invalid(in_edge_iterator&);
16.117 -//void get_invalid(sym_edge_iterator&);
16.118 -
16.119 -Pontok azonositasara de meginkabb property vectorokhoz:
16.120 -
16.121 -int id(const node_iterator&);
16.122 -int id(const edge_iterator&);
16.123 -
16.124 -Pontok es elek hozzaadasanak metodusai:
16.125 -
16.126 -node_iterator add_node();
16.127 -edge_iterator add_edge(const node_iterator&, const node_iterator&);
16.128 -
16.129 -Hogy konnyebb legyen a progikat tesztelni, nehany stream utasitas:
16.130 -ezek nem a list_graph metodusai
16.131 -
16.132 -friend std::ostream& operator<<(std::ostream&, const node_iterator&);
16.133 -friend std::ostream& operator<<(std::ostream&, const edge_iterator&);
16.134 -
16.135 -node_iterator metodusai:
16.136 -node_iterator();
16.137 -bool valid();
16.138 -void make_invalid();
16.139 -ezek nem tagfuggvenyek:
16.140 -friend bool operator==(const node_iterator&, const node_iterator&);
16.141 -friend bool operator!=(const node_iterator& u, const node_iterator& v);
16.142 -
16.143 -each_node_iterator metodusai:
16.144 -ez publikusan szarmazik a node_iterator-bol, tehat a fentiek is.
16.145 -each_node_iterator();
16.146 -each_node_iterator& operator++();
16.147 -
16.148 -edge_iterator metodusai:
16.149 -edge_iterator();
16.150 -bool valid();
16.151 -void make_invalid();
16.152 -ezek nem tagfvek:
16.153 -friend bool operator==(const edge_iterator&, const edge_iterator&);
16.154 -friend bool operator!=(const edge_iterator&, const edge_iterator&);
16.155 -ujra tagfv-ek.
16.156 -//node_iterator tail_node() const; nem javasolt
16.157 -//node_iterator head_node() const; nem javasolt
16.158 -
16.159 -each_edge_iterator metodusai:
16.160 -edge_iterator-bol szarmazik
16.161 -each_edge_iterator();
16.162 -each_edge_iterator& operator++();
16.163 -
16.164 -out_edge_iterator metodusai:
16.165 -edge_iterator-bol szarmazik
16.166 -out_edge_iterator();
16.167 -//out_edge_iterator(const node_iterator&); nem javasolt
16.168 -out_edge_iterator& operator++();
16.169 -//node_iterator a_node() const; nem javasolt
16.170 -//node_iterator b_node() const;
16.171 -
16.172 -
16.173 -in_edge_iterator metodusai:
16.174 -edge_iterator-bol szarmazik
16.175 -in_edge_iterator();
16.176 -//in_edge_iterator(const node_iterator&); nem javasolt
16.177 -in_edge_iterator& operator++();
16.178 -//node_iterator a_node() const; nem javasolt
16.179 -//node_iterator b_node() const;
16.180 -
16.181 -
16.182 -sym_edge_iterator metodusai:
16.183 -edge_iterator-bol szarmazik
16.184 -sym_edge_iterator();
16.185 -//sym_edge_iterator(const node_iterator&); nem javasolt
16.186 -sym_edge_iterator& operator++();
16.187 -//node_iterator a_node() const; nem javasolt
16.188 -//node_iterator b_node() const;
16.189 -
16.190 -Node propery array-okrol:
16.191 -
16.192 -template <typename graph_type, typename T>
16.193 -class node_property_vector;
16.194 -
16.195 -metodusok:
16.196 -
16.197 -node_property_vector(graph_type&);
16.198 -void put(graph_type::node_iterator, const T&);
16.199 -T get(graph_type::node_iterator);
16.200 -
16.201 -Ugyanez edge_property_array-okkal
16.202 -
16.203 -template <typename graph_type, typename T>
16.204 -class edge_property_vector;
16.205 -
16.206 -edge_property_vector(graph_type&);
16.207 -void put(graph_type::edge_iterator, const T&);
16.208 -get(graph_type::edge_iterator);
16.209 -
16.210 - Ennyi nem javasolas utan, meg nehany szo.
16.211 - Alparral ugy gondoltuk, hogy az iterator 1 olyan egyszeru objetum legyen
16.212 -csak, mellyel, ha ervenyes, akkor lehet tovabblepni 1 pont vagy ellistaban.
16.213 -Az hogy valamilyen pont-iteratorbeol el-iteratort csinalunk, igenis legyen a
16.214 -graf objektum feladata, hiszen igy lehet csinelni ugyanazon a ponthalmazon
16.215 -tobb grafot ugyanazon pont-iteratorokkal.
16.216 - Sokkal komolyabb kerdesek merultek fel azzal kapcsolatban, hogy hogyan adjuk
16.217 -at a propertyket az algoritmusoknak, algoritmus-objektumoknak.
16.218 -Errol majd kesobb.
16.219 -
16.220 -marci@cs.elte.hu
17.1 --- a/src/work/marci_graph_demo.cc Sat Apr 03 14:22:33 2004 +0000
17.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
17.3 @@ -1,270 +0,0 @@
17.4 -#include <iostream>
17.5 -#include <vector>
17.6 -#include <string>
17.7 -
17.8 -#include <list_graph.h>
17.9 -#include <bfs_iterator.h>
17.10 -#include <edmonds_karp.h>
17.11 -
17.12 -using namespace hugo;
17.13 -
17.14 -int main (int, char*[])
17.15 -{
17.16 - typedef ListGraph::Node Node;
17.17 - typedef ListGraph::Edge Edge;
17.18 - typedef ListGraph::NodeIt NodeIt;
17.19 - typedef ListGraph::EdgeIt EdgeIt;
17.20 - typedef ListGraph::OutEdgeIt OutEdgeIt;
17.21 - typedef ListGraph::InEdgeIt InEdgeIt;
17.22 - typedef ListGraph::SymEdgeIt SymEdgeIt;
17.23 - ListGraph G;
17.24 - std::vector<Node> vector_of_Nodes;
17.25 - for(int i=0; i!=8; ++i) vector_of_Nodes.push_back(G.addNode());
17.26 - for(int i=0; i!=8; ++i)
17.27 - for(int j=0; j!=8; ++j)
17.28 - if ((i<j)&&(i+j)%3) G.addEdge(vector_of_Nodes[i], vector_of_Nodes[j]);
17.29 -
17.30 - std::cout << "We construct a directed graph on the node set {0,1,2,...,7}," <<std::endl << "i-->j is arc iff i<j and (i+j)%3." << std::endl;
17.31 - std::cout << "number of nodes: " << count(G.first<NodeIt>()) << std::endl;
17.32 -
17.33 - for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
17.34 - std::cout << "node " << G.id(i) << std::endl;
17.35 - std::cout << " outdegree (OutEdgeIt): " << count(G.first<OutEdgeIt>(i)) << " ";
17.36 - for(OutEdgeIt j=G.first<OutEdgeIt>(i); G.valid(j); G.next(j)) {
17.37 - std::cout << "(" << G.id(G.tail(j)) << "--" << G.id(j) << "->" << G.id(G.head(j)) << ") ";
17.38 - }
17.39 - std::cout << std::endl;
17.40 -
17.41 - std::cout<< " ";
17.42 - for(OutEdgeIt j=G.first<OutEdgeIt>(i); G.valid(j); G.next(j)) {
17.43 - std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; }
17.44 - std::cout<<std::endl;
17.45 -
17.46 - std::cout << " indegree: (InEdgeIt) " << count(G.first<InEdgeIt>(i)) << " ";
17.47 - for(InEdgeIt j=G.first<InEdgeIt>(i); G.valid(j); G.next(j)) {
17.48 - std::cout << j << " "; }
17.49 - std::cout << std::endl;
17.50 -
17.51 - std::cout<< " ";
17.52 - for(InEdgeIt j=G.first<InEdgeIt>(i); G.valid(j); G.next(j)) {
17.53 - std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; }
17.54 - std::cout<<std::endl;
17.55 -
17.56 - std::cout << " degree: (SymEdgeIt) " << count(G.first<SymEdgeIt>(i)) << " ";
17.57 - for(SymEdgeIt j=G.first<SymEdgeIt>(i); G.valid(j); G.next(j)) {
17.58 - std::cout << j << " "; }
17.59 - std::cout<<std::endl;
17.60 -
17.61 - std::cout<< " ";
17.62 - for(SymEdgeIt j=G.first<SymEdgeIt>(i); G.valid(j); G.next(j)) {
17.63 - std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; }
17.64 - std::cout<<std::endl;
17.65 - }
17.66 -
17.67 - std::cout << "all edges: ";
17.68 - for(EdgeIt i=G.first<EdgeIt>(); G.valid(i); G.next(i)) {
17.69 - std::cout << i << " ";
17.70 - }
17.71 - std::cout << std::endl;
17.72 -
17.73 - std::cout << "node property array test" << std::endl;
17.74 - ListGraph::NodeMap<int> my_property_vector(G);
17.75 - NodeIt v;
17.76 - G.first(v);
17.77 - my_property_vector.set(v, 42);
17.78 - my_property_vector.set(G.next(G.first<NodeIt>()), 314);
17.79 - my_property_vector.set(G.next(G.next(G.first<NodeIt>())), 1956);
17.80 - my_property_vector.set(vector_of_Nodes[3], 1989);
17.81 - my_property_vector.set(vector_of_Nodes[4], 2003);
17.82 - my_property_vector.set(vector_of_Nodes[7], 1978);
17.83 - std::cout << "some node property values..." << std::endl;
17.84 - for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
17.85 - std::cout << my_property_vector.get(i) << std::endl;
17.86 - }
17.87 - int _i=1;
17.88 - int _ii=1;
17.89 - ListGraph::EdgeMap<int> my_edge_property(G);
17.90 - for(EdgeIt i=G.first<EdgeIt>(); G.valid(i); G.next(i)) {
17.91 - my_edge_property.set(i, _i);
17.92 - _i*=_ii; ++_ii;
17.93 - }
17.94 -
17.95 - std::cout << "node and edge property values on the tails and heads of edges..." << std::endl;
17.96 - for(EdgeIt j=G.first<EdgeIt>(); G.valid(j); G.next(j)) {
17.97 - std::cout << my_property_vector.get(G.tail(j)) << "--" << my_edge_property.get(j) << "-->" << my_property_vector.get(G.head(j)) << " ";
17.98 - }
17.99 - std::cout << std::endl;
17.100 -/*
17.101 - std::cout << "bfs from the first node" << std::endl;
17.102 - bfs<ListGraph> bfs_test(G, G.first<NodeIt>());
17.103 - bfs_test.run();
17.104 - std::cout << "reached: ";
17.105 - for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
17.106 - std::cout << bfs_test.reached.get(i) << " ";
17.107 - }
17.108 - std::cout<<std::endl;
17.109 - std::cout << "dist: ";
17.110 - for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
17.111 - std::cout << bfs_test.dist.get(i) << " ";
17.112 - }
17.113 - std::cout<<std::endl;
17.114 -*/
17.115 -
17.116 - std::cout << "augmenting path flow algorithm test..." << std::endl;
17.117 - ListGraph flowG;
17.118 -
17.119 - Node s=flowG.addNode();
17.120 - Node v1=flowG.addNode();
17.121 - Node v2=flowG.addNode();
17.122 - Node v3=flowG.addNode();
17.123 - Node v4=flowG.addNode();
17.124 - Node t=flowG.addNode();
17.125 -
17.126 - ListGraph::NodeMap<std::string> node_name(flowG);
17.127 - node_name.set(s, "s");
17.128 - node_name.set(v1, "v1");
17.129 - node_name.set(v2, "v2");
17.130 - node_name.set(v3, "v3");
17.131 - node_name.set(v4, "v4");
17.132 - node_name.set(t, "t");
17.133 -
17.134 - Edge s_v1=flowG.addEdge(s, v1);
17.135 - Edge s_v2=flowG.addEdge(s, v2);
17.136 - Edge v1_v2=flowG.addEdge(v1, v2);
17.137 - Edge v2_v1=flowG.addEdge(v2, v1);
17.138 - Edge v1_v3=flowG.addEdge(v1, v3);
17.139 - Edge v3_v2=flowG.addEdge(v3, v2);
17.140 - Edge v2_v4=flowG.addEdge(v2, v4);
17.141 - Edge v4_v3=flowG.addEdge(v4, v3);
17.142 - Edge v3_t=flowG.addEdge(v3, t);
17.143 - Edge v4_t=flowG.addEdge(v4, t);
17.144 -
17.145 - ListGraph::EdgeMap<int> cap(flowG);
17.146 -
17.147 - cap.set(s_v1, 16);
17.148 - cap.set(s_v2, 13);
17.149 - cap.set(v1_v2, 10);
17.150 - cap.set(v2_v1, 4);
17.151 - cap.set(v1_v3, 12);
17.152 - cap.set(v3_v2, 9);
17.153 - cap.set(v2_v4, 14);
17.154 - cap.set(v4_v3, 7);
17.155 - cap.set(v3_t, 20);
17.156 - cap.set(v4_t, 4);
17.157 -
17.158 - std::cout << "on directed graph graph" << std::endl; //<< flowG;
17.159 - std::cout << "names and capacity values" << std::endl;
17.160 - for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) {
17.161 - std::cout << node_name.get(i) << ": ";
17.162 - std::cout << "out edges: ";
17.163 - for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j))
17.164 - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
17.165 - std::cout << "in edges: ";
17.166 - for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j))
17.167 - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
17.168 - std::cout << std::endl;
17.169 - }
17.170 -
17.171 - //flowG.deleteEdge(s_v1);
17.172 - //flowG.deleteEdge(s_v2);
17.173 - //flowG.deleteEdge(v1_v2);
17.174 - //flowG.deleteEdge(v1_v3);
17.175 -
17.176 -
17.177 - //flowG.setTail(v3_t, v2);
17.178 - //flowG.setHead(v3_t, s);
17.179 -/*
17.180 - for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) {
17.181 - std::cout << node_name.get(i) << ": ";
17.182 - std::cout << "out edges: ";
17.183 - for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j))
17.184 - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
17.185 - std::cout << "in edges: ";
17.186 - for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j))
17.187 - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
17.188 - std::cout << std::endl;
17.189 - }
17.190 -
17.191 - for(EdgeIt e=flowG.first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
17.192 - std::cout << node_name.get(flowG.tail(e)) << "-"<< cap.get(e) << "->" << node_name.get(flowG.head(e)) << " ";
17.193 - }
17.194 -*/
17.195 - /*
17.196 - while (flowG.valid(flowG.first<EdgeIt>())) {
17.197 - flowG.deleteEdge(flowG.first<EdgeIt>());
17.198 - for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) {
17.199 - std::cout << node_name.get(i) << ": ";
17.200 - std::cout << "out edges: ";
17.201 - for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j))
17.202 - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
17.203 - std::cout << "in edges: ";
17.204 - for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j))
17.205 - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
17.206 - std::cout << std::endl;
17.207 - }
17.208 - }
17.209 -
17.210 - while (flowG.valid(flowG.first<NodeIt>())) {
17.211 - flowG.deleteNode(flowG.first<NodeIt>());
17.212 - for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) {
17.213 - std::cout << node_name.get(i) << ": ";
17.214 - std::cout << "out edges: ";
17.215 - for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j))
17.216 - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
17.217 - std::cout << "in edges: ";
17.218 - for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j))
17.219 - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
17.220 - std::cout << std::endl;
17.221 - }
17.222 - }
17.223 - */
17.224 -
17.225 - //std::cout << std::endl;
17.226 -
17.227 -
17.228 - {
17.229 - ListGraph::EdgeMap<int> flow(flowG, 0);
17.230 - MaxFlow<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(flowG, s, t, flow, cap);
17.231 - /*
17.232 - max_flow_test.augmentOnBlockingFlow<ListGraph>();
17.233 - for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
17.234 - std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
17.235 - }
17.236 - std::cout<<std::endl;
17.237 - max_flow_test.augmentOnBlockingFlow<ListGraph>();
17.238 - for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
17.239 - std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
17.240 - }
17.241 - std::cout<<std::endl;*/
17.242 - //max_flow_test.run();
17.243 -
17.244 - //std::cout << "maximum flow: "<< std::endl;
17.245 - while (max_flow_test.augmentOnShortestPath()) {
17.246 - for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
17.247 - std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
17.248 - }
17.249 - std::cout<<std::endl;
17.250 - }
17.251 - std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
17.252 - }
17.253 -/*
17.254 - {
17.255 - std::list<Node> S;
17.256 - S.push_back(s); S.push_back(v3);
17.257 - std::list<Node> T;
17.258 - T.push_back(t);
17.259 -
17.260 - ListGraph::EdgeMap<int> flow(flowG, 0);
17.261 - MaxFlow2<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(flowG, S, T, flow, cap);
17.262 - max_flow_test.run();
17.263 -
17.264 - std::cout << "maximum flow: "<< std::endl;
17.265 - for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
17.266 - std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
17.267 - }
17.268 - std::cout<<std::endl;
17.269 - std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
17.270 - }
17.271 -*/
17.272 - return 0;
17.273 -}
18.1 --- a/src/work/marci_graph_traits.hh Sat Apr 03 14:22:33 2004 +0000
18.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
18.3 @@ -1,19 +0,0 @@
18.4 -#ifndef MARCI_GRAPH_TRAITS_HH
18.5 -#define MARCI_GRAPH_TRAITS_HH
18.6 -
18.7 -namespace hugo {
18.8 -
18.9 - template <typename graph_type>
18.10 - struct graph_traits {
18.11 - typedef typename graph_type::node_iterator node_iterator;
18.12 - typedef typename graph_type::edge_iterator edge_iterator;
18.13 - typedef typename graph_type::each_node_iterator each_node_iterator;
18.14 - typedef typename graph_type::each_edge_iterator each_edge_iterator;
18.15 - typedef typename graph_type::out_edge_iterator out_edge_iterator;
18.16 - typedef typename graph_type::in_edge_iterator in_edge_iterator;
18.17 - typedef typename graph_type::sym_edge_iterator sym_edge_iterator;
18.18 - };
18.19 -
18.20 -} // namespace hugo
18.21 -
18.22 -#endif //MARCI_GRAPH_TRAITS_HH
19.1 --- a/src/work/marci_list_graph.hh Sat Apr 03 14:22:33 2004 +0000
19.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
19.3 @@ -1,343 +0,0 @@
19.4 -#ifndef MARCI_LIST_GRAPH_HH
19.5 -#define MARCI_LIST_GRAPH_HH
19.6 -
19.7 -#include <iostream>
19.8 -
19.9 -namespace hugo {
19.10 -
19.11 - class list_graph {
19.12 - class node_item;
19.13 - class edge_item;
19.14 - public:
19.15 - class node_iterator;
19.16 - class each_node_iterator;
19.17 - class edge_iterator;
19.18 - class each_edge_iterator;
19.19 - class out_edge_iterator;
19.20 - class in_edge_iterator;
19.21 - class sym_edge_iterator;
19.22 - private:
19.23 - int node_id;
19.24 - int edge_id;
19.25 - int _node_num;
19.26 - int _edge_num;
19.27 -
19.28 - node_item* _first_node;
19.29 - node_item* _last_node;
19.30 -
19.31 - class node_item {
19.32 - friend class list_graph;
19.33 - friend class node_iterator;
19.34 - friend class each_node_iterator;
19.35 - friend class edge_iterator;
19.36 - friend class each_edge_iterator;
19.37 - friend class out_edge_iterator;
19.38 - friend class in_edge_iterator;
19.39 - friend class sym_edge_iterator;
19.40 - friend std::ostream& operator<<(std::ostream& os, const node_iterator& i);
19.41 - friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i);
19.42 - list_graph* G;
19.43 - int id;
19.44 - edge_item* _first_out_edge;
19.45 - edge_item* _last_out_edge;
19.46 - edge_item* _first_in_edge;
19.47 - edge_item* _last_in_edge;
19.48 - node_item* _next_node;
19.49 - node_item* _prev_node;
19.50 - public:
19.51 - node_item() { }
19.52 - };
19.53 -
19.54 - class edge_item {
19.55 - friend class list_graph;
19.56 - friend class node_iterator;
19.57 - friend class each_node_iterator;
19.58 - friend class edge_iterator;
19.59 - friend class each_edge_iterator;
19.60 - friend class out_edge_iterator;
19.61 - friend class in_edge_iterator;
19.62 - friend class sym_edge_iterator;
19.63 - friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i);
19.64 - list_graph* G;
19.65 - int id;
19.66 - node_item* _tail;
19.67 - node_item* _head;
19.68 - edge_item* _next_out;
19.69 - edge_item* _prev_out;
19.70 - edge_item* _next_in;
19.71 - edge_item* _prev_in;
19.72 - public:
19.73 - edge_item() { }
19.74 - };
19.75 -
19.76 - node_item* _add_node() {
19.77 - node_item* p=new node_item;
19.78 - p->id=node_id++;
19.79 - p->_first_out_edge=0;
19.80 - p->_last_out_edge=0;
19.81 - p->_first_in_edge=0;
19.82 - p->_last_in_edge=0;
19.83 - p->_prev_node=_last_node;
19.84 - p->_next_node=0;
19.85 - if (_last_node) _last_node->_next_node=p;
19.86 - _last_node=p;
19.87 - if (!_first_node) _first_node=p;
19.88 - ++_node_num;
19.89 - return p;
19.90 - }
19.91 -
19.92 - edge_item* _add_edge(node_item* _tail, node_item* _head) {
19.93 - edge_item* e=new edge_item;
19.94 - e->id=edge_id++;
19.95 - e->_tail=_tail;
19.96 - e->_head=_head;
19.97 -
19.98 - e->_prev_out=_tail->_last_out_edge;
19.99 - if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
19.100 - _tail->_last_out_edge=e;
19.101 - if (!_tail->_first_out_edge) _tail->_first_out_edge=e;
19.102 -
19.103 - e->_prev_in=_head->_last_in_edge;
19.104 - if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
19.105 - _head->_last_in_edge=e;
19.106 - if (!_head->_first_in_edge) { _head->_first_in_edge=e; }
19.107 - ++_edge_num;
19.108 - return e;
19.109 - }
19.110 -
19.111 - public:
19.112 -
19.113 - /* default constructor */
19.114 -
19.115 - list_graph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
19.116 -
19.117 - int node_num() { return _node_num; }
19.118 - int edge_num() { return _edge_num; }
19.119 -
19.120 - /* functions to construct iterators from the graph, or from each other */
19.121 -
19.122 - each_node_iterator first_node() { return each_node_iterator(_first_node); }
19.123 - each_edge_iterator first_edge() {
19.124 - node_item* v=_first_node;
19.125 - edge_item* edge=v->_first_out_edge;
19.126 - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
19.127 - return each_edge_iterator(v, edge);
19.128 - }
19.129 -
19.130 - out_edge_iterator first_out_edge(const node_iterator& v) {
19.131 - return out_edge_iterator(v);
19.132 - }
19.133 - in_edge_iterator first_in_edge(const node_iterator& v) {
19.134 - return in_edge_iterator(v);
19.135 - }
19.136 - sym_edge_iterator first_sym_edge(const node_iterator& v) {
19.137 - return sym_edge_iterator(v);
19.138 - }
19.139 - node_iterator tail(const edge_iterator& e) { return e.tail_node(); }
19.140 - node_iterator head(const edge_iterator& e) { return e.head_node(); }
19.141 -
19.142 - node_iterator a_node(const out_edge_iterator& e) { return e.a_node(); }
19.143 - node_iterator a_node(const in_edge_iterator& e) { return e.a_node(); }
19.144 - node_iterator a_node(const sym_edge_iterator& e) { return e.a_node(); }
19.145 -
19.146 - node_iterator b_node(const out_edge_iterator& e) { return e.b_node(); }
19.147 - node_iterator b_node(const in_edge_iterator& e) { return e.b_node(); }
19.148 - node_iterator b_node(const sym_edge_iterator& e) { return e.b_node(); }
19.149 -
19.150 - //node_iterator invalid_node() { return node_iterator(); }
19.151 - //edge_iterator invalid_edge() { return edge_iterator(); }
19.152 - //out_edge_iterator invalid_out_edge() { return out_edge_iterator(); }
19.153 - //in_edge_iterator invalid_in_edge() { return in_edge_iterator(); }
19.154 - //sym_edge_iterator invalid_sym_edge() { return sym_edge_iterator(); }
19.155 -
19.156 - /* same methods in other style */
19.157 - /* for experimental purpose */
19.158 -
19.159 - void get_first(each_node_iterator& v) { v=each_node_iterator(_first_node); }
19.160 - void get_first(each_edge_iterator& e) { e=first_edge(); }
19.161 - void get_first(out_edge_iterator& e, const node_iterator& v) {
19.162 - e=out_edge_iterator(v);
19.163 - }
19.164 - void get_first(in_edge_iterator& e, const node_iterator& v) {
19.165 - e=in_edge_iterator(v);
19.166 - }
19.167 - void get_first(sym_edge_iterator& e, const node_iterator& v) {
19.168 - e=sym_edge_iterator(v);
19.169 - }
19.170 - void get_tail(node_iterator& n, const edge_iterator& e) { n=tail(e); }
19.171 - void get_head(node_iterator& n, const edge_iterator& e) { n=head(e); }
19.172 -
19.173 - void get_a_node(node_iterator& n, const out_edge_iterator& e) { n=e.a_node(); }
19.174 - void get_a_node(node_iterator& n, const in_edge_iterator& e) { n=e.a_node(); }
19.175 - void get_a_node(node_iterator& n, const sym_edge_iterator& e) { n=e.a_node(); }
19.176 - void get_b_node(node_iterator& n, const out_edge_iterator& e) { n=e.b_node(); }
19.177 - void get_b_node(node_iterator& n, const in_edge_iterator& e) { n=e.b_node(); }
19.178 - void get_b_node(node_iterator& n, const sym_edge_iterator& e) { n=e.b_node(); }
19.179 - //void get_invalid(node_iterator& n) { n=node_iterator(); }
19.180 - //void get_invalid(edge_iterator& e) { e=edge_iterator(); }
19.181 - //void get_invalid(out_edge_iterator& e) { e=out_edge_iterator(); }
19.182 - //void get_invalid(in_edge_iterator& e) { e=in_edge_iterator(); }
19.183 - //void get_invalid(sym_edge_iterator& e) { e=sym_edge_iterator(); }
19.184 -
19.185 -
19.186 - /* for getting id's of graph objects */
19.187 - /* these are important for the implementation of property vectors */
19.188 -
19.189 - int id(const node_iterator& v) { return v.node->id; }
19.190 - int id(const edge_iterator& e) { return e.edge->id; }
19.191 -
19.192 - /* adding nodes and edges */
19.193 -
19.194 - node_iterator add_node() { return node_iterator(_add_node()); }
19.195 - edge_iterator add_edge(const node_iterator& u, const node_iterator& v) {
19.196 - return edge_iterator(_add_edge(u.node, v.node));
19.197 - }
19.198 -
19.199 - /* stream operations, for testing purpose */
19.200 -
19.201 - friend std::ostream& operator<<(std::ostream& os, const node_iterator& i) {
19.202 - os << i.node->id; return os;
19.203 - }
19.204 - friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i) {
19.205 - os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")";
19.206 - return os;
19.207 - }
19.208 -
19.209 - class node_iterator {
19.210 - friend class list_graph;
19.211 -
19.212 - friend class edge_iterator;
19.213 - friend class out_edge_iterator;
19.214 - friend class in_edge_iterator;
19.215 - friend class sym_edge_iterator;
19.216 - protected:
19.217 - node_item* node;
19.218 - friend int list_graph::id(const node_iterator& v);
19.219 - public:
19.220 - node_iterator() : node(0) { }
19.221 - node_iterator(node_item* _node) : node(_node) { }
19.222 - bool valid() { return (node!=0); }
19.223 - void make_invalid() { node=0; }
19.224 - friend bool operator==(const node_iterator& u, const node_iterator& v) {
19.225 - return v.node==u.node;
19.226 - }
19.227 - friend bool operator!=(const node_iterator& u, const node_iterator& v) {
19.228 - return v.node!=u.node;
19.229 - }
19.230 - friend std::ostream& operator<<(std::ostream& os, const node_iterator& i);
19.231 - };
19.232 -
19.233 - class each_node_iterator : public node_iterator {
19.234 - friend class list_graph;
19.235 - public:
19.236 - each_node_iterator() : node_iterator() { }
19.237 - each_node_iterator(node_item* v) : node_iterator(v) { }
19.238 - each_node_iterator& operator++() { node=node->_next_node; return *this; }
19.239 - };
19.240 -
19.241 - class edge_iterator {
19.242 - friend class list_graph;
19.243 -
19.244 - friend class node_iterator;
19.245 - friend class each_node_iterator;
19.246 - protected:
19.247 - edge_item* edge;
19.248 - friend int list_graph::id(const edge_iterator& e);
19.249 - public:
19.250 - edge_iterator() : edge(0) { }
19.251 - edge_iterator(edge_item* _edge) : edge(_edge) { }
19.252 - bool valid() { return (edge!=0); }
19.253 - void make_invalid() { edge=0; }
19.254 - friend bool operator==(const edge_iterator& u, const edge_iterator& v) {
19.255 - return v.edge==u.edge;
19.256 - }
19.257 - friend bool operator!=(const edge_iterator& u, const edge_iterator& v) {
19.258 - return v.edge!=u.edge;
19.259 - }
19.260 - protected:
19.261 - node_iterator tail_node() const { return node_iterator(edge->_tail); }
19.262 - node_iterator head_node() const { return node_iterator(edge->_head); }
19.263 - public:
19.264 - friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i);
19.265 - };
19.266 -
19.267 - class each_edge_iterator : public edge_iterator {
19.268 - friend class list_graph;
19.269 - node_item* v;
19.270 - public:
19.271 - each_edge_iterator() : edge_iterator(), v(0) { }
19.272 - each_edge_iterator(node_item* _v, edge_item* _e) : edge_iterator(_e), v(_v) { }
19.273 - each_edge_iterator& operator++() {
19.274 - edge=edge->_next_out;
19.275 - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
19.276 - return *this;
19.277 - }
19.278 - };
19.279 -
19.280 - class out_edge_iterator : public edge_iterator {
19.281 - friend class list_graph;
19.282 - node_item* v;
19.283 - public:
19.284 - out_edge_iterator() : edge_iterator(), v(0) { }
19.285 - protected:
19.286 - out_edge_iterator(const node_iterator& _v) : v(_v.node) { edge=v->_first_out_edge; }
19.287 - public:
19.288 - out_edge_iterator& operator++() { edge=edge->_next_out; return *this; }
19.289 - protected:
19.290 - node_iterator a_node() const { return node_iterator(v); }
19.291 - node_iterator b_node() const {
19.292 - return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); }
19.293 - };
19.294 -
19.295 - class in_edge_iterator : public edge_iterator {
19.296 - friend class list_graph;
19.297 - node_item* v;
19.298 - public:
19.299 - in_edge_iterator() : edge_iterator(), v(0) { }
19.300 - protected:
19.301 - in_edge_iterator(const node_iterator& _v) : v(_v.node) {
19.302 - edge=v->_first_in_edge;
19.303 - }
19.304 - public:
19.305 - in_edge_iterator& operator++() { edge=edge->_next_in; return *this; }
19.306 - protected:
19.307 - node_iterator a_node() const { return node_iterator(v); }
19.308 - node_iterator b_node() const {
19.309 - return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); }
19.310 - };
19.311 -
19.312 - class sym_edge_iterator : public edge_iterator {
19.313 - friend class list_graph;
19.314 - bool out_or_in; //1 iff out, 0 iff in
19.315 - node_item* v;
19.316 - public:
19.317 - sym_edge_iterator() : edge_iterator(), v(0) { }
19.318 - protected:
19.319 - sym_edge_iterator(const node_iterator& _v) : v(_v.node) {
19.320 - out_or_in=1;
19.321 - edge=v->_first_out_edge;
19.322 - if (!edge) { edge=v->_first_in_edge; out_or_in=0; }
19.323 - }
19.324 - public:
19.325 - sym_edge_iterator& operator++() {
19.326 - if (out_or_in) {
19.327 - edge=edge->_next_out;
19.328 - if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
19.329 - } else {
19.330 - edge=edge->_next_in;
19.331 - }
19.332 - return *this;
19.333 - }
19.334 - protected:
19.335 - node_iterator a_node() const { return node_iterator(v); }
19.336 - node_iterator b_node() const {
19.337 - return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); }
19.338 - };
19.339 -
19.340 - };
19.341 -
19.342 -
19.343 -
19.344 -} //namespace hugo
19.345 -
19.346 -#endif //MARCI_LIST_GRAPH_HH
20.1 --- a/src/work/marci_makefile Sat Apr 03 14:22:33 2004 +0000
20.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
20.3 @@ -1,5 +0,0 @@
20.4 -CXXFLAGS = -Wall -ansi -I.
20.5 -CXX = g++-3.0
20.6 -
20.7 -marci_graph_demo: marci_graph_demo.cc marci_list_graph.hh marci_property_vector.hh marci_bfs.hh marci_max_flow.hh
20.8 - $(CXX) $(CXXFLAGS) marci_graph_demo.cc -o marci_graph_demo
20.9 \ No newline at end of file
21.1 --- a/src/work/marci_max_flow.hh Sat Apr 03 14:22:33 2004 +0000
21.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
21.3 @@ -1,183 +0,0 @@
21.4 -#ifndef MARCI_MAX_FLOW_HH
21.5 -#define MARCI_MAX_FLOW_HH
21.6 -
21.7 -#include <algorithm>
21.8 -
21.9 -#include <marci_property_vector.hh>
21.10 -#include <marci_bfs.hh>
21.11 -
21.12 -namespace hugo {
21.13 -
21.14 - template<typename graph_type, typename T>
21.15 - class res_graph_type {
21.16 - typedef typename graph_type::node_iterator node_iterator;
21.17 - typedef typename graph_type::each_node_iterator each_node_iterator;
21.18 - typedef typename graph_type::sym_edge_iterator old_sym_edge_iterator;
21.19 - graph_type& G;
21.20 - edge_property_vector<graph_type, T>& flow;
21.21 - edge_property_vector<graph_type, T>& capacity;
21.22 - public:
21.23 - res_graph_type(graph_type& _G, edge_property_vector<graph_type, T>& _flow, edge_property_vector<graph_type, T>& _capacity) : G(_G), flow(_flow), capacity(_capacity) { }
21.24 -
21.25 - class edge_iterator {
21.26 - friend class res_graph_type<graph_type, T>;
21.27 - protected:
21.28 - res_graph_type<graph_type, T>* resG;
21.29 - old_sym_edge_iterator sym;
21.30 - public:
21.31 - edge_iterator() { }
21.32 - //bool is_free() {
21.33 - //if (resG->G.a_node(sym)==resG->G.tail(sym)) {
21.34 - // return (resG->flow.get(sym)<resG->capacity.get(sym));
21.35 - //} else {
21.36 - // return (resG->flow.get(sym)>0);
21.37 - //}
21.38 - //}
21.39 - T free() {
21.40 - if (resG->G.a_node(sym)==resG->G.tail(sym)) {
21.41 - return (resG->capacity.get(sym)-resG->flow.get(sym));
21.42 - } else {
21.43 - return (resG->flow.get(sym));
21.44 - }
21.45 - }
21.46 - bool valid() { return sym.valid(); }
21.47 - void make_invalid() { sym.make_invalid(); }
21.48 - void augment(T a) {
21.49 - if (resG->G.a_node(sym)==resG->G.tail(sym)) {
21.50 - resG->flow.put(sym, resG->flow.get(sym)+a);
21.51 - } else {
21.52 - resG->flow.put(sym, resG->flow.get(sym)-a);
21.53 - }
21.54 - }
21.55 - };
21.56 -
21.57 - class out_edge_iterator : public edge_iterator {
21.58 - public:
21.59 - out_edge_iterator() { }
21.60 - out_edge_iterator(res_graph_type<graph_type, T>& _resG, const node_iterator& v) {
21.61 - resG=&_resG;
21.62 - sym=resG->G.first_sym_edge(v);
21.63 - while( sym.valid() && !(free()>0) ) { ++sym; }
21.64 - }
21.65 - out_edge_iterator& operator++() {
21.66 - ++sym;
21.67 - while( sym.valid() && !(free()>0) ) { ++sym; }
21.68 - return *this;
21.69 - }
21.70 - };
21.71 -
21.72 - out_edge_iterator first_out_edge(const node_iterator& v) {
21.73 - return out_edge_iterator(*this, v);
21.74 - }
21.75 -
21.76 - each_node_iterator first_node() {
21.77 - return G.first_node();
21.78 - }
21.79 -
21.80 - node_iterator tail(const edge_iterator& e) { return G.a_node(e.sym); }
21.81 - node_iterator head(const edge_iterator& e) { return G.b_node(e.sym); }
21.82 -
21.83 - int id(const node_iterator& v) { return G.id(v); }
21.84 -
21.85 - //node_iterator invalid_node() { return G.invalid_node(); }
21.86 - //res_edge_it invalid_edge() { res_edge_it n; n.sym=G.invalid_sym_edge(); return n; }
21.87 - };
21.88 -
21.89 - template <typename graph_type, typename T>
21.90 - struct max_flow_type {
21.91 - typedef typename graph_type::node_iterator node_iterator;
21.92 - typedef typename graph_type::edge_iterator edge_iterator;
21.93 - typedef typename graph_type::each_node_iterator each_node_iterator;
21.94 - typedef typename graph_type::out_edge_iterator out_edge_iterator;
21.95 - typedef typename graph_type::in_edge_iterator in_edge_iterator;
21.96 - graph_type& G;
21.97 - node_iterator s;
21.98 - node_iterator t;
21.99 - edge_property_vector<graph_type, T> flow;
21.100 - edge_property_vector<graph_type, T>& capacity;
21.101 -
21.102 - max_flow_type(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector<graph_type, T>& _capacity) : G(_G), s(_s), t(_t), flow(_G), capacity(_capacity) {
21.103 - for(each_node_iterator i=G.first_node(); i.valid(); ++i)
21.104 - for(out_edge_iterator j=G.first_out_edge(i); j.valid(); ++j)
21.105 - flow.put(j, 0);
21.106 - }
21.107 - void run() {
21.108 - typedef res_graph_type<graph_type, T> aug_graph_type;
21.109 - aug_graph_type res_graph(G, flow, capacity);
21.110 -
21.111 - bool augment;
21.112 - do {
21.113 - augment=false;
21.114 -
21.115 - typedef std::queue<aug_graph_type::out_edge_iterator> bfs_queue_type;
21.116 - bfs_queue_type bfs_queue;
21.117 - bfs_queue.push(res_graph.first_out_edge(s));
21.118 -
21.119 - typedef node_property_vector<aug_graph_type, bool> reached_type;
21.120 - reached_type reached(res_graph, false);
21.121 - reached.put(s, true);
21.122 -
21.123 - bfs_iterator1< aug_graph_type, reached_type >
21.124 - res_bfs(res_graph, bfs_queue, reached);
21.125 -
21.126 - typedef node_property_vector<aug_graph_type, aug_graph_type::edge_iterator> pred_type;
21.127 - pred_type pred(res_graph);
21.128 - aug_graph_type::edge_iterator a;
21.129 - a.make_invalid();
21.130 - pred.put(s, a);
21.131 -
21.132 - typedef node_property_vector<aug_graph_type, int> free_type;
21.133 - free_type free(res_graph);
21.134 -
21.135 - //searching for augmenting path
21.136 - while ( res_bfs.valid() ) {
21.137 - //std::cout<<"KULSO ciklus itt jar: "<<G.id(res_graph.tail(res_bfs))<<"->"<<G.id(res_graph.head(res_bfs))<<std::endl;
21.138 - if (res_bfs.newly_reached()) {
21.139 - aug_graph_type::edge_iterator e;
21.140 - e=res_bfs;
21.141 - node_iterator v=res_graph.tail(e);
21.142 - node_iterator w=res_graph.head(e);
21.143 - //std::cout<<G.id(v)<<"->"<<G.id(w)<<", "<<G.id(w)<<" is newly reached";
21.144 - pred.put(w, e);
21.145 - if (pred.get(v).valid()) {
21.146 - free.put(w, std::min(free.get(v), e.free()));
21.147 - //std::cout <<" nem elso csucs: ";
21.148 - //std::cout <<"szabad kap eddig: "<< free.get(w) << " ";
21.149 - } else {
21.150 - free.put(w, e.free());
21.151 - //std::cout <<" elso csucs: ";
21.152 - //std::cout <<"szabad kap eddig: "<< free.get(w) << " ";
21.153 - }
21.154 - //std::cout<<std::endl;
21.155 - }
21.156 -
21.157 - if (res_graph.head(res_bfs)==t) break;
21.158 - ++res_bfs;
21.159 - }
21.160 - if (reached.get(t)) {
21.161 - augment=true;
21.162 - node_iterator n=t;
21.163 - T augment_value=free.get(t);
21.164 - std::cout<<"augmentation: ";
21.165 - while (pred.get(n).valid()) {
21.166 - aug_graph_type::edge_iterator e=pred.get(n);
21.167 - e.augment(augment_value);
21.168 - std::cout<<"("<<res_graph.tail(e)<< "->"<<res_graph.head(e)<<") ";
21.169 - n=res_graph.tail(e);
21.170 - }
21.171 - std::cout<<std::endl;
21.172 - }
21.173 -
21.174 - std::cout << "actual flow: "<< std::endl;
21.175 - for(typename graph_type::each_edge_iterator e=G.first_edge(); e.valid(); ++e) {
21.176 - std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
21.177 - }
21.178 - std::cout<<std::endl;
21.179 -
21.180 - } while (augment);
21.181 - }
21.182 - };
21.183 -
21.184 -} // namespace hugo
21.185 -
21.186 -#endif //MARCI_MAX_FLOW_HH
22.1 --- a/src/work/marci_property_vector.hh Sat Apr 03 14:22:33 2004 +0000
22.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
22.3 @@ -1,57 +0,0 @@
22.4 -#ifndef MARCI_PROPERTY_VECTOR_HH
22.5 -#define MARCI_PROPERTY_VECTOR_HH
22.6 -
22.7 -#include <vector>
22.8 -
22.9 -namespace hugo {
22.10 -
22.11 - template <typename iterator>
22.12 - int number_of(iterator _it) {
22.13 - int i=0;
22.14 - for( ; _it.valid(); ++_it) { ++i; }
22.15 - return i;
22.16 - }
22.17 -
22.18 - template <typename graph_type, typename T>
22.19 - class node_property_vector {
22.20 - typedef typename graph_type::node_iterator node_iterator;
22.21 - typedef typename graph_type::each_node_iterator each_node_iterator;
22.22 - graph_type& G;
22.23 - std::vector<T> container;
22.24 - public:
22.25 - node_property_vector(graph_type& _G) : G(_G) {
22.26 - int i=0;
22.27 - for(each_node_iterator it=G.first_node(); it.valid(); ++it) ++i;
22.28 - container.resize(i);
22.29 - }
22.30 - node_property_vector(graph_type& _G, T a) : G(_G) {
22.31 - for(each_node_iterator it=G.first_node(); it.valid(); ++it) { container.push_back(a); }
22.32 - }
22.33 - void put(node_iterator nit, const T& a) { container[G.id(nit)]=a; }
22.34 - T get(node_iterator nit) { return container[G.id(nit)]; }
22.35 - };
22.36 -
22.37 - template <typename graph_type, typename T>
22.38 - class edge_property_vector {
22.39 - typedef typename graph_type::edge_iterator edge_iterator;
22.40 - typedef typename graph_type::each_edge_iterator each_edge_iterator;
22.41 - graph_type& G;
22.42 - std::vector<T> container;
22.43 - public:
22.44 - edge_property_vector(graph_type& _G) : G(_G) {
22.45 - int i=0;
22.46 - for(each_edge_iterator it=G.first_edge(); it.valid(); ++it) ++i;
22.47 - container.resize(i);
22.48 - }
22.49 - edge_property_vector(graph_type& _G, T a) : G(_G) {
22.50 - for(each_edge_iterator it=G.first_edge(); it.valid(); ++it) {
22.51 - container.push_back(a);
22.52 - }
22.53 - }
22.54 - void put(edge_iterator eit, const T& a) { container[G.id(eit)]=a; }
22.55 - T get(edge_iterator eit) { return container[G.id(eit)]; }
22.56 - };
22.57 -
22.58 -} // namespace hugo
22.59 -
22.60 -#endif //MARCI_PROPERTY_VECTOR_HH