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