src/lemon/concept/undir_graph.h
author alpar
Sun, 16 Jan 2005 22:27:34 +0000
changeset 1082 e9eae612f01c
parent 1022 567f392d1d2e
child 1158 29961fa390a3
permissions -rw-r--r--
findEdge bugfix.
klao@962
     1
/* -*- C++ -*-
klao@962
     2
 *
klao@962
     3
 * src/lemon/concept/undir_graph_component.h - Part of LEMON, a generic
klao@962
     4
 * C++ optimization library
klao@962
     5
 *
klao@962
     6
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi
klao@962
     7
 * Kutatocsoport (Egervary Combinatorial Optimization Research Group,
klao@962
     8
 * EGRES).
klao@962
     9
 *
klao@962
    10
 * Permission to use, modify and distribute this software is granted
klao@962
    11
 * provided that this copyright notice appears in all copies. For
klao@962
    12
 * precise terms see the accompanying LICENSE file.
klao@962
    13
 *
klao@962
    14
 * This software is provided "AS IS" with no warranty of any kind,
klao@962
    15
 * express or implied, and with no claim as to its suitability for any
klao@962
    16
 * purpose.
klao@962
    17
 *
klao@962
    18
 */
klao@962
    19
klao@1030
    20
///\ingroup graph_concepts
klao@962
    21
///\file
klao@962
    22
///\brief Undirected graphs and components of.
klao@962
    23
klao@962
    24
klao@962
    25
#ifndef LEMON_CONCEPT_UNDIR_GRAPH_H
klao@962
    26
#define LEMON_CONCEPT_UNDIR_GRAPH_H
klao@962
    27
klao@962
    28
#include <lemon/concept/graph_component.h>
klao@962
    29
klao@962
    30
