[906] | 1 | /* -*- C++ -*- |
---|
[921] | 2 | * src/lemon/full_graph.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 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. |
---|
| 10 | * |
---|
| 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 |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
[591] | 16 | |
---|
[921] | 17 | #ifndef LEMON_FULL_GRAPH_H |
---|
| 18 | #define LEMON_FULL_GRAPH_H |
---|
[591] | 19 | |
---|
[946] | 20 | |
---|
| 21 | #include <lemon/idmappable_graph_extender.h> |
---|
| 22 | |
---|
| 23 | #include <lemon/iterable_graph_extender.h> |
---|
| 24 | |
---|
| 25 | #include <lemon/alteration_observer_registry.h> |
---|
| 26 | #include <lemon/default_map.h> |
---|
| 27 | |
---|
[591] | 28 | ///\ingroup graphs |
---|
| 29 | ///\file |
---|
| 30 | ///\brief FullGraph and SymFullGraph classes. |
---|
| 31 | |
---|
| 32 | |
---|
[921] | 33 | #include <lemon/invalid.h> |
---|
[591] | 34 | |
---|
[921] | 35 | namespace lemon { |
---|
[591] | 36 | |
---|
| 37 | /// \addtogroup graphs |
---|
| 38 | /// @{ |
---|
| 39 | |
---|
[946] | 40 | class FullGraphBase { |
---|
[591] | 41 | int NodeNum; |
---|
| 42 | int EdgeNum; |
---|
| 43 | public: |
---|
[782] | 44 | |
---|
[946] | 45 | typedef FullGraphBase Graph; |
---|
[591] | 46 | |
---|
| 47 | class Node; |
---|
| 48 | class Edge; |
---|
[782] | 49 | |
---|
[591] | 50 | public: |
---|
| 51 | |
---|
[946] | 52 | FullGraphBase() {} |
---|
| 53 | |
---|
| 54 | |
---|
[591] | 55 | ///Creates a full graph with \c n nodes. |
---|
[946] | 56 | void construct(int n) { NodeNum = n; EdgeNum = n * n; } |
---|
[591] | 57 | /// |
---|
[946] | 58 | // FullGraphBase(const FullGraphBase &_g) |
---|
| 59 | // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { } |
---|
[591] | 60 | |
---|
[813] | 61 | ///Number of nodes. |
---|
| 62 | int nodeNum() const { return NodeNum; } |
---|
| 63 | ///Number of edges. |
---|
| 64 | int edgeNum() const { return EdgeNum; } |
---|
[591] | 65 | |
---|
[813] | 66 | /// Maximum node ID. |
---|
| 67 | |
---|
| 68 | /// Maximum node ID. |
---|
| 69 | ///\sa id(Node) |
---|
| 70 | int maxNodeId() const { return NodeNum-1; } |
---|
| 71 | /// Maximum edge ID. |
---|
| 72 | |
---|
| 73 | /// Maximum edge ID. |
---|
| 74 | ///\sa id(Edge) |
---|
| 75 | int maxEdgeId() const { return EdgeNum-1; } |
---|
[591] | 76 | |
---|
[946] | 77 | Node tail(Edge e) const { return e.id % NodeNum; } |
---|
| 78 | Node head(Edge e) const { return e.id / NodeNum; } |
---|
[591] | 79 | |
---|
| 80 | |
---|
[813] | 81 | /// Node ID. |
---|
| 82 | |
---|
| 83 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 84 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 85 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 86 | /// |
---|
| 87 | /// The ID of the \ref INVALID node is -1. |
---|
| 88 | ///\return The ID of the node \c v. |
---|
[946] | 89 | |
---|
| 90 | static int id(Node v) { return v.id; } |
---|
[813] | 91 | /// Edge ID. |
---|
| 92 | |
---|
| 93 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 94 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 95 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 96 | /// |
---|
| 97 | /// The ID of the \ref INVALID edge is -1. |
---|
| 98 | ///\return The ID of the edge \c e. |
---|
[946] | 99 | static int id(Edge e) { return e.id; } |
---|
[591] | 100 | |
---|
[774] | 101 | /// Finds an edge between two nodes. |
---|
| 102 | |
---|
| 103 | /// Finds an edge from node \c u to node \c v. |
---|
| 104 | /// |
---|
| 105 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 106 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 107 | /// the next edge from \c u to \c v after \c prev. |
---|
| 108 | /// \return The found edge or INVALID if there is no such an edge. |
---|
| 109 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 110 | { |
---|
[946] | 111 | return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID; |
---|
[774] | 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
[591] | 115 | class Node { |
---|
[946] | 116 | friend class FullGraphBase; |
---|
[591] | 117 | |
---|
| 118 | protected: |
---|
[946] | 119 | int id; |
---|
| 120 | Node(int _id) { id = _id;} |
---|
[591] | 121 | public: |
---|
| 122 | Node() {} |
---|
[946] | 123 | Node (Invalid) { id = -1; } |
---|
| 124 | bool operator==(const Node node) const {return id == node.id;} |
---|
| 125 | bool operator!=(const Node node) const {return id != node.id;} |
---|
| 126 | bool operator<(const Node node) const {return id < node.id;} |
---|
[591] | 127 | }; |
---|
| 128 | |
---|
[946] | 129 | |
---|
| 130 | |
---|
| 131 | class Edge { |
---|
| 132 | friend class FullGraphBase; |
---|
| 133 | |
---|
| 134 | protected: |
---|
| 135 | int id; // NodeNum * head + tail; |
---|
| 136 | |
---|
| 137 | Edge(int _id) : id(_id) {} |
---|
| 138 | |
---|
| 139 | Edge(const FullGraphBase& _graph, int tail, int head) |
---|
| 140 | : id(_graph.NodeNum * head+tail) {} |
---|
[591] | 141 | public: |
---|
[946] | 142 | Edge() { } |
---|
| 143 | Edge (Invalid) { id = -1; } |
---|
| 144 | bool operator==(const Edge edge) const {return id == edge.id;} |
---|
| 145 | bool operator!=(const Edge edge) const {return id != edge.id;} |
---|
| 146 | bool operator<(const Edge edge) const {return id < edge.id;} |
---|
[591] | 147 | }; |
---|
| 148 | |
---|
[946] | 149 | void first(Node& node) const { |
---|
| 150 | node.id = NodeNum-1; |
---|
| 151 | } |
---|
[591] | 152 | |
---|
[946] | 153 | static void next(Node& node) { |
---|
| 154 | --node.id; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void first(Edge& edge) const { |
---|
| 158 | edge.id = EdgeNum-1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | static void next(Edge& edge) { |
---|
| 162 | --edge.id; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void firstOut(Edge& edge, const Node& node) const { |
---|
| 166 | edge.id = EdgeNum + node.id - NodeNum; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void nextOut(Edge& edge) const { |
---|
| 170 | edge.id -= NodeNum; |
---|
| 171 | if (edge.id < 0) edge.id = -1; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | void firstIn(Edge& edge, const Node& node) const { |
---|
| 175 | edge.id = node.id * NodeNum; |
---|
| 176 | } |
---|
[591] | 177 | |
---|
[946] | 178 | void nextIn(Edge& edge) const { |
---|
| 179 | ++edge.id; |
---|
| 180 | if (edge.id % NodeNum == 0) edge.id = -1; |
---|
| 181 | } |
---|
[591] | 182 | |
---|
| 183 | }; |
---|
| 184 | |
---|
[946] | 185 | |
---|
| 186 | typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase; |
---|
| 187 | typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase; |
---|
| 188 | typedef IdMappableGraphExtender<IterableFullGraphBase> IdMappableFullGraphBase; |
---|
| 189 | typedef DefaultMappableGraphExtender<IdMappableFullGraphBase> MappableFullGraphBase; |
---|
| 190 | |
---|
[951] | 191 | ///A full graph class. |
---|
| 192 | |
---|
| 193 | ///This is a simple and fast directed full graph implementation. |
---|
| 194 | ///It is completely static, so you can neither add nor delete either |
---|
| 195 | ///edges or nodes. |
---|
| 196 | ///Thus it conforms to |
---|
[959] | 197 | ///the \ref concept::StaticGraph "StaticGraph" concept |
---|
| 198 | ///\sa concept::StaticGraph. |
---|
[951] | 199 | ///\todo What about loops? |
---|
| 200 | ///\todo Don't we need SymEdgeMap? |
---|
| 201 | /// |
---|
| 202 | ///\author Alpar Juttner |
---|
[946] | 203 | class FullGraph : public MappableFullGraphBase { |
---|
| 204 | public: |
---|
| 205 | |
---|
| 206 | FullGraph(int n) { construct(n); } |
---|
| 207 | }; |
---|
| 208 | |
---|
| 209 | template <> |
---|
| 210 | int countNodes<FullGraph>(const FullGraph& graph) { |
---|
| 211 | return graph.nodeNum(); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | template <> |
---|
| 215 | int countEdges<FullGraph>(const FullGraph& graph) { |
---|
| 216 | return graph.edgeNum(); |
---|
| 217 | } |
---|
| 218 | |
---|
[591] | 219 | /// @} |
---|
| 220 | |
---|
[921] | 221 | } //namespace lemon |
---|
[591] | 222 | |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | |
---|
[921] | 226 | #endif //LEMON_FULL_GRAPH_H |
---|