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
24 #include <lemon/bits/graph_extender.h>
26 #include <lemon/bits/invalid.h>
27 #include <lemon/bits/utility.h>
32 ///\brief FullGraph class.
37 /// \brief Base of the FullGrpah.
39 /// Base of the FullGrpah.
45 typedef FullGraphBase Graph;
55 ///Creates a full graph with \c n nodes.
56 void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
58 typedef True NodeNumTag;
59 typedef True EdgeNumTag;
61 /// \brief Returns the node with the given index.
63 /// Returns the node with the given index. Because it is a
64 /// static size graph the node's of the graph can be indiced
65 /// by the range from 0 to \e nodeNum()-1 and the index of
66 /// the node can accessed by the \e index() member.
67 Node operator()(int index) const { return Node(index); }
69 /// \brief Returns the index of the node.
71 /// Returns the index of the node. Because it is a
72 /// static size graph the node's of the graph can be indiced
73 /// by the range from 0 to \e nodeNum()-1 and the index of
74 /// the node can accessed by the \e index() member.
75 int index(const Node& node) const { return node.id; }
78 int nodeNum() const { return _nodeNum; }
80 int edgeNum() const { return _edgeNum; }
86 int maxNodeId() const { return _nodeNum-1; }
91 int maxEdgeId() const { return _edgeNum-1; }
93 Node source(Edge e) const { return e.id % _nodeNum; }
94 Node target(Edge e) const { return e.id / _nodeNum; }
99 /// The ID of a valid Node is a nonnegative integer not greater than
100 /// \ref maxNodeId(). The range of the ID's is not surely continuous
101 /// and the greatest node ID can be actually less then \ref maxNodeId().
103 /// The ID of the \ref INVALID node is -1.
104 ///\return The ID of the node \c v.
106 static int id(Node v) { return v.id; }
109 /// The ID of a valid Edge is a nonnegative integer not greater than
110 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
111 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
113 /// The ID of the \ref INVALID edge is -1.
114 ///\return The ID of the edge \c e.
115 static int id(Edge e) { return e.id; }
117 static Node nodeFromId(int id) { return Node(id);}
119 static Edge edgeFromId(int id) { return Edge(id);}
121 typedef True FindEdgeTag;
123 /// Finds an edge between two nodes.
125 /// Finds an edge from node \c u to node \c v.
127 /// If \c prev is \ref INVALID (this is the default value), then
128 /// It finds the first edge from \c u to \c v. Otherwise it looks for
129 /// the next edge from \c u to \c v after \c prev.
130 /// \return The found edge or INVALID if there is no such an edge.
131 Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
132 return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
137 friend class FullGraphBase;
141 Node(int _id) : id(_id) {}
144 Node (Invalid) : id(-1) {}
145 bool operator==(const Node node) const {return id == node.id;}
146 bool operator!=(const Node node) const {return id != node.id;}
147 bool operator<(const Node node) const {return id < node.id;}
153 friend class FullGraphBase;
156 int id; // _nodeNum * target + source;
158 Edge(int _id) : id(_id) {}
160 Edge(const FullGraphBase& _graph, int source, int target)
161 : id(_graph._nodeNum * target+source) {}
164 Edge (Invalid) { id = -1; }
165 bool operator==(const Edge edge) const {return id == edge.id;}
166 bool operator!=(const Edge edge) const {return id != edge.id;}
167 bool operator<(const Edge edge) const {return id < edge.id;}
170 void first(Node& node) const {
171 node.id = _nodeNum-1;
174 static void next(Node& node) {
178 void first(Edge& edge) const {
179 edge.id = _edgeNum-1;
182 static void next(Edge& edge) {
186 void firstOut(Edge& edge, const Node& node) const {
187 edge.id = _edgeNum + node.id - _nodeNum;
190 void nextOut(Edge& edge) const {
192 if (edge.id < 0) edge.id = -1;
195 void firstIn(Edge& edge, const Node& node) const {
196 edge.id = node.id * _nodeNum;
199 void nextIn(Edge& edge) const {
201 if (edge.id % _nodeNum == 0) edge.id = -1;
206 typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
210 /// \brief A full graph class.
212 /// This is a simple and fast directed full graph implementation.
213 /// It is completely static, so you can neither add nor delete either
215 /// Thus it conforms to
216 /// the \ref concept::Graph "Graph" concept
217 /// \sa concept::Graph.
219 /// \sa FullGraphBase
222 /// \author Alpar Juttner
223 class FullGraph : public ExtendedFullGraphBase {
226 typedef ExtendedFullGraphBase Parent;
228 /// \brief Constructor
229 FullGraph() { construct(0); }
231 /// \brief Constructor
233 FullGraph(int n) { construct(n); }
235 /// \brief Resize the graph
237 /// Resize the graph. The function will fully destroy and build the graph.
238 /// This cause that the maps of the graph will reallocated
239 /// automatically and the previous values will be lost.
241 Parent::getNotifier(Edge()).clear();
242 Parent::getNotifier(Node()).clear();
244 Parent::getNotifier(Node()).build();
245 Parent::getNotifier(Edge()).build();
252 #endif //LEMON_FULL_GRAPH_H