An additional simplier interface for static size graphs.
Node operator()(int) for getting node by index
int index(Node node) for getting index by node
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.
39 /// \brief Base of the FullGrpah.
41 /// Base of the FullGrpah.
47 typedef FullGraphBase Graph;
57 ///Creates a full graph with \c n nodes.
58 void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
60 typedef True NodeNumTag;
61 typedef True EdgeNumTag;
63 /// \brief Returns the node with the given index.
65 /// Returns the node with the given index. Because it is a
66 /// static size graph the node's of the graph can be indiced
67 /// by the range from 0 to \e nodeNum()-1 and the index of
68 /// the node can accessed by the \e index() member.
69 Node operator()(int index) const { return Node(index); }
71 /// \brief Returns the index of the node.
73 /// Returns the index of the node. Because it is a
74 /// static size graph the node's of the graph can be indiced
75 /// by the range from 0 to \e nodeNum()-1 and the index of
76 /// the node can accessed by the \e index() member.
77 int index(const Node& node) const { return node.id; }
80 int nodeNum() const { return _nodeNum; }
82 int edgeNum() const { return _edgeNum; }
88 int maxNodeId() const { return _nodeNum-1; }
93 int maxEdgeId() const { return _edgeNum-1; }
95 Node source(Edge e) const { return e.id % _nodeNum; }
96 Node target(Edge e) const { return e.id / _nodeNum; }
101 /// The ID of a valid Node is a nonnegative integer not greater than
102 /// \ref maxNodeId(). The range of the ID's is not surely continuous
103 /// and the greatest node ID can be actually less then \ref maxNodeId().
105 /// The ID of the \ref INVALID node is -1.
106 ///\return The ID of the node \c v.
108 static int id(Node v) { return v.id; }
111 /// The ID of a valid Edge is a nonnegative integer not greater than
112 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
113 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
115 /// The ID of the \ref INVALID edge is -1.
116 ///\return The ID of the edge \c e.
117 static int id(Edge e) { return e.id; }
119 static Node nodeFromId(int id) { return Node(id);}
121 static Edge edgeFromId(int id) { return Edge(id);}
123 typedef True FindEdgeTag;
125 /// Finds an edge between two nodes.
127 /// Finds an edge from node \c u to node \c v.
129 /// If \c prev is \ref INVALID (this is the default value), then
130 /// It finds the first edge from \c u to \c v. Otherwise it looks for
131 /// the next edge from \c u to \c v after \c prev.
132 /// \return The found edge or INVALID if there is no such an edge.
133 Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
134 return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
139 friend class FullGraphBase;
143 Node(int _id) : id(_id) {}
146 Node (Invalid) : id(-1) {}
147 bool operator==(const Node node) const {return id == node.id;}
148 bool operator!=(const Node node) const {return id != node.id;}
149 bool operator<(const Node node) const {return id < node.id;}
155 friend class FullGraphBase;
158 int id; // _nodeNum * target + source;
160 Edge(int _id) : id(_id) {}
162 Edge(const FullGraphBase& _graph, int source, int target)
163 : id(_graph._nodeNum * target+source) {}
166 Edge (Invalid) { id = -1; }
167 bool operator==(const Edge edge) const {return id == edge.id;}
168 bool operator!=(const Edge edge) const {return id != edge.id;}
169 bool operator<(const Edge edge) const {return id < edge.id;}
172 void first(Node& node) const {
173 node.id = _nodeNum-1;
176 static void next(Node& node) {
180 void first(Edge& edge) const {
181 edge.id = _edgeNum-1;
184 static void next(Edge& edge) {
188 void firstOut(Edge& edge, const Node& node) const {
189 edge.id = _edgeNum + node.id - _nodeNum;
192 void nextOut(Edge& edge) const {
194 if (edge.id < 0) edge.id = -1;
197 void firstIn(Edge& edge, const Node& node) const {
198 edge.id = node.id * _nodeNum;
201 void nextIn(Edge& edge) const {
203 if (edge.id % _nodeNum == 0) edge.id = -1;
208 typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
212 /// \brief A full graph class.
214 /// This is a simple and fast directed full graph implementation.
215 /// It is completely static, so you can neither add nor delete either
217 /// Thus it conforms to
218 /// the \ref concept::StaticGraph "StaticGraph" concept
219 /// \sa concept::StaticGraph.
221 /// \sa FullGraphBase
224 /// \author Alpar Juttner
225 class FullGraph : public ExtendedFullGraphBase {
228 typedef ExtendedFullGraphBase Parent;
230 /// \brief Constructor
232 FullGraph(int n) { construct(n); }
234 /// \brief Resize the graph
236 /// Resize the graph. The function will fully destroy and build the graph.
237 /// This cause that the maps of the graph will reallocated
238 /// automatically and the previous values will be lost.
240 Parent::getNotifier(Edge()).clear();
241 Parent::getNotifier(Node()).clear();
243 Parent::getNotifier(Node()).build();
244 Parent::getNotifier(Edge()).build();
249 /// \brief Base of the FullUGrpah.
251 /// Base of the FullUGrpah.
252 class FullUGraphBase {
257 typedef FullUGraphBase Graph;
267 ///Creates a full graph with \c n nodes.
268 void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
270 /// \brief Returns the node with the given index.
272 /// Returns the node with the given index. Because it is a
273 /// static size graph the node's of the graph can be indiced
274 /// by the range from 0 to \e nodeNum()-1 and the index of
275 /// the node can accessed by the \e index() member.
276 Node operator()(int index) const { return Node(index); }
278 /// \brief Returns the index of the node.
280 /// Returns the index of the node. Because it is a
281 /// static size graph the node's of the graph can be indiced
282 /// by the range from 0 to \e nodeNum()-1 and the index of
283 /// the node can accessed by the \e index() member.
284 int index(const Node& node) const { return node.id; }
286 typedef True NodeNumTag;
287 typedef True EdgeNumTag;
290 int nodeNum() const { return _nodeNum; }
292 int edgeNum() const { return _edgeNum; }
298 int maxNodeId() const { return _nodeNum-1; }
303 int maxEdgeId() const { return _edgeNum-1; }
305 Node source(Edge e) const {
306 /// \todo we may do it faster
307 return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
310 Node target(Edge e) const {
311 int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
312 return Node(e.id - (source) * (source - 1) / 2);
318 /// The ID of a valid Node is a nonnegative integer not greater than
319 /// \ref maxNodeId(). The range of the ID's is not surely continuous
320 /// and the greatest node ID can be actually less then \ref maxNodeId().
322 /// The ID of the \ref INVALID node is -1.
323 /// \return The ID of the node \c v.
325 static int id(Node v) { return v.id; }
329 /// The ID of a valid Edge is a nonnegative integer not greater than
330 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
331 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
333 /// The ID of the \ref INVALID edge is -1.
334 ///\return The ID of the edge \c e.
335 static int id(Edge e) { return e.id; }
337 /// \brief Finds an edge between two nodes.
339 /// Finds an edge from node \c u to node \c v.
341 /// If \c prev is \ref INVALID (this is the default value), then
342 /// It finds the first edge from \c u to \c v. Otherwise it looks for
343 /// the next edge from \c u to \c v after \c prev.
344 /// \return The found edge or INVALID if there is no such an edge.
345 Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
346 if (prev.id != -1 || u.id <= v.id) return Edge(-1);
347 return Edge(u.id * (u.id - 1) / 2 + v.id);
350 typedef True FindEdgeTag;
354 friend class FullUGraphBase;
358 Node(int _id) { id = _id;}
361 Node (Invalid) { id = -1; }
362 bool operator==(const Node node) const {return id == node.id;}
363 bool operator!=(const Node node) const {return id != node.id;}
364 bool operator<(const Node node) const {return id < node.id;}
370 friend class FullUGraphBase;
373 int id; // _nodeNum * target + source;
375 Edge(int _id) : id(_id) {}
379 Edge (Invalid) { id = -1; }
380 bool operator==(const Edge edge) const {return id == edge.id;}
381 bool operator!=(const Edge edge) const {return id != edge.id;}
382 bool operator<(const Edge edge) const {return id < edge.id;}
385 void first(Node& node) const {
386 node.id = _nodeNum - 1;
389 static void next(Node& node) {
393 void first(Edge& edge) const {
394 edge.id = _edgeNum - 1;
397 static void next(Edge& edge) {
401 void firstOut(Edge& edge, const Node& node) const {
404 edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
407 /// \todo with specialized iterators we can make faster iterating
408 void nextOut(Edge& edge) const {
409 int src = source(edge).id;
410 int trg = target(edge).id;
412 edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
415 void firstIn(Edge& edge, const Node& node) const {
416 int src = node.id + 1;
418 edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
421 void nextIn(Edge& edge) const {
422 int src = source(edge).id;
423 int trg = target(edge).id;
425 edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
430 typedef UGraphExtender<UGraphBaseExtender<FullUGraphBase> >
431 ExtendedFullUGraphBase;
435 /// \brief An undirected full graph class.
437 /// This is a simple and fast undirected full graph implementation.
438 /// It is completely static, so you can neither add nor delete either
441 /// The main difference beetween the \e FullGraph and \e FullUGraph class
442 /// is that this class conforms to the undirected graph concept and
443 /// it does not contain the loop edges.
445 /// \sa FullUGraphBase
448 /// \author Balazs Dezso
449 class FullUGraph : public ExtendedFullUGraphBase {
452 typedef ExtendedFullUGraphBase Parent;
454 /// \brief Constructor
455 FullUGraph(int n) { construct(n); }
457 /// \brief Resize the graph
459 /// Resize the graph. The function will fully destroy and build the graph.
460 /// This cause that the maps of the graph will reallocated
461 /// automatically and the previous values will be lost.
463 Parent::getNotifier(Edge()).clear();
464 Parent::getNotifier(UEdge()).clear();
465 Parent::getNotifier(Node()).clear();
467 Parent::getNotifier(Node()).build();
468 Parent::getNotifier(UEdge()).build();
469 Parent::getNotifier(Edge()).build();
474 class FullBpUGraphBase {
484 class NodeSetError : public LogicError {
485 virtual const char* exceptionName() const {
486 return "lemon::FullBpUGraph::NodeSetError";
491 friend class FullBpUGraphBase;
495 Node(int _id) : id(_id) {}
498 Node(Invalid) { id = -1; }
499 bool operator==(const Node i) const {return id==i.id;}
500 bool operator!=(const Node i) const {return id!=i.id;}
501 bool operator<(const Node i) const {return id<i.id;}
505 friend class FullBpUGraphBase;
509 Edge(int _id) { id = _id;}
512 Edge (Invalid) { id = -1; }
513 bool operator==(const Edge i) const {return id==i.id;}
514 bool operator!=(const Edge i) const {return id!=i.id;}
515 bool operator<(const Edge i) const {return id<i.id;}
518 void construct(int aNodeNum, int bNodeNum) {
519 _aNodeNum = aNodeNum;
520 _bNodeNum = bNodeNum;
521 _edgeNum = aNodeNum * bNodeNum;
524 void firstANode(Node& node) const {
525 node.id = 2 * _aNodeNum - 2;
526 if (node.id < 0) node.id = -1;
528 void nextANode(Node& node) const {
530 if (node.id < 0) node.id = -1;
533 void firstBNode(Node& node) const {
534 node.id = 2 * _bNodeNum - 1;
536 void nextBNode(Node& node) const {
540 void first(Node& node) const {
542 node.id = 2 * _aNodeNum - 2;
544 node.id = 2 * _bNodeNum - 1;
547 void next(Node& node) const {
550 node.id = 2 * _bNodeNum - 1;
554 void first(Edge& edge) const {
555 edge.id = _edgeNum - 1;
557 void next(Edge& edge) const {
561 void firstOut(Edge& edge, const Node& node) const {
562 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
563 edge.id = (node.id >> 1) * _bNodeNum;
565 void nextOut(Edge& edge) const {
567 if (edge.id % _bNodeNum == 0) edge.id = -1;
570 void firstIn(Edge& edge, const Node& node) const {
571 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
572 edge.id = (node.id >> 1);
574 void nextIn(Edge& edge) const {
575 edge.id += _bNodeNum;
576 if (edge.id >= _edgeNum) edge.id = -1;
579 static int id(const Node& node) {
582 static Node nodeFromId(int id) {
585 int maxNodeId() const {
586 return _aNodeNum > _bNodeNum ?
587 _aNodeNum * 2 - 2 : _bNodeNum * 2 - 1;
590 static int id(const Edge& edge) {
593 static Edge edgeFromId(int id) {
596 int maxEdgeId() const {
600 static int aNodeId(const Node& node) {
603 static Node fromANodeId(int id, Node) {
604 return Node(id << 1);
606 int maxANodeId() const {
610 static int bNodeId(const Node& node) {
613 static Node fromBNodeId(int id) {
614 return Node((id << 1) + 1);
616 int maxBNodeId() const {
620 Node aNode(const Edge& edge) const {
621 return Node((edge.id / _bNodeNum) << 1);
623 Node bNode(const Edge& edge) const {
624 return Node(((edge.id % _bNodeNum) << 1) + 1);
627 static bool aNode(const Node& node) {
628 return (node.id & 1) == 0;
631 static bool bNode(const Node& node) {
632 return (node.id & 1) == 1;
635 static Node aNode(int index) {
636 return Node(index << 1);
639 static Node bNode(int index) {
640 return Node((index << 1) + 1);
646 typedef BpUGraphExtender< BpUGraphBaseExtender<
647 FullBpUGraphBase> > ExtendedFullBpUGraphBase;
652 /// \brief An undirected full bipartite graph class.
654 /// This is a simple and fast bipartite undirected full graph implementation.
655 /// It is completely static, so you can neither add nor delete either
658 /// \sa FullUGraphBase
661 /// \author Balazs Dezso
663 public ExtendedFullBpUGraphBase {
666 typedef ExtendedFullBpUGraphBase Parent;
668 FullBpUGraph(int aNodeNum, int bNodeNum) {
669 Parent::construct(aNodeNum, bNodeNum);
671 /// \brief Resize the graph
673 void resize(int n, int m) {
674 Parent::getNotifier(Edge()).clear();
675 Parent::getNotifier(UEdge()).clear();
676 Parent::getNotifier(Node()).clear();
678 Parent::getNotifier(Node()).build();
679 Parent::getNotifier(UEdge()).build();
680 Parent::getNotifier(Edge()).build();
687 #endif //LEMON_FULL_GRAPH_H