lemon/concepts/digraph.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 29 Feb 2008 11:01:39 +0000
changeset 103 b68a7e348e00
parent 57 c1acf0018c0a
child 107 31a2e6d28f61
permissions -rw-r--r--
Port kruskal() and UnionFind from svn -r3468

The class type interface of Kruskal has not been ported yet
deba@57
     1
/* -*- C++ -*-
deba@57
     2
 *
deba@57
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@57
     4
 *
deba@57
     5
 * Copyright (C) 2003-2007
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#ifndef LEMON_CONCEPT_DIGRAPH_H
deba@57
    20
#define LEMON_CONCEPT_DIGRAPH_H
deba@57
    21
deba@57
    22
///\ingroup graph_concepts
deba@57
    23
///\file
deba@57
    24
///\brief The concept of directed graphs.
deba@57
    25
deba@57
    26
#include <lemon/bits/invalid.h>
deba@57
    27
#include <lemon/bits/utility.h>
deba@57
    28
#include <lemon/concepts/maps.h>
deba@57
    29
#include <lemon/concept_check.h>
deba@57
    30
#include <lemon/concepts/graph_components.h>
deba@57
    31
deba@57
    32
namespace lemon {
deba@57
    33
  namespace concepts {
deba@57
    34
deba@57
    35
    /// \ingroup graph_concepts
deba@57
    36
    ///
deba@57
    37
    /// \brief Class describing the concept of directed graphs.
deba@57
    38
    ///
deba@57
    39
    /// This class describes the \ref concept "concept" of the
deba@57
    40
    /// immutable directed digraphs.
deba@57
    41
    ///
deba@57
    42
    /// Note that actual digraph implementation like @ref ListDigraph or
deba@57
    43
    /// @ref SmartDigraph may have several additional functionality.
deba@57
    44
    ///
deba@57
    45
    /// \sa concept
deba@57
    46
    class Digraph {
deba@57
    47
    private:
deba@57
    48
      ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
deba@57
    49
      
deba@57
    50
      ///Digraphs are \e not copy constructible. Use DigraphCopy() instead.
deba@57
    51
      ///
deba@57
    52
      Digraph(const Digraph &) {};
deba@57
    53
      ///\brief Assignment of \ref Digraph "Digraph"s to another ones are
deba@57
    54
      ///\e not allowed. Use DigraphCopy() instead.
deba@57
    55
      
deba@57
    56
      ///Assignment of \ref Digraph "Digraph"s to another ones are
deba@57
    57
      ///\e not allowed.  Use DigraphCopy() instead.
deba@57
    58
deba@57
    59
      void operator=(const Digraph &) {}
deba@57
    60
    public:
deba@57
    61
      ///\e
deba@57
    62
deba@57
    63
      /// Defalult constructor.
deba@57
    64
deba@57
    65
      /// Defalult constructor.
deba@57
    66
      ///
deba@57
    67
      Digraph() { }
deba@57
    68
      /// Class for identifying a node of the digraph
deba@57
    69
deba@57
    70
      /// This class identifies a node of the digraph. It also serves
deba@57
    71
      /// as a base class of the node iterators,
deba@57
    72
      /// thus they will convert to this type.
deba@57
    73
      class Node {
deba@57
    74
      public:
deba@57
    75
        /// Default constructor
deba@57
    76
deba@57
    77
        /// @warning The default constructor sets the iterator
deba@57
    78
        /// to an undefined value.
deba@57
    79
        Node() { }
deba@57
    80
        /// Copy constructor.
deba@57
    81
deba@57
    82
        /// Copy constructor.
deba@57
    83
        ///
deba@57
    84
        Node(const Node&) { }
deba@57
    85
deba@57
    86
        /// Invalid constructor \& conversion.
deba@57
    87
deba@57
    88
        /// This constructor initializes the iterator to be invalid.
deba@57
    89
        /// \sa Invalid for more details.
deba@57
    90
        Node(Invalid) { }
deba@57
    91
        /// Equality operator
deba@57
    92
deba@57
    93
        /// Two iterators are equal if and only if they point to the
deba@57
    94
        /// same object or both are invalid.
deba@57
    95
        bool operator==(Node) const { return true; }
deba@57
    96
deba@57
    97
        /// Inequality operator
deba@57
    98
        
deba@57
    99
        /// \sa operator==(Node n)
deba@57
   100
        ///
deba@57
   101
        bool operator!=(Node) const { return true; }
deba@57
   102
deba@57
   103
	/// Artificial ordering operator.
deba@57
   104
	
deba@57
   105
	/// To allow the use of digraph descriptors as key type in std::map or
deba@57
   106
	/// similar associative container we require this.
deba@57
   107
	///
deba@57
   108
	/// \note This operator only have to define some strict ordering of
deba@57
   109
	/// the items; this order has nothing to do with the iteration
deba@57
   110
	/// ordering of the items.
deba@57
   111
	bool operator<(Node) const { return false; }
deba@57
   112
deba@57
   113
      };
deba@57
   114
    
deba@57
   115
      /// This iterator goes through each node.
deba@57
   116
deba@57
   117
      /// This iterator goes through each node.
deba@57
   118
      /// Its usage is quite simple, for example you can count the number
deba@57
   119
      /// of nodes in digraph \c g of type \c Digraph like this:
deba@57
   120
      ///\code
deba@57
   121
      /// int count=0;
deba@57
   122
      /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
deba@57
   123
      ///\endcode
deba@57
   124
      class NodeIt : public Node {
deba@57
   125
      public:
deba@57
   126
        /// Default constructor
deba@57
   127
deba@57
   128
        /// @warning The default constructor sets the iterator
deba@57
   129
        /// to an undefined value.
deba@57
   130
        NodeIt() { }
deba@57
   131
        /// Copy constructor.
deba@57
   132
        
deba@57
   133
        /// Copy constructor.
deba@57
   134
        ///
deba@57
   135
        NodeIt(const NodeIt& n) : Node(n) { }
deba@57
   136
        /// Invalid constructor \& conversion.
deba@57
   137
deba@57
   138
        /// Initialize the iterator to be invalid.
deba@57
   139
        /// \sa Invalid for more details.
deba@57
   140
        NodeIt(Invalid) { }
deba@57
   141
        /// Sets the iterator to the first node.
deba@57
   142
deba@57
   143
        /// Sets the iterator to the first node of \c g.
deba@57
   144
        ///
deba@57
   145
        NodeIt(const Digraph&) { }
deba@57
   146
        /// Node -> NodeIt conversion.
deba@57
   147
deba@57
   148
        /// Sets the iterator to the node of \c the digraph pointed by 
deba@57
   149
	/// the trivial iterator.
deba@57
   150
        /// This feature necessitates that each time we 
deba@57
   151
        /// iterate the arc-set, the iteration order is the same.
deba@57
   152
        NodeIt(const Digraph&, const Node&) { }
deba@57
   153
        /// Next node.
deba@57
   154
deba@57
   155
        /// Assign the iterator to the next node.
deba@57
   156
        ///
deba@57
   157
        NodeIt& operator++() { return *this; }
deba@57
   158
      };
deba@57
   159
    
deba@57
   160
    
deba@57
   161
      /// Class for identifying an arc of the digraph
deba@57
   162
deba@57
   163
      /// This class identifies an arc of the digraph. It also serves
deba@57
   164
      /// as a base class of the arc iterators,
deba@57
   165
      /// thus they will convert to this type.
deba@57
   166
      class Arc {
deba@57
   167
      public:
deba@57
   168
        /// Default constructor
deba@57
   169
deba@57
   170
        /// @warning The default constructor sets the iterator
deba@57
   171
        /// to an undefined value.
deba@57
   172
        Arc() { }
deba@57
   173
        /// Copy constructor.
deba@57
   174
deba@57
   175
        /// Copy constructor.
deba@57
   176
        ///
deba@57
   177
        Arc(const Arc&) { }
deba@57
   178
        /// Initialize the iterator to be invalid.
deba@57
   179
deba@57
   180
        /// Initialize the iterator to be invalid.
deba@57
   181
        ///
deba@57
   182
        Arc(Invalid) { }
deba@57
   183
        /// Equality operator
deba@57
   184
deba@57
   185
        /// Two iterators are equal if and only if they point to the
deba@57
   186
        /// same object or both are invalid.
deba@57
   187
        bool operator==(Arc) const { return true; }
deba@57
   188
        /// Inequality operator
deba@57
   189
deba@57
   190
        /// \sa operator==(Arc n)
deba@57
   191
        ///
deba@57
   192
        bool operator!=(Arc) const { return true; }
deba@57
   193
deba@57
   194
	/// Artificial ordering operator.
deba@57
   195
	
deba@57
   196
	/// To allow the use of digraph descriptors as key type in std::map or
deba@57
   197
	/// similar associative container we require this.
deba@57
   198
	///
deba@57
   199
	/// \note This operator only have to define some strict ordering of
deba@57
   200
	/// the items; this order has nothing to do with the iteration
deba@57
   201
	/// ordering of the items.
deba@57
   202
	bool operator<(Arc) const { return false; }
deba@57
   203
      };
deba@57
   204
    
deba@57
   205
      /// This iterator goes trough the outgoing arcs of a node.
deba@57
   206
deba@57
   207
      /// This iterator goes trough the \e outgoing arcs of a certain node
deba@57
   208
      /// of a digraph.
deba@57
   209
      /// Its usage is quite simple, for example you can count the number
deba@57
   210
      /// of outgoing arcs of a node \c n
deba@57
   211
      /// in digraph \c g of type \c Digraph as follows.
deba@57
   212
      ///\code
deba@57
   213
      /// int count=0;
deba@57
   214
      /// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
deba@57
   215
      ///\endcode
deba@57
   216
    
deba@57
   217
      class OutArcIt : public Arc {
deba@57
   218
      public:
deba@57
   219
        /// Default constructor
deba@57
   220
deba@57
   221
        /// @warning The default constructor sets the iterator
deba@57
   222
        /// to an undefined value.
deba@57
   223
        OutArcIt() { }
deba@57
   224
        /// Copy constructor.
deba@57
   225
deba@57
   226
        /// Copy constructor.
deba@57
   227
        ///
deba@57
   228
        OutArcIt(const OutArcIt& e) : Arc(e) { }
deba@57
   229
        /// Initialize the iterator to be invalid.
deba@57
   230
deba@57
   231
        /// Initialize the iterator to be invalid.
deba@57
   232
        ///
deba@57
   233
        OutArcIt(Invalid) { }
deba@57
   234
        /// This constructor sets the iterator to the first outgoing arc.
deba@57
   235
    
deba@57
   236
        /// This constructor sets the iterator to the first outgoing arc of
deba@57
   237
        /// the node.
deba@57
   238
        OutArcIt(const Digraph&, const Node&) { }
deba@57
   239
        /// Arc -> OutArcIt conversion
deba@57
   240
deba@57
   241
        /// Sets the iterator to the value of the trivial iterator.
deba@57
   242
	/// This feature necessitates that each time we 
deba@57
   243
        /// iterate the arc-set, the iteration order is the same.
deba@57
   244
        OutArcIt(const Digraph&, const Arc&) { }
deba@57
   245
        ///Next outgoing arc
deba@57
   246
        
deba@57
   247
        /// Assign the iterator to the next 
deba@57
   248
        /// outgoing arc of the corresponding node.
deba@57
   249
        OutArcIt& operator++() { return *this; }
deba@57
   250
      };
deba@57
   251
deba@57
   252
      /// This iterator goes trough the incoming arcs of a node.
deba@57
   253
deba@57
   254
      /// This iterator goes trough the \e incoming arcs of a certain node
deba@57
   255
      /// of a digraph.
deba@57
   256
      /// Its usage is quite simple, for example you can count the number
deba@57
   257
      /// of outgoing arcs of a node \c n
deba@57
   258
      /// in digraph \c g of type \c Digraph as follows.
deba@57
   259
      ///\code
deba@57
   260
      /// int count=0;
deba@57
   261
      /// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
deba@57
   262
      ///\endcode
deba@57
   263
deba@57
   264
      class InArcIt : public Arc {
deba@57
   265
      public:
deba@57
   266
        /// Default constructor
deba@57
   267
deba@57
   268
        /// @warning The default constructor sets the iterator
deba@57
   269
        /// to an undefined value.
deba@57
   270
        InArcIt() { }
deba@57
   271
        /// Copy constructor.
deba@57
   272
deba@57
   273
        /// Copy constructor.
deba@57
   274
        ///
deba@57
   275
        InArcIt(const InArcIt& e) : Arc(e) { }
deba@57
   276
        /// Initialize the iterator to be invalid.
deba@57
   277
deba@57
   278
        /// Initialize the iterator to be invalid.
deba@57
   279
        ///
deba@57
   280
        InArcIt(Invalid) { }
deba@57
   281
        /// This constructor sets the iterator to first incoming arc.
deba@57
   282
    
deba@57
   283
        /// This constructor set the iterator to the first incoming arc of
deba@57
   284
        /// the node.
deba@57
   285
        InArcIt(const Digraph&, const Node&) { }
deba@57
   286
        /// Arc -> InArcIt conversion
deba@57
   287
deba@57
   288
        /// Sets the iterator to the value of the trivial iterator \c e.
deba@57
   289
        /// This feature necessitates that each time we 
deba@57
   290
        /// iterate the arc-set, the iteration order is the same.
deba@57
   291
        InArcIt(const Digraph&, const Arc&) { }
deba@57
   292
        /// Next incoming arc
deba@57
   293
deba@57
   294
        /// Assign the iterator to the next inarc of the corresponding node.
deba@57
   295
        ///
deba@57
   296
        InArcIt& operator++() { return *this; }
deba@57
   297
      };
deba@57
   298
      /// This iterator goes through each arc.
deba@57
   299
deba@57
   300
      /// This iterator goes through each arc of a digraph.
deba@57
   301
      /// Its usage is quite simple, for example you can count the number
deba@57
   302
      /// of arcs in a digraph \c g of type \c Digraph as follows:
deba@57
   303
      ///\code
deba@57
   304
      /// int count=0;
deba@57
   305
      /// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count;
deba@57
   306
      ///\endcode
deba@57
   307
      class ArcIt : public Arc {
deba@57
   308
      public:
deba@57
   309
        /// Default constructor
deba@57
   310
deba@57
   311
        /// @warning The default constructor sets the iterator
deba@57
   312
        /// to an undefined value.
deba@57
   313
        ArcIt() { }
deba@57
   314
        /// Copy constructor.
deba@57
   315
deba@57
   316
        /// Copy constructor.
deba@57
   317
        ///
deba@57
   318
        ArcIt(const ArcIt& e) : Arc(e) { }
deba@57
   319
        /// Initialize the iterator to be invalid.
deba@57
   320
deba@57
   321
        /// Initialize the iterator to be invalid.
deba@57
   322
        ///
deba@57
   323
        ArcIt(Invalid) { }
deba@57
   324
        /// This constructor sets the iterator to the first arc.
deba@57
   325
    
deba@57
   326
        /// This constructor sets the iterator to the first arc of \c g.
deba@57
   327
        ///@param g the digraph
deba@57
   328
        ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); }
deba@57
   329
        /// Arc -> ArcIt conversion
deba@57
   330
deba@57
   331
        /// Sets the iterator to the value of the trivial iterator \c e.
deba@57
   332
        /// This feature necessitates that each time we 
deba@57
   333
        /// iterate the arc-set, the iteration order is the same.
deba@57
   334
        ArcIt(const Digraph&, const Arc&) { } 
deba@57
   335
        ///Next arc
deba@57
   336
        
deba@57
   337
        /// Assign the iterator to the next arc.
deba@57
   338
        ArcIt& operator++() { return *this; }
deba@57
   339
      };
deba@57
   340
      ///Gives back the target node of an arc.
deba@57
   341
deba@57
   342
      ///Gives back the target node of an arc.
deba@57
   343
      ///
deba@57
   344
      Node target(Arc) const { return INVALID; }
deba@57
   345
      ///Gives back the source node of an arc.
deba@57
   346
deba@57
   347
      ///Gives back the source node of an arc.
deba@57
   348
      ///
deba@57
   349
      Node source(Arc) const { return INVALID; }
deba@57
   350
deba@61
   351
      /// \brief Returns the ID of the node.
deba@61
   352
      int id(Node) const { return -1; } 
deba@61
   353
deba@61
   354
      /// \brief Returns the ID of the arc.
deba@61
   355
      int id(Arc) const { return -1; } 
deba@61
   356
deba@61
   357
      /// \brief Returns the node with the given ID.
deba@61
   358
      ///
deba@61
   359
      /// \pre The argument should be a valid node ID in the graph.
deba@61
   360
      Node nodeFromId(int) const { return INVALID; } 
deba@61
   361
deba@61
   362
      /// \brief Returns the arc with the given ID.
deba@61
   363
      ///
deba@61
   364
      /// \pre The argument should be a valid arc ID in the graph.
deba@61
   365
      Arc arcFromId(int) const { return INVALID; } 
deba@61
   366
deba@61
   367
      /// \brief Returns an upper bound on the node IDs.
deba@61
   368
      int maxNodeId() const { return -1; } 
deba@61
   369
deba@61
   370
      /// \brief Returns an upper bound on the arc IDs.
deba@61
   371
      int maxArcId() const { return -1; } 
deba@61
   372
deba@57
   373
      void first(Node&) const {}
deba@57
   374
      void next(Node&) const {}
deba@57
   375
deba@57
   376
      void first(Arc&) const {}
deba@57
   377
      void next(Arc&) const {}
deba@57
   378
deba@57
   379
deba@57
   380
      void firstIn(Arc&, const Node&) const {}
deba@57
   381
      void nextIn(Arc&) const {}
deba@57
   382
deba@57
   383
      void firstOut(Arc&, const Node&) const {}
deba@57
   384
      void nextOut(Arc&) const {}
deba@57
   385
deba@61
   386
      // The second parameter is dummy.
deba@61
   387
      Node fromId(int, Node) const { return INVALID; }
deba@61
   388
      // The second parameter is dummy.
deba@61
   389
      Arc fromId(int, Arc) const { return INVALID; }
deba@61
   390
deba@61
   391
      // Dummy parameter.
deba@61
   392
      int maxId(Node) const { return -1; } 
deba@61
   393
      // Dummy parameter.
deba@61
   394
      int maxId(Arc) const { return -1; } 
deba@61
   395
deba@57
   396
      /// \brief The base node of the iterator.
deba@57
   397
      ///
deba@57
   398
      /// Gives back the base node of the iterator.
deba@57
   399
      /// It is always the target of the pointed arc.
deba@57
   400
      Node baseNode(const InArcIt&) const { return INVALID; }
deba@57
   401
deba@57
   402
      /// \brief The running node of the iterator.
deba@57
   403
      ///
deba@57
   404
      /// Gives back the running node of the iterator.
deba@57
   405
      /// It is always the source of the pointed arc.
deba@57
   406
      Node runningNode(const InArcIt&) const { return INVALID; }
deba@57
   407
deba@57
   408
      /// \brief The base node of the iterator.
deba@57
   409
      ///
deba@57
   410
      /// Gives back the base node of the iterator.
deba@57
   411
      /// It is always the source of the pointed arc.
deba@57
   412
      Node baseNode(const OutArcIt&) const { return INVALID; }
deba@57
   413
deba@57
   414
      /// \brief The running node of the iterator.
deba@57
   415
      ///
deba@57
   416
      /// Gives back the running node of the iterator.
deba@57
   417
      /// It is always the target of the pointed arc.
deba@57
   418
      Node runningNode(const OutArcIt&) const { return INVALID; }
deba@57
   419
deba@57
   420
      /// \brief The opposite node on the given arc.
deba@57
   421
      ///
deba@57
   422
      /// Gives back the opposite node on the given arc.
deba@57
   423
      Node oppositeNode(const Node&, const Arc&) const { return INVALID; }
deba@57
   424
deba@57
   425
      /// \brief Read write map of the nodes to type \c T.
deba@57
   426
      /// 
deba@57
   427
      /// ReadWrite map of the nodes to type \c T.
deba@57
   428
      /// \sa Reference
deba@57
   429
      template<class T> 
deba@57
   430
      class NodeMap : public ReadWriteMap< Node, T > {
deba@57
   431
      public:
deba@57
   432
deba@57
   433
        ///\e
deba@57
   434
        NodeMap(const Digraph&) { }
deba@57
   435
        ///\e
deba@57
   436
        NodeMap(const Digraph&, T) { }
deba@57
   437
deba@57
   438
        ///Copy constructor
deba@57
   439
        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
deba@57
   440
        ///Assignment operator
deba@57
   441
        template <typename CMap>
deba@57
   442
        NodeMap& operator=(const CMap&) { 
deba@57
   443
          checkConcept<ReadMap<Node, T>, CMap>();
deba@57
   444
          return *this; 
deba@57
   445
        }
deba@57
   446
      };
deba@57
   447
deba@57
   448
      /// \brief Read write map of the arcs to type \c T.
deba@57
   449
      ///
deba@57
   450
      /// Reference map of the arcs to type \c T.
deba@57
   451
      /// \sa Reference
deba@57
   452
      template<class T> 
deba@57
   453
      class ArcMap : public ReadWriteMap<Arc,T> {
deba@57
   454
      public:
deba@57
   455
deba@57
   456
        ///\e
deba@57
   457
        ArcMap(const Digraph&) { }
deba@57
   458
        ///\e
deba@57
   459
        ArcMap(const Digraph&, T) { }
deba@57
   460
        ///Copy constructor
deba@57
   461
        ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
deba@57
   462
        ///Assignment operator
deba@57
   463
        template <typename CMap>
deba@57
   464
        ArcMap& operator=(const CMap&) { 
deba@57
   465
          checkConcept<ReadMap<Arc, T>, CMap>();
deba@57
   466
          return *this; 
deba@57
   467
        }
deba@57
   468
      };
deba@57
   469
deba@57
   470
      template <typename RDigraph>
deba@57
   471
      struct Constraints {
deba@57
   472
        void constraints() {
deba@57
   473
          checkConcept<IterableDigraphComponent<>, Digraph>();
deba@61
   474
	  checkConcept<IDableDigraphComponent<>, Digraph>();
deba@57
   475
          checkConcept<MappableDigraphComponent<>, Digraph>();
deba@57
   476
        }
deba@57
   477
      };
deba@57
   478
deba@57
   479
    };
deba@57
   480
    
deba@57
   481
  } //namespace concepts  
deba@57
   482
} //namespace lemon
deba@57
   483
deba@57
   484
deba@57
   485
deba@57
   486
#endif // LEMON_CONCEPT_DIGRAPH_H