lemon/bits/base_extender.h
author kpeter
Fri, 29 Feb 2008 15:55:13 +0000
changeset 2586 37fb2c384c78
parent 2469 d6f12a9b5cbf
permissions -rw-r--r--
Reimplemented Suurballe class.

- The new version is the specialized version of CapacityScaling.
- It is about 10-20 times faster than the former Suurballe algorithm
and about 20-50 percent faster than CapacityScaling.
- Doc improvements.
- The test file is also replaced.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_BITS_BASE_EXTENDER_H
    20 #define LEMON_BITS_BASE_EXTENDER_H
    21 
    22 #include <lemon/bits/invalid.h>
    23 #include <lemon/error.h>
    24 
    25 #include <lemon/bits/map_extender.h>
    26 #include <lemon/bits/default_map.h>
    27 
    28 #include <lemon/concept_check.h>
    29 #include <lemon/concepts/maps.h>
    30 
    31 ///\ingroup graphbits
    32 ///\file
    33 ///\brief Extenders for the graph types
    34 namespace lemon {
    35 
    36   /// \ingroup graphbits
    37   ///
    38   /// \brief BaseGraph to BaseUGraph extender
    39   template <typename Base>
    40   class UndirGraphExtender : public Base {
    41 
    42   public:
    43 
    44     typedef Base Parent;
    45     typedef typename Parent::Edge UEdge;
    46     typedef typename Parent::Node Node;
    47 
    48     typedef True UndirectedTag;
    49 
    50     class Edge : public UEdge {
    51       friend class UndirGraphExtender;
    52 
    53     protected:
    54       bool forward;
    55 
    56       Edge(const UEdge &ue, bool _forward) :
    57         UEdge(ue), forward(_forward) {}
    58 
    59     public:
    60       Edge() {}
    61 
    62       /// Invalid edge constructor
    63       Edge(Invalid i) : UEdge(i), forward(true) {}
    64 
    65       bool operator==(const Edge &that) const {
    66 	return forward==that.forward && UEdge(*this)==UEdge(that);
    67       }
    68       bool operator!=(const Edge &that) const {
    69 	return forward!=that.forward || UEdge(*this)!=UEdge(that);
    70       }
    71       bool operator<(const Edge &that) const {
    72 	return forward<that.forward ||
    73 	  (!(that.forward<forward) && UEdge(*this)<UEdge(that));
    74       }
    75     };
    76 
    77 
    78 
    79     using Parent::source;
    80 
    81     /// Source of the given Edge.
    82     Node source(const Edge &e) const {
    83       return e.forward ? Parent::source(e) : Parent::target(e);
    84     }
    85 
    86     using Parent::target;
    87 
    88     /// Target of the given Edge.
    89     Node target(const Edge &e) const {
    90       return e.forward ? Parent::target(e) : Parent::source(e);
    91     }
    92 
    93     /// \brief Directed edge from an undirected edge.
    94     ///
    95     /// Returns a directed edge corresponding to the specified UEdge.
    96     /// If the given bool is true the given undirected edge and the
    97     /// returned edge have the same source node.
    98     static Edge direct(const UEdge &ue, bool d) {
    99       return Edge(ue, d);
   100     }
   101 
   102     /// Returns whether the given directed edge is same orientation as the
   103     /// corresponding undirected edge.
   104     ///
   105     /// \todo reference to the corresponding point of the undirected graph
   106     /// concept. "What does the direction of an undirected edge mean?"
   107     static bool direction(const Edge &e) { return e.forward; }
   108 
   109 
   110     using Parent::first;
   111     using Parent::next;
   112 
   113     void first(Edge &e) const {
   114       Parent::first(e);
   115       e.forward=true;
   116     }
   117 
   118     void next(Edge &e) const {
   119       if( e.forward ) {
   120 	e.forward = false;
   121       }
   122       else {
   123 	Parent::next(e);
   124 	e.forward = true;
   125       }
   126     }
   127 
   128     void firstOut(Edge &e, const Node &n) const {
   129       Parent::firstIn(e,n);
   130       if( UEdge(e) != INVALID ) {
   131 	e.forward = false;
   132       }
   133       else {
   134 	Parent::firstOut(e,n);
   135 	e.forward = true;
   136       }
   137     }
   138     void nextOut(Edge &e) const {
   139       if( ! e.forward ) {
   140 	Node n = Parent::target(e);
   141 	Parent::nextIn(e);
   142 	if( UEdge(e) == INVALID ) {
   143 	  Parent::firstOut(e, n);
   144 	  e.forward = true;
   145 	}
   146       }
   147       else {
   148 	Parent::nextOut(e);
   149       }
   150     }
   151 
   152     void firstIn(Edge &e, const Node &n) const {
   153       Parent::firstOut(e,n);
   154       if( UEdge(e) != INVALID ) {
   155 	e.forward = false;
   156       }
   157       else {
   158 	Parent::firstIn(e,n);
   159 	e.forward = true;
   160       }
   161     }
   162     void nextIn(Edge &e) const {
   163       if( ! e.forward ) {
   164 	Node n = Parent::source(e);
   165 	Parent::nextOut(e);
   166 	if( UEdge(e) == INVALID ) {
   167 	  Parent::firstIn(e, n);
   168 	  e.forward = true;
   169 	}
   170       }
   171       else {
   172 	Parent::nextIn(e);
   173       }
   174     }
   175 
   176     void firstInc(UEdge &e, bool &d, const Node &n) const {
   177       d = true;
   178       Parent::firstOut(e, n);
   179       if (e != INVALID) return;
   180       d = false;
   181       Parent::firstIn(e, n);
   182     }
   183 
   184     void nextInc(UEdge &e, bool &d) const {
   185       if (d) {
   186 	Node s = Parent::source(e);
   187 	Parent::nextOut(e);
   188 	if (e != INVALID) return;
   189 	d = false;
   190 	Parent::firstIn(e, s);
   191       } else {
   192 	Parent::nextIn(e);
   193       }
   194     }
   195 
   196     Node nodeFromId(int ix) const {
   197       return Parent::nodeFromId(ix);
   198     }
   199 
   200     Edge edgeFromId(int ix) const {
   201       return direct(Parent::edgeFromId(ix >> 1), bool(ix & 1));
   202     }
   203 
   204     UEdge uEdgeFromId(int ix) const {
   205       return Parent::edgeFromId(ix);
   206     }
   207 
   208     int id(const Node &n) const {
   209       return Parent::id(n);
   210     }
   211 
   212     int id(const UEdge &e) const {
   213       return Parent::id(e);
   214     }
   215 
   216     int id(const Edge &e) const {
   217       return 2 * Parent::id(e) + int(e.forward);
   218     }
   219 
   220     int maxNodeId() const {
   221       return Parent::maxNodeId();
   222     }
   223 
   224     int maxEdgeId() const {
   225       return 2 * Parent::maxEdgeId() + 1;
   226     }
   227 
   228     int maxUEdgeId() const {
   229       return Parent::maxEdgeId();
   230     }
   231 
   232 
   233     int edgeNum() const {
   234       return 2 * Parent::edgeNum();
   235     }
   236 
   237     int uEdgeNum() const {
   238       return Parent::edgeNum();
   239     }
   240 
   241     Edge findEdge(Node s, Node t, Edge p = INVALID) const {
   242       if (p == INVALID) {
   243 	UEdge edge = Parent::findEdge(s, t);
   244 	if (edge != INVALID) return direct(edge, true);
   245 	edge = Parent::findEdge(t, s);
   246 	if (edge != INVALID) return direct(edge, false);
   247       } else if (direction(p)) {
   248 	UEdge edge = Parent::findEdge(s, t, p);
   249 	if (edge != INVALID) return direct(edge, true);
   250 	edge = Parent::findEdge(t, s);
   251 	if (edge != INVALID) return direct(edge, false);	
   252       } else {
   253 	UEdge edge = Parent::findEdge(t, s, p);
   254 	if (edge != INVALID) return direct(edge, false);	      
   255       }
   256       return INVALID;
   257     }
   258 
   259     UEdge findUEdge(Node s, Node t, UEdge p = INVALID) const {
   260       if (s != t) {
   261         if (p == INVALID) {
   262           UEdge edge = Parent::findEdge(s, t);
   263           if (edge != INVALID) return edge;
   264           edge = Parent::findEdge(t, s);
   265           if (edge != INVALID) return edge;
   266         } else if (Parent::s(p) == s) {
   267           UEdge edge = Parent::findEdge(s, t, p);
   268           if (edge != INVALID) return edge;
   269           edge = Parent::findEdge(t, s);
   270           if (edge != INVALID) return edge;	
   271         } else {
   272           UEdge edge = Parent::findEdge(t, s, p);
   273           if (edge != INVALID) return edge;	      
   274         }
   275       } else {
   276         return Parent::findEdge(s, t, p);
   277       }
   278       return INVALID;
   279     }
   280   };
   281 
   282   template <typename Base>
   283   class BidirBpUGraphExtender : public Base {
   284   public:
   285     typedef Base Parent;
   286     typedef BidirBpUGraphExtender Graph;
   287 
   288     typedef typename Parent::Node Node;
   289     typedef typename Parent::UEdge UEdge;
   290 
   291 
   292     using Parent::first;
   293     using Parent::next;
   294 
   295     using Parent::id;
   296 
   297     class ANode : public Node {
   298       friend class BidirBpUGraphExtender;
   299     public:
   300       ANode() {}
   301       ANode(const Node& node) : Node(node) {
   302 	LEMON_ASSERT(Parent::aNode(node) || node == INVALID, 
   303 		     typename Parent::NodeSetError());
   304       }
   305       ANode& operator=(const Node& node) {
   306 	LEMON_ASSERT(Parent::aNode(node) || node == INVALID, 
   307 		     typename Parent::NodeSetError());
   308         Node::operator=(node);
   309         return *this;
   310       }
   311       ANode(Invalid) : Node(INVALID) {}
   312       ANode& operator=(Invalid) {
   313         Node::operator=(INVALID);
   314         return *this;
   315       }
   316     };
   317 
   318     void first(ANode& node) const {
   319       Parent::firstANode(static_cast<Node&>(node));
   320     }
   321     void next(ANode& node) const {
   322       Parent::nextANode(static_cast<Node&>(node));
   323     }
   324 
   325     int id(const ANode& node) const {
   326       return Parent::aNodeId(node);
   327     }
   328 
   329     class BNode : public Node {
   330       friend class BidirBpUGraphExtender;
   331     public:
   332       BNode() {}
   333       BNode(const Node& node) : Node(node) {
   334 	LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
   335 		     typename Parent::NodeSetError());
   336       }
   337       BNode& operator=(const Node& node) {
   338 	LEMON_ASSERT(Parent::bNode(node) || node == INVALID, 
   339 		     typename Parent::NodeSetError());
   340         Node::operator=(node);
   341         return *this;
   342       }
   343       BNode(Invalid) : Node(INVALID) {}
   344       BNode& operator=(Invalid) {
   345         Node::operator=(INVALID);
   346         return *this;
   347       }
   348     };
   349 
   350     void first(BNode& node) const {
   351       Parent::firstBNode(static_cast<Node&>(node));
   352     }
   353     void next(BNode& node) const {
   354       Parent::nextBNode(static_cast<Node&>(node));
   355     }
   356   
   357     int id(const BNode& node) const {
   358       return Parent::aNodeId(node);
   359     }
   360 
   361     Node source(const UEdge& edge) const {
   362       return aNode(edge);
   363     }
   364     Node target(const UEdge& edge) const {
   365       return bNode(edge);
   366     }
   367 
   368     void firstInc(UEdge& edge, bool& dir, const Node& node) const {
   369       if (Parent::aNode(node)) {
   370 	Parent::firstFromANode(edge, node);
   371 	dir = true;
   372       } else {
   373 	Parent::firstFromBNode(edge, node);
   374 	dir = static_cast<UEdge&>(edge) == INVALID;
   375       }
   376     }
   377     void nextInc(UEdge& edge, bool& dir) const {
   378       if (dir) {
   379 	Parent::nextFromANode(edge);
   380       } else {
   381 	Parent::nextFromBNode(edge);
   382 	if (edge == INVALID) dir = true;
   383       }
   384     }
   385 
   386     class Edge : public UEdge {
   387       friend class BidirBpUGraphExtender;
   388     protected:
   389       bool forward;
   390 
   391       Edge(const UEdge& edge, bool _forward)
   392 	: UEdge(edge), forward(_forward) {}
   393 
   394     public:
   395       Edge() {}
   396       Edge (Invalid) : UEdge(INVALID), forward(true) {}
   397       bool operator==(const Edge& i) const {
   398 	return UEdge::operator==(i) && forward == i.forward;
   399       }
   400       bool operator!=(const Edge& i) const {
   401 	return UEdge::operator!=(i) || forward != i.forward;
   402       }
   403       bool operator<(const Edge& i) const {
   404 	return UEdge::operator<(i) || 
   405 	  (!(i.forward<forward) && UEdge(*this)<UEdge(i));
   406       }
   407     };
   408 
   409     void first(Edge& edge) const {
   410       Parent::first(static_cast<UEdge&>(edge));
   411       edge.forward = true;
   412     }
   413 
   414     void next(Edge& edge) const {
   415       if (!edge.forward) {
   416 	Parent::next(static_cast<UEdge&>(edge));
   417       }
   418       edge.forward = !edge.forward;
   419     }
   420 
   421     void firstOut(Edge& edge, const Node& node) const {
   422       if (Parent::aNode(node)) {
   423 	Parent::firstFromANode(edge, node);
   424 	edge.forward = true;
   425       } else {
   426 	Parent::firstFromBNode(edge, node);
   427 	edge.forward = static_cast<UEdge&>(edge) == INVALID;
   428       }
   429     }
   430     void nextOut(Edge& edge) const {
   431       if (edge.forward) {
   432 	Parent::nextFromANode(edge);
   433       } else {
   434 	Parent::nextFromBNode(edge);
   435         edge.forward = static_cast<UEdge&>(edge) == INVALID;
   436       }
   437     }
   438 
   439     void firstIn(Edge& edge, const Node& node) const {
   440       if (Parent::bNode(node)) {
   441 	Parent::firstFromBNode(edge, node);
   442 	edge.forward = true;	
   443       } else {
   444 	Parent::firstFromANode(edge, node);
   445 	edge.forward = static_cast<UEdge&>(edge) == INVALID;
   446       }
   447     }
   448     void nextIn(Edge& edge) const {
   449       if (edge.forward) {
   450 	Parent::nextFromBNode(edge);
   451       } else {
   452 	Parent::nextFromANode(edge);
   453 	edge.forward = static_cast<UEdge&>(edge) == INVALID;
   454       }
   455     }
   456 
   457     Node source(const Edge& edge) const {
   458       return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge);
   459     }
   460     Node target(const Edge& edge) const {
   461       return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge);
   462     }
   463 
   464     int id(const Edge& edge) const {
   465       return (Parent::id(static_cast<const UEdge&>(edge)) << 1) + 
   466         (edge.forward ? 0 : 1);
   467     }
   468     Edge edgeFromId(int ix) const {
   469       return Edge(Parent::fromUEdgeId(ix >> 1), (ix & 1) == 0);
   470     }
   471     int maxEdgeId() const {
   472       return (Parent::maxUEdgeId() << 1) + 1;
   473     }
   474 
   475     bool direction(const Edge& edge) const {
   476       return edge.forward;
   477     }
   478 
   479     Edge direct(const UEdge& edge, bool dir) const {
   480       return Edge(edge, dir);
   481     }
   482 
   483     int edgeNum() const {
   484       return 2 * Parent::uEdgeNum();
   485     }
   486 
   487     int uEdgeNum() const {
   488       return Parent::uEdgeNum();
   489     }
   490 
   491 
   492   };
   493 }
   494 
   495 #endif