Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

full_graph.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * lemon/full_graph.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Research Group on Combinatorial Optimization, 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 
00020 #include <cmath>
00021 
00022 
00023 #include <lemon/bits/iterable_graph_extender.h>
00024 #include <lemon/bits/alteration_notifier.h>
00025 #include <lemon/bits/default_map.h>
00026 
00027 #include <lemon/bits/undir_graph_extender.h>
00028 
00029 #include <lemon/invalid.h>
00030 #include <lemon/utility.h>
00031 
00032 
00036 
00037 
00038 namespace lemon {
00039 
00040   class FullGraphBase {
00041     int _nodeNum;
00042     int _edgeNum;
00043   public:
00044 
00045     typedef FullGraphBase Graph;
00046 
00047     class Node;
00048     class Edge;
00049 
00050   public:
00051 
00052     FullGraphBase() {}
00053 
00054 
00056     void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
00058     //    FullGraphBase(const FullGraphBase &_g)
00059     //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
00060     
00061     typedef True NodeNumTag;
00062     typedef True EdgeNumTag;
00063 
00065     int nodeNum() const { return _nodeNum; }
00067     int edgeNum() const { return _edgeNum; }
00068 
00070     
00073     int maxId(Node = INVALID) const { return _nodeNum-1; }
00075     
00078     int maxId(Edge = INVALID) const { return _edgeNum-1; }
00079 
00080     Node source(Edge e) const { return e.id % _nodeNum; }
00081     Node target(Edge e) const { return e.id / _nodeNum; }
00082 
00083 
00085     
00092 
00093     static int id(Node v) { return v.id; }
00095     
00102     static int id(Edge e) { return e.id; }
00103 
00104     static Node fromId(int id, Node) { return Node(id);}
00105     
00106     static Edge fromId(int id, Edge) { return Edge(id);}
00107 
00108     typedef True FindEdgeTag;
00109 
00111     
00118     Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
00119       return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
00120     }
00121     
00122       
00123     class Node {
00124       friend class FullGraphBase;
00125 
00126     protected:
00127       int id;
00128       Node(int _id) : id(_id) {}
00129     public:
00130       Node() {}
00131       Node (Invalid) : id(-1) {}
00132       bool operator==(const Node node) const {return id == node.id;}
00133       bool operator!=(const Node node) const {return id != node.id;}
00134       bool operator<(const Node node) const {return id < node.id;}
00135     };
00136     
00137 
00138 
00139     class Edge {
00140       friend class FullGraphBase;
00141       
00142     protected:
00143       int id;  // _nodeNum * target + source;
00144 
00145       Edge(int _id) : id(_id) {}
00146 
00147       Edge(const FullGraphBase& _graph, int source, int target) 
00148         : id(_graph._nodeNum * target+source) {}
00149     public:
00150       Edge() { }
00151       Edge (Invalid) { id = -1; }
00152       bool operator==(const Edge edge) const {return id == edge.id;}
00153       bool operator!=(const Edge edge) const {return id != edge.id;}
00154       bool operator<(const Edge edge) const {return id < edge.id;}
00155     };
00156 
00157     void first(Node& node) const {
00158       node.id = _nodeNum-1;
00159     }
00160 
00161     static void next(Node& node) {
00162       --node.id;
00163     }
00164 
00165     void first(Edge& edge) const {
00166       edge.id = _edgeNum-1;
00167     }
00168 
00169     static void next(Edge& edge) {
00170       --edge.id;
00171     }
00172 
00173     void firstOut(Edge& edge, const Node& node) const {
00174       edge.id = _edgeNum + node.id - _nodeNum;
00175     }
00176 
00177     void nextOut(Edge& edge) const {
00178       edge.id -= _nodeNum;
00179       if (edge.id < 0) edge.id = -1;
00180     }
00181 
00182     void firstIn(Edge& edge, const Node& node) const {
00183       edge.id = node.id * _nodeNum;
00184     }
00185     
00186     void nextIn(Edge& edge) const {
00187       ++edge.id;
00188       if (edge.id % _nodeNum == 0) edge.id = -1;
00189     }
00190 
00191   };
00192 
00193 
00194   typedef AlterableGraphExtender<FullGraphBase> 
00195   AlterableFullGraphBase;
00196   typedef IterableGraphExtender<AlterableFullGraphBase> 
00197   IterableFullGraphBase;
00198   typedef DefaultMappableGraphExtender<IterableFullGraphBase> 
00199   MappableFullGraphBase;
00200 
00213   class FullGraph : public MappableFullGraphBase {
00214   public:
00215 
00216     FullGraph(int n) { construct(n); }
00217   };
00218 
00220 
00221   class UndirFullGraphBase {
00222     int _nodeNum;
00223     int _edgeNum;
00224   public:
00225 
00226     typedef UndirFullGraphBase Graph;
00227 
00228     class Node;
00229     class Edge;
00230 
00231   public:
00232 
00233     UndirFullGraphBase() {}
00234 
00235 
00237     void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
00239     //    FullGraphBase(const FullGraphBase &_g)
00240     //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
00241     
00242     typedef True NodeNumTag;
00243     typedef True EdgeNumTag;
00244 
00246     int nodeNum() const { return _nodeNum; }
00248     int edgeNum() const { return _edgeNum; }
00249 
00251     
00254     int maxId(Node = INVALID) const { return _nodeNum-1; }
00256     
00259     int maxId(Edge = INVALID) const { return _edgeNum-1; }
00260 
00261     Node source(Edge e) const { 
00263       return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
00264     }
00265 
00266     Node target(Edge e) const { 
00267       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
00268       return Node(e.id - (source) * (source - 1) / 2);
00269     }
00270 
00271 
00273     
00280 
00281     static int id(Node v) { return v.id; }
00283     
00290     static int id(Edge e) { return e.id; }
00291 
00293     
00300     Edge findEdge(Node u,Node v, Edge prev = INVALID) 
00301     {
00302       return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
00303     }
00304     
00305       
00306     class Node {
00307       friend class UndirFullGraphBase;
00308 
00309     protected:
00310       int id;
00311       Node(int _id) { id = _id;}
00312     public:
00313       Node() {}
00314       Node (Invalid) { id = -1; }
00315       bool operator==(const Node node) const {return id == node.id;}
00316       bool operator!=(const Node node) const {return id != node.id;}
00317       bool operator<(const Node node) const {return id < node.id;}
00318     };
00319     
00320 
00321 
00322     class Edge {
00323       friend class UndirFullGraphBase;
00324       
00325     protected:
00326       int id;  // _nodeNum * target + source;
00327 
00328       Edge(int _id) : id(_id) {}
00329 
00330       Edge(const UndirFullGraphBase& _graph, int source, int target) 
00331         : id(_graph._nodeNum * target+source) {}
00332     public:
00333       Edge() { }
00334       Edge (Invalid) { id = -1; }
00335       bool operator==(const Edge edge) const {return id == edge.id;}
00336       bool operator!=(const Edge edge) const {return id != edge.id;}
00337       bool operator<(const Edge edge) const {return id < edge.id;}
00338     };
00339 
00340     void first(Node& node) const {
00341       node.id = _nodeNum-1;
00342     }
00343 
00344     static void next(Node& node) {
00345       --node.id;
00346     }
00347 
00348     void first(Edge& edge) const {
00349       edge.id = _edgeNum-1;
00350     }
00351 
00352     static void next(Edge& edge) {
00353       --edge.id;
00354     }
00355 
00356     void firstOut(Edge& edge, const Node& node) const {      
00357       edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
00358     }
00359 
00361     void nextOut(Edge& e) const {
00362       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
00363       int target = e.id - (source) * (source - 1) / 2; 
00364       ++target;
00365       e.id = target < source ? source * (source - 1) / 2 + target : -1;
00366     }
00367 
00368     void firstIn(Edge& edge, const Node& node) const {
00369       edge.id = node.id * (node.id + 1) / 2 - 1;
00370     }
00371     
00372     void nextIn(Edge& e) const {
00373       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
00374       int target = e.id - (source) * (source - 1) / 2; ++target;
00375       ++source;
00376       e.id = source < _nodeNum ? source * (source - 1) / 2 + target : -1;
00377     }
00378 
00379   };
00380 
00381   typedef UndirGraphExtender<UndirFullGraphBase>
00382   UndirUndirFullGraphBase;
00383   typedef AlterableUndirGraphExtender<UndirUndirFullGraphBase> 
00384   AlterableUndirFullGraphBase;
00385   typedef IterableUndirGraphExtender<AlterableUndirFullGraphBase> 
00386   IterableUndirFullGraphBase;
00387   typedef MappableUndirGraphExtender<IterableUndirFullGraphBase> 
00388   MappableUndirFullGraphBase;
00389 
00405   class UndirFullGraph : public MappableUndirFullGraphBase {
00406   public:
00407     UndirFullGraph(int n) { construct(n); }
00408   };
00409 
00410 } //namespace lemon
00411 
00412 
00413 #endif //LEMON_FULL_GRAPH_H

Generated on Sat Aug 27 14:14:51 2005 for LEMON by  doxygen 1.4.4