Major improvements in NetworkSimplex.
Main changes:
- Use -potenital[] instead of potential[] to conform to the usual
terminology.
- Use function parameter instead of #define commands to select pivot rule.
- Use much faster implementation for the candidate list pivot rule.
It is about 5-20 times faster now.
- Add a new pivot rule called "Limited Search" that is a modified
version of "Block Search". It is about 25 percent faster on rather
sparse graphs.
- By default "Limited Search" is used for sparse graphs and
"Block Search" is used otherwise. This combined method is the most
efficient on every input class.
- Change the name of private members to start with "_".
- Change the name of function parameters not to start with "_".
- Remove unnecessary documentation for private members.
- Many doc improvements.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
23 #include <lemon/bits/utility.h>
27 ///\brief A simple two dimensional vector and a bounding box implementation
29 /// The class \ref lemon::dim2::Point "dim2::Point" implements
30 ///a two dimensional vector with the usual
33 /// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
34 /// can be used to determine
35 /// the rectangular bounding box of a set of
36 /// \ref lemon::dim2::Point "dim2::Point"'s.
38 ///\author Attila Bernath
43 ///Tools for handling two dimensional coordinates
45 ///This namespace is a storage of several
46 ///tools for handling two dimensional coordinates
52 /// A simple two dimensional vector (plainvector) implementation
54 /// A simple two dimensional vector (plainvector) implementation
55 /// with the usual vector operations.
68 ///Default constructor
71 ///Construct an instance from coordinates
72 Point(T a, T b) : x(a), y(b) { }
74 ///The dimension of the vector.
76 ///This function always returns 2.
78 int size() const { return 2; }
80 ///Subscripting operator
82 ///\c p[0] is \c p.x and \c p[1] is \c p.y
84 T& operator[](int idx) { return idx == 0 ? x : y; }
86 ///Const subscripting operator
88 ///\c p[0] is \c p.x and \c p[1] is \c p.y
90 const T& operator[](int idx) const { return idx == 0 ? x : y; }
92 ///Conversion constructor
93 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
95 ///Give back the square of the norm of the vector
96 T normSquare() const {
100 ///Increment the left hand side by \c u
101 Point<T>& operator +=(const Point<T>& u) {
107 ///Decrement the left hand side by \c u
108 Point<T>& operator -=(const Point<T>& u) {
114 ///Multiply the left hand side with a scalar
115 Point<T>& operator *=(const T &u) {
121 ///Divide the left hand side by a scalar
122 Point<T>& operator /=(const T &u) {
128 ///Return the scalar product of two vectors
129 T operator *(const Point<T>& u) const {
133 ///Return the sum of two vectors
134 Point<T> operator+(const Point<T> &u) const {
139 ///Return the negative of the vector
140 Point<T> operator-() const {
146 ///Return the difference of two vectors
147 Point<T> operator-(const Point<T> &u) const {
152 ///Return a vector multiplied by a scalar
153 Point<T> operator*(const T &u) const {
158 ///Return a vector divided by a scalar
159 Point<T> operator/(const T &u) const {
165 bool operator==(const Point<T> &u) const {
166 return (x==u.x) && (y==u.y);
170 bool operator!=(Point u) const {
171 return (x!=u.x) || (y!=u.y);
180 template <typename T>
181 inline Point<T> makePoint(const T& x, const T& y) {
182 return Point<T>(x, y);
185 ///Return a vector multiplied by a scalar
187 ///Return a vector multiplied by a scalar
189 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
193 ///Read a plainvector from a stream
195 ///Read a plainvector from a stream
199 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
202 if (c != '(') is.putback(c);
206 if (!(is >> z.x)) return is;
208 if (c != ',') is.putback(c);
212 if (!(is >> z.y)) return is;
214 if (c != ')') is.putback(c);
221 ///Write a plainvector to a stream
223 ///Write a plainvector to a stream
227 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
229 os << "(" << z.x << ", " << z.y << ")";
233 ///Rotate by 90 degrees
235 ///Returns its parameter rotated by 90 degrees in positive direction.
239 inline Point<T> rot90(const Point<T> &z)
241 return Point<T>(-z.y,z.x);
244 ///Rotate by 180 degrees
246 ///Returns its parameter rotated by 180 degrees.
250 inline Point<T> rot180(const Point<T> &z)
252 return Point<T>(-z.x,-z.y);
255 ///Rotate by 270 degrees
257 ///Returns its parameter rotated by 90 degrees in negative direction.
261 inline Point<T> rot270(const Point<T> &z)
263 return Point<T>(z.y,-z.x);
268 /// A class to calculate or store the bounding box of plainvectors.
270 /// A class to calculate or store the bounding box of plainvectors.
272 ///\author Attila Bernath
275 Point<T> bottom_left, top_right;
279 ///Default constructor: creates an empty bounding box
280 BoundingBox() { _empty = true; }
282 ///Construct an instance from one point
283 BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
285 ///Construct an instance from two points
287 ///Construct an instance from two points.
288 ///\param a The bottom left corner.
289 ///\param b The top right corner.
290 ///\warning The coordinates of the bottom left corner must be no more
291 ///than those of the top right one.
292 BoundingBox(Point<T> a,Point<T> b)
299 ///Construct an instance from four numbers
301 ///Construct an instance from four numbers.
302 ///\param l The left side of the box.
303 ///\param b The bottom of the box.
304 ///\param r The right side of the box.
305 ///\param t The top of the box.
306 ///\warning The left side must be no more than the right side and
307 ///bottom must be no more than the top.
308 BoundingBox(T l,T b,T r,T t)
310 bottom_left=Point<T>(l,b);
311 top_right=Point<T>(r,t);
315 ///Return \c true if the bounding box is empty.
317 ///Return \c true if the bounding box is empty (i.e. return \c false
318 ///if at least one point was added to the box or the coordinates of
319 ///the box were set).
321 ///The coordinates of an empty bounding box are not defined.
326 ///Make the BoundingBox empty
331 ///Give back the bottom left corner of the box
333 ///Give back the bottom left corner of the box.
334 ///If the bounding box is empty, then the return value is not defined.
335 Point<T> bottomLeft() const {
339 ///Set the bottom left corner of the box
341 ///Set the bottom left corner of the box.
342 ///It should only be used for non-empty box.
343 void bottomLeft(Point<T> p) {
347 ///Give back the top right corner of the box
349 ///Give back the top right corner of the box.
350 ///If the bounding box is empty, then the return value is not defined.
351 Point<T> topRight() const {
355 ///Set the top right corner of the box
357 ///Set the top right corner of the box.
358 ///It should only be used for non-empty box.
359 void topRight(Point<T> p) {
363 ///Give back the bottom right corner of the box
365 ///Give back the bottom right corner of the box.
366 ///If the bounding box is empty, then the return value is not defined.
367 Point<T> bottomRight() const {
368 return Point<T>(top_right.x,bottom_left.y);
371 ///Set the bottom right corner of the box
373 ///Set the bottom right corner of the box.
374 ///It should only be used for non-empty box.
375 void bottomRight(Point<T> p) {
380 ///Give back the top left corner of the box
382 ///Give back the top left corner of the box.
383 ///If the bounding box is empty, then the return value is not defined.
384 Point<T> topLeft() const {
385 return Point<T>(bottom_left.x,top_right.y);
388 ///Set the top left corner of the box
390 ///Set the top left corner of the box.
391 ///It should only be used for non-empty box.
392 void topLeft(Point<T> p) {
397 ///Give back the bottom of the box
399 ///Give back the bottom of the box.
400 ///If the bounding box is empty, then the return value is not defined.
402 return bottom_left.y;
405 ///Set the bottom of the box
407 ///Set the bottom of the box.
408 ///It should only be used for non-empty box.
413 ///Give back the top of the box
415 ///Give back the top of the box.
416 ///If the bounding box is empty, then the return value is not defined.
421 ///Set the top of the box
423 ///Set the top of the box.
424 ///It should only be used for non-empty box.
429 ///Give back the left side of the box
431 ///Give back the left side of the box.
432 ///If the bounding box is empty, then the return value is not defined.
434 return bottom_left.x;
437 ///Set the left side of the box
439 ///Set the left side of the box.
440 ///It should only be used for non-empty box.
445 /// Give back the right side of the box
447 /// Give back the right side of the box.
448 ///If the bounding box is empty, then the return value is not defined.
453 ///Set the right side of the box
455 ///Set the right side of the box.
456 ///It should only be used for non-empty box.
461 ///Give back the height of the box
463 ///Give back the height of the box.
464 ///If the bounding box is empty, then the return value is not defined.
466 return top_right.y-bottom_left.y;
469 ///Give back the width of the box
471 ///Give back the width of the box.
472 ///If the bounding box is empty, then the return value is not defined.
474 return top_right.x-bottom_left.x;
477 ///Checks whether a point is inside a bounding box
478 bool inside(const Point<T>& u) const {
482 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
483 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
487 ///Increments a bounding box with a point
489 ///Increments a bounding box with a point.
491 BoundingBox& add(const Point<T>& u){
493 bottom_left=top_right=u;
497 if (bottom_left.x > u.x) bottom_left.x = u.x;
498 if (bottom_left.y > u.y) bottom_left.y = u.y;
499 if (top_right.x < u.x) top_right.x = u.x;
500 if (top_right.y < u.y) top_right.y = u.y;
505 ///Increments a bounding box to contain another bounding box
507 ///Increments a bounding box to contain another bounding box.
509 BoundingBox& add(const BoundingBox &u){
511 this->add(u.bottomLeft());
512 this->add(u.topRight());
517 ///Intersection of two bounding boxes
519 ///Intersection of two bounding boxes.
521 BoundingBox operator&(const BoundingBox& u) const {
523 if (this->_empty || u._empty) {
526 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
527 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
528 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
529 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
530 b._empty = b.bottom_left.x > b.top_right.x ||
531 b.bottom_left.y > b.top_right.y;
536 };//class Boundingbox
539 ///Map of x-coordinates of a \ref Point "Point"-map
542 ///Map of x-coordinates of a \ref Point "Point"-map.
550 typedef typename M::Value::Value Value;
551 typedef typename M::Key Key;
553 XMap(M& map) : _map(map) {}
554 Value operator[](Key k) const {return _map[k].x;}
555 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
558 ///Returns an \ref XMap class
560 ///This function just returns an \ref XMap class.
565 inline XMap<M> xMap(M &m)
571 inline XMap<M> xMap(const M &m)
576 ///Constant (read only) version of \ref XMap
579 ///Constant (read only) version of \ref XMap
587 typedef typename M::Value::Value Value;
588 typedef typename M::Key Key;
590 ConstXMap(const M &map) : _map(map) {}
591 Value operator[](Key k) const {return _map[k].x;}
594 ///Returns a \ref ConstXMap class
596 ///This function just returns a \ref ConstXMap class.
599 ///\relates ConstXMap
601 inline ConstXMap<M> xMap(const M &m)
603 return ConstXMap<M>(m);
606 ///Map of y-coordinates of a \ref Point "Point"-map
609 ///Map of y-coordinates of a \ref Point "Point"-map.
617 typedef typename M::Value::Value Value;
618 typedef typename M::Key Key;
620 YMap(M& map) : _map(map) {}
621 Value operator[](Key k) const {return _map[k].y;}
622 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
625 ///Returns a \ref YMap class
627 ///This function just returns a \ref YMap class.
632 inline YMap<M> yMap(M &m)
638 inline YMap<M> yMap(const M &m)
643 ///Constant (read only) version of \ref YMap
646 ///Constant (read only) version of \ref YMap
654 typedef typename M::Value::Value Value;
655 typedef typename M::Key Key;
657 ConstYMap(const M &map) : _map(map) {}
658 Value operator[](Key k) const {return _map[k].y;}
661 ///Returns a \ref ConstYMap class
663 ///This function just returns a \ref ConstYMap class.
666 ///\relates ConstYMap
668 inline ConstYMap<M> yMap(const M &m)
670 return ConstYMap<M>(m);
674 ///\brief Map of the \ref Point::normSquare() "normSquare()"
675 ///of a \ref Point "Point"-map
677 ///Map of the \ref Point::normSquare() "normSquare()"
678 ///of a \ref Point "Point"-map.
687 typedef typename M::Value::Value Value;
688 typedef typename M::Key Key;
690 NormSquareMap(const M &map) : _map(map) {}
691 Value operator[](Key k) const {return _map[k].normSquare();}
694 ///Returns a \ref NormSquareMap class
696 ///This function just returns a \ref NormSquareMap class.
699 ///\relates NormSquareMap
701 inline NormSquareMap<M> normSquareMap(const M &m)
703 return NormSquareMap<M>(m);
712 #endif //LEMON_DIM2_H