COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/full_graph.h @ 1979:c2992fd74dad

Last change on this file since 1979:c2992fd74dad was 1979:c2992fd74dad, checked in by Balazs Dezso, 18 years ago

Mergeing extendermerge branch
Changes:

the extender system
resize for static size graph
UGraphExtender => UndirectGraphExtender?

UGraphExtenders with changed meaning

Some UGraphExtender /SubUGraphExtenders, DirectUGraphExtender/
GridGraph? => GridUGraph
radix sort to ansi compatible

File size: 15.9 KB
RevLine 
[906]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
[591]18
[921]19#ifndef LEMON_FULL_GRAPH_H
20#define LEMON_FULL_GRAPH_H
[591]21
[983]22#include <cmath>
23
[946]24
[1791]25#include <lemon/bits/graph_extender.h>
[1566]26
[1979]27
[977]28#include <lemon/invalid.h>
29#include <lemon/utility.h>
30
31
[591]32///\ingroup graphs
33///\file
[1909]34///\brief FullGraph and FullUGraph classes.
[591]35
36
[921]37namespace lemon {
[591]38
[946]39  class FullGraphBase {
[1566]40    int _nodeNum;
41    int _edgeNum;
[591]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.
[1566]55    void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
[591]56    ///
[946]57    //    FullGraphBase(const FullGraphBase &_g)
[1566]58    //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
[591]59   
[977]60    typedef True NodeNumTag;
61    typedef True EdgeNumTag;
62
[813]63    ///Number of nodes.
[1566]64    int nodeNum() const { return _nodeNum; }
[813]65    ///Number of edges.
[1566]66    int edgeNum() const { return _edgeNum; }
[591]67
[813]68    /// Maximum node ID.
69   
70    /// Maximum node ID.
71    ///\sa id(Node)
[1791]72    int maxNodeId() const { return _nodeNum-1; }
[813]73    /// Maximum edge ID.
74   
75    /// Maximum edge ID.
76    ///\sa id(Edge)
[1791]77    int maxEdgeId() const { return _edgeNum-1; }
[591]78
[1566]79    Node source(Edge e) const { return e.id % _nodeNum; }
80    Node target(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
[1791]103    static Node nodeFromId(int id) { return Node(id);}
[1106]104   
[1791]105    static Edge edgeFromId(int id) { return Edge(id);}
[1106]106
[1566]107    typedef True FindEdgeTag;
108
[774]109    /// Finds an edge between two nodes.
110   
111    /// Finds an edge from node \c u to node \c v.
112    ///
113    /// If \c prev is \ref INVALID (this is the default value), then
114    /// It finds the first edge from \c u to \c v. Otherwise it looks for
115    /// the next edge from \c u to \c v after \c prev.
116    /// \return The found edge or INVALID if there is no such an edge.
[1566]117    Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
[946]118      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
[774]119    }
120   
121     
[591]122    class Node {
[946]123      friend class FullGraphBase;
[591]124
125    protected:
[946]126      int id;
[1643]127      Node(int _id) : id(_id) {}
[591]128    public:
129      Node() {}
[1643]130      Node (Invalid) : id(-1) {}
[946]131      bool operator==(const Node node) const {return id == node.id;}
132      bool operator!=(const Node node) const {return id != node.id;}
133      bool operator<(const Node node) const {return id < node.id;}
[591]134    };
135   
[946]136
137
138    class Edge {
139      friend class FullGraphBase;
140     
141    protected:
[1566]142      int id;  // _nodeNum * target + source;
[946]143
144      Edge(int _id) : id(_id) {}
145
[986]146      Edge(const FullGraphBase& _graph, int source, int target)
[1566]147        : id(_graph._nodeNum * target+source) {}
[591]148    public:
[946]149      Edge() { }
150      Edge (Invalid) { id = -1; }
151      bool operator==(const Edge edge) const {return id == edge.id;}
152      bool operator!=(const Edge edge) const {return id != edge.id;}
153      bool operator<(const Edge edge) const {return id < edge.id;}
[591]154    };
155
[946]156    void first(Node& node) const {
[1566]157      node.id = _nodeNum-1;
[946]158    }
[591]159
[946]160    static void next(Node& node) {
161      --node.id;
162    }
163
164    void first(Edge& edge) const {
[1566]165      edge.id = _edgeNum-1;
[946]166    }
167
168    static void next(Edge& edge) {
169      --edge.id;
170    }
171
172    void firstOut(Edge& edge, const Node& node) const {
[1566]173      edge.id = _edgeNum + node.id - _nodeNum;
[946]174    }
175
176    void nextOut(Edge& edge) const {
[1566]177      edge.id -= _nodeNum;
[946]178      if (edge.id < 0) edge.id = -1;
179    }
180
181    void firstIn(Edge& edge, const Node& node) const {
[1566]182      edge.id = node.id * _nodeNum;
[946]183    }
[591]184   
[946]185    void nextIn(Edge& edge) const {
186      ++edge.id;
[1566]187      if (edge.id % _nodeNum == 0) edge.id = -1;
[946]188    }
[591]189
190  };
191
[1979]192  typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase;
[946]193
[1566]194  /// \ingroup graphs
[951]195  ///
[1566]196  /// \brief A full graph class.
197  ///
198  /// This is a simple and fast directed full graph implementation.
199  /// It is completely static, so you can neither add nor delete either
200  /// edges or nodes.
201  /// Thus it conforms to
202  /// the \ref concept::StaticGraph "StaticGraph" concept
203  /// \sa concept::StaticGraph.
204  ///
205  /// \author Alpar Juttner
[1669]206  class FullGraph : public ExtendedFullGraphBase {
[946]207  public:
208
[1979]209    typedef ExtendedFullGraphBase Parent;
210
211    /// \brief Constructor
212    ///
[946]213    FullGraph(int n) { construct(n); }
[1979]214
215    /// \brief Resize the graph
216    ///
217    void resize(int n) {
218      Parent::getNotifier(Edge()).clear();
219      Parent::getNotifier(Node()).clear();
220      construct(n);
221      Parent::getNotifier(Node()).build();
222      Parent::getNotifier(Edge()).build();
223    }
[946]224  };
225
[983]226
[1909]227  class FullUGraphBase {
[1566]228    int _nodeNum;
229    int _edgeNum;
[983]230  public:
231
[1909]232    typedef FullUGraphBase Graph;
[983]233
234    class Node;
235    class Edge;
236
237  public:
238
[1909]239    FullUGraphBase() {}
[983]240
241
242    ///Creates a full graph with \c n nodes.
[1566]243    void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
[983]244    ///
245    //    FullGraphBase(const FullGraphBase &_g)
[1566]246    //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
[983]247   
248    typedef True NodeNumTag;
249    typedef True EdgeNumTag;
250
251    ///Number of nodes.
[1566]252    int nodeNum() const { return _nodeNum; }
[983]253    ///Number of edges.
[1566]254    int edgeNum() const { return _edgeNum; }
[983]255
256    /// Maximum node ID.
257   
258    /// Maximum node ID.
259    ///\sa id(Node)
[1791]260    int maxNodeId() const { return _nodeNum-1; }
[983]261    /// Maximum edge ID.
262   
263    /// Maximum edge ID.
264    ///\sa id(Edge)
[1791]265    int maxEdgeId() const { return _edgeNum-1; }
[983]266
[986]267    Node source(Edge e) const {
[983]268      /// \todo we may do it faster
[1643]269      return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
[983]270    }
271
[986]272    Node target(Edge e) const {
273      int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
[1643]274      return Node(e.id - (source) * (source - 1) / 2);
[983]275    }
276
277
278    /// Node ID.
279   
280    /// The ID of a valid Node is a nonnegative integer not greater than
281    /// \ref maxNodeId(). The range of the ID's is not surely continuous
282    /// and the greatest node ID can be actually less then \ref maxNodeId().
283    ///
284    /// The ID of the \ref INVALID node is -1.
285    ///\return The ID of the node \c v.
286
287    static int id(Node v) { return v.id; }
288    /// Edge ID.
289   
290    /// The ID of a valid Edge is a nonnegative integer not greater than
291    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
292    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
293    ///
294    /// The ID of the \ref INVALID edge is -1.
295    ///\return The ID of the edge \c e.
296    static int id(Edge e) { return e.id; }
297
298    /// Finds an edge between two nodes.
299   
300    /// Finds an edge from node \c u to node \c v.
301    ///
302    /// If \c prev is \ref INVALID (this is the default value), then
303    /// It finds the first edge from \c u to \c v. Otherwise it looks for
304    /// the next edge from \c u to \c v after \c prev.
305    /// \return The found edge or INVALID if there is no such an edge.
[1703]306    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
307      if (prev.id != -1 || u.id <= v.id) return -1;
308      return Edge(u.id * (u.id - 1) / 2 + v.id);
[983]309    }
[1703]310
311    typedef True FindEdgeTag;
[983]312   
313     
314    class Node {
[1909]315      friend class FullUGraphBase;
[983]316
317    protected:
318      int id;
319      Node(int _id) { id = _id;}
320    public:
321      Node() {}
322      Node (Invalid) { id = -1; }
323      bool operator==(const Node node) const {return id == node.id;}
324      bool operator!=(const Node node) const {return id != node.id;}
325      bool operator<(const Node node) const {return id < node.id;}
326    };
327   
328
329
330    class Edge {
[1909]331      friend class FullUGraphBase;
[983]332     
333    protected:
[1566]334      int id;  // _nodeNum * target + source;
[983]335
336      Edge(int _id) : id(_id) {}
337
338    public:
339      Edge() { }
340      Edge (Invalid) { id = -1; }
341      bool operator==(const Edge edge) const {return id == edge.id;}
342      bool operator!=(const Edge edge) const {return id != edge.id;}
343      bool operator<(const Edge edge) const {return id < edge.id;}
344    };
345
346    void first(Node& node) const {
[1703]347      node.id = _nodeNum - 1;
[983]348    }
349
350    static void next(Node& node) {
351      --node.id;
352    }
353
354    void first(Edge& edge) const {
[1703]355      edge.id = _edgeNum - 1;
[983]356    }
357
358    static void next(Edge& edge) {
359      --edge.id;
360    }
361
362    void firstOut(Edge& edge, const Node& node) const {     
[1703]363      int src = node.id;
364      int trg = 0;
365      edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
[983]366    }
367
368    /// \todo with specialized iterators we can make faster iterating
[1703]369    void nextOut(Edge& edge) const {
370      int src = source(edge).id;
371      int trg = target(edge).id;
372      ++trg;
373      edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
[983]374    }
375
376    void firstIn(Edge& edge, const Node& node) const {
[1703]377      int src = node.id + 1;
378      int trg = node.id;
379      edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
[983]380    }
381   
[1703]382    void nextIn(Edge& edge) const {
383      int src = source(edge).id;
384      int trg = target(edge).id;
385      ++src;
386      edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
[983]387    }
388
389  };
390
[1979]391  typedef UGraphExtender<UGraphBaseExtender<FullUGraphBase> >
392  ExtendedFullUGraphBase;
[1555]393
[1566]394  /// \ingroup graphs
395  ///
396  /// \brief An undirected full graph class.
397  ///
[1726]398  /// This is a simple and fast undirected full graph implementation.
[1566]399  /// It is completely static, so you can neither add nor delete either
400  /// edges or nodes.
401  ///
[1909]402  /// The main difference beetween the \e FullGraph and \e FullUGraph class
[1566]403  /// is that this class conforms to the undirected graph concept and
[1726]404  /// it does not contain the loop edges.
[1566]405  ///
406  /// \sa FullGraph
407  ///
408  /// \author Balazs Dezso
[1909]409  class FullUGraph : public ExtendedFullUGraphBase {
[1566]410  public:
[1979]411
412    typedef ExtendedFullUGraphBase Parent;
413
414    /// \brief Constructor
[1909]415    FullUGraph(int n) { construct(n); }
[1979]416
417    /// \brief Resize the graph
418    ///
419    void resize(int n) {
420      Parent::getNotifier(Edge()).clear();
421      Parent::getNotifier(UEdge()).clear();
422      Parent::getNotifier(Node()).clear();
423      construct(n);
424      Parent::getNotifier(Node()).build();
425      Parent::getNotifier(UEdge()).build();
426      Parent::getNotifier(Edge()).build();
427    }
[1566]428  };
[591]429
[1820]430
[1910]431  class FullBpUGraphBase {
[1820]432  protected:
433
[1910]434    int _aNodeNum;
435    int _bNodeNum;
[1820]436
437    int _edgeNum;
438
439  public:
440
441    class NodeSetError : public LogicError {
442      virtual const char* exceptionName() const {
[1910]443        return "lemon::FullBpUGraph::NodeSetError";
[1820]444      }
445    };
446 
447    class Node {
[1910]448      friend class FullBpUGraphBase;
[1820]449    protected:
450      int id;
451
452      Node(int _id) : id(_id) {}
453    public:
454      Node() {}
455      Node(Invalid) { id = -1; }
456      bool operator==(const Node i) const {return id==i.id;}
457      bool operator!=(const Node i) const {return id!=i.id;}
458      bool operator<(const Node i) const {return id<i.id;}
459    };
460
461    class Edge {
[1910]462      friend class FullBpUGraphBase;
[1820]463    protected:
464      int id;
465
466      Edge(int _id) { id = _id;}
467    public:
468      Edge() {}
469      Edge (Invalid) { id = -1; }
470      bool operator==(const Edge i) const {return id==i.id;}
471      bool operator!=(const Edge i) const {return id!=i.id;}
472      bool operator<(const Edge i) const {return id<i.id;}
473    };
474
[1910]475    void construct(int aNodeNum, int bNodeNum) {
476      _aNodeNum = aNodeNum;
477      _bNodeNum = bNodeNum;
478      _edgeNum = aNodeNum * bNodeNum;
[1820]479    }
480
[1910]481    void firstANode(Node& node) const {
482      node.id = 2 * _aNodeNum - 2;
[1820]483      if (node.id < 0) node.id = -1;
484    }
[1910]485    void nextANode(Node& node) const {
[1820]486      node.id -= 2;
487      if (node.id < 0) node.id = -1;
488    }
489
[1910]490    void firstBNode(Node& node) const {
491      node.id = 2 * _bNodeNum - 1;
[1820]492    }
[1910]493    void nextBNode(Node& node) const {
[1820]494      node.id -= 2;
495    }
496
497    void first(Node& node) const {
[1910]498      if (_aNodeNum > 0) {
499        node.id = 2 * _aNodeNum - 2;
[1820]500      } else {
[1910]501        node.id = 2 * _bNodeNum - 1;
[1820]502      }
503    }
504    void next(Node& node) const {
505      node.id -= 2;
506      if (node.id == -2) {
[1910]507        node.id = 2 * _bNodeNum - 1;
[1820]508      }
509    }
510 
511    void first(Edge& edge) const {
512      edge.id = _edgeNum - 1;
513    }
514    void next(Edge& edge) const {
515      --edge.id;
516    }
517
[1910]518    void firstOut(Edge& edge, const Node& node) const {
[1820]519      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
[1910]520      edge.id = (node.id >> 1) * _bNodeNum;
[1820]521    }
[1910]522    void nextOut(Edge& edge) const {
[1820]523      ++(edge.id);
[1910]524      if (edge.id % _bNodeNum == 0) edge.id = -1;
[1820]525    }
526
[1910]527    void firstIn(Edge& edge, const Node& node) const {
[1820]528      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
529      edge.id = (node.id >> 1);
530    }
[1910]531    void nextIn(Edge& edge) const {
532      edge.id += _bNodeNum;
[1820]533      if (edge.id >= _edgeNum) edge.id = -1;
534    }
535
536    static int id(const Node& node) {
537      return node.id;
538    }
539    static Node nodeFromId(int id) {
540      return Node(id);
541    }
542    int maxNodeId() const {
[1910]543      return _aNodeNum > _bNodeNum ?
544        _aNodeNum * 2 - 2 : _bNodeNum * 2 - 1;
[1820]545    }
546 
547    static int id(const Edge& edge) {
548      return edge.id;
549    }
550    static Edge edgeFromId(int id) {
551      return Edge(id);
552    }
553    int maxEdgeId() const {
554      return _edgeNum - 1;
555    }
556 
[1910]557    static int aNodeId(const Node& node) {
[1820]558      return node.id >> 1;
559    }
[1910]560    static Node fromANodeId(int id, Node) {
[1820]561      return Node(id << 1);
562    }
[1910]563    int maxANodeId() const {
564      return _aNodeNum;
[1820]565    }
566
[1910]567    static int bNodeId(const Node& node) {
[1820]568      return node.id >> 1;
569    }
[1910]570    static Node fromBNodeId(int id) {
[1820]571      return Node((id << 1) + 1);
572    }
[1910]573    int maxBNodeId() const {
574      return _bNodeNum;
[1820]575    }
576
[1910]577    Node aNode(const Edge& edge) const {
578      return Node((edge.id / _bNodeNum) << 1);
[1820]579    }
[1910]580    Node bNode(const Edge& edge) const {
581      return Node(((edge.id % _bNodeNum) << 1) + 1);
[1820]582    }
583
[1910]584    static bool aNode(const Node& node) {
[1820]585      return (node.id & 1) == 0;
586    }
587
[1910]588    static bool bNode(const Node& node) {
[1820]589      return (node.id & 1) == 1;
590    }
591
[1910]592    static Node aNode(int index) {
[1820]593      return Node(index << 1);
594    }
595
[1910]596    static Node bNode(int index) {
[1820]597      return Node((index << 1) + 1);
598    }
599
600  };
601
602
[1979]603  typedef BpUGraphExtender< BpUGraphBaseExtender<
604    FullBpUGraphBase> > ExtendedFullBpUGraphBase;
[1820]605
606
[1910]607  /// \ingroup graphs
608  ///
609  /// \brief An undirected full bipartite graph class.
610  ///
611  /// This is a simple and fast bipartite undirected full graph implementation.
612  /// It is completely static, so you can neither add nor delete either
613  /// edges or nodes.
614  ///
615  /// \sa FullGraph
616  ///
617  /// \author Balazs Dezso
618  class FullBpUGraph :
619    public ExtendedFullBpUGraphBase {
[1820]620  public:
[1979]621
[1910]622    typedef ExtendedFullBpUGraphBase Parent;
[1979]623
[1910]624    FullBpUGraph(int aNodeNum, int bNodeNum) {
625      Parent::construct(aNodeNum, bNodeNum);
[1820]626    }
[1979]627    /// \brief Resize the graph
628    ///
629    void resize(int n, int m) {
630      Parent::getNotifier(Edge()).clear();
631      Parent::getNotifier(UEdge()).clear();
632      Parent::getNotifier(Node()).clear();
633      construct(n, m);
634      Parent::getNotifier(Node()).build();
635      Parent::getNotifier(UEdge()).build();
636      Parent::getNotifier(Edge()).build();
637    }
[1820]638  };
639
[921]640} //namespace lemon
[591]641
642
[921]643#endif //LEMON_FULL_GRAPH_H
Note: See TracBrowser for help on using the repository browser.