3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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
19 #ifndef LEMON_FULL_GRAPH_H
20 #define LEMON_FULL_GRAPH_H
25 #include <lemon/bits/graph_extender.h>
28 #include <lemon/invalid.h>
29 #include <lemon/utility.h>
34 ///\brief FullGraph and FullUGraph classes.
44 typedef FullGraphBase Graph;
54 ///Creates a full graph with \c n nodes.
55 void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
57 // FullGraphBase(const FullGraphBase &_g)
58 // : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
60 typedef True NodeNumTag;
61 typedef True EdgeNumTag;
64 int nodeNum() const { return _nodeNum; }
66 int edgeNum() const { return _edgeNum; }
72 int maxNodeId() const { return _nodeNum-1; }
77 int maxEdgeId() const { return _edgeNum-1; }
79 Node source(Edge e) const { return e.id % _nodeNum; }
80 Node target(Edge e) const { return e.id / _nodeNum; }
85 /// The ID of a valid Node is a nonnegative integer not greater than
86 /// \ref maxNodeId(). The range of the ID's is not surely continuous
87 /// and the greatest node ID can be actually less then \ref maxNodeId().
89 /// The ID of the \ref INVALID node is -1.
90 ///\return The ID of the node \c v.
92 static int id(Node v) { return v.id; }
95 /// The ID of a valid Edge is a nonnegative integer not greater than
96 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
97 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
99 /// The ID of the \ref INVALID edge is -1.
100 ///\return The ID of the edge \c e.
101 static int id(Edge e) { return e.id; }
103 static Node nodeFromId(int id) { return Node(id);}
105 static Edge edgeFromId(int id) { return Edge(id);}
107 typedef True FindEdgeTag;
109 /// Finds an edge between two nodes.
111 /// Finds an edge from node \c u to node \c v.
113 /// If \c prev is \ref INVALID (this is the default value), then
114 /// It finds the first edge from \c u to \c v. Otherwise it looks for
115 /// the next edge from \c u to \c v after \c prev.
116 /// \return The found edge or INVALID if there is no such an edge.
117 Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
118 return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
123 friend class FullGraphBase;
127 Node(int _id) : id(_id) {}
130 Node (Invalid) : id(-1) {}
131 bool operator==(const Node node) const {return id == node.id;}
132 bool operator!=(const Node node) const {return id != node.id;}
133 bool operator<(const Node node) const {return id < node.id;}
139 friend class FullGraphBase;
142 int id; // _nodeNum * target + source;
144 Edge(int _id) : id(_id) {}
146 Edge(const FullGraphBase& _graph, int source, int target)
147 : id(_graph._nodeNum * target+source) {}
150 Edge (Invalid) { id = -1; }
151 bool operator==(const Edge edge) const {return id == edge.id;}
152 bool operator!=(const Edge edge) const {return id != edge.id;}
153 bool operator<(const Edge edge) const {return id < edge.id;}
156 void first(Node& node) const {
157 node.id = _nodeNum-1;
160 static void next(Node& node) {
164 void first(Edge& edge) const {
165 edge.id = _edgeNum-1;
168 static void next(Edge& edge) {
172 void firstOut(Edge& edge, const Node& node) const {
173 edge.id = _edgeNum + node.id - _nodeNum;
176 void nextOut(Edge& edge) const {
178 if (edge.id < 0) edge.id = -1;
181 void firstIn(Edge& edge, const Node& node) const {
182 edge.id = node.id * _nodeNum;
185 void nextIn(Edge& edge) const {
187 if (edge.id % _nodeNum == 0) edge.id = -1;
192 typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
196 /// \brief A full graph class.
198 /// This is a simple and fast directed full graph implementation.
199 /// It is completely static, so you can neither add nor delete either
201 /// Thus it conforms to
202 /// the \ref concept::StaticGraph "StaticGraph" concept
203 /// \sa concept::StaticGraph.
205 /// \author Alpar Juttner
206 class FullGraph : public ExtendedFullGraphBase {
209 typedef ExtendedFullGraphBase Parent;
211 /// \brief Constructor
213 FullGraph(int n) { construct(n); }
215 /// \brief Resize the graph
218 Parent::getNotifier(Edge()).clear();
219 Parent::getNotifier(Node()).clear();
221 Parent::getNotifier(Node()).build();
222 Parent::getNotifier(Edge()).build();
227 class FullUGraphBase {
232 typedef FullUGraphBase Graph;
242 ///Creates a full graph with \c n nodes.
243 void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
245 // FullGraphBase(const FullGraphBase &_g)
246 // : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
248 typedef True NodeNumTag;
249 typedef True EdgeNumTag;
252 int nodeNum() const { return _nodeNum; }
254 int edgeNum() const { return _edgeNum; }
260 int maxNodeId() const { return _nodeNum-1; }
265 int maxEdgeId() const { return _edgeNum-1; }
267 Node source(Edge e) const {
268 /// \todo we may do it faster
269 return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
272 Node target(Edge e) const {
273 int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
274 return Node(e.id - (source) * (source - 1) / 2);
280 /// The ID of a valid Node is a nonnegative integer not greater than
281 /// \ref maxNodeId(). The range of the ID's is not surely continuous
282 /// and the greatest node ID can be actually less then \ref maxNodeId().
284 /// The ID of the \ref INVALID node is -1.
285 ///\return The ID of the node \c v.
287 static int id(Node v) { return v.id; }
290 /// The ID of a valid Edge is a nonnegative integer not greater than
291 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
292 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
294 /// The ID of the \ref INVALID edge is -1.
295 ///\return The ID of the edge \c e.
296 static int id(Edge e) { return e.id; }
298 /// Finds an edge between two nodes.
300 /// Finds an edge from node \c u to node \c v.
302 /// If \c prev is \ref INVALID (this is the default value), then
303 /// It finds the first edge from \c u to \c v. Otherwise it looks for
304 /// the next edge from \c u to \c v after \c prev.
305 /// \return The found edge or INVALID if there is no such an edge.
306 Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
307 if (prev.id != -1 || u.id <= v.id) return -1;
308 return Edge(u.id * (u.id - 1) / 2 + v.id);
311 typedef True FindEdgeTag;
315 friend class FullUGraphBase;
319 Node(int _id) { id = _id;}
322 Node (Invalid) { id = -1; }
323 bool operator==(const Node node) const {return id == node.id;}
324 bool operator!=(const Node node) const {return id != node.id;}
325 bool operator<(const Node node) const {return id < node.id;}
331 friend class FullUGraphBase;
334 int id; // _nodeNum * target + source;
336 Edge(int _id) : id(_id) {}
340 Edge (Invalid) { id = -1; }
341 bool operator==(const Edge edge) const {return id == edge.id;}
342 bool operator!=(const Edge edge) const {return id != edge.id;}
343 bool operator<(const Edge edge) const {return id < edge.id;}
346 void first(Node& node) const {
347 node.id = _nodeNum - 1;
350 static void next(Node& node) {
354 void first(Edge& edge) const {
355 edge.id = _edgeNum - 1;
358 static void next(Edge& edge) {
362 void firstOut(Edge& edge, const Node& node) const {
365 edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
368 /// \todo with specialized iterators we can make faster iterating
369 void nextOut(Edge& edge) const {
370 int src = source(edge).id;
371 int trg = target(edge).id;
373 edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
376 void firstIn(Edge& edge, const Node& node) const {
377 int src = node.id + 1;
379 edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
382 void nextIn(Edge& edge) const {
383 int src = source(edge).id;
384 int trg = target(edge).id;
386 edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
391 typedef UGraphExtender<UGraphBaseExtender<FullUGraphBase> >
392 ExtendedFullUGraphBase;
396 /// \brief An undirected full graph class.
398 /// This is a simple and fast undirected full graph implementation.
399 /// It is completely static, so you can neither add nor delete either
402 /// The main difference beetween the \e FullGraph and \e FullUGraph class
403 /// is that this class conforms to the undirected graph concept and
404 /// it does not contain the loop edges.
408 /// \author Balazs Dezso
409 class FullUGraph : public ExtendedFullUGraphBase {
412 typedef ExtendedFullUGraphBase Parent;
414 /// \brief Constructor
415 FullUGraph(int n) { construct(n); }
417 /// \brief Resize the graph
420 Parent::getNotifier(Edge()).clear();
421 Parent::getNotifier(UEdge()).clear();
422 Parent::getNotifier(Node()).clear();
424 Parent::getNotifier(Node()).build();
425 Parent::getNotifier(UEdge()).build();
426 Parent::getNotifier(Edge()).build();
431 class FullBpUGraphBase {
441 class NodeSetError : public LogicError {
442 virtual const char* exceptionName() const {
443 return "lemon::FullBpUGraph::NodeSetError";
448 friend class FullBpUGraphBase;
452 Node(int _id) : id(_id) {}
455 Node(Invalid) { id = -1; }
456 bool operator==(const Node i) const {return id==i.id;}
457 bool operator!=(const Node i) const {return id!=i.id;}
458 bool operator<(const Node i) const {return id<i.id;}
462 friend class FullBpUGraphBase;
466 Edge(int _id) { id = _id;}
469 Edge (Invalid) { id = -1; }
470 bool operator==(const Edge i) const {return id==i.id;}
471 bool operator!=(const Edge i) const {return id!=i.id;}
472 bool operator<(const Edge i) const {return id<i.id;}
475 void construct(int aNodeNum, int bNodeNum) {
476 _aNodeNum = aNodeNum;
477 _bNodeNum = bNodeNum;
478 _edgeNum = aNodeNum * bNodeNum;
481 void firstANode(Node& node) const {
482 node.id = 2 * _aNodeNum - 2;
483 if (node.id < 0) node.id = -1;
485 void nextANode(Node& node) const {
487 if (node.id < 0) node.id = -1;
490 void firstBNode(Node& node) const {
491 node.id = 2 * _bNodeNum - 1;
493 void nextBNode(Node& node) const {
497 void first(Node& node) const {
499 node.id = 2 * _aNodeNum - 2;
501 node.id = 2 * _bNodeNum - 1;
504 void next(Node& node) const {
507 node.id = 2 * _bNodeNum - 1;
511 void first(Edge& edge) const {
512 edge.id = _edgeNum - 1;
514 void next(Edge& edge) const {
518 void firstOut(Edge& edge, const Node& node) const {
519 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
520 edge.id = (node.id >> 1) * _bNodeNum;
522 void nextOut(Edge& edge) const {
524 if (edge.id % _bNodeNum == 0) edge.id = -1;
527 void firstIn(Edge& edge, const Node& node) const {
528 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
529 edge.id = (node.id >> 1);
531 void nextIn(Edge& edge) const {
532 edge.id += _bNodeNum;
533 if (edge.id >= _edgeNum) edge.id = -1;
536 static int id(const Node& node) {
539 static Node nodeFromId(int id) {
542 int maxNodeId() const {
543 return _aNodeNum > _bNodeNum ?
544 _aNodeNum * 2 - 2 : _bNodeNum * 2 - 1;
547 static int id(const Edge& edge) {
550 static Edge edgeFromId(int id) {
553 int maxEdgeId() const {
557 static int aNodeId(const Node& node) {
560 static Node fromANodeId(int id, Node) {
561 return Node(id << 1);
563 int maxANodeId() const {
567 static int bNodeId(const Node& node) {
570 static Node fromBNodeId(int id) {
571 return Node((id << 1) + 1);
573 int maxBNodeId() const {
577 Node aNode(const Edge& edge) const {
578 return Node((edge.id / _bNodeNum) << 1);
580 Node bNode(const Edge& edge) const {
581 return Node(((edge.id % _bNodeNum) << 1) + 1);
584 static bool aNode(const Node& node) {
585 return (node.id & 1) == 0;
588 static bool bNode(const Node& node) {
589 return (node.id & 1) == 1;
592 static Node aNode(int index) {
593 return Node(index << 1);
596 static Node bNode(int index) {
597 return Node((index << 1) + 1);
603 typedef BpUGraphExtender< BpUGraphBaseExtender<
604 FullBpUGraphBase> > ExtendedFullBpUGraphBase;
609 /// \brief An undirected full bipartite graph class.
611 /// This is a simple and fast bipartite undirected full graph implementation.
612 /// It is completely static, so you can neither add nor delete either
617 /// \author Balazs Dezso
619 public ExtendedFullBpUGraphBase {
622 typedef ExtendedFullBpUGraphBase Parent;
624 FullBpUGraph(int aNodeNum, int bNodeNum) {
625 Parent::construct(aNodeNum, bNodeNum);
627 /// \brief Resize the graph
629 void resize(int n, int m) {
630 Parent::getNotifier(Edge()).clear();
631 Parent::getNotifier(UEdge()).clear();
632 Parent::getNotifier(Node()).clear();
634 Parent::getNotifier(Node()).build();
635 Parent::getNotifier(UEdge()).build();
636 Parent::getNotifier(Edge()).build();
643 #endif //LEMON_FULL_GRAPH_H