lemon/euler.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 07 Mar 2010 09:42:47 +0000
branch1.1
changeset 720 86a880ba752d
parent 584 2ebfdb89ec66
child 761 f1398882a928
permissions -rw-r--r--
Update NEWS file
alpar@504
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@504
     2
 *
alpar@504
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@504
     4
 *
alpar@504
     5
 * Copyright (C) 2003-2009
alpar@504
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@504
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@504
     8
 *
alpar@504
     9
 * Permission to use, modify and distribute this software is granted
alpar@504
    10
 * provided that this copyright notice appears in all copies. For
alpar@504
    11
 * precise terms see the accompanying LICENSE file.
alpar@504
    12
 *
alpar@504
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@504
    14
 * express or implied, and with no claim as to its suitability for any
alpar@504
    15
 * purpose.
alpar@504
    16
 *
alpar@504
    17
 */
alpar@504
    18
alpar@504
    19
#ifndef LEMON_EULER_H
alpar@504
    20
#define LEMON_EULER_H
alpar@504
    21
alpar@504
    22
#include<lemon/core.h>
alpar@504
    23
#include<lemon/adaptors.h>
alpar@504
    24
#include<lemon/connectivity.h>
alpar@504
    25
#include <list>
alpar@504
    26
kpeter@578
    27
/// \ingroup graph_properties
alpar@504
    28
/// \file
kpeter@584
    29
/// \brief Euler tour iterators and a function for checking the \e Eulerian 
kpeter@584
    30
/// property.
alpar@504
    31
///
kpeter@584
    32
///This file provides Euler tour iterators and a function to check
kpeter@584
    33
///if a (di)graph is \e Eulerian.
alpar@504
    34
alpar@504
    35
