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