The graph_factory branch (@ 1321) has been merged to trunk.
2 * src/lemon/full_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_FULL_GRAPH_H
18 #define LEMON_FULL_GRAPH_H
21 #include <lemon/idmappable_graph_extender.h>
23 #include <lemon/iterable_graph_extender.h>
25 #include <lemon/alteration_observer_registry.h>
26 #include <lemon/default_map.h>
30 ///\brief FullGraph and SymFullGraph classes.
33 #include <lemon/invalid.h>
37 /// \addtogroup graphs
40 ///A full graph class.
42 ///This is a simple and fast directed full graph implementation.
43 ///It is completely static, so you can neither add nor delete either
45 ///Thus it conforms to
46 ///the \ref skeleton::StaticGraph "StaticGraph" concept
47 ///\sa skeleton::StaticGraph.
48 ///\todo What about loops?
49 ///\todo Don't we need SymEdgeMap?
51 ///\author Alpar Juttner
57 typedef FullGraphBase Graph;
67 ///Creates a full graph with \c n nodes.
68 void construct(int n) { NodeNum = n; EdgeNum = n * n; }
70 // FullGraphBase(const FullGraphBase &_g)
71 // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
74 int nodeNum() const { return NodeNum; }
76 int edgeNum() const { return EdgeNum; }
82 int maxNodeId() const { return NodeNum-1; }
87 int maxEdgeId() const { return EdgeNum-1; }
89 Node tail(Edge e) const { return e.id % NodeNum; }
90 Node head(Edge e) const { return e.id / NodeNum; }
95 /// The ID of a valid Node is a nonnegative integer not greater than
96 /// \ref maxNodeId(). The range of the ID's is not surely continuous
97 /// and the greatest node ID can be actually less then \ref maxNodeId().
99 /// The ID of the \ref INVALID node is -1.
100 ///\return The ID of the node \c v.
102 static int id(Node v) { return v.id; }
105 /// The ID of a valid Edge is a nonnegative integer not greater than
106 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
107 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
109 /// The ID of the \ref INVALID edge is -1.
110 ///\return The ID of the edge \c e.
111 static int id(Edge e) { return e.id; }
113 /// Finds an edge between two nodes.
115 /// Finds an edge from node \c u to node \c v.
117 /// If \c prev is \ref INVALID (this is the default value), then
118 /// It finds the first edge from \c u to \c v. Otherwise it looks for
119 /// the next edge from \c u to \c v after \c prev.
120 /// \return The found edge or INVALID if there is no such an edge.
121 Edge findEdge(Node u,Node v, Edge prev = INVALID)
123 return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
128 friend class FullGraphBase;
132 Node(int _id) { id = _id;}
135 Node (Invalid) { id = -1; }
136 bool operator==(const Node node) const {return id == node.id;}
137 bool operator!=(const Node node) const {return id != node.id;}
138 bool operator<(const Node node) const {return id < node.id;}
144 friend class FullGraphBase;
147 int id; // NodeNum * head + tail;
149 Edge(int _id) : id(_id) {}
151 Edge(const FullGraphBase& _graph, int tail, int head)
152 : id(_graph.NodeNum * head+tail) {}
155 Edge (Invalid) { id = -1; }
156 bool operator==(const Edge edge) const {return id == edge.id;}
157 bool operator!=(const Edge edge) const {return id != edge.id;}
158 bool operator<(const Edge edge) const {return id < edge.id;}
161 void first(Node& node) const {
165 static void next(Node& node) {
169 void first(Edge& edge) const {
173 static void next(Edge& edge) {
177 void firstOut(Edge& edge, const Node& node) const {
178 edge.id = EdgeNum + node.id - NodeNum;
181 void nextOut(Edge& edge) const {
183 if (edge.id < 0) edge.id = -1;
186 void firstIn(Edge& edge, const Node& node) const {
187 edge.id = node.id * NodeNum;
190 void nextIn(Edge& edge) const {
192 if (edge.id % NodeNum == 0) edge.id = -1;
198 typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase;
199 typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
200 typedef IdMappableGraphExtender<IterableFullGraphBase> IdMappableFullGraphBase;
201 typedef DefaultMappableGraphExtender<IdMappableFullGraphBase> MappableFullGraphBase;
203 class FullGraph : public MappableFullGraphBase {
206 FullGraph(int n) { construct(n); }
210 int countNodes<FullGraph>(const FullGraph& graph) {
211 return graph.nodeNum();
215 int countEdges<FullGraph>(const FullGraph& graph) {
216 return graph.edgeNum();
226 #endif //LEMON_FULL_GRAPH_H