namespace lemon {
klao@962
    31
  namespace concept {
klao@962
    32
klao@1030
    33
    /// \addtogroup graph_concepts
klao@1030
    34
    /// @{
klao@1030
    35
klao@1030
    36
klao@1030
    37
    /// Skeleton class which describes an edge with direction in \ref
klao@1030
    38
    /// UndirGraph "undirected graph".
klao@1030
    39
    template <typename UndirEdge>
klao@1030
    40
    class UndirGraphEdge : public UndirEdge {
klao@1030
    41
    public:
klao@1030
    42
klao@1030
    43
      /// \e
klao@1030
    44
      UndirGraphEdge() {}
klao@1030
    45
klao@1030
    46
      /// \e
klao@1030
    47
      UndirGraphEdge(const UndirGraphEdge&) {}
klao@1030
    48
klao@1030
    49
      /// \e
klao@1030
    50
      UndirGraphEdge(Invalid) {}
klao@1030
    51
klao@1030
    52
      /// \brief Constructs a directed version of an undirected edge
klao@1030
    53
      ///
klao@1030
    54
      /// \param forward If \c true the direction of the contructed edge
klao@1030
    55
      /// is the same as the inherent direction of the \c undir_edge; if
klao@1030
    56
      /// \c false --- the opposite.
klao@1030
    57
      UndirGraphEdge(UndirEdge undir_edge, bool forward) {
klao@1030
    58
	ignore_unused_variable_warning(undir_edge);
klao@1030
    59
	ignore_unused_variable_warning(forward);
klao@1030
    60
      }
klao@1030
    61
klao@1030
    62
      /// \e
klao@1030
    63
      UndirGraphEdge& operator=(UndirGraphEdge) { return *this; }
klao@1030
    64
klao@1030
    65
      /// \e
klao@1030
    66
      bool operator==(UndirGraphEdge) const { return true; }
klao@1030
    67
      /// \e
klao@1030
    68
      bool operator!=(UndirGraphEdge) const { return false; }
klao@1030
    69
klao@1030
    70
      /// \e
klao@1030
    71
      bool operator<(UndirGraphEdge) const { return false; }
klao@1030
    72
klao@1030
    73
      template <typename Edge>
klao@1030
    74
      struct Constraints {
klao@1030
    75
	void constraints() {
klao@1030
    76
	  /// \bug This should be is_base_and_derived ...
klao@1030
    77
	  UndirEdge ue = e;
klao@1030
    78
	  ue = e;
klao@1030
    79
	  Edge forward(ue, true);
klao@1030
    80
	  Edge backward(ue, false);
klao@1030
    81
klao@1030
    82
	  ignore_unused_variable_warning(forward);
klao@1030
    83
	  ignore_unused_variable_warning(backward);
klao@1030
    84
	}
klao@1030
    85
	Edge e;
klao@1030
    86
      };
klao@1030
    87
    };
klao@1030
    88
    
klao@962
    89
klao@962
    90
    struct BaseIterableUndirGraphConcept {
deba@989
    91
klao@1022
    92
      template <typename Graph>
klao@1022
    93
      struct Constraints {
klao@962
    94
klao@1022
    95
	typedef typename Graph::UndirEdge UndirEdge;
klao@1022
    96
	typedef typename Graph::Edge Edge;
klao@1022
    97
	typedef typename Graph::Node Node;
klao@962
    98
klao@1022
    99
	void constraints() {
klao@1022
   100
	  checkConcept<BaseIterableGraphComponent, Graph>();
klao@1030
   101
	  checkConcept<GraphItem<>, UndirEdge>();
klao@1030
   102
	  checkConcept<UndirGraphEdge<UndirEdge>, Edge>();
klao@962
   103
klao@1030
   104
	  graph.first(ue);
klao@1030
   105
	  graph.next(ue);
klao@1022
   106
klao@1030
   107
	  const_constraints();
klao@1030
   108
	}
klao@1030
   109
	void const_constraints() {
klao@1022
   110
	  Node n;
klao@1022
   111
	  n = graph.target(ue);
klao@1022
   112
	  n = graph.source(ue);
klao@1030
   113
	  n = graph.oppositeNode(n0, ue);
klao@1022
   114
klao@1030
   115
	  bool b;
klao@1030
   116
	  b = graph.forward(e);
klao@1030
   117
	  ignore_unused_variable_warning(b);
klao@1022
   118
	}
klao@1030
   119
klao@1030
   120
	Graph graph;
klao@1022
   121
	Edge e;
klao@1030
   122
	Node n0;
klao@1030
   123
	UndirEdge ue;
klao@1022
   124
      };
klao@1022
   125
klao@962
   126
    };
klao@962
   127
klao@1022
   128
klao@962
   129
    struct IterableUndirGraphConcept {
klao@962
   130
klao@1022
   131
      template <typename Graph>
klao@1022
   132
      struct Constraints {
klao@1022
   133
	void constraints() {
klao@1022
   134
	  /// \todo we don't need the iterable component to be base iterable
klao@1022
   135
	  /// Don't we really???
klao@1022
   136
	  //checkConcept< BaseIterableUndirGraphConcept, Graph > ();
klao@962
   137
klao@1022
   138
	  checkConcept<IterableGraphComponent, Graph> ();
klao@1021
   139
klao@1022
   140
	  typedef typename Graph::UndirEdge UndirEdge;
klao@1022
   141
	  typedef typename Graph::UndirEdgeIt UndirEdgeIt;
klao@1030
   142
	  typedef typename Graph::IncEdgeIt IncEdgeIt;
klao@1022
   143
klao@1022
   144
	  checkConcept<GraphIterator<Graph, UndirEdge>, UndirEdgeIt>();
klao@1030
   145
	  checkConcept<GraphIncIterator<Graph, UndirEdge>, IncEdgeIt>();
klao@1022
   146
	}
klao@1022
   147
      };
klao@1022
   148
klao@1022
   149
    };
klao@1022
   150
klao@1022
   151
    struct MappableUndirGraphConcept {
klao@1022
   152
klao@1022
   153
      template <typename Graph>
klao@1022
   154
      struct Constraints {
klao@1022
   155
klao@1022
   156
	struct Dummy {
klao@1022
   157
	  int value;
klao@1022
   158
	  Dummy() : value(0) {}
klao@1022
   159
	  Dummy(int _v) : value(_v) {}
klao@1022
   160
	};
klao@1022
   161
klao@1022
   162
	void constraints() {
klao@1022
   163
	  checkConcept<MappableGraphComponent, Graph>();
klao@1022
   164
klao@1022
   165
	  typedef typename Graph::template UndirEdgeMap<int> IntMap;
klao@1022
   166
	  checkConcept<GraphMap<Graph, typename Graph::UndirEdge, int>,
klao@1022
   167
	    IntMap >();
klao@1022
   168
klao@1022
   169
	  typedef typename Graph::template UndirEdgeMap<bool> BoolMap;
klao@1022
   170
	  checkConcept<GraphMap<Graph, typename Graph::UndirEdge, bool>,
klao@1022
   171
	    BoolMap >();
klao@1022
   172
klao@1022
   173
	  typedef typename Graph::template UndirEdgeMap<Dummy> DummyMap;
klao@1022
   174
	  checkConcept<GraphMap<Graph, typename Graph::UndirEdge, Dummy>,
klao@1022
   175
	    DummyMap >();
klao@1022
   176
	}
klao@1022
   177
      };
klao@1022
   178
klao@1022
   179
    };
klao@1022
   180
klao@1022
   181
    struct ExtendableUndirGraphConcept {
klao@1022
   182
klao@1022
   183
      template <typename Graph>
klao@1022
   184
      struct Constraints {
klao@1022
   185
	void constraints() {
klao@1022
   186
	  node_a = graph.addNode();
klao@1022
   187
	  uedge = graph.addEdge(node_a, node_b);
klao@1022
   188
	}
klao@1022
   189
	typename Graph::Node node_a, node_b;
klao@1022
   190
	typename Graph::UndirEdge uedge;
klao@1022
   191
	Graph graph;
klao@1022
   192
      };
klao@1022
   193
klao@1022
   194
    };
klao@1022
   195
klao@1022
   196
    struct ErasableUndirGraphConcept {
klao@1022
   197
klao@1022
   198
      template <typename Graph>
klao@1022
   199
      struct Constraints {
klao@1022
   200
	void constraints() {
klao@1022
   201
	  graph.erase(n);
klao@1022
   202
	  graph.erase(e);
klao@1022
   203
	}
klao@1022
   204
	Graph graph;
klao@1022
   205
	typename Graph::Node n;
klao@1022
   206
	typename Graph::UndirEdge e;
klao@1022
   207
      };
klao@1022
   208
klao@1022
   209
    };
klao@1022
   210
klao@1030
   211
    /// Class describing the concept of Undirected Graphs.
klao@1030
   212
klao@1030
   213
    /// This class describes the common interface of all Undirected
klao@1030
   214
    /// Graphs.
klao@1030
   215
    ///
klao@1030
   216
    /// As all concept describing classes it provides only interface
klao@1030
   217
    /// without any sensible implementation. So any algorithm for
klao@1030
   218
    /// undirected graph should compile with this class, but it will not
klao@1030
   219
    /// run properly, of couse.
klao@1030
   220
    ///
klao@1030
   221
    /// In LEMON undirected graphs also fulfill the concept of directed
klao@1030
   222
    /// graphs (\ref lemon::concept::Graph "Graph Concept"). For
klao@1030
   223
    /// explanation of this and more see also the page \ref undir_graphs,
klao@1030
   224
    /// a tutorial about undirected graphs.
klao@1030
   225
klao@1022
   226
    class UndirGraph {
klao@1022
   227
    public:
klao@1022
   228
klao@1030
   229
      /// Type describing a node in the graph
klao@1030
   230
      typedef GraphNode Node;
klao@1030
   231
klao@1030
   232
      /// Type describing an undirected edge
klao@1030
   233
      typedef GraphItem<'u'> UndirEdge;
klao@1030
   234
klao@1030
   235
      /// Type describing an UndirEdge with direction
klao@1030
   236
#ifndef DOXYGEN
klao@1030
   237
      typedef UndirGraphEdge<UndirEdge> Edge;
klao@1030
   238
#else
klao@1030
   239
      typedef UndirGraphEdge Edge;
klao@1030
   240
#endif
klao@1030
   241
klao@1030
   242
      /// Iterator type which iterates over all nodes
klao@1030
   243
#ifndef DOXYGEN
klao@1030
   244
      typedef GraphIterator<UndirGraph, Node> NodeIt;
klao@1030
   245
#else
klao@1030
   246
      typedef GraphIterator NodeIt;
klao@1030
   247
#endif
klao@1030
   248
klao@1030
   249
      /// Iterator type which iterates over all undirected edges
klao@1030
   250
#ifndef DOXYGEN
klao@1030
   251
      typedef GraphIterator<UndirGraph, UndirEdge> UndirEdgeIt;
klao@1030
   252
#else
klao@1030
   253
      typedef GraphIterator UndirEdgeIt;
klao@1030
   254
#endif
klao@1030
   255
klao@1030
   256
      /// Iterator type which iterates over all directed edges.
klao@1030
   257
klao@1030
   258
      /// Iterator type which iterates over all edges (each undirected
klao@1030
   259
      /// edge occurs twice with both directions.
klao@1030
   260
#ifndef DOXYGEN
klao@1030
   261
      typedef GraphIterator<UndirGraph, Edge> EdgeIt;
klao@1030
   262
#else
klao@1030
   263
      typedef GraphIterator EdgeIt;
klao@1030
   264
#endif
klao@1030
   265
klao@1030
   266
klao@1030
   267
      /// Iterator of undirected edges incident to a node
klao@1030
   268
#ifndef DOXYGEN
klao@1030
   269
      typedef GraphIncIterator<UndirGraph, UndirEdge, 'u'> IncEdgeIt;
klao@1030
   270
#else
klao@1030
   271
      typedef GraphIncIterator IncEdgeIt;
klao@1030
   272
#endif
klao@1030
   273
klao@1030
   274
      /// Iterator of edges incoming to a node
klao@1030
   275
#ifndef DOXYGEN
klao@1030
   276
      typedef GraphIncIterator<UndirGraph, Edge, 'i'> InEdgeIt;
klao@1030
   277
#else
klao@1030
   278
      typedef GraphIncIterator InEdgeIt;
klao@1030
   279
#endif
klao@1030
   280
klao@1030
   281
      /// Iterator of edges outgoing from a node
klao@1030
   282
#ifndef DOXYGEN
klao@1030
   283
      typedef GraphIncIterator<UndirGraph, Edge, 'o'> OutEdgeIt;
klao@1030
   284
#else
klao@1030
   285
      typedef GraphIncIterator OutEdgeIt;
klao@1030
   286
#endif
klao@1030
   287
klao@1030
   288
      /// NodeMap template
klao@1030
   289
#ifdef DOXYGEN
klao@1030
   290
      typedef GraphMap NodeMap<T>;
klao@1030
   291
#endif
klao@1030
   292
klao@1030
   293
      /// UndirEdgeMap template
klao@1030
   294
#ifdef DOXYGEN
klao@1030
   295
      typedef GraphMap UndirEdgeMap<T>;
klao@1030
   296
#endif
klao@1030
   297
klao@1030
   298
      /// EdgeMap template
klao@1030
   299
#ifdef DOXYGEN
klao@1030
   300
      typedef GraphMap EdgeMap<T>;
klao@1030
   301
#endif
klao@1030
   302
klao@1030
   303
      template <typename T>
klao@1030
   304
      class NodeMap : public GraphMap<UndirGraph, Node, T> {
klao@1030
   305
	typedef GraphMap<UndirGraph, Node, T> Parent;
klao@1030
   306
      public:
klao@1030
   307
klao@1030
   308
	explicit NodeMap(const UndirGraph &g) : Parent(g) {}
klao@1030
   309
	NodeMap(const UndirGraph &g, T t) : Parent(g, t) {}
klao@1030
   310
      };
klao@1030
   311
klao@1030
   312
      template <typename T>
klao@1030
   313
      class UndirEdgeMap : public GraphMap<UndirGraph, UndirEdge, T> {
klao@1030
   314
	typedef GraphMap<UndirGraph, UndirEdge, T> Parent;
klao@1030
   315
      public:
klao@1030
   316
klao@1030
   317
	explicit UndirEdgeMap(const UndirGraph &g) : Parent(g) {}
klao@1030
   318
	UndirEdgeMap(const UndirGraph &g, T t) : Parent(g, t) {}
klao@1030
   319
      };
klao@1030
   320
klao@1030
   321
      template <typename T>
klao@1030
   322
      class EdgeMap : public GraphMap<UndirGraph, Edge, T> {
klao@1030
   323
	typedef GraphMap<UndirGraph, Edge, T> Parent;
klao@1030
   324
      public:
klao@1030
   325
klao@1030
   326
	explicit EdgeMap(const UndirGraph &g) : Parent(g) {}
klao@1030
   327
	EdgeMap(const UndirGraph &g, T t) : Parent(g, t) {}
klao@1030
   328
      };
klao@1030
   329
klao@1030
   330
      /// Is the Edge oriented "forward"?
klao@1030
   331
klao@1030
   332
      /// Returns whether the given directed edge is same orientation as
klao@1030
   333
      /// the corresponding undirected edge.
klao@1030
   334
      ///
klao@1030
   335
      /// \todo "What does the direction of an undirected edge mean?"
klao@1030
   336
      bool forward(Edge) const { return true; }
klao@1030
   337
klao@1030
   338
      /// Opposite node on an edge
klao@1030
   339
klao@1030
   340
      /// \return the opposite of the given Node on the given Edge
klao@1030
   341
      ///
klao@1030
   342
      /// \todo What should we do if given Node and Edge are not incident?
klao@1030
   343
      Node oppositeNode(Node, UndirEdge) const { return INVALID; }
klao@1030
   344
klao@1030
   345
      /// First node of the undirected edge.
klao@1030
   346
klao@1030
   347
      /// \return the first node of the given UndirEdge.
klao@1030
   348
      ///
klao@1030
   349
      /// Naturally undirectected edges don't have direction and thus
klao@1030
   350
      /// don't have source and target node. But we use these two methods
klao@1030
   351
      /// to query the two endnodes of the edge. The direction of the edge
klao@1030
   352
      /// which arises this way is called the inherent direction of the
klao@1030
   353
      /// undirected edge, and is used to define the "forward" direction
klao@1030
   354
      /// of the directed versions of the edges.
klao@1030
   355
      /// \sa forward
klao@1030
   356
      Node source(UndirEdge) const { return INVALID; }
klao@1030
   357
klao@1030
   358
      /// Second node of the undirected edge.
klao@1030
   359
      Node target(UndirEdge) const { return INVALID; }
klao@1030
   360
klao@1030
   361
      /// Source node of the directed edge.
klao@1030
   362
      Node source(Edge) const { return INVALID; }
klao@1030
   363
klao@1030
   364
      /// Target node of the directed edge.
klao@1030
   365
      Node target(Edge) const { return INVALID; }
klao@1030
   366
klao@1030
   367
      /// First node of the graph
klao@1030
   368
klao@1030
   369
      /// \note This method is part of so called \ref
klao@1030
   370
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   371
      /// be used in an end-user program.
klao@1030
   372
      void first(Node&) const {}
klao@1030
   373
      /// Next node of the graph
klao@1030
   374
klao@1030
   375
      /// \note This method is part of so called \ref
klao@1030
   376
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   377
      /// be used in an end-user program.
klao@1030
   378
      void next(Node&) const {}
klao@1030
   379
klao@1030
   380
      /// First undirected edge of the graph
klao@1030
   381
klao@1030
   382
      /// \note This method is part of so called \ref
klao@1030
   383
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   384
      /// be used in an end-user program.
klao@1030
   385
      void first(UndirEdge&) const {}
klao@1030
   386
      /// Next undirected edge of the graph
klao@1030
   387
klao@1030
   388
      /// \note This method is part of so called \ref
klao@1030
   389
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   390
      /// be used in an end-user program.
klao@1030
   391
      void next(UndirEdge&) const {}
klao@1030
   392
klao@1030
   393
      /// First directed edge of the graph
klao@1030
   394
klao@1030
   395
      /// \note This method is part of so called \ref
klao@1030
   396
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   397
      /// be used in an end-user program.
klao@1030
   398
      void first(Edge&) const {}
klao@1030
   399
      /// Next directed edge of the graph
klao@1030
   400
klao@1030
   401
      /// \note This method is part of so called \ref
klao@1030
   402
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   403
      /// be used in an end-user program.
klao@1030
   404
      void next(Edge&) const {}
klao@1030
   405
klao@1030
   406
      /// First outgoing edge from a given node
klao@1030
   407
klao@1030
   408
      /// \note This method is part of so called \ref
klao@1030
   409
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   410
      /// be used in an end-user program.
klao@1030
   411
      void firstOut(Edge&, Node) const {}
klao@1030
   412
      /// Next outgoing edge to a node
klao@1030
   413
klao@1030
   414
      /// \note This method is part of so called \ref
klao@1030
   415
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   416
      /// be used in an end-user program.
klao@1030
   417
      void nextOut(Edge&) const {}
klao@1030
   418
klao@1030
   419
      /// First incoming edge to a given node
klao@1030
   420
klao@1030
   421
      /// \note This method is part of so called \ref
klao@1030
   422
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   423
      /// be used in an end-user program.
klao@1030
   424
      void firstIn(Edge&, Node) const {}
klao@1030
   425
      /// Next incoming edge to a node
klao@1030
   426
klao@1030
   427
      /// \note This method is part of so called \ref
klao@1030
   428
      /// developpers_interface "Developpers' interface", so it shouldn't
klao@1030
   429
      /// be used in an end-user program.
klao@1030
   430
      void nextIn(Edge&) const {}
klao@1030
   431
klao@1030
   432
klao@1022
   433
      template <typename Graph>
klao@1022
   434
      struct Constraints {
klao@1022
   435
	void constraints() {
klao@1022
   436
	  checkConcept<BaseIterableUndirGraphConcept, Graph>();
klao@1022
   437
	  checkConcept<IterableUndirGraphConcept, Graph>();
klao@1022
   438
	  checkConcept<MappableUndirGraphConcept, Graph>();
klao@1022
   439
	}
klao@1022
   440
      };
klao@1022
   441
klao@1022
   442
    };
klao@1022
   443
klao@1022
   444
    class ExtendableUndirGraph : public UndirGraph {
klao@1022
   445
    public:
klao@1022
   446
klao@1022
   447
      template <typename Graph>
klao@1022
   448
      struct Constraints {
klao@1022
   449
	void constraints() {
klao@1022
   450
	  checkConcept<BaseIterableUndirGraphConcept, Graph>();
klao@1022
   451
	  checkConcept<IterableUndirGraphConcept, Graph>();
klao@1022
   452
	  checkConcept<MappableUndirGraphConcept, Graph>();
klao@1022
   453
klao@1022
   454
	  checkConcept<UndirGraph, Graph>();
klao@1022
   455
	  checkConcept<ExtendableUndirGraphConcept, Graph>();
klao@1022
   456
	  checkConcept<ClearableGraphComponent, Graph>();
klao@1022
   457
	}
klao@1022
   458
      };
klao@1022
   459
klao@1022
   460
    };
klao@1022
   461
klao@1022
   462
    class ErasableUndirGraph : public ExtendableUndirGraph {
klao@1022
   463
    public:
klao@1022
   464
klao@1022
   465
      template <typename Graph>
klao@1022
   466
      struct Constraints {
klao@1022
   467
	void constraints() {
klao@1022
   468
	  checkConcept<ExtendableUndirGraph, Graph>();
klao@1022
   469
	  checkConcept<ErasableUndirGraphConcept, Graph>();
klao@1022
   470
	}
klao@1022
   471
      };
klao@1022
   472
klao@962
   473
    };
klao@962
   474
klao@1030
   475
    /// @}
klao@1030
   476
klao@962
   477
  }
klao@962
   478
klao@962
   479
}
klao@962
   480
klao@962
   481
#endif