namespace lemon {
alpar@504
    36
kpeter@584
    37
  ///Euler tour iterator for digraphs.
alpar@504
    38
kpeter@584
    39
  /// \ingroup graph_prop
kpeter@584
    40
  ///This iterator provides an Euler tour (Eulerian circuit) of a \e directed
kpeter@584
    41
  ///graph (if there exists) and it converts to the \c Arc type of the digraph.
alpar@504
    42
  ///
kpeter@584
    43
  ///For example, if the given digraph has an Euler tour (i.e it has only one
kpeter@584
    44
  ///non-trivial component and the in-degree is equal to the out-degree 
kpeter@584
    45
  ///for all nodes), then the following code will put the arcs of \c g
kpeter@584
    46
  ///to the vector \c et according to an Euler tour of \c g.
alpar@504
    47
  ///\code
alpar@504
    48
  ///  std::vector<ListDigraph::Arc> et;
kpeter@584
    49
  ///  for(DiEulerIt<ListDigraph> e(g); e!=INVALID; ++e)
alpar@504
    50
  ///    et.push_back(e);
alpar@504
    51
  ///\endcode
kpeter@584
    52
  ///If \c g has no Euler tour, then the resulted walk will not be closed
kpeter@584
    53
  ///or not contain all arcs.
alpar@504
    54
  ///\sa EulerIt
kpeter@550
    55
  template<typename GR>
alpar@504
    56
  class DiEulerIt
alpar@504
    57
  {
kpeter@550
    58
    typedef typename GR::Node Node;
kpeter@550
    59
    typedef typename GR::NodeIt NodeIt;
kpeter@550
    60
    typedef typename GR::Arc Arc;
kpeter@550
    61
    typedef typename GR::ArcIt ArcIt;
kpeter@550
    62
    typedef typename GR::OutArcIt OutArcIt;
kpeter@550
    63
    typedef typename GR::InArcIt InArcIt;
alpar@504
    64
kpeter@550
    65
    const GR &g;
kpeter@584
    66
    typename GR::template NodeMap<OutArcIt> narc;
alpar@504
    67
    std::list<Arc> euler;
alpar@504
    68
alpar@504
    69
  public:
alpar@504
    70
alpar@504
    71
    ///Constructor
alpar@504
    72
kpeter@584
    73
    ///Constructor.
kpeter@550
    74
    ///\param gr A digraph.
kpeter@584
    75
    ///\param start The starting point of the tour. If it is not given,
kpeter@584
    76
    ///the tour will start from the first node that has an outgoing arc.
kpeter@550
    77
    DiEulerIt(const GR &gr, typename GR::Node start = INVALID)
kpeter@584
    78
      : g(gr), narc(g)
alpar@504
    79
    {
kpeter@583
    80
      if (start==INVALID) {
kpeter@583
    81
        NodeIt n(g);
kpeter@583
    82
        while (n!=INVALID && OutArcIt(g,n)==INVALID) ++n;
kpeter@583
    83
        start=n;
kpeter@583
    84
      }
kpeter@583
    85
      if (start!=INVALID) {
kpeter@584
    86
        for (NodeIt n(g); n!=INVALID; ++n) narc[n]=OutArcIt(g,n);
kpeter@584
    87
        while (narc[start]!=INVALID) {
kpeter@584
    88
          euler.push_back(narc[start]);
kpeter@584
    89
          Node next=g.target(narc[start]);
kpeter@584
    90
          ++narc[start];
kpeter@583
    91
          start=next;
kpeter@583
    92
        }
alpar@504
    93
      }
alpar@504
    94
    }
alpar@504
    95
kpeter@584
    96
    ///Arc conversion
alpar@504
    97
    operator Arc() { return euler.empty()?INVALID:euler.front(); }
kpeter@584
    98
    ///Compare with \c INVALID
alpar@504
    99
    bool operator==(Invalid) { return euler.empty(); }
kpeter@584
   100
    ///Compare with \c INVALID
alpar@504
   101
    bool operator!=(Invalid) { return !euler.empty(); }
alpar@504
   102
alpar@504
   103
    ///Next arc of the tour
kpeter@584
   104
kpeter@584
   105
    ///Next arc of the tour
kpeter@584
   106
    ///
alpar@504
   107
    DiEulerIt &operator++() {
alpar@504
   108
      Node s=g.target(euler.front());
alpar@504
   109
      euler.pop_front();
alpar@504
   110
      typename std::list<Arc>::iterator next=euler.begin();
kpeter@584
   111
      while(narc[s]!=INVALID) {
kpeter@584
   112
        euler.insert(next,narc[s]);
kpeter@584
   113
        Node n=g.target(narc[s]);
kpeter@584
   114
        ++narc[s];
alpar@504
   115
        s=n;
alpar@504
   116
      }
alpar@504
   117
      return *this;
alpar@504
   118
    }
alpar@504
   119
    ///Postfix incrementation
alpar@504
   120
kpeter@584
   121
    /// Postfix incrementation.
kpeter@584
   122
    ///
alpar@504
   123
    ///\warning This incrementation
kpeter@584
   124
    ///returns an \c Arc, not a \ref DiEulerIt, as one may
alpar@504
   125
    ///expect.
alpar@504
   126
    Arc operator++(int)
alpar@504
   127
    {
alpar@504
   128
      Arc e=*this;
alpar@504
   129
      ++(*this);
alpar@504
   130
      return e;
alpar@504
   131
    }
alpar@504
   132
  };
alpar@504
   133
kpeter@584
   134
  ///Euler tour iterator for graphs.
alpar@504
   135
kpeter@578
   136
  /// \ingroup graph_properties
kpeter@584
   137
  ///This iterator provides an Euler tour (Eulerian circuit) of an
kpeter@584
   138
  ///\e undirected graph (if there exists) and it converts to the \c Arc
kpeter@584
   139
  ///and \c Edge types of the graph.
alpar@504
   140
  ///
kpeter@584
   141
  ///For example, if the given graph has an Euler tour (i.e it has only one 
kpeter@584
   142
  ///non-trivial component and the degree of each node is even),
alpar@504
   143
  ///the following code will print the arc IDs according to an
alpar@504
   144
  ///Euler tour of \c g.
alpar@504
   145
  ///\code
kpeter@584
   146
  ///  for(EulerIt<ListGraph> e(g); e!=INVALID; ++e) {
alpar@504
   147
  ///    std::cout << g.id(Edge(e)) << std::eol;
alpar@504
   148
  ///  }
alpar@504
   149
  ///\endcode
kpeter@584
   150
  ///Although this iterator is for undirected graphs, it still returns 
kpeter@584
   151
  ///arcs in order to indicate the direction of the tour.
kpeter@584
   152
  ///(But arcs convert to edges, of course.)
alpar@504
   153
  ///
kpeter@584
   154
  ///If \c g has no Euler tour, then the resulted walk will not be closed
kpeter@584
   155
  ///or not contain all edges.
kpeter@550
   156
  template<typename GR>
alpar@504
   157
  class EulerIt
alpar@504
   158
  {
kpeter@550
   159
    typedef typename GR::Node Node;
kpeter@550
   160
    typedef typename GR::NodeIt NodeIt;
kpeter@550
   161
    typedef typename GR::Arc Arc;
kpeter@550
   162
    typedef typename GR::Edge Edge;
kpeter@550
   163
    typedef typename GR::ArcIt ArcIt;
kpeter@550
   164
    typedef typename GR::OutArcIt OutArcIt;
kpeter@550
   165
    typedef typename GR::InArcIt InArcIt;
alpar@504
   166
kpeter@550
   167
    const GR &g;
kpeter@584
   168
    typename GR::template NodeMap<OutArcIt> narc;
kpeter@550
   169
    typename GR::template EdgeMap<bool> visited;
alpar@504
   170
    std::list<Arc> euler;
alpar@504
   171
alpar@504
   172
  public:
alpar@504
   173
alpar@504
   174
    ///Constructor
alpar@504
   175
kpeter@584
   176
    ///Constructor.
kpeter@584
   177
    ///\param gr A graph.
kpeter@584
   178
    ///\param start The starting point of the tour. If it is not given,
kpeter@584
   179
    ///the tour will start from the first node that has an incident edge.
kpeter@550
   180
    EulerIt(const GR &gr, typename GR::Node start = INVALID)
kpeter@584
   181
      : g(gr), narc(g), visited(g, false)
alpar@504
   182
    {
kpeter@583
   183
      if (start==INVALID) {
kpeter@583
   184
        NodeIt n(g);
kpeter@583
   185
        while (n!=INVALID && OutArcIt(g,n)==INVALID) ++n;
kpeter@583
   186
        start=n;
kpeter@583
   187
      }
kpeter@583
   188
      if (start!=INVALID) {
kpeter@584
   189
        for (NodeIt n(g); n!=INVALID; ++n) narc[n]=OutArcIt(g,n);
kpeter@584
   190
        while(narc[start]!=INVALID) {
kpeter@584
   191
          euler.push_back(narc[start]);
kpeter@584
   192
          visited[narc[start]]=true;
kpeter@584
   193
          Node next=g.target(narc[start]);
kpeter@584
   194
          ++narc[start];
kpeter@583
   195
          start=next;
kpeter@584
   196
          while(narc[start]!=INVALID && visited[narc[start]]) ++narc[start];
kpeter@583
   197
        }
alpar@504
   198
      }
alpar@504
   199
    }
alpar@504
   200
kpeter@584
   201
    ///Arc conversion
alpar@504
   202
    operator Arc() const { return euler.empty()?INVALID:euler.front(); }
kpeter@584
   203
    ///Edge conversion
alpar@504
   204
    operator Edge() const { return euler.empty()?INVALID:euler.front(); }
kpeter@584
   205
    ///Compare with \c INVALID
alpar@504
   206
    bool operator==(Invalid) const { return euler.empty(); }
kpeter@584
   207
    ///Compare with \c INVALID
alpar@504
   208
    bool operator!=(Invalid) const { return !euler.empty(); }
alpar@504
   209
alpar@504
   210
    ///Next arc of the tour
kpeter@584
   211
kpeter@584
   212
    ///Next arc of the tour
kpeter@584
   213
    ///
alpar@504
   214
    EulerIt &operator++() {
alpar@504
   215
      Node s=g.target(euler.front());
alpar@504
   216
      euler.pop_front();
alpar@504
   217
      typename std::list<Arc>::iterator next=euler.begin();
kpeter@584
   218
      while(narc[s]!=INVALID) {
kpeter@584
   219
        while(narc[s]!=INVALID && visited[narc[s]]) ++narc[s];
kpeter@584
   220
        if(narc[s]==INVALID) break;
alpar@504
   221
        else {
kpeter@584
   222
          euler.insert(next,narc[s]);
kpeter@584
   223
          visited[narc[s]]=true;
kpeter@584
   224
          Node n=g.target(narc[s]);
kpeter@584
   225
          ++narc[s];
alpar@504
   226
          s=n;
alpar@504
   227
        }
alpar@504
   228
      }
alpar@504
   229
      return *this;
alpar@504
   230
    }
alpar@504
   231
alpar@504
   232
    ///Postfix incrementation
alpar@504
   233
kpeter@584
   234
    /// Postfix incrementation.
kpeter@584
   235
    ///
kpeter@584
   236
    ///\warning This incrementation returns an \c Arc (which converts to 
kpeter@584
   237
    ///an \c Edge), not an \ref EulerIt, as one may expect.
alpar@504
   238
    Arc operator++(int)
alpar@504
   239
    {
alpar@504
   240
      Arc e=*this;
alpar@504
   241
      ++(*this);
alpar@504
   242
      return e;
alpar@504
   243
    }
alpar@504
   244
  };
alpar@504
   245
alpar@504
   246
kpeter@646
   247
  ///Check if the given graph is Eulerian
alpar@504
   248
kpeter@578
   249
  /// \ingroup graph_properties
kpeter@646
   250
  ///This function checks if the given graph is Eulerian.
kpeter@584
   251
  ///It works for both directed and undirected graphs.
kpeter@584
   252
  ///
kpeter@584
   253
  ///By definition, a digraph is called \e Eulerian if
kpeter@584
   254
  ///and only if it is connected and the number of incoming and outgoing
alpar@504
   255
  ///arcs are the same for each node.
alpar@505
   256
  ///Similarly, an undirected graph is called \e Eulerian if
kpeter@584
   257
  ///and only if it is connected and the number of incident edges is even
kpeter@584
   258
  ///for each node.
kpeter@584
   259
  ///
kpeter@584
   260
  ///\note There are (di)graphs that are not Eulerian, but still have an
kpeter@584
   261
  /// Euler tour, since they may contain isolated nodes.
kpeter@584
   262
  ///
kpeter@584
   263
  ///\sa DiEulerIt, EulerIt
kpeter@550
   264
  template<typename GR>
alpar@504
   265
#ifdef DOXYGEN
alpar@504
   266
  bool
alpar@504
   267
#else
kpeter@550
   268
  typename enable_if<UndirectedTagIndicator<GR>,bool>::type
kpeter@550
   269
  eulerian(const GR &g)
alpar@504
   270
  {
kpeter@550
   271
    for(typename GR::NodeIt n(g);n!=INVALID;++n)
alpar@504
   272
      if(countIncEdges(g,n)%2) return false;
alpar@504
   273
    return connected(g);
alpar@504
   274
  }
kpeter@550
   275
  template<class GR>
kpeter@550
   276
  typename disable_if<UndirectedTagIndicator<GR>,bool>::type
alpar@504
   277
#endif
kpeter@550
   278
  eulerian(const GR &g)
alpar@504
   279
  {
kpeter@550
   280
    for(typename GR::NodeIt n(g);n!=INVALID;++n)
alpar@504
   281
      if(countInArcs(g,n)!=countOutArcs(g,n)) return false;
kpeter@584
   282
    return connected(undirector(g));
alpar@504
   283
  }
alpar@504
   284
alpar@504
   285
}
alpar@504
   286
alpar@504
   287
#endif