src/lemon/dfs.h
author alpar
Fri, 28 Jan 2005 08:53:48 +0000
changeset 1101 9286569c3749
parent 946 c94ef40a22ce
child 1164 80bb73097736
permissions -rw-r--r--
Wrap a long line
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/dfs.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_DFS_H
alpar@921
    18
#define LEMON_DFS_H
alpar@780
    19
alpar@780
    20
///\ingroup flowalgs
alpar@780
    21
///\file
alpar@781
    22
///\brief %DFS algorithm.
alpar@780
    23
///
alpar@780
    24
///\todo Revise Manual.
alpar@780
    25
klao@946
    26
#include <lemon/graph_utils.h>
alpar@921
    27
#include <lemon/invalid.h>
alpar@780
    28
alpar@921
    29
namespace lemon {
alpar@780
    30
alpar@780
    31
/// \addtogroup flowalgs
alpar@780
    32
/// @{
alpar@780
    33
alpar@781
    34
  ///%DFS algorithm class.
alpar@780
    35
alpar@781
    36
  ///This class provides an efficient implementation of %DFS algorithm.
alpar@780
    37
  ///
alpar@780
    38
  ///\param GR The graph type the algorithm runs on.
alpar@780
    39
  ///
alpar@781
    40
  ///\author Alpar Juttner
alpar@780
    41
alpar@780
    42
#ifdef DOXYGEN
alpar@780
    43
  template <typename GR>
alpar@780
    44
#else
alpar@780
    45
  template <typename GR>
alpar@780
    46
#endif
alpar@780
    47
  class Dfs{
alpar@780
    48
  public:
alpar@780
    49
    ///The type of the underlying graph.
alpar@780
    50
    typedef GR Graph;
alpar@911
    51
    ///\e
alpar@780
    52
    typedef typename Graph::Node Node;
alpar@911
    53
    ///\e
alpar@780
    54
    typedef typename Graph::NodeIt NodeIt;
alpar@911
    55
    ///\e
alpar@780
    56
    typedef typename Graph::Edge Edge;
alpar@911
    57
    ///\e
alpar@780
    58
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@780
    59
    
alpar@780
    60
    ///\brief The type of the map that stores the last
alpar@781
    61
    ///edges of the paths on the %DFS tree.
alpar@780
    62
    typedef typename Graph::template NodeMap<Edge> PredMap;
alpar@780
    63
    ///\brief The type of the map that stores the last but one
alpar@781
    64
    ///nodes of the paths on the %DFS tree.
alpar@780
    65
    typedef typename Graph::template NodeMap<Node> PredNodeMap;
alpar@781
    66
    ///The type of the map that stores the dists of the nodes on the %DFS tree.
alpar@780
    67
    typedef typename Graph::template NodeMap<int> DistMap;
alpar@780
    68
alpar@780
    69
  private:
alpar@802
    70
    /// Pointer to the underlying graph.
alpar@780
    71
    const Graph *G;
alpar@802
    72
    ///Pointer to the map of predecessors edges.
alpar@780
    73
    PredMap *predecessor;
alpar@802
    74
    ///Indicates if \ref predecessor is locally allocated (\c true) or not.
alpar@780
    75
    bool local_predecessor;
alpar@802
    76
    ///Pointer to the map of predecessors nodes.
alpar@780
    77
    PredNodeMap *pred_node;
alpar@802
    78
    ///Indicates if \ref pred_node is locally allocated (\c true) or not.
alpar@780
    79
    bool local_pred_node;
alpar@802
    80
    ///Pointer to the map of distances.
alpar@780
    81
    DistMap *distance;
alpar@802
    82
    ///Indicates if \ref distance is locally allocated (\c true) or not.
alpar@780
    83
    bool local_distance;
alpar@780
    84
alpar@802
    85
    ///The source node of the last execution.
alpar@780
    86
    Node source;
alpar@780
    87
alpar@780
    88
alpar@781
    89
    ///Initializes the maps.
alpar@780
    90
    void init_maps() 
alpar@780
    91
    {
alpar@780
    92
      if(!predecessor) {
alpar@780
    93
	local_predecessor = true;
alpar@780
    94
	predecessor = new PredMap(*G);
alpar@780
    95
      }
alpar@780
    96
      if(!pred_node) {
alpar@780
    97
	local_pred_node = true;
alpar@780
    98
	pred_node = new PredNodeMap(*G);
alpar@780
    99
      }
alpar@780
   100
      if(!distance) {
alpar@780
   101
	local_distance = true;
alpar@780
   102
	distance = new DistMap(*G);
alpar@780
   103
      }
alpar@780
   104
    }
alpar@780
   105
    
alpar@780
   106
  public :    
alpar@802
   107
    ///Constructor.
alpar@802
   108
    
alpar@802
   109
    ///\param _G the graph the algorithm will run on.
alpar@911
   110
    ///
alpar@780
   111
    Dfs(const Graph& _G) :
alpar@780
   112
      G(&_G),
alpar@780
   113
      predecessor(NULL), local_predecessor(false),
alpar@780
   114
      pred_node(NULL), local_pred_node(false),
alpar@780
   115
      distance(NULL), local_distance(false)
alpar@780
   116
    { }
alpar@780
   117
    
alpar@802
   118
    ///Destructor.
alpar@780
   119
    ~Dfs() 
alpar@780
   120
    {
alpar@780
   121
      if(local_predecessor) delete predecessor;
alpar@780
   122
      if(local_pred_node) delete pred_node;
alpar@780
   123
      if(local_distance) delete distance;
alpar@780
   124
    }
alpar@780
   125
alpar@780
   126
    ///Sets the map storing the predecessor edges.
alpar@780
   127
alpar@780
   128
    ///Sets the map storing the predecessor edges.
alpar@780
   129
    ///If you don't use this function before calling \ref run(),
alpar@780
   130
    ///it will allocate one. The destuctor deallocates this
alpar@780
   131
    ///automatically allocated map, of course.
alpar@780
   132
    ///\return <tt> (*this) </tt>
alpar@780
   133
    Dfs &setPredMap(PredMap &m) 
alpar@780
   134
    {
alpar@780
   135
      if(local_predecessor) {
alpar@780
   136
	delete predecessor;
alpar@780
   137
	local_predecessor=false;
alpar@780
   138
      }
alpar@780
   139
      predecessor = &m;
alpar@780
   140
      return *this;
alpar@780
   141
    }
alpar@780
   142
alpar@780
   143
    ///Sets the map storing the predecessor nodes.
alpar@780
   144
alpar@780
   145
    ///Sets the map storing the predecessor nodes.
alpar@780
   146
    ///If you don't use this function before calling \ref run(),
alpar@780
   147
    ///it will allocate one. The destuctor deallocates this
alpar@780
   148
    ///automatically allocated map, of course.
alpar@780
   149
    ///\return <tt> (*this) </tt>
alpar@780
   150
    Dfs &setPredNodeMap(PredNodeMap &m) 
alpar@780
   151
    {
alpar@780
   152
      if(local_pred_node) {
alpar@780
   153
	delete pred_node;
alpar@780
   154
	local_pred_node=false;
alpar@780
   155
      }
alpar@780
   156
      pred_node = &m;
alpar@780
   157
      return *this;
alpar@780
   158
    }
alpar@780
   159
alpar@780
   160
    ///Sets the map storing the distances calculated by the algorithm.
alpar@780
   161
alpar@780
   162
    ///Sets the map storing the distances calculated by the algorithm.
alpar@780
   163
    ///If you don't use this function before calling \ref run(),
alpar@780
   164
    ///it will allocate one. The destuctor deallocates this
alpar@780
   165
    ///automatically allocated map, of course.
alpar@780
   166
    ///\return <tt> (*this) </tt>
alpar@780
   167
    Dfs &setDistMap(DistMap &m) 
alpar@780
   168
    {
alpar@780
   169
      if(local_distance) {
alpar@780
   170
	delete distance;
alpar@780
   171
	local_distance=false;
alpar@780
   172
      }
alpar@780
   173
      distance = &m;
alpar@780
   174
      return *this;
alpar@780
   175
    }
alpar@780
   176
    
alpar@780
   177
  ///Runs %DFS algorithm from node \c s.
alpar@780
   178
alpar@780
   179
  ///This method runs the %DFS algorithm from a root node \c s
alpar@780
   180
  ///in order to
alpar@781
   181
  ///compute 
alpar@781
   182
  ///- a %DFS tree and
alpar@781
   183
  ///- the distance of each node from the root on this tree.
alpar@780
   184
 
alpar@780
   185
    void run(Node s) {
alpar@780
   186
      
alpar@780
   187
      init_maps();
alpar@780
   188
      
alpar@780
   189
      source = s;
alpar@780
   190
      
alpar@780
   191
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@780
   192
	predecessor->set(u,INVALID);
alpar@780
   193
	pred_node->set(u,INVALID);
alpar@780
   194
      }
alpar@780
   195
      
klao@946
   196
      int N = countNodes(*G);
alpar@780
   197
      std::vector<typename Graph::OutEdgeIt> Q(N);
alpar@780
   198
alpar@780
   199
      int Qh=0;
alpar@780
   200
      
klao@946
   201
      Q[Qh] = OutEdgeIt(*G, s);
alpar@780
   202
      distance->set(s, 0);
alpar@780
   203
alpar@780
   204
      Node n=s;
alpar@780
   205
      Node m;
alpar@780
   206
      OutEdgeIt e;
alpar@780
   207
      do {
alpar@780
   208
	if((e=Q[Qh])!=INVALID)
alpar@986
   209
	  if((m=G->target(e))!=s && (*predecessor)[m=G->target(e)]==INVALID) {
alpar@780
   210
	    predecessor->set(m,e);
alpar@780
   211
	    pred_node->set(m,n);
klao@946
   212
	    Q[++Qh] = OutEdgeIt(*G, m);
alpar@780
   213
	    distance->set(m,Qh);
alpar@780
   214
	    n=m;
alpar@780
   215
	  }
alpar@780
   216
	  else ++Q[Qh];
alpar@986
   217
	else if(--Qh>=0) n=G->source(Q[Qh]);
alpar@780
   218
      } while(Qh>=0);
alpar@780
   219
    }
alpar@780
   220
    
alpar@781
   221
    ///The distance of a node from the root on the %DFS tree.
alpar@780
   222
alpar@781
   223
    ///Returns the distance of a node from the root on the %DFS tree.
alpar@780
   224
    ///\pre \ref run() must be called before using this function.
alpar@780
   225
    ///\warning If node \c v in unreachable from the root the return value
alpar@780
   226
    ///of this funcion is undefined.
alpar@780
   227
    int dist(Node v) const { return (*distance)[v]; }
alpar@780
   228
alpar@781
   229
    ///Returns the 'previous edge' of the %DFS path tree.
alpar@780
   230
alpar@781
   231
    ///For a node \c v it returns the last edge of the path on the %DFS tree
alpar@781
   232
    ///from the root to \c
alpar@780
   233
    ///v. It is \ref INVALID
alpar@780
   234
    ///if \c v is unreachable from the root or if \c v=s. The
alpar@781
   235
    ///%DFS tree used here is equal to the %DFS tree used in
alpar@780
   236
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
alpar@780
   237
    ///this function.
alpar@780
   238
    Edge pred(Node v) const { return (*predecessor)[v]; }
alpar@780
   239
alpar@781
   240
    ///Returns the 'previous node' of the %DFS tree.
alpar@780
   241
alpar@781
   242
    ///For a node \c v it returns the 'previous node' on the %DFS tree,
alpar@781
   243
    ///i.e. it returns the last but one node of the path from the
alpar@781
   244
    ///root to \c /v on the %DFS tree.
alpar@781
   245
    ///It is INVALID if \c v is unreachable from the root or if
alpar@781
   246
    ///\c v=s.
alpar@781
   247
    ///\pre \ref run() must be called before
alpar@780
   248
    ///using this function.
alpar@780
   249
    Node predNode(Node v) const { return (*pred_node)[v]; }
alpar@780
   250
    
alpar@781
   251
    ///Returns a reference to the NodeMap of distances on the %DFS tree.
alpar@780
   252
    
alpar@781
   253
    ///Returns a reference to the NodeMap of distances on the %DFS tree.
alpar@781
   254
    ///\pre \ref run() must
alpar@780
   255
    ///be called before using this function.
alpar@780
   256
    const DistMap &distMap() const { return *distance;}
alpar@780
   257
 
alpar@781
   258
    ///Returns a reference to the %DFS tree map.
alpar@780
   259
alpar@780
   260
    ///Returns a reference to the NodeMap of the edges of the
alpar@781
   261
    ///%DFS tree.
alpar@780
   262
    ///\pre \ref run() must be called before using this function.
alpar@780
   263
    const PredMap &predMap() const { return *predecessor;}
alpar@780
   264
 
alpar@781
   265
    ///Returns a reference to the map of last but one nodes of the %DFS tree.
alpar@780
   266
alpar@781
   267
    ///Returns a reference to the NodeMap of the last but one nodes of the paths
alpar@781
   268
    ///on the
alpar@781
   269
    ///%DFS tree.
alpar@780
   270
    ///\pre \ref run() must be called before using this function.
alpar@780
   271
    const PredNodeMap &predNodeMap() const { return *pred_node;}
alpar@780
   272
alpar@780
   273
    ///Checks if a node is reachable from the root.
alpar@780
   274
alpar@780
   275
    ///Returns \c true if \c v is reachable from the root.
alpar@802
   276
    ///\note The root node is reported to be reached!
alpar@780
   277
    ///
alpar@780
   278
    ///\pre \ref run() must be called before using this function.
alpar@780
   279
    ///
alpar@780
   280
    bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
alpar@780
   281
    
alpar@780
   282
  };
alpar@780
   283
  
alpar@780
   284
/// @}
alpar@780
   285
  
alpar@921
   286
} //END OF NAMESPACE LEMON
alpar@780
   287
alpar@780
   288
#endif
alpar@780
   289
alpar@780
   290