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