src/work/marci/graph_concept.h
author marci
Fri, 30 Apr 2004 16:46:19 +0000
changeset 498 eb8bfa683d92
parent 333 e0a80761dfd9
child 651 a56e043aeab1
permissions -rw-r--r--
bipartite graphs
marci@325
     1
// -*- c++ -*-
marci@325
     2
#ifndef HUGO_GRAPH_H
marci@325
     3
#define HUGO_GRAPH_H
marci@325
     4
marci@325
     5
///\file
marci@332
     6
///\brief Declaration of GraphSkeleturo.
marci@325
     7
marci@325
     8
#include <invalid.h>
marci@325
     9
marci@325
    10
/// The namespace of HugoLib
marci@325
    11
namespace hugo {
marci@325
    12
marci@332
    13
  /// @defgroup empty_graph The GraphSkeleturo class
marci@332
    14
  /// @{
marci@325
    15
marci@325
    16
  /// An empty graph class.
marci@325
    17
  
marci@325
    18
  /// This class provides all the common features of a graph structure,
marci@325
    19
  /// however completely without implementations and real data structures
marci@325
    20
  /// behind the interface.
marci@325
    21
  /// All graph algorithms should compile with this class, but it will not
marci@325
    22
  /// run properly, of course.
marci@325
    23
  ///
marci@325
    24
  /// It can be used for checking the interface compatibility,
marci@325
    25
  /// or it can serve as a skeleton of a new graph structure.
marci@325
    26
  /// 
marci@325
    27
  /// Also, you will find here the full documentation of a certain graph
marci@325
    28
  /// feature, the documentation of a real graph imlementation
marci@325
    29
  /// like @ref ListGraph or
marci@325
    30
  /// @ref SmartGraph will just refer to this structure.
marci@332
    31
  class GraphSkeleturo
marci@325
    32
  {
marci@325
    33
  public:
marci@325
    34
    /// Defalult constructor.
marci@332
    35
    GraphSkeleturo() {}
marci@325
    36
    ///Copy consructor.
marci@325
    37
marci@325
    38
    ///\todo It is not clear, what we expect from a copy constructor.
marci@325
    39
    ///E.g. How to assign the nodes/edges to each other? What about maps?
marci@332
    40
    GraphSkeleturo(const GraphSkeleturo &G) {}
marci@325
    41
marci@325
    42
    /// The base type of the node iterators.
marci@325
    43
marci@325
    44
    /// This is the base type of each node iterators,
marci@325
    45
    /// thus each kind of node iterator will convert to this.
marci@325
    46
    class Node {
marci@325
    47
    public:
marci@325
    48
      /// @warning The default constructor sets the iterator
marci@325
    49
      /// to an undefined value.
marci@325
    50
      Node() {}   //FIXME
marci@325
    51
      /// Invalid constructor \& conversion.
marci@325
    52
marci@325
    53
      /// This constructor initializes the iterator to be invalid.
marci@325
    54
      /// \sa Invalid for more details.
marci@325
    55
marci@325
    56
      Node(Invalid) {}
marci@325
    57
      //Node(const Node &) {}
marci@325
    58
marci@325
    59
      /// Two iterators are equal if and only if they point to the
marci@325
    60
      /// same object or both are invalid.
marci@325
    61
      bool operator==(Node n) const { return true; }
marci@325
    62
marci@325
    63
      /// \sa \ref operator==(Node n)
marci@325
    64
      ///
marci@325
    65
      bool operator!=(Node n) const { return true; }
marci@325
    66
marci@325
    67
      bool operator<(Node n) const { return true; }
marci@325
    68
    };
marci@325
    69
    
marci@325
    70
    /// This iterator goes through each node.
marci@325
    71
marci@325
    72
    /// This iterator goes through each node.
marci@325
    73
    /// Its usage is quite simple, for example you can count the number
marci@325
    74
    /// of nodes in graph \c G of type \c Graph like this:
marci@325
    75
    /// \code
marci@325
    76
    ///int count=0;
marci@325
    77
    ///for(Graph::NodeIt n(G);G.valid(n);G.next(n)) count++;
marci@325
    78
    /// \endcode
marci@325
    79
    class NodeIt : public Node {
marci@325
    80
    public:
marci@325
    81
      /// @warning The default constructor sets the iterator
marci@325
    82
      /// to an undefined value.
marci@325
    83
      NodeIt() {} //FIXME
marci@325
    84
      /// Invalid constructor \& conversion.
marci@325
    85
marci@325
    86
      /// Initialize the iterator to be invalid
marci@325
    87
      /// \sa Invalid for more details.
marci@325
    88
      NodeIt(Invalid) {}
marci@325
    89
      /// Sets the iterator to the first node of \c G.
marci@332
    90
      NodeIt(const GraphSkeleturo &G) {}
marci@325
    91
      /// @warning The default constructor sets the iterator
marci@325
    92
      /// to an undefined value.
marci@325
    93
      NodeIt(const NodeIt &) {}
marci@325
    94
    };
marci@325
    95
    
marci@325
    96
    
marci@325
    97
    /// The base type of the edge iterators.
marci@325
    98
    class Edge {
marci@325
    99
    public:
marci@325
   100
      /// @warning The default constructor sets the iterator
marci@325
   101
      /// to an undefined value.
marci@325
   102
      Edge() {}   //FIXME
marci@325
   103
      /// Initialize the iterator to be invalid
marci@325
   104
      Edge(Invalid) {}
marci@325
   105
      /// Two iterators are equal if and only if they point to the
marci@325
   106
      /// same object or both are invalid.
marci@325
   107
      bool operator==(Edge n) const { return true; }
marci@325
   108
      bool operator!=(Edge n) const { return true; }
marci@325
   109
      bool operator<(Edge n) const { return true; }
marci@325
   110
    };
marci@325
   111
    
marci@325
   112
    //  class SymEdgeIt : public Edge {};
marci@325
   113
marci@325
   114
    /// This iterator goes through each edge.
marci@325
   115
marci@325
   116
    /// This iterator goes through each edge of a graph.
marci@325
   117
    /// Its usage is quite simple, for example you can count the number
marci@325
   118
    /// of edges in a graph \c G of type \c Graph as follows:
marci@325
   119
    /// \code
marci@325
   120
    ///int count=0;
marci@325
   121
    ///for(Graph::EdgeIt e(G);G.valid(e);G.next(e)) count++;
marci@325
   122
    /// \endcode
marci@325
   123
    class EdgeIt : public Edge {
marci@325
   124
    public:
marci@325
   125
      /// @warning The default constructor sets the iterator
marci@325
   126
      /// to an undefined value.
marci@325
   127
      EdgeIt() {}
marci@325
   128
      /// Initialize the iterator to be invalid
marci@325
   129
      EdgeIt(Invalid) {}
marci@332
   130
      EdgeIt(const GraphSkeleturo &) {}
marci@325
   131
    };
marci@325
   132
marci@325
   133
    /// First node of the graph.
marci@325
   134
marci@325
   135
    /// \post \c i and the return value will be the first node.
marci@325
   136
    ///
marci@325
   137
    NodeIt &first(NodeIt &i) const { return i;}
marci@325
   138
marci@325
   139
    /// The first incoming edge.
marci@325
   140
    InEdgeIt &first(InEdgeIt &i, Node n) const { return i;}
marci@325
   141
    /// The first outgoing edge.
marci@325
   142
    OutEdgeIt &first(OutEdgeIt &i, Node n) const { return i;}
marci@325
   143
    //  SymEdgeIt &first(SymEdgeIt &, Node) const { return i;}
marci@325
   144
    /// The first edge of the Graph.
marci@325
   145
    EdgeIt &first(EdgeIt &i) const { return i;}
marci@325
   146
marci@325
   147
//     Node getNext(Node) const {}
marci@325
   148
//     InEdgeIt getNext(InEdgeIt) const {}
marci@325
   149
//     OutEdgeIt getNext(OutEdgeIt) const {}
marci@325
   150
//     //SymEdgeIt getNext(SymEdgeIt) const {}
marci@325
   151
//     EdgeIt getNext(EdgeIt) const {}
marci@325
   152
marci@325
   153
    /// Go to the next node.
marci@325
   154
    NodeIt &next(NodeIt &i) const { return i;}
marci@325
   155
    /// Go to the next incoming edge.
marci@325
   156
    InEdgeIt &next(InEdgeIt &i) const { return i;}
marci@325
   157
    /// Go to the next outgoing edge.
marci@325
   158
    OutEdgeIt &next(OutEdgeIt &i) const { return i;}
marci@325
   159
    //SymEdgeIt &next(SymEdgeIt &) const {}
marci@325
   160
    /// Go to the next edge.
marci@325
   161
    EdgeIt &next(EdgeIt &i) const { return i;}
marci@325
   162
marci@325
   163
    ///Gives back the head node of an edge.
marci@325
   164
    Node head(Edge) const { return INVALID; }
marci@325
   165
    ///Gives back the tail node of an edge.
marci@325
   166
    Node tail(Edge) const { return INVALID; }
marci@325
   167
  
marci@325
   168
    //   Node aNode(InEdgeIt) const {}
marci@325
   169
    //   Node aNode(OutEdgeIt) const {}
marci@325
   170
    //   Node aNode(SymEdgeIt) const {}
marci@325
   171
marci@325
   172
    //   Node bNode(InEdgeIt) const {}
marci@325
   173
    //   Node bNode(OutEdgeIt) const {}
marci@325
   174
    //   Node bNode(SymEdgeIt) const {}
marci@325
   175
marci@325
   176
    /// Checks if a node iterator is valid
marci@325
   177
marci@325
   178
    ///\todo Maybe, it would be better if iterator converted to
marci@325
   179
    ///bool directly, as Jacint prefers.
marci@325
   180
    bool valid(const Node&) const { return true;}
marci@325
   181
    /// Checks if an edge iterator is valid
marci@325
   182
marci@325
   183
    ///\todo Maybe, it would be better if iterator converted to
marci@325
   184
    ///bool directly, as Jacint prefers.
marci@325
   185
    bool valid(const Edge&) const { return true;}
marci@325
   186
marci@325
   187
    ///Gives back the \e id of a node.
marci@325
   188
marci@325
   189
    ///\warning Not all graph structures provide this feature.
marci@325
   190
    ///
marci@325
   191
    int id(const Node&) const { return 0;}
marci@325
   192
    ///Gives back the \e id of an edge.
marci@325
   193
marci@325
   194
    ///\warning Not all graph structures provide this feature.
marci@325
   195
    ///
marci@325
   196
    int id(const Edge&) const { return 0;}
marci@325
   197
marci@325
   198
    //void setInvalid(Node &) const {};
marci@325
   199
    //void setInvalid(Edge &) const {};
marci@325
   200
  
marci@325
   201
    ///Add a new node to the graph.
marci@325
   202
marci@325
   203
    /// \return the new node.
marci@325
   204
    ///
marci@325
   205
    Node addNode() { return INVALID;}
marci@325
   206
    ///Add a new edge to the graph.
marci@325
   207
marci@325
   208
    ///Add a new edge to the graph with tail node \c tail
marci@325
   209
    ///and head node \c head.
marci@325
   210
    ///\return the new edge.
marci@325
   211
    Edge addEdge(Node tail, Node head) { return INVALID;}
marci@325
   212
    
marci@325
   213
    /// Resets the graph.
marci@325
   214
marci@325
   215
    /// This function deletes all edges and nodes of the graph.
marci@325
   216
    /// It also frees the memory allocated to store them.
marci@325
   217
    void clear() {}
marci@325
   218
marci@325
   219
    ///Read/write/reference map of the nodes to type \c T.
marci@325
   220
marci@325
   221
    ///Read/write/reference map of the nodes to type \c T.
marci@332
   222
    /// \sa MemoryMapSkeleturo
marci@325
   223
    /// \todo We may need copy constructor
marci@325
   224
    /// \todo We may need conversion from other nodetype
marci@325
   225
    /// \todo We may need operator=
marci@325
   226
    /// \warning Making maps that can handle bool type (NodeMap<bool>)
marci@325
   227
    /// needs extra attention!
marci@325
   228
marci@325
   229
    template<class T> class NodeMap
marci@325
   230
    {
marci@325
   231
    public:
marci@325
   232
      typedef T ValueType;
marci@325
   233
      typedef Node KeyType;
marci@325
   234
marci@332
   235
      NodeMap(const GraphSkeleturo &G) {}
marci@332
   236
      NodeMap(const GraphSkeleturo &G, T t) {}
marci@325
   237
marci@325
   238
      template<typename TT> NodeMap(const NodeMap<TT> &m) {}
marci@325
   239
marci@325
   240
      /// Sets the value of a node.
marci@325
   241
marci@325
   242
      /// Sets the value associated with node \c i to the value \c t.
marci@325
   243
      ///
marci@325
   244
      void set(Node i, T t) {}
marci@325
   245
      /// Gets the value of a node.
marci@325
   246
      T get(Node i) const {return *(T*)0;}  //FIXME: Is it necessary
marci@325
   247
      T &operator[](Node i) {return *(T*)0;}
marci@325
   248
      const T &operator[](Node i) const {return *(T*)0;}
marci@325
   249
marci@325
   250
      /// Updates the map if the graph has been changed
marci@325
   251
marci@325
   252
      /// \todo Do we need this?
marci@325
   253
      ///
marci@325
   254
      void update() {}
marci@325
   255
      void update(T a) {}   //FIXME: Is it necessary
marci@325
   256
    };
marci@325
   257
marci@325
   258
    ///Read/write/reference map of the edges to type \c T.
marci@325
   259
marci@325
   260
    ///Read/write/reference map of the edges to type \c T.
marci@325
   261
    ///It behaves exactly in the same way as \ref NodeMap.
marci@325
   262
    /// \sa NodeMap
marci@332
   263
    /// \sa MemoryMapSkeleturo
marci@325
   264
    /// \todo We may need copy constructor
marci@325
   265
    /// \todo We may need conversion from other edgetype
marci@325
   266
    /// \todo We may need operator=
marci@325
   267
    template<class T> class EdgeMap
marci@325
   268
    {
marci@325
   269
    public:
marci@325
   270
      typedef T ValueType;
marci@325
   271
      typedef Edge KeyType;
marci@325
   272
marci@332
   273
      EdgeMap(const GraphSkeleturo &G) {}
marci@332
   274
      EdgeMap(const GraphSkeleturo &G, T t) {}
marci@325
   275
    
marci@325
   276
      void set(Edge i, T t) {}
marci@325
   277
      T get(Edge i) const {return *(T*)0;}
marci@325
   278
      T &operator[](Edge i) {return *(T*)0;}
marci@325
   279
    
marci@325
   280
      void update() {}
marci@325
   281
      void update(T a) {}   //FIXME: Is it necessary
marci@325
   282
    };
marci@325
   283
  };
marci@325
   284
marci@325
   285
  /// An empty eraseable graph class.
marci@325
   286
  
marci@325
   287
  /// This class provides all the common features of an \e eraseable graph
marci@325
   288
  /// structure,
marci@325
   289
  /// however completely without implementations and real data structures
marci@325
   290
  /// behind the interface.
marci@325
   291
  /// All graph algorithms should compile with this class, but it will not
marci@325
   292
  /// run properly, of course.
marci@325
   293
  ///
marci@325
   294
  /// \todo This blabla could be replaced by a sepatate description about
marci@332
   295
  /// Skeleturos.
marci@325
   296
  ///
marci@325
   297
  /// It can be used for checking the interface compatibility,
marci@325
   298
  /// or it can serve as a skeleton of a new graph structure.
marci@325
   299
  /// 
marci@325
   300
  /// Also, you will find here the full documentation of a certain graph
marci@325
   301
  /// feature, the documentation of a real graph imlementation
marci@325
   302
  /// like @ref ListGraph or
marci@325
   303
  /// @ref SmartGraph will just refer to this structure.
marci@332
   304
  class EraseableGraphSkeleturo : public GraphSkeleturo
marci@325
   305
  {
marci@325
   306
  public:
marci@325
   307
    /// Deletes a node.
marci@325
   308
    void erase(Node n) {}
marci@325
   309
    /// Deletes an edge.
marci@325
   310
    void erase(Edge e) {}
marci@325
   311
marci@325
   312
    /// Defalult constructor.
marci@332
   313
    GraphSkeleturo() {}
marci@325
   314
    ///Copy consructor.
marci@332
   315
    GraphSkeleturo(const GraphSkeleturo &G) {}
marci@325
   316
  };
marci@325
   317
marci@334
   318
  /// An empty out-edge-iterable graph class.
marci@334
   319
  
marci@334
   320
  /// An empty graph class which provides a function to 
marci@334
   321
  /// iterate on out-edges of any node.
marci@334
   322
  class OutEdgeIterableGraphSkeleturo : public GraphSkeleturo
marci@334
   323
  {
marci@334
   324
  public:
marci@334
   325
marci@334
   326
    /// This iterator goes trough the outgoing edges of a node.
marci@334
   327
marci@334
   328
    /// This iterator goes trough the \e outgoing edges of a certain node
marci@334
   329
    /// of a graph.
marci@334
   330
    /// Its usage is quite simple, for example you can count the number
marci@334
   331
    /// of outgoing edges of a node \c n
marci@334
   332
    /// in graph \c G of type \c Graph as follows.
marci@334
   333
    /// \code
marci@334
   334
    ///int count=0;
marci@334
   335
    ///for(Graph::OutEdgeIt e(G,n); G.valid(e); G.next(e)) ++count;
marci@334
   336
    /// \endcode
marci@334
   337
    class OutEdgeIt : public Edge {
marci@334
   338
    public:
marci@334
   339
      /// @warning The default constructor sets the iterator
marci@334
   340
      /// to an undefined value.
marci@334
   341
      OutEdgeIt() {}
marci@334
   342
      /// Initialize the iterator to be invalid
marci@334
   343
      OutEdgeIt(Invalid) {}
marci@334
   344
      /// This constructor sets the iterator to first outgoing edge.
marci@334
   345
    
marci@334
   346
      /// This constructor set the iterator to the first outgoing edge of
marci@334
   347
      /// node
marci@334
   348
      ///@param n the node
marci@334
   349
      ///@param G the graph
marci@334
   350
      OutEdgeIt(const GraphSkeleturo & G, Node n) {}
marci@334
   351
    };
marci@334
   352
  };
marci@334
   353
marci@334
   354
  /// An empty in-edge-iterable graph class.
marci@334
   355
  
marci@334
   356
  /// An empty graph class which provides a function to 
marci@334
   357
  /// iterate on in-edges of any node.
marci@334
   358
  class InEdgeIterableGraphSkeleturo : public GraphSkeleturo
marci@334
   359
  {
marci@334
   360
  public:
marci@334
   361
marci@334
   362
    /// This iterator goes trough the incoming edges of a node.
marci@334
   363
marci@334
   364
    /// This iterator goes trough the \e incoming edges of a certain node
marci@334
   365
    /// of a graph.
marci@334
   366
    /// Its usage is quite simple, for example you can count the number
marci@334
   367
    /// of incoming edges of a node \c n
marci@334
   368
    /// in graph \c G of type \c Graph as follows.
marci@334
   369
    /// \code
marci@334
   370
    ///int count=0;
marci@334
   371
    ///for(Graph::InEdgeIt e(G,n); G.valid(e); G.next(e)) ++count;
marci@334
   372
    /// \endcode
marci@334
   373
    class InEdgeIt : public Edge {
marci@334
   374
    public:
marci@334
   375
      /// @warning The default constructor sets the iterator
marci@334
   376
      /// to an undefined value.
marci@334
   377
      InEdgeIt() {}
marci@334
   378
      /// Initialize the iterator to be invalid
marci@334
   379
      InEdgeIt(Invalid) {}
marci@334
   380
      /// This constructor sets the iterator to first incomig edge.
marci@334
   381
    
marci@334
   382
      /// This constructor set the iterator to the first incomig edge of
marci@334
   383
      /// node
marci@334
   384
      ///@param n the node
marci@334
   385
      ///@param G the graph
marci@334
   386
      InEdgeIt(const GraphSkeleturo & G, Node n) {}
marci@334
   387
    };
marci@334
   388
  };
marci@334
   389
marci@334
   390
marci@333
   391
  /// An empty node-eraseable graph class.
marci@333
   392
  
marci@333
   393
  /// An empty graph class which provides a function to 
marci@333
   394
  /// delete any of its nodes.
marci@333
   395
  class NodeEraseableGraphSkeleturo : public GraphSkeleturo
marci@333
   396
  {
marci@333
   397
  public:
marci@333
   398
    /// Deletes a node.
marci@333
   399
    void erase(Node n) {}
marci@333
   400
  };
marci@333
   401
marci@333
   402
  /// An empty edge-eraseable graph class.
marci@333
   403
  
marci@333
   404
  /// An empty graph class which provides a function to delete any 
marci@333
   405
  /// of its edges.
marci@333
   406
  class EdgeEraseableGraphSkeleturo : public GraphSkeleturo
marci@333
   407
  {
marci@333
   408
  public:
marci@333
   409
    /// Deletes a node.
marci@333
   410
    void erase(Edge n) {}
marci@333
   411
  };
marci@333
   412
marci@333
   413
  /// An empty graph class which provides a function to get the number of its nodes.
marci@325
   414
  
marci@325
   415
  /// This graph class provides a function for getting the number of its 
marci@325
   416
  /// nodes. 
marci@325
   417
  /// Clearly, for physical graph structures it can be expected to have such a 
marci@325
   418
  /// function. For wrappers or graphs which are given in an implicit way, 
marci@325
   419
  /// the implementation can be circumstantial, that is why this composes a 
marci@325
   420
  /// separate concept.
marci@333
   421
  class NodeCountingGraphSkeleturo : public GraphSkeleturo
marci@325
   422
  {
marci@325
   423
  public:
marci@325
   424
    /// Returns the number of nodes.
marci@325
   425
    int nodeNum() const { return 0;}
marci@325
   426
  };
marci@325
   427
marci@333
   428
  /// An empty graph class which provides a function to get the number of its edges.
marci@325
   429
  
marci@325
   430
  /// This graph class provides a function for getting the number of its 
marci@325
   431
  /// edges. 
marci@325
   432
  /// Clearly, for physical graph structures it can be expected to have such a 
marci@325
   433
  /// function. For wrappers or graphs which are given in an implicit way, 
marci@325
   434
  /// the implementation can be circumstantial, that is why this composes a 
marci@325
   435
  /// separate concept.
marci@333
   436
  class EdgeCountingGraphSkeleturo : public GraphSkeleturo
marci@325
   437
  {
marci@325
   438
  public:
marci@325
   439
    /// Returns the number of edges.
marci@325
   440
    int edgeNum() const { return 0;}
marci@325
   441
  };
marci@332
   442
  
marci@332
   443
  /// @}
marci@325
   444
marci@325
   445
} //namespace hugo
marci@325
   446
marci@325
   447
marci@332
   448
marci@332
   449
// class EmptyBipGraph : public Graph Skeleturo
marci@325
   450
// {
marci@325
   451
//   class ANode {};
marci@325
   452
//   class BNode {};
marci@325
   453
marci@325
   454
//   ANode &next(ANode &) {}
marci@325
   455
//   BNode &next(BNode &) {}
marci@325
   456
marci@325
   457
//   ANode &getFirst(ANode &) const {}
marci@325
   458
//   BNode &getFirst(BNode &) const {}
marci@325
   459
marci@325
   460
//   enum NodeClass { A = 0, B = 1 };
marci@325
   461
//   NodeClass getClass(Node n) {}
marci@325
   462
marci@325
   463
// }
marci@325
   464
marci@325
   465
#endif // HUGO_GRAPH_H