[906] | 1 | /* -*- C++ -*- |
---|
[921] | 2 | * src/lemon/smart_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 | */ |
---|
[105] | 16 | |
---|
[921] | 17 | #ifndef LEMON_SMART_GRAPH_H |
---|
| 18 | #define LEMON_SMART_GRAPH_H |
---|
[104] | 19 | |
---|
[491] | 20 | ///\ingroup graphs |
---|
[242] | 21 | ///\file |
---|
| 22 | ///\brief SmartGraph and SymSmartGraph classes. |
---|
| 23 | |
---|
[104] | 24 | #include <vector> |
---|
| 25 | |
---|
[921] | 26 | #include <lemon/invalid.h> |
---|
[157] | 27 | |
---|
[946] | 28 | #include <lemon/erasable_graph_extender.h> |
---|
| 29 | #include <lemon/clearable_graph_extender.h> |
---|
| 30 | #include <lemon/extendable_graph_extender.h> |
---|
[937] | 31 | |
---|
[946] | 32 | #include <lemon/idmappable_graph_extender.h> |
---|
[919] | 33 | |
---|
[946] | 34 | #include <lemon/iterable_graph_extender.h> |
---|
[782] | 35 | |
---|
[946] | 36 | #include <lemon/alteration_observer_registry.h> |
---|
| 37 | #include <lemon/default_map.h> |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | #include <lemon/graph_utils.h> |
---|
| 41 | |
---|
[782] | 42 | |
---|
[921] | 43 | namespace lemon { |
---|
[104] | 44 | |
---|
[946] | 45 | /// \addtogroup graphs |
---|
| 46 | /// @{ |
---|
[185] | 47 | |
---|
[969] | 48 | ///Base of SmartGraph |
---|
| 49 | |
---|
| 50 | ///Base of SmartGraph |
---|
| 51 | /// |
---|
[946] | 52 | class SmartGraphBase { |
---|
[104] | 53 | |
---|
| 54 | struct NodeT |
---|
| 55 | { |
---|
| 56 | int first_in,first_out; |
---|
[157] | 57 | NodeT() : first_in(-1), first_out(-1) {} |
---|
[104] | 58 | }; |
---|
| 59 | struct EdgeT |
---|
| 60 | { |
---|
| 61 | int head, tail, next_in, next_out; |
---|
| 62 | //FIXME: is this necessary? |
---|
[157] | 63 | EdgeT() : next_in(-1), next_out(-1) {} |
---|
[104] | 64 | }; |
---|
| 65 | |
---|
| 66 | std::vector<NodeT> nodes; |
---|
[129] | 67 | |
---|
[104] | 68 | std::vector<EdgeT> edges; |
---|
| 69 | |
---|
[185] | 70 | |
---|
[104] | 71 | public: |
---|
[782] | 72 | |
---|
[946] | 73 | typedef SmartGraphBase Graph; |
---|
[104] | 74 | |
---|
[164] | 75 | class Node; |
---|
| 76 | class Edge; |
---|
[108] | 77 | |
---|
[104] | 78 | |
---|
| 79 | public: |
---|
| 80 | |
---|
[946] | 81 | SmartGraphBase() : nodes(), edges() { } |
---|
| 82 | SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { } |
---|
[104] | 83 | |
---|
[813] | 84 | ///Number of nodes. |
---|
| 85 | int nodeNum() const { return nodes.size(); } |
---|
| 86 | ///Number of edges. |
---|
| 87 | int edgeNum() const { return edges.size(); } |
---|
[104] | 88 | |
---|
[813] | 89 | /// Maximum node ID. |
---|
| 90 | |
---|
| 91 | /// Maximum node ID. |
---|
| 92 | ///\sa id(Node) |
---|
| 93 | int maxNodeId() const { return nodes.size()-1; } |
---|
| 94 | /// Maximum edge ID. |
---|
| 95 | |
---|
| 96 | /// Maximum edge ID. |
---|
| 97 | ///\sa id(Edge) |
---|
| 98 | int maxEdgeId() const { return edges.size()-1; } |
---|
[108] | 99 | |
---|
[164] | 100 | Node tail(Edge e) const { return edges[e.n].tail; } |
---|
| 101 | Node head(Edge e) const { return edges[e.n].head; } |
---|
[104] | 102 | |
---|
[813] | 103 | /// Node ID. |
---|
| 104 | |
---|
| 105 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 106 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 107 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 108 | /// |
---|
| 109 | /// The ID of the \ref INVALID node is -1. |
---|
| 110 | ///\return The ID of the node \c v. |
---|
[713] | 111 | static int id(Node v) { return v.n; } |
---|
[813] | 112 | /// Edge ID. |
---|
| 113 | |
---|
| 114 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 115 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 116 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 117 | /// |
---|
| 118 | /// The ID of the \ref INVALID edge is -1. |
---|
| 119 | ///\return The ID of the edge \c e. |
---|
[713] | 120 | static int id(Edge e) { return e.n; } |
---|
[104] | 121 | |
---|
[164] | 122 | Node addNode() { |
---|
| 123 | Node n; n.n=nodes.size(); |
---|
[104] | 124 | nodes.push_back(NodeT()); //FIXME: Hmmm... |
---|
| 125 | return n; |
---|
| 126 | } |
---|
[108] | 127 | |
---|
[164] | 128 | Edge addEdge(Node u, Node v) { |
---|
| 129 | Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm... |
---|
[104] | 130 | edges[e.n].tail=u.n; edges[e.n].head=v.n; |
---|
| 131 | edges[e.n].next_out=nodes[u.n].first_out; |
---|
| 132 | edges[e.n].next_in=nodes[v.n].first_in; |
---|
| 133 | nodes[u.n].first_out=nodes[v.n].first_in=e.n; |
---|
[108] | 134 | |
---|
[104] | 135 | return e; |
---|
| 136 | } |
---|
| 137 | |
---|
[782] | 138 | void clear() { |
---|
| 139 | edges.clear(); |
---|
| 140 | nodes.clear(); |
---|
| 141 | } |
---|
[104] | 142 | |
---|
[946] | 143 | |
---|
[164] | 144 | class Node { |
---|
[946] | 145 | friend class SmartGraphBase; |
---|
[104] | 146 | |
---|
| 147 | protected: |
---|
| 148 | int n; |
---|
[164] | 149 | Node(int nn) {n=nn;} |
---|
[104] | 150 | public: |
---|
[164] | 151 | Node() {} |
---|
[503] | 152 | Node (Invalid) { n=-1; } |
---|
[164] | 153 | bool operator==(const Node i) const {return n==i.n;} |
---|
| 154 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
| 155 | bool operator<(const Node i) const {return n<i.n;} |
---|
[104] | 156 | }; |
---|
| 157 | |
---|
| 158 | |
---|
[164] | 159 | class Edge { |
---|
[946] | 160 | friend class SmartGraphBase; |
---|
[185] | 161 | |
---|
[104] | 162 | protected: |
---|
| 163 | int n; |
---|
[905] | 164 | Edge(int nn) {n=nn;} |
---|
[706] | 165 | public: |
---|
[164] | 166 | Edge() { } |
---|
[174] | 167 | Edge (Invalid) { n=-1; } |
---|
[164] | 168 | bool operator==(const Edge i) const {return n==i.n;} |
---|
| 169 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
| 170 | bool operator<(const Edge i) const {return n<i.n;} |
---|
[946] | 171 | }; |
---|
[905] | 172 | |
---|
[946] | 173 | void first(Node& node) const { |
---|
| 174 | node.n = nodes.size() - 1; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | static void next(Node& node) { |
---|
| 178 | --node.n; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void first(Edge& edge) const { |
---|
| 182 | edge.n = edges.size() - 1; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | static void next(Edge& edge) { |
---|
| 186 | --edge.n; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | void firstOut(Edge& edge, const Node& node) const { |
---|
| 190 | edge.n = nodes[node.n].first_out; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | void nextOut(Edge& edge) const { |
---|
| 194 | edge.n = edges[edge.n].next_out; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | void firstIn(Edge& edge, const Node& node) const { |
---|
| 198 | edge.n = nodes[node.n].first_in; |
---|
| 199 | } |
---|
[104] | 200 | |
---|
[946] | 201 | void nextIn(Edge& edge) const { |
---|
| 202 | edge.n = edges[edge.n].next_in; |
---|
| 203 | } |
---|
[105] | 204 | |
---|
[969] | 205 | Edge _findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 206 | { |
---|
| 207 | int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out; |
---|
| 208 | while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out; |
---|
| 209 | prev.n=e; |
---|
| 210 | return prev; |
---|
| 211 | } |
---|
[104] | 212 | }; |
---|
[185] | 213 | |
---|
[946] | 214 | typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase; |
---|
| 215 | typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase; |
---|
| 216 | typedef IdMappableGraphExtender<IterableSmartGraphBase> IdMappableSmartGraphBase; |
---|
| 217 | typedef DefaultMappableGraphExtender<IdMappableSmartGraphBase> MappableSmartGraphBase; |
---|
| 218 | typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase; |
---|
| 219 | typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase; |
---|
[937] | 220 | |
---|
[950] | 221 | ///A smart graph class. |
---|
[937] | 222 | |
---|
[950] | 223 | ///This is a simple and fast graph implementation. |
---|
| 224 | ///It is also quite memory efficient, but at the price |
---|
| 225 | ///that <b> it does not support node and edge deletion</b>. |
---|
| 226 | ///It conforms to |
---|
[959] | 227 | ///the \ref concept::ExtendableGraph "ExtendableGraph" concept. |
---|
| 228 | ///\sa concept::ExtendableGraph. |
---|
[950] | 229 | /// |
---|
| 230 | ///\todo Some member functions could be \c static. |
---|
| 231 | /// |
---|
| 232 | ///\todo A possibly useful functionality: a function saveState() |
---|
| 233 | ///(or snapshot() ) would |
---|
| 234 | ///give back a data sturcture X and then the function restoreState(X) |
---|
| 235 | ///(or rollBack() ) |
---|
| 236 | ///would remove the nodes and edges added after the call of saveState(). |
---|
| 237 | ///Of course it should be used as a stack. (Maybe X is not necessary.) |
---|
| 238 | /// |
---|
| 239 | ///\author Alpar Juttner |
---|
[969] | 240 | class SmartGraph :public ClearableSmartGraphBase { |
---|
| 241 | public: |
---|
| 242 | /// Finds an edge between two nodes. |
---|
| 243 | |
---|
| 244 | /// Finds an edge from node \c u to node \c v. |
---|
| 245 | /// |
---|
| 246 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 247 | /// it finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 248 | /// the next edge from \c u to \c v after \c prev. |
---|
| 249 | /// \return The found edge or \ref INVALID if there is no such an edge. |
---|
| 250 | /// |
---|
| 251 | /// Thus you can iterate through each edge from \c u to \c v as it follows. |
---|
| 252 | /// \code |
---|
| 253 | /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) { |
---|
| 254 | /// ... |
---|
| 255 | /// } |
---|
| 256 | /// \endcode |
---|
| 257 | /// \todo Possibly it should be a global function. |
---|
| 258 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 259 | { |
---|
| 260 | return _findEdge(u,v,prev); |
---|
| 261 | } |
---|
| 262 | }; |
---|
[950] | 263 | |
---|
[946] | 264 | template <> |
---|
| 265 | int countNodes<SmartGraph>(const SmartGraph& graph) { |
---|
| 266 | return graph.nodeNum(); |
---|
| 267 | } |
---|
[937] | 268 | |
---|
[946] | 269 | template <> |
---|
| 270 | int countEdges<SmartGraph>(const SmartGraph& graph) { |
---|
| 271 | return graph.edgeNum(); |
---|
| 272 | } |
---|
[937] | 273 | |
---|
[407] | 274 | /// @} |
---|
[921] | 275 | } //namespace lemon |
---|
[104] | 276 | |
---|
[157] | 277 | |
---|
| 278 | |
---|
| 279 | |
---|
[921] | 280 | #endif //LEMON_SMART_GRAPH_H |
---|