full_graph.h

Go to the documentation of this file.
00001 /* -*- C++ -*- 00002 * src/lemon/full_graph.h - Part of LEMON, a generic C++ optimization library 00003 * 00004 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 00005 * (Egervary Combinatorial Optimization Research Group, EGRES). 00006 * 00007 * Permission to use, modify and distribute this software is granted 00008 * provided that this copyright notice appears in all copies. For 00009 * precise terms see the accompanying LICENSE file. 00010 * 00011 * This software is provided "AS IS" with no warranty of any kind, 00012 * express or implied, and with no claim as to its suitability for any 00013 * purpose. 00014 * 00015 */ 00016 00017 #ifndef LEMON_FULL_GRAPH_H 00018 #define LEMON_FULL_GRAPH_H 00019 00023 00024 #include <vector> 00025 #include <climits> 00026 00027 #include <lemon/invalid.h> 00028 00029 #include <lemon/map_registry.h> 00030 #include <lemon/array_map.h> 00031 00032 #include <lemon/map_defines.h> 00033 00034 namespace lemon { 00035 00038 00040 00051 class FullGraph { 00052 int NodeNum; 00053 int EdgeNum; 00054 public: 00055 00056 typedef FullGraph Graph; 00057 00058 class Node; 00059 class Edge; 00060 00061 class NodeIt; 00062 class EdgeIt; 00063 class OutEdgeIt; 00064 class InEdgeIt; 00065 00066 00067 // Create map registries. 00068 CREATE_MAP_REGISTRIES; 00069 // Create node and edge maps. 00070 CREATE_MAPS(ArrayMap); 00071 00072 public: 00073 00075 FullGraph(int n) : NodeNum(n), EdgeNum(NodeNum*NodeNum) { } 00077 FullGraph(const FullGraph &_g) 00078 : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { } 00079 00081 int nodeNum() const { return NodeNum; } 00083 int edgeNum() const { return EdgeNum; } 00084 00086 00089 int maxNodeId() const { return NodeNum-1; } 00091 00094 int maxEdgeId() const { return EdgeNum-1; } 00095 00096 Node tail(Edge e) const { return e.n%NodeNum; } 00097 Node head(Edge e) const { return e.n/NodeNum; } 00098 00099 NodeIt& first(NodeIt& v) const { 00100 v=NodeIt(*this); return v; } 00101 EdgeIt& first(EdgeIt& e) const { 00102 e=EdgeIt(*this); return e; } 00103 OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 00104 e=OutEdgeIt(*this,v); return e; } 00105 InEdgeIt& first(InEdgeIt& e, const Node v) const { 00106 e=InEdgeIt(*this,v); return e; } 00107 00109 00116 static int id(Node v) { return v.n; } 00118 00125 static int id(Edge e) { return e.n; } 00126 00128 00135 Edge findEdge(Node u,Node v, Edge prev = INVALID) 00136 { 00137 return prev.n == -1 ? Edge(*this, u.n, v.n) : INVALID; 00138 } 00139 00140 00141 class Node { 00142 friend class FullGraph; 00143 template <typename T> friend class NodeMap; 00144 00145 friend class Edge; 00146 friend class OutEdgeIt; 00147 friend class InEdgeIt; 00148 friend class SymEdge; 00149 00150 protected: 00151 int n; 00152 friend int FullGraph::id(Node v); 00153 Node(int nn) {n=nn;} 00154 public: 00155 Node() {} 00156 Node (Invalid) { n=-1; } 00157 bool operator==(const Node i) const {return n==i.n;} 00158 bool operator!=(const Node i) const {return n!=i.n;} 00159 bool operator<(const Node i) const {return n<i.n;} 00160 }; 00161 00162 class NodeIt : public Node { 00163 const FullGraph *G; 00164 friend class FullGraph; 00165 public: 00166 NodeIt() : Node() { } 00167 NodeIt(const FullGraph& _G,Node n) : Node(n), G(&_G) { } 00168 NodeIt(Invalid i) : Node(i) { } 00169 NodeIt(const FullGraph& _G) : Node(_G.NodeNum?0:-1), G(&_G) { } 00171 NodeIt& operator++() { n=(n+2)%(G->NodeNum+1)-1;return *this; } 00172 }; 00173 00174 class Edge { 00175 friend class FullGraph; 00176 template <typename T> friend class EdgeMap; 00177 00178 friend class Node; 00179 friend class NodeIt; 00180 protected: 00181 int n; //NodeNum*head+tail; 00182 friend int FullGraph::id(Edge e); 00183 00184 Edge(int nn) : n(nn) {} 00185 Edge(const FullGraph &G, int tail, int head) : n(G.NodeNum*head+tail) {} 00186 public: 00187 Edge() { } 00188 Edge (Invalid) { n=-1; } 00189 bool operator==(const Edge i) const {return n==i.n;} 00190 bool operator!=(const Edge i) const {return n!=i.n;} 00191 bool operator<(const Edge i) const {return n<i.n;} 00194 int &idref() {return n;} 00195 const int &idref() const {return n;} 00196 }; 00197 00198 class EdgeIt : public Edge { 00199 friend class FullGraph; 00200 public: 00201 EdgeIt(const FullGraph& _G) : Edge(_G.EdgeNum-1) { } 00202 EdgeIt(const FullGraph&, Edge e) : Edge(e) { } 00203 EdgeIt (Invalid i) : Edge(i) { } 00204 EdgeIt() : Edge() { } 00205 EdgeIt& operator++() { --n; return *this; } 00206 00209 int &idref() {return n;} 00210 }; 00211 00212 class OutEdgeIt : public Edge { 00213 const FullGraph *G; 00214 friend class FullGraph; 00215 public: 00216 OutEdgeIt() : Edge() { } 00217 OutEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { } 00218 OutEdgeIt (Invalid i) : Edge(i) { } 00219 00220 OutEdgeIt(const FullGraph& _G,const Node v) : Edge(v.n), G(&_G) {} 00221 00222 OutEdgeIt& operator++() 00223 { n+=G->NodeNum; if(n>=G->EdgeNum) n=-1; return *this; } 00224 00225 }; 00226 00227 class InEdgeIt : public Edge { 00228 const FullGraph *G; 00229 friend class FullGraph; 00230 public: 00231 InEdgeIt() : Edge() { } 00232 InEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { } 00233 InEdgeIt (Invalid i) : Edge(i) { } 00234 InEdgeIt(const FullGraph& _G,Node v) : Edge(v.n*_G.NodeNum), G(&_G) {} 00235 InEdgeIt& operator++() 00236 { if(!((++n)%G->NodeNum)) n=-1; return *this; } 00237 }; 00238 00239 }; 00240 00242 00243 } //namespace lemon 00244 00245 00246 00247 00248 #endif //LEMON_FULL_GRAPH_H

Generated on Thu Sep 30 12:18:33 2004 for LEMON by doxygen 1.3.8