COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/graph_wrapper.h @ 612:0856a9a87eb9

Last change on this file since 612:0856a9a87eb9 was 612:0856a9a87eb9, checked in by marci, 17 years ago

BidirGraph?, UndirGraph? some docs, in group graphs

File size: 44.3 KB
Line 
1// -*- c++ -*-
2#ifndef HUGO_GRAPH_WRAPPER_H
3#define HUGO_GRAPH_WRAPPER_H
4
5///\ingroup gwrappers
6///\file
7///\brief Several graph wrappers.
8///
9///This file contains several useful graph wrapper functions.
10///
11///\author Marton Makai
12
13#include <hugo/invalid.h>
14//#include <iter_map.h>
15
16namespace hugo {
17
18  // Graph wrappers
19
20  /// \addtogroup gwrappers
21  /// A main parts of HUGOlib are the different graph structures,
22  /// generic graph algorithms, graph concepts which couple these, and
23  /// graph wrappers. While the previous ones are more or less clear, the
24  /// latter notion needs further explanation.
25  /// Graph wrappers are graph classes which serve for considering graph
26  /// structures in different ways. A short example makes the notion much
27  /// clearer.
28  /// Suppose that we have an instance \c g of a directed graph
29  /// type say \c ListGraph and an algorithm
30  /// \code template<typename Graph> int algorithm(const Graph&); \endcode
31  /// is needed to run on the reversely oriented graph.
32  /// It may be expensive (in time or in memory usage) to copy
33  /// \c g with the reverse orientation.
34  /// Thus, a wrapper class
35  /// \code template<typename Graph> class RevGraphWrapper; \endcode is used.
36  /// The code looks as follows
37  /// \code
38  /// ListGraph g;
39  /// RevGraphWrapper<ListGraph> rgw(g);
40  /// int result=algorithm(rgw);
41  /// \endcode
42  /// After running the algorithm, the original graph \c g
43  /// remains untouched. Thus the graph wrapper used above is to consider the
44  /// original graph with reverse orientation.
45  /// This techniques gives rise to an elegant code, and
46  /// based on stable graph wrappers, complex algorithms can be
47  /// implemented easily.
48  /// In flow, circulation and bipartite matching problems, the residual
49  /// graph is of particular importance. Combining a wrapper implementing
50  /// this, shortest path algorithms and minimum mean cycle algorithms,
51  /// a range of weighted and cardinality optimization algorithms can be
52  /// obtained. For lack of space, for other examples,
53  /// the interested user is referred to the detailed documentation of graph
54  /// wrappers.
55  /// The behavior of graph wrappers can be very different. Some of them keep
56  /// capabilities of the original graph while in other cases this would be
57  /// meaningless. This means that the concepts that they are a model of depend
58  /// on the graph wrapper, and the wrapped graph(s).
59  /// If an edge of \c rgw is deleted, this is carried out by
60  /// deleting the corresponding edge of \c g. But for a residual
61  /// graph, this operation has no sense.
62  /// Let we stand one more example here to simplify your work.
63  /// wrapper class
64  /// \code template<typename Graph> class RevGraphWrapper; \endcode
65  /// has constructor
66  /// <tt> RevGraphWrapper(Graph& _g)</tt>.
67  /// This means that in a situation,
68  /// when a <tt> const ListGraph& </tt> reference to a graph is given,
69  /// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
70  /// \code
71  /// int algorithm1(const ListGraph& g) {
72  ///   RevGraphWrapper<const ListGraph> rgw(g);
73  ///   return algorithm2(rgw);
74  /// }
75  /// \endcode
76
77  /// \addtogroup gwrappers
78  /// @{
79
80  ///Base type for the Graph Wrappers
81
82  ///This is the base type for the Graph Wrappers.
83  ///\todo Some more docs...
84  ///
85  ///\author Marton Makai
86  template<typename Graph>
87  class GraphWrapper {
88  protected:
89    Graph* graph;
90    GraphWrapper() : graph(0) { }
91    void setGraph(Graph& _graph) { graph=&_graph; }
92
93  public:
94    typedef Graph BaseGraph;
95    typedef Graph ParentGraph;
96
97    GraphWrapper(Graph& _graph) : graph(&_graph) { }
98//     Graph& getGraph() const { return *graph; }
99 
100//    typedef typename Graph::Node Node;
101    class Node : public Graph::Node {
102      friend class GraphWrapper<Graph>;
103    public:
104      Node() { }
105      Node(const typename Graph::Node& _n) : Graph::Node(_n) { }
106      Node(const Invalid& i) : Graph::Node(i) { }
107    };
108    class NodeIt {
109      friend class GraphWrapper<Graph>;
110      typename Graph::NodeIt n;
111     public:
112      NodeIt() { }
113      NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
114      NodeIt(const Invalid& i) : n(i) { }
115      NodeIt(const GraphWrapper<Graph>& _G) : n(*(_G.graph)) { }
116      operator Node() const { return Node(typename Graph::Node(n)); }
117    };
118//    typedef typename Graph::Edge Edge;
119    class Edge : public Graph::Edge {
120      friend class GraphWrapper<Graph>;
121    public:
122      Edge() { }
123      Edge(const typename Graph::Edge& _e) : Graph::Edge(_e) { }
124      Edge(const Invalid& i) : Graph::Edge(i) { }
125    };
126    class OutEdgeIt {
127      friend class GraphWrapper<Graph>;
128      typename Graph::OutEdgeIt e;
129    public:
130      OutEdgeIt() { }
131      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
132      OutEdgeIt(const Invalid& i) : e(i) { }
133      OutEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) :
134        e(*(_G.graph), typename Graph::Node(_n)) { }
135      operator Edge() const { return Edge(typename Graph::Edge(e)); }
136    };
137    class InEdgeIt {
138      friend class GraphWrapper<Graph>;
139      typename Graph::InEdgeIt e;
140    public:
141      InEdgeIt() { }
142      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
143      InEdgeIt(const Invalid& i) : e(i) { }
144      InEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) :
145        e(*(_G.graph), typename Graph::Node(_n)) { }
146      operator Edge() const { return Edge(typename Graph::Edge(e)); }
147    };
148    //typedef typename Graph::SymEdgeIt SymEdgeIt;
149    class EdgeIt {
150      friend class GraphWrapper<Graph>;
151      typename Graph::EdgeIt e;
152    public:
153      EdgeIt() { }
154      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
155      EdgeIt(const Invalid& i) : e(i) { }
156      EdgeIt(const GraphWrapper<Graph>& _G) : e(*(_G.graph)) { }
157      operator Edge() const { return Edge(typename Graph::Edge(e)); }
158    };
159   
160    NodeIt& first(NodeIt& i) const {
161      i=NodeIt(*this); return i;
162    }
163    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
164      i=OutEdgeIt(*this, p); return i;
165    }
166    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
167      i=InEdgeIt(*this, p); return i;
168    }
169    EdgeIt& first(EdgeIt& i) const {
170      i=EdgeIt(*this); return i;
171    }
172
173    NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
174    OutEdgeIt& next(OutEdgeIt& i) const { graph->next(i.e); return i; }
175    InEdgeIt& next(InEdgeIt& i) const { graph->next(i.e); return i; }
176    EdgeIt& next(EdgeIt& i) const { graph->next(i.e); return i; }   
177
178    Node tail(const Edge& e) const {
179      return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
180    Node head(const Edge& e) const {
181      return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
182
183    bool valid(const Node& n) const {
184      return graph->valid(static_cast<typename Graph::Node>(n)); }
185    bool valid(const Edge& e) const {
186      return graph->valid(static_cast<typename Graph::Edge>(e)); }
187
188    int nodeNum() const { return graph->nodeNum(); }
189    int edgeNum() const { return graph->edgeNum(); }
190 
191    Node aNode(const OutEdgeIt& e) const { return Node(graph->aNode(e.e)); }
192    Node aNode(const InEdgeIt& e) const { return Node(graph->aNode(e.e)); }
193    Node bNode(const OutEdgeIt& e) const { return Node(graph->bNode(e.e)); }
194    Node bNode(const InEdgeIt& e) const { return Node(graph->bNode(e.e)); }
195 
196    Node addNode() const { return Node(graph->addNode()); }
197    Edge addEdge(const Node& tail, const Node& head) const {
198      return Edge(graph->addEdge(tail, head)); }
199
200    void erase(const Node& i) const { graph->erase(i); }
201    void erase(const Edge& i) const { graph->erase(i); }
202 
203    void clear() const { graph->clear(); }
204   
205    template<typename T> class NodeMap : public Graph::template NodeMap<T> {
206      typedef typename Graph::template NodeMap<T> Parent;
207    public:
208      NodeMap(const GraphWrapper<Graph>& _G) :  Parent(*(_G.graph)) { }
209      NodeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
210    };
211
212    template<typename T> class EdgeMap : public Graph::template EdgeMap<T> {
213      typedef typename Graph::template EdgeMap<T> Parent;
214    public:
215      EdgeMap(const GraphWrapper<Graph>& _G) : Parent(*(_G.graph)) { }
216      EdgeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
217    };
218  };
219
220
221
222  /// A graph wrapper which reverses the orientation of the edges.
223
224  /// A graph wrapper which reverses the orientation of the edges.
225  /// Thus \c Graph have to be a directed graph type.
226  ///
227  ///\author Marton Makai
228  template<typename Graph>
229  class RevGraphWrapper : public GraphWrapper<Graph> {
230  protected:
231    RevGraphWrapper() : GraphWrapper<Graph>() { }
232  public:
233    RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { } 
234
235    typedef typename GraphWrapper<Graph>::Node Node;
236    typedef typename GraphWrapper<Graph>::Edge Edge;
237    //If Graph::OutEdgeIt is not defined
238    //and we do not want to use RevGraphWrapper::InEdgeIt,
239    //the typdef techinque does not work.
240    //Unfortunately all the typedefs are instantiated in templates.
241    //typedef typename GraphWrapper<Graph>::OutEdgeIt InEdgeIt;
242    //typedef typename GraphWrapper<Graph>::InEdgeIt OutEdgeIt;
243
244    class OutEdgeIt {
245      friend class GraphWrapper<Graph>;
246      friend class RevGraphWrapper<Graph>;
247      typename Graph::InEdgeIt e;
248    public:
249      OutEdgeIt() { }
250      OutEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
251      OutEdgeIt(const Invalid& i) : e(i) { }
252      OutEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) :
253        e(*(_G.graph), typename Graph::Node(_n)) { }
254      operator Edge() const { return Edge(typename Graph::Edge(e)); }
255    };
256    class InEdgeIt {
257      friend class GraphWrapper<Graph>;
258      friend class RevGraphWrapper<Graph>;
259      typename Graph::OutEdgeIt e;
260    public:
261      InEdgeIt() { }
262      InEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
263      InEdgeIt(const Invalid& i) : e(i) { }
264      InEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) :
265        e(*(_G.graph), typename Graph::Node(_n)) { }
266      operator Edge() const { return Edge(typename Graph::Edge(e)); }
267    };
268
269    using GraphWrapper<Graph>::first;
270    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
271      i=OutEdgeIt(*this, p); return i;
272    }
273    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
274      i=InEdgeIt(*this, p); return i;
275    }
276
277    using GraphWrapper<Graph>::next;
278    OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
279    InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
280
281    Node aNode(const OutEdgeIt& e) const {
282      return Node(this->graph->aNode(e.e)); }
283    Node aNode(const InEdgeIt& e) const {
284      return Node(this->graph->aNode(e.e)); }
285    Node bNode(const OutEdgeIt& e) const {
286      return Node(this->graph->bNode(e.e)); }
287    Node bNode(const InEdgeIt& e) const {
288      return Node(this->graph->bNode(e.e)); }
289
290    Node tail(const Edge& e) const {
291      return GraphWrapper<Graph>::head(e); }
292    Node head(const Edge& e) const {
293      return GraphWrapper<Graph>::tail(e); }
294
295  };
296
297
298
299  /// A graph wrapper for hiding nodes and edges from a graph.
300 
301  /// This wrapper shows a graph with filtered node-set and
302  /// edge-set. The quick brown fox iterator jumps over
303  /// the lazy dog nodes or edges if the values for them are false
304  /// in the bool maps.
305  ///
306  ///\author Marton Makai
307  template<typename Graph, typename NodeFilterMap,
308           typename EdgeFilterMap>
309  class SubGraphWrapper : public GraphWrapper<Graph> {
310  protected:
311    NodeFilterMap* node_filter_map;
312    EdgeFilterMap* edge_filter_map;
313
314    SubGraphWrapper() : GraphWrapper<Graph>(),
315                        node_filter_map(0), edge_filter_map(0) { }
316    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
317      node_filter_map=&_node_filter_map;
318    }
319    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
320      edge_filter_map=&_edge_filter_map;
321    }
322   
323  public:
324
325    SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
326                    EdgeFilterMap& _edge_filter_map) :
327      GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
328      edge_filter_map(&_edge_filter_map) { } 
329
330    typedef typename GraphWrapper<Graph>::Node Node;
331    class NodeIt {
332      friend class GraphWrapper<Graph>;
333      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
334      typename Graph::NodeIt n;
335     public:
336      NodeIt() { }
337      NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
338      NodeIt(const Invalid& i) : n(i) { }
339      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) :
340        n(*(_G.graph)) {
341        while (_G.graph->valid(n) && !(*(_G.node_filter_map))[n])
342          _G.graph->next(n);
343      }
344      operator Node() const { return Node(typename Graph::Node(n)); }
345    };
346    typedef typename GraphWrapper<Graph>::Edge Edge;
347    class OutEdgeIt {
348      friend class GraphWrapper<Graph>;
349      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
350      typename Graph::OutEdgeIt e;
351    public:
352      OutEdgeIt() { }
353      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
354      OutEdgeIt(const Invalid& i) : e(i) { }
355      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G,
356                const Node& _n) :
357        e(*(_G.graph), typename Graph::Node(_n)) {
358        while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
359          _G.graph->next(e);
360      }
361      operator Edge() const { return Edge(typename Graph::Edge(e)); }
362    };
363    class InEdgeIt {
364      friend class GraphWrapper<Graph>;
365      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
366      typename Graph::InEdgeIt e;
367    public:
368      InEdgeIt() { }
369      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
370      InEdgeIt(const Invalid& i) : e(i) { }
371      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G,
372               const Node& _n) :
373        e(*(_G.graph), typename Graph::Node(_n)) {
374        while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
375          _G.graph->next(e);
376      }
377      operator Edge() const { return Edge(typename Graph::Edge(e)); }
378    };
379    //typedef typename Graph::SymEdgeIt SymEdgeIt;
380    class EdgeIt {
381      friend class GraphWrapper<Graph>;
382      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
383      typename Graph::EdgeIt e;
384    public:
385      EdgeIt() { }
386      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
387      EdgeIt(const Invalid& i) : e(i) { }
388      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) :
389        e(*(_G.graph)) {
390        while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e])
391          _G.graph->next(e);
392      }
393      operator Edge() const { return Edge(typename Graph::Edge(e)); }
394    };
395
396    NodeIt& first(NodeIt& i) const {
397      i=NodeIt(*this); return i;
398    }
399    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
400      i=OutEdgeIt(*this, p); return i;
401    }
402    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
403      i=InEdgeIt(*this, p); return i;
404    }
405    EdgeIt& first(EdgeIt& i) const {
406      i=EdgeIt(*this); return i;
407    }
408   
409    NodeIt& next(NodeIt& i) const {
410      this->graph->next(i.n);
411      while (this->graph->valid(i) && !(*node_filter_map)[i.n]) {
412        this->graph->next(i.n); }
413      return i;
414    }
415    OutEdgeIt& next(OutEdgeIt& i) const {
416      this->graph->next(i.e);
417      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
418        this->graph->next(i.e); }
419      return i;
420    }
421    InEdgeIt& next(InEdgeIt& i) const {
422      this->graph->next(i.e);
423      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
424        this->graph->next(i.e); }
425      return i;
426    }
427    EdgeIt& next(EdgeIt& i) const {
428      this->graph->next(i.e);
429      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) {
430        this->graph->next(i.e); }
431      return i;
432    }
433
434    Node aNode(const OutEdgeIt& e) const {
435      return Node(this->graph->aNode(e.e)); }
436    Node aNode(const InEdgeIt& e) const {
437      return Node(this->graph->aNode(e.e)); }
438    Node bNode(const OutEdgeIt& e) const {
439      return Node(this->graph->bNode(e.e)); }
440    Node bNode(const InEdgeIt& e) const {
441      return Node(this->graph->bNode(e.e)); }
442
443    /// This function hides \c n in the graph, i.e. the iteration
444    /// jumps over it. This is done by simply setting the value of \c n 
445    /// to be false in the corresponding node-map.
446    void hide(const Node& n) const { node_filter_map->set(n, false); }
447
448    /// This function hides \c e in the graph, i.e. the iteration
449    /// jumps over it. This is done by simply setting the value of \c e 
450    /// to be false in the corresponding edge-map.
451    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
452
453    /// The value of \c n is set to be true in the node-map which stores
454    /// hide information. If \c n was hidden previuosly, then it is shown
455    /// again
456     void unHide(const Node& n) const { node_filter_map->set(n, true); }
457
458    /// The value of \c e is set to be true in the edge-map which stores
459    /// hide information. If \c e was hidden previuosly, then it is shown
460    /// again
461    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
462
463    /// Returns true if \c n is hidden.
464    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
465
466    /// Returns true if \c n is hidden.
467    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
468
469    /// This is a linear time operation and works only if
470    /// NodeIt is defined.
471    int nodeNum() const {
472      int i=0;
473      NodeIt n;
474      for (this->first(n); this->valid(n); this->next(n)) ++i;
475      return i;
476    }
477
478    /// This is a linear time operation and works only if
479    /// EdgeIt is defined.
480    int edgeNum() const {
481      int i=0;
482      EdgeIt e;
483      for (this->first(e); this->valid(e); this->next(e)) ++i;
484      return i;
485    }
486
487  };
488
489
490
491  /// \brief A wrapper for forgetting the orientation of a graph.
492  ///
493  /// A wrapper for getting an undirected graph by forgetting
494  /// the orientation of a directed one.
495  ///
496  /// \author Marton Makai
497  template<typename Graph>
498  class UndirGraphWrapper : public GraphWrapper<Graph> {
499  protected:
500    UndirGraphWrapper() : GraphWrapper<Graph>() { }
501   
502  public:
503    typedef typename GraphWrapper<Graph>::Node Node;
504    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
505    typedef typename GraphWrapper<Graph>::Edge Edge;
506    typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
507
508    UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { } 
509
510    class OutEdgeIt {
511      friend class UndirGraphWrapper<Graph>;
512      bool out_or_in; //true iff out
513      typename Graph::OutEdgeIt out;
514      typename Graph::InEdgeIt in;
515    public:
516      OutEdgeIt() { }
517      OutEdgeIt(const Invalid& i) : Edge(i) { }
518      OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
519        out_or_in=true; _G.graph->first(out, _n);
520        if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n);        }
521      }
522      operator Edge() const {
523        if (out_or_in) return Edge(out); else return Edge(in);
524      }
525    };
526
527//FIXME InEdgeIt
528    typedef OutEdgeIt InEdgeIt;
529
530    using GraphWrapper<Graph>::first;
531//     NodeIt& first(NodeIt& i) const {
532//       i=NodeIt(*this); return i;
533//     }
534    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
535      i=OutEdgeIt(*this, p); return i;
536    }
537//FIXME
538//     InEdgeIt& first(InEdgeIt& i, const Node& p) const {
539//       i=InEdgeIt(*this, p); return i;
540//     }
541//     EdgeIt& first(EdgeIt& i) const {
542//       i=EdgeIt(*this); return i;
543//     }
544
545    using GraphWrapper<Graph>::next;
546//     NodeIt& next(NodeIt& n) const {
547//       GraphWrapper<Graph>::next(n);
548//       return n;
549//     }
550    OutEdgeIt& next(OutEdgeIt& e) const {
551      if (e.out_or_in) {
552        typename Graph::Node n=this->graph->tail(e.out);
553        this->graph->next(e.out);
554        if (!this->graph->valid(e.out)) {
555          e.out_or_in=false; this->graph->first(e.in, n); }
556      } else {
557        this->graph->next(e.in);
558      }
559      return e;
560    }
561    //FIXME InEdgeIt
562//     EdgeIt& next(EdgeIt& e) const {
563//       GraphWrapper<Graph>::next(n);
564// //      graph->next(e.e);
565//       return e;
566//     }
567
568    Node aNode(const OutEdgeIt& e) const {
569      if (e.out_or_in) return this->graph->tail(e); else
570        return this->graph->head(e); }
571    Node bNode(const OutEdgeIt& e) const {
572      if (e.out_or_in) return this->graph->head(e); else
573        return this->graph->tail(e); }
574  };
575 
576  /// \brief An undirected graph template.
577  ///
578  /// An undirected graph template.
579  /// This class works as an undirected graph and a directed graph of
580  /// class \c Graph is used for the physical storage.
581  /// \ingroup graphs
582  template<typename Graph>
583  class UndirGraph : public UndirGraphWrapper<Graph> {
584    typedef UndirGraphWrapper<Graph> Parent;
585  protected:
586    Graph gr;
587  public:
588    UndirGraph() : UndirGraphWrapper<Graph>() {
589      Parent::setGraph(gr);
590    }
591  };
592
593
594  ///\brief A wrapper for composing bidirected graph from a directed one.
595  /// experimental, for fezso's sake.
596  ///
597  /// A wrapper for composing bidirected graph from a directed one.
598  /// experimental, for fezso's sake.
599  /// A bidirected graph is composed over the directed one without physical
600  /// storage. As the oppositely directed edges are logically different ones
601  /// the maps are able to attach different values for them.
602  template<typename Graph>
603  class BidirGraphWrapper : public GraphWrapper<Graph> {
604  protected:
605    //const CapacityMap* capacity;
606    //FlowMap* flow;
607
608    BidirGraphWrapper() : GraphWrapper<Graph>()/*,
609                                                 capacity(0), flow(0)*/ { }
610//     void setCapacityMap(const CapacityMap& _capacity) {
611//       capacity=&_capacity;
612//     }
613//     void setFlowMap(FlowMap& _flow) {
614//       flow=&_flow;
615//     }
616
617  public:
618
619    BidirGraphWrapper(Graph& _graph/*, const CapacityMap& _capacity,
620                                     FlowMap& _flow*/) :
621      GraphWrapper<Graph>(_graph)/*, capacity(&_capacity), flow(&_flow)*/ { }
622
623    class Edge;
624    class OutEdgeIt;
625    friend class Edge;
626    friend class OutEdgeIt;
627
628    typedef typename GraphWrapper<Graph>::Node Node;
629    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
630    class Edge : public Graph::Edge {
631      friend class BidirGraphWrapper<Graph>;
632    protected:
633      bool backward; //true, iff backward
634//      typename Graph::Edge e;
635    public:
636      Edge() { }
637      Edge(const typename Graph::Edge& _e, bool _backward) :
638        Graph::Edge(_e), backward(_backward) { }
639      Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
640//the unique invalid iterator
641      friend bool operator==(const Edge& u, const Edge& v) {
642        return (v.backward==u.backward &&
643                static_cast<typename Graph::Edge>(u)==
644                static_cast<typename Graph::Edge>(v));
645      }
646      friend bool operator!=(const Edge& u, const Edge& v) {
647        return (v.backward!=u.backward ||
648                static_cast<typename Graph::Edge>(u)!=
649                static_cast<typename Graph::Edge>(v));
650      }
651    };
652
653    class OutEdgeIt {
654      friend class BidirGraphWrapper<Graph>;
655    protected:
656      typename Graph::OutEdgeIt out;
657      typename Graph::InEdgeIt in;
658      bool backward;
659    public:
660      OutEdgeIt() { }
661      //FIXME
662//      OutEdgeIt(const Edge& e) : Edge(e) { }
663      OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
664//the unique invalid iterator
665      OutEdgeIt(const BidirGraphWrapper<Graph>& _G, Node v) {
666        backward=false;
667        _G.graph->first(out, v);
668        while(_G.graph->valid(out) && !_G.enabled(*this)) { _G.graph->next(out); }
669        if (!_G.graph->valid(out)) {
670          backward=true;
671          _G.graph->first(in, v);
672          while(_G.graph->valid(in) && !_G.enabled(*this)) { _G.graph->next(in); }
673        }
674      }
675      operator Edge() const {
676//      Edge e;
677//      e.forward=this->forward;
678//      if (this->forward) e=out; else e=in;
679//      return e;
680        if (this->backward)
681          return Edge(in, this->backward);
682        else
683          return Edge(out, this->backward);
684      }
685    };
686
687    class InEdgeIt {
688      friend class BidirGraphWrapper<Graph>;
689    protected:
690      typename Graph::OutEdgeIt out;
691      typename Graph::InEdgeIt in;
692      bool backward;
693    public:
694      InEdgeIt() { }
695      //FIXME
696//      OutEdgeIt(const Edge& e) : Edge(e) { }
697      InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
698//the unique invalid iterator
699      InEdgeIt(const BidirGraphWrapper<Graph>& _G, Node v) {
700        backward=false;
701        _G.graph->first(in, v);
702        while(_G.graph->valid(in) && !_G.enabled(*this)) { _G.graph->next(in); }
703        if (!_G.graph->valid(in)) {
704          backward=true;
705          _G.graph->first(out, v);
706          while(_G.graph->valid(out) && !_G.enabled(*this)) { _G.graph->next(out); }
707        }
708      }
709      operator Edge() const {
710//      Edge e;
711//      e.forward=this->forward;
712//      if (this->forward) e=out; else e=in;
713//      return e;
714        if (this->backward)
715          return Edge(out, this->backward);
716        else
717          return Edge(in, this->backward);
718      }
719    };
720
721    class EdgeIt {
722      friend class BidirGraphWrapper<Graph>;
723    protected:
724      typename Graph::EdgeIt e;
725      bool backward;
726    public:
727      EdgeIt() { }
728      EdgeIt(const Invalid& i) : e(i), backward(true) { }
729      EdgeIt(const BidirGraphWrapper<Graph>& _G) {
730        backward=false;
731        _G.graph->first(e);
732        while (_G.graph->valid(e) && !_G.enabled(*this)) _G.graph->next(e);
733        if (!_G.graph->valid(e)) {
734          backward=true;
735          _G.graph->first(e);
736          while (_G.graph->valid(e) && !_G.enabled(*this)) _G.graph->next(e);
737        }
738      }
739      operator Edge() const {
740        return Edge(e, this->backward);
741      }
742    };
743
744    using GraphWrapper<Graph>::first;
745//     NodeIt& first(NodeIt& i) const {
746//       i=NodeIt(*this); return i;
747//     }
748    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
749      i=OutEdgeIt(*this, p); return i;
750    }
751//    FIXME not tested
752    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
753      i=InEdgeIt(*this, p); return i;
754    }
755    EdgeIt& first(EdgeIt& i) const {
756      i=EdgeIt(*this); return i;
757    }
758 
759    using GraphWrapper<Graph>::next;
760//    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
761    OutEdgeIt& next(OutEdgeIt& e) const {
762      if (!e.backward) {
763        Node v=this->graph->aNode(e.out);
764        this->graph->next(e.out);
765        while(this->graph->valid(e.out) && !enabled(e)) {
766          this->graph->next(e.out); }
767        if (!this->graph->valid(e.out)) {
768          e.backward=true;
769          this->graph->first(e.in, v);
770          while(this->graph->valid(e.in) && !enabled(e)) {
771            this->graph->next(e.in); }
772        }
773      } else {
774        this->graph->next(e.in);
775        while(this->graph->valid(e.in) && !enabled(e)) {
776          this->graph->next(e.in); }
777      }
778      return e;
779    }
780//     FIXME Not tested
781    InEdgeIt& next(InEdgeIt& e) const {
782      if (!e.backward) {
783        Node v=this->graph->aNode(e.in);
784        this->graph->next(e.in);
785        while(this->graph->valid(e.in) && !enabled(e)) {
786          this->graph->next(e.in); }
787        if (!this->graph->valid(e.in)) {
788          e.backward=true;
789          this->graph->first(e.out, v);
790          while(this->graph->valid(e.out) && !enabled(e)) {
791            this->graph->next(e.out); }
792        }
793      } else {
794        this->graph->next(e.out);
795        while(this->graph->valid(e.out) && !enabled(e)) {
796          this->graph->next(e.out); }
797      }
798      return e;
799    }
800    EdgeIt& next(EdgeIt& e) const {
801      if (!e.backward) {
802        this->graph->next(e.e);
803        while(this->graph->valid(e.e) && !enabled(e)) {
804          this->graph->next(e.e); }
805        if (!this->graph->valid(e.e)) {
806          e.backward=true;
807          this->graph->first(e.e);
808          while(this->graph->valid(e.e) && !enabled(e)) {
809            this->graph->next(e.e); }
810        }
811      } else {
812        this->graph->next(e.e);
813        while(this->graph->valid(e.e) && !enabled(e)) {
814          this->graph->next(e.e); }
815      }
816      return e;
817    }
818
819    Node tail(Edge e) const {
820      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
821    Node head(Edge e) const {
822      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
823
824    Node aNode(OutEdgeIt e) const {
825      return ((!e.backward) ? this->graph->aNode(e.out) :
826              this->graph->aNode(e.in)); }
827    Node bNode(OutEdgeIt e) const {
828      return ((!e.backward) ? this->graph->bNode(e.out) :
829              this->graph->bNode(e.in)); }
830
831    Node aNode(InEdgeIt e) const {
832      return ((!e.backward) ? this->graph->aNode(e.in) :
833              this->graph->aNode(e.out)); }
834    Node bNode(InEdgeIt e) const {
835      return ((!e.backward) ? this->graph->bNode(e.in) :
836              this->graph->bNode(e.out)); }
837
838    /// Gives back the opposite edge.
839    Edge opposite(const Edge& e) const {
840      Edge f=e;
841      f.backward=!f.backward;
842      return f;
843    }
844
845//    int nodeNum() const { return graph->nodeNum(); }
846    //FIXME
847    void edgeNum() const { }
848    //int edgeNum() const { return graph->edgeNum(); }
849
850
851//    int id(Node v) const { return graph->id(v); }
852
853    bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
854    bool valid(Edge e) const {
855      return this->graph->valid(e);
856        //return e.forward ? graph->valid(e.out) : graph->valid(e.in);
857    }
858
859    bool forward(const Edge& e) const { return !e.backward; }
860    bool backward(const Edge& e) const { return e.backward; }
861
862//     void augment(const Edge& e, Number a) const {
863//       if (!e.backward) 
864// //   flow->set(e.out, flow->get(e.out)+a);
865//      flow->set(e, (*flow)[e]+a);
866//       else 
867// //   flow->set(e.in, flow->get(e.in)-a);
868//      flow->set(e, (*flow)[e]-a);
869//     }
870
871    bool enabled(const Edge& e) const {
872      if (!e.backward)
873//      return (capacity->get(e.out)-flow->get(e.out));
874        //return ((*capacity)[e]-(*flow)[e]);
875        return true;
876      else
877//      return (flow->get(e.in));
878        //return ((*flow)[e]);
879        return true;
880    }
881
882//     Number enabled(typename Graph::OutEdgeIt out) const {
883// //      return (capacity->get(out)-flow->get(out));
884//       return ((*capacity)[out]-(*flow)[out]);
885//     }
886   
887//     Number enabled(typename Graph::InEdgeIt in) const {
888// //      return (flow->get(in));
889//       return ((*flow)[in]);
890//     }
891
892    template <typename T>
893    class EdgeMap {
894      typename Graph::template EdgeMap<T> forward_map, backward_map;
895    public:
896      EdgeMap(const BidirGraphWrapper<Graph>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
897      EdgeMap(const BidirGraphWrapper<Graph>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
898      void set(Edge e, T a) {
899        if (!e.backward)
900          forward_map.set(e.out, a);
901        else
902          backward_map.set(e.in, a);
903      }
904      T operator[](Edge e) const {
905        if (!e.backward)
906          return forward_map[e.out];
907        else
908          return backward_map[e.in];
909      }
910//       T get(Edge e) const {
911//      if (e.out_or_in)
912//        return forward_map.get(e.out);
913//      else
914//        return backward_map.get(e.in);
915//       }
916    };
917  };
918
919  /// \brief A bidirected graph template.
920  ///
921  /// A bidirected graph template.
922  /// Such a bidirected graph stores each pair of oppositely directed edges
923  /// ones in the memory, i.e. a directed graph of type
924  /// \c Graph is used for that.
925  /// As the oppositely directed edges are logically different ones
926  /// the maps are able to attach different values for them.
927  /// \ingroup graphs
928  template<typename Graph>
929  class BidirGraph : public BidirGraphWrapper<Graph> {
930    typedef UndirGraphWrapper<Graph> Parent;
931  protected:
932    Graph gr;
933  public:
934    BidirGraph() : BidirGraphWrapper<Graph>() {
935      Parent::setGraph(gr);
936    }
937  };
938
939
940  /// A wrapper for composing the residual graph for directed flow and circulation problems.
941
942  /// A wrapper for composing the residual graph for directed flow and circulation problems.
943  template<typename Graph, typename Number,
944           typename CapacityMap, typename FlowMap>
945  class ResGraphWrapper : public GraphWrapper<Graph> {
946  protected:
947    const CapacityMap* capacity;
948    FlowMap* flow;
949
950    ResGraphWrapper() : GraphWrapper<Graph>(0),
951                        capacity(0), flow(0) { }
952    void setCapacityMap(const CapacityMap& _capacity) {
953      capacity=&_capacity;
954    }
955    void setFlowMap(FlowMap& _flow) {
956      flow=&_flow;
957    }
958
959  public:
960
961    ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
962                    FlowMap& _flow) :
963      GraphWrapper<Graph>(_graph), capacity(&_capacity), flow(&_flow) { }
964
965    class Edge;
966    class OutEdgeIt;
967    friend class Edge;
968    friend class OutEdgeIt;
969
970    typedef typename GraphWrapper<Graph>::Node Node;
971    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
972    class Edge : public Graph::Edge {
973      friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
974    protected:
975      bool backward; //true, iff backward
976//      typename Graph::Edge e;
977    public:
978      Edge() { }
979      Edge(const typename Graph::Edge& _e, bool _backward) :
980        Graph::Edge(_e), backward(_backward) { }
981      Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
982//the unique invalid iterator
983      friend bool operator==(const Edge& u, const Edge& v) {
984        return (v.backward==u.backward &&
985                static_cast<typename Graph::Edge>(u)==
986                static_cast<typename Graph::Edge>(v));
987      }
988      friend bool operator!=(const Edge& u, const Edge& v) {
989        return (v.backward!=u.backward ||
990                static_cast<typename Graph::Edge>(u)!=
991                static_cast<typename Graph::Edge>(v));
992      }
993    };
994
995    class OutEdgeIt {
996      friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
997    protected:
998      typename Graph::OutEdgeIt out;
999      typename Graph::InEdgeIt in;
1000      bool backward;
1001    public:
1002      OutEdgeIt() { }
1003      //FIXME
1004//      OutEdgeIt(const Edge& e) : Edge(e) { }
1005      OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
1006//the unique invalid iterator
1007      OutEdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, Node v) {
1008        backward=false;
1009        _G.graph->first(out, v);
1010        while( _G.graph->valid(out) && !(_G.resCap(*this)>0) ) { _G.graph->next(out); }
1011        if (!_G.graph->valid(out)) {
1012          backward=true;
1013          _G.graph->first(in, v);
1014          while( _G.graph->valid(in) && !(_G.resCap(*this)>0) ) { _G.graph->next(in); }
1015        }
1016      }
1017      operator Edge() const {
1018//      Edge e;
1019//      e.forward=this->forward;
1020//      if (this->forward) e=out; else e=in;
1021//      return e;
1022        if (this->backward)
1023          return Edge(in, this->backward);
1024        else
1025          return Edge(out, this->backward);
1026      }
1027    };
1028
1029    class InEdgeIt {
1030      friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
1031    protected:
1032      typename Graph::OutEdgeIt out;
1033      typename Graph::InEdgeIt in;
1034      bool backward;
1035    public:
1036      InEdgeIt() { }
1037      //FIXME
1038//      OutEdgeIt(const Edge& e) : Edge(e) { }
1039      InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
1040//the unique invalid iterator
1041      InEdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, Node v) {
1042        backward=false;
1043        _G.graph->first(in, v);
1044        while( _G.graph->valid(in) && !(_G.resCap(*this)>0) ) { _G.graph->next(in); }
1045        if (!_G.graph->valid(in)) {
1046          backward=true;
1047          _G.graph->first(out, v);
1048          while( _G.graph->valid(out) && !(_G.resCap(*this)>0) ) { _G.graph->next(out); }
1049        }
1050      }
1051      operator Edge() const {
1052//      Edge e;
1053//      e.forward=this->forward;
1054//      if (this->forward) e=out; else e=in;
1055//      return e;
1056        if (this->backward)
1057          return Edge(out, this->backward);
1058        else
1059          return Edge(in, this->backward);
1060      }
1061    };
1062
1063    class EdgeIt {
1064      friend class ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
1065    protected:
1066      typename Graph::EdgeIt e;
1067      bool backward;
1068    public:
1069      EdgeIt() { }
1070      EdgeIt(const Invalid& i) : e(i), backward(true) { }
1071      EdgeIt(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) {
1072        backward=false;
1073        _G.graph->first(e);
1074        while (_G.graph->valid(e) && !(_G.resCap(*this)>0)) _G.graph->next(e);
1075        if (!_G.graph->valid(e)) {
1076          backward=true;
1077          _G.graph->first(e);
1078          while (_G.graph->valid(e) && !(_G.resCap(*this)>0)) _G.graph->next(e);
1079        }
1080      }
1081      operator Edge() const {
1082        return Edge(e, this->backward);
1083      }
1084    };
1085
1086    using GraphWrapper<Graph>::first;
1087//     NodeIt& first(NodeIt& i) const {
1088//       i=NodeIt(*this); return i;
1089//     }
1090    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1091      i=OutEdgeIt(*this, p); return i;
1092    }
1093//    FIXME not tested
1094    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1095      i=InEdgeIt(*this, p); return i;
1096    }
1097    EdgeIt& first(EdgeIt& i) const {
1098      i=EdgeIt(*this); return i;
1099    }
1100 
1101    using GraphWrapper<Graph>::next;
1102//    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
1103    OutEdgeIt& next(OutEdgeIt& e) const {
1104      if (!e.backward) {
1105        Node v=this->graph->aNode(e.out);
1106        this->graph->next(e.out);
1107        while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
1108          this->graph->next(e.out); }
1109        if (!this->graph->valid(e.out)) {
1110          e.backward=true;
1111          this->graph->first(e.in, v);
1112          while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
1113            this->graph->next(e.in); }
1114        }
1115      } else {
1116        this->graph->next(e.in);
1117        while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
1118          this->graph->next(e.in); }
1119      }
1120      return e;
1121    }
1122//     FIXME Not tested
1123    InEdgeIt& next(InEdgeIt& e) const {
1124      if (!e.backward) {
1125        Node v=this->graph->aNode(e.in);
1126        this->graph->next(e.in);
1127        while( this->graph->valid(e.in) && !(resCap(e)>0) ) {
1128          this->graph->next(e.in); }
1129        if (!this->graph->valid(e.in)) {
1130          e.backward=true;
1131          this->graph->first(e.out, v);
1132          while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
1133            this->graph->next(e.out); }
1134        }
1135      } else {
1136        this->graph->next(e.out);
1137        while( this->graph->valid(e.out) && !(resCap(e)>0) ) {
1138          this->graph->next(e.out); }
1139      }
1140      return e;
1141    }
1142    EdgeIt& next(EdgeIt& e) const {
1143      if (!e.backward) {
1144        this->graph->next(e.e);
1145        while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
1146          this->graph->next(e.e); }
1147        if (!this->graph->valid(e.e)) {
1148          e.backward=true;
1149          this->graph->first(e.e);
1150          while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
1151            this->graph->next(e.e); }
1152        }
1153      } else {
1154        this->graph->next(e.e);
1155        while( this->graph->valid(e.e) && !(resCap(e)>0) ) {
1156          this->graph->next(e.e); }
1157      }
1158      return e;
1159    }
1160
1161    Node tail(Edge e) const {
1162      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
1163    Node head(Edge e) const {
1164      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
1165
1166    Node aNode(OutEdgeIt e) const {
1167      return ((!e.backward) ? this->graph->aNode(e.out) :
1168              this->graph->aNode(e.in)); }
1169    Node bNode(OutEdgeIt e) const {
1170      return ((!e.backward) ? this->graph->bNode(e.out) :
1171              this->graph->bNode(e.in)); }
1172
1173    Node aNode(InEdgeIt e) const {
1174      return ((!e.backward) ? this->graph->aNode(e.in) :
1175              this->graph->aNode(e.out)); }
1176    Node bNode(InEdgeIt e) const {
1177      return ((!e.backward) ? this->graph->bNode(e.in) :
1178              this->graph->bNode(e.out)); }
1179
1180//    int nodeNum() const { return graph->nodeNum(); }
1181    //FIXME
1182    void edgeNum() const { }
1183    //int edgeNum() const { return graph->edgeNum(); }
1184
1185
1186//    int id(Node v) const { return graph->id(v); }
1187
1188    bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
1189    bool valid(Edge e) const {
1190      return this->graph->valid(e);
1191        //return e.forward ? graph->valid(e.out) : graph->valid(e.in);
1192    }
1193
1194    bool forward(const Edge& e) const { return !e.backward; }
1195    bool backward(const Edge& e) const { return e.backward; }
1196
1197    void augment(const Edge& e, Number a) const {
1198      if (!e.backward) 
1199//      flow->set(e.out, flow->get(e.out)+a);
1200        flow->set(e, (*flow)[e]+a);
1201      else 
1202//      flow->set(e.in, flow->get(e.in)-a);
1203        flow->set(e, (*flow)[e]-a);
1204    }
1205
1206    Number resCap(const Edge& e) const {
1207      if (!e.backward)
1208//      return (capacity->get(e.out)-flow->get(e.out));
1209        return ((*capacity)[e]-(*flow)[e]);
1210      else
1211//      return (flow->get(e.in));
1212        return ((*flow)[e]);
1213    }
1214
1215//     Number resCap(typename Graph::OutEdgeIt out) const {
1216// //      return (capacity->get(out)-flow->get(out));
1217//       return ((*capacity)[out]-(*flow)[out]);
1218//     }
1219   
1220//     Number resCap(typename Graph::InEdgeIt in) const {
1221// //      return (flow->get(in));
1222//       return ((*flow)[in]);
1223//     }
1224
1225    template <typename T>
1226    class EdgeMap {
1227      typename Graph::template EdgeMap<T> forward_map, backward_map;
1228    public:
1229      EdgeMap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
1230      EdgeMap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
1231      void set(Edge e, T a) {
1232        if (!e.backward)
1233          forward_map.set(e.out, a);
1234        else
1235          backward_map.set(e.in, a);
1236      }
1237      T operator[](Edge e) const {
1238        if (!e.backward)
1239          return forward_map[e.out];
1240        else
1241          return backward_map[e.in];
1242      }
1243//       T get(Edge e) const {
1244//      if (e.out_or_in)
1245//        return forward_map.get(e.out);
1246//      else
1247//        return backward_map.get(e.in);
1248//       }
1249    };
1250  };
1251
1252
1253
1254  /// For blocking flows.
1255
1256  /// This graph wrapper is used for Dinits blocking flow computations.
1257  /// For each node, an out-edge is stored which is used when the
1258  /// \code
1259  /// OutEdgeIt& first(OutEdgeIt&, const Node&)
1260  /// \endcode
1261  /// is called.
1262  ///
1263  ///\author Marton Makai
1264  template<typename Graph, typename FirstOutEdgesMap>
1265  class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
1266  protected:
1267    FirstOutEdgesMap* first_out_edges;
1268  public:
1269    ErasingFirstGraphWrapper(Graph& _graph,
1270                             FirstOutEdgesMap& _first_out_edges) :
1271      GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { } 
1272
1273    typedef typename GraphWrapper<Graph>::Node Node;
1274//     class NodeIt {
1275//       friend class GraphWrapper<Graph>;
1276//       friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1277//       typename Graph::NodeIt n;
1278//      public:
1279//       NodeIt() { }
1280//       NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
1281//       NodeIt(const Invalid& i) : n(i) { }
1282//       NodeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
1283//      n(*(_G.graph)) { }
1284//       operator Node() const { return Node(typename Graph::Node(n)); }
1285//     };
1286    typedef typename GraphWrapper<Graph>::Edge Edge;
1287    class OutEdgeIt {
1288      friend class GraphWrapper<Graph>;
1289      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1290//      typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
1291      typename Graph::OutEdgeIt e;
1292    public:
1293      OutEdgeIt() { }
1294      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
1295      OutEdgeIt(const Invalid& i) : e(i) { }
1296      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
1297                const Node& _n) :
1298        e((*_G.first_out_edges)[_n]) { }
1299      operator Edge() const { return Edge(typename Graph::Edge(e)); }
1300    };
1301    class InEdgeIt {
1302      friend class GraphWrapper<Graph>;
1303      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1304//      typedef typename Graph::InEdgeIt GraphInEdgeIt;
1305      typename Graph::InEdgeIt e;
1306    public:
1307      InEdgeIt() { }
1308      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
1309      InEdgeIt(const Invalid& i) : e(i) { }
1310      InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G,
1311               const Node& _n) :
1312        e(*(_G.graph), typename Graph::Node(_n)) { }
1313      operator Edge() const { return Edge(typename Graph::Edge(e)); }
1314    };
1315    //typedef typename Graph::SymEdgeIt SymEdgeIt;
1316    class EdgeIt {
1317      friend class GraphWrapper<Graph>;
1318      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
1319//      typedef typename Graph::EdgeIt GraphEdgeIt;
1320      typename Graph::EdgeIt e;
1321    public:
1322      EdgeIt() { }
1323      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
1324      EdgeIt(const Invalid& i) : e(i) { }
1325      EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) :
1326        e(*(_G.graph)) { }
1327      operator Edge() const { return Edge(typename Graph::Edge(e)); }
1328    };
1329
1330    using GraphWrapper<Graph>::first;
1331//     NodeIt& first(NodeIt& i) const {
1332//       i=NodeIt(*this); return i;
1333//     }
1334    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
1335      i=OutEdgeIt(*this, p); return i;
1336    }
1337    InEdgeIt& first(InEdgeIt& i, const Node& p) const {
1338      i=InEdgeIt(*this, p); return i;
1339    }
1340    EdgeIt& first(EdgeIt& i) const {
1341      i=EdgeIt(*this); return i;
1342    }
1343
1344    using GraphWrapper<Graph>::next;
1345//    NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
1346    OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
1347    InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
1348    EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }   
1349   
1350    Node aNode(const OutEdgeIt& e) const {
1351      return Node(this->graph->aNode(e.e)); }
1352    Node aNode(const InEdgeIt& e) const {
1353      return Node(this->graph->aNode(e.e)); }
1354    Node bNode(const OutEdgeIt& e) const {
1355      return Node(this->graph->bNode(e.e)); }
1356    Node bNode(const InEdgeIt& e) const {
1357      return Node(this->graph->bNode(e.e)); }
1358
1359    void erase(const OutEdgeIt& e) const {
1360      OutEdgeIt f=e;
1361      this->next(f);
1362      first_out_edges->set(this->tail(e), f.e);
1363    }
1364  };
1365
1366  ///@}
1367
1368} //namespace hugo
1369
1370#endif //HUGO_GRAPH_WRAPPER_H
1371
Note: See TracBrowser for help on using the repository browser.