src/include/bfs.h
author marci
Tue, 23 Mar 2004 11:12:48 +0000
changeset 237 7fb8b67d2c5e
parent 6 b63d1bc367f7
permissions -rw-r--r--
.
alpar@2
     1
// -*-mode: c++; -*-
alpar@2
     2
#ifndef _BFS_H_
alpar@2
     3
#define _BFS_H_
alpar@2
     4
alpar@2
     5
#include <queue>
alpar@2
     6
#include <graph.h>
alpar@2
     7
alpar@2
     8
namespace NEGRO 
alpar@2
     9
{
alpar@2
    10
  using namespace std;
alpar@2
    11
  
alpar@4
    12
  //   template <typename G,typename> class bfs_T
alpar@4
    13
  //   {
alpar@4
    14
  //     typedef G Graph;
alpar@4
    15
  //     typedef Graph::OutEdgeIterator EdgeIterator; //Ez kell
alpar@4
    16
  //     typedef Graph::NodeIterator NodeIterator;
alpar@2
    17
    
alpar@4
    18
  //     class bfs_node_D 
alpar@4
    19
  //     {
alpar@4
    20
  //       EdgeIterator Tree;
alpar@4
    21
  //       int Dist;
alpar@4
    22
  //       int Priority;
alpar@4
    23
  //     }
alpar@4
    24
  //   }
alpar@2
    25
    
alpar@2
    26
alpar@4
    27
  //     //    template <bfs_T<G>>
alpar@4
    28
  //     //    void bfs(bfs_T::Graph &G,const bfs_T::NodeIterator &start_node,const &maps)
alpar@2
    29
alpar@2
    30
    
alpar@2
    31
  
alpar@4
    32
  //   template <class G>
alpar@4
    33
  //   class bfs_maps_T
alpar@4
    34
  //   {
alpar@4
    35
  //     typedef visited; //node->bool (RW)
alpar@4
    36
  //     typedef tree;    //node->EdgeIterator (W)
alpar@4
    37
  //     typedef dist;   //node->int (W)
alpar@4
    38
  //     typedef priority; //node->int (W)
alpar@4
    39
  //   };
alpar@2
    40
  
alpar@2
    41
  
alpar@4
    42
  //   Nem jo! Osszeakad a masik Put-tal
alpar@4
    43
  //   class do_nothing_map {};
alpar@4
    44
  //   template <typename V,typename T>
alpar@4
    45
  //   void Put(const do_nothing_map &p,const V &v,const T &t) {}
alpar@2
    46
  
alpar@2
    47
  struct do_nothing_map {
alpar@8
    48
    template <typename V,typename T> static void Put(V v,T t) {}
alpar@8
    49
    template <typename G> void SetG(G &g) {}
alpar@2
    50
  };
alpar@2
    51
  
alpar@2
    52
alpar@2
    53
  template <typename I,typename C, typename T, T C::*M>
alpar@2
    54
  class class_element_map {
alpar@2
    55
  public:
alpar@2
    56
    typedef T value_type;
alpar@3
    57
    static void Put(const I &i, const T &t) {(*i).*M=t;}
alpar@2
    58
    static T Get(const I i) {return (*i).*M;}
alpar@2
    59
    T &operator[](I i) {return (*i).*M;}
alpar@2
    60
  };
alpar@2
    61
alpar@2
    62
  /* 
alpar@2
    63
     template <typename C,typename I,typename T, T C::*M>
alpar@3
    64
     void Put(class_element_map<C,T,M> p,I i, T t)
alpar@2
    65
     {
alpar@2
    66
     i->*M=t;   
alpar@2
    67
     };
alpar@2
    68
  */
alpar@2
    69
alpar@2
    70
  template <typename P,typename I,typename T>
alpar@3
    71
  inline void Put(P &p,const I &i, const T &t)
alpar@2
    72
  {
alpar@3
    73
    p.Put(i,t);   
alpar@2
    74
  };
alpar@2
    75
  template <typename P,typename I>
alpar@2
    76
  inline typename P::value_type Get(const P &p,const I &i)
alpar@2
    77
  {
alpar@2
    78
    return p.Get(i);   
alpar@2
    79
  };
alpar@2
    80
alpar@2
    81
  /*
alpar@4
    82
    template <typename G,typename T, typename G::EdgeType::*M>
alpar@4
    83
    T Get(edge_element_map p,G::EdgeIterator i)
alpar@4
    84
    {
alpar@2
    85
    return i->*M;
alpar@4
    86
    };
alpar@2
    87
  */
alpar@2
    88
  
alpar@2
    89
  /* 
alpar@4
    90
     template <class G>
alpar@4
    91
     class default_bfs_maps
alpar@4
    92
     {
alpar@4
    93
     public:
alpar@4
    94
     typedef typename G::NodeType NodeType;
alpar@2
    95
    
alpar@4
    96
     class_element_map<typename G::NodeIterator,
alpar@4
    97
     typename G::NodeType,
alpar@4
    98
     bool,&NodeType::isVis> visited; //node->bool (RW)
alpar@4
    99
     do_nothing_map tree;   //node->EdgeIterator (W)
alpar@4
   100
     do_nothing_map dist;   //node->int (W)
alpar@4
   101
     do_nothing_map priority; //node->int (W)
alpar@4
   102
     };
alpar@2
   103
  */
alpar@2
   104
alpar@2
   105
  template<class G>
alpar@2
   106
  struct bfs_node_data 
alpar@2
   107
  {
alpar@2
   108
    bool visited;
alpar@2
   109
    typename G::EdgeIterator tree;
alpar@2
   110
    int dist;
alpar@2
   111
    int priority;
alpar@2
   112
  };
alpar@2
   113
  
alpar@2
   114
  template <class G>
alpar@2
   115
  class bfs_static_maps
alpar@2
   116
  {
alpar@2
   117
  public:
alpar@2
   118
    typedef typename G::NodeType NodeType;
alpar@2
   119
    
alpar@2
   120
    /*    class_element_map<typename G::NodeIterator,
alpar@4
   121
	  typename G::NodeType,
alpar@4
   122
	  bfs_node_data<G>,&NT::D> n_d; //node-> data
alpar@2
   123
    */
alpar@2
   124
    class 
alpar@2
   125
    {
alpar@2
   126
    public:
alpar@2
   127
      bfs_node_data<G> NodeType::*d;
alpar@2
   128
      typedef bool value_type;
alpar@3
   129
      void Put(typename G::NodeIterator &i, const value_type &t)
alpar@2
   130
      {((*i).*d).visited=t;}
alpar@2
   131
      value_type Get(const typename G::NodeIterator &i) const
alpar@2
   132
      {return ((*i).*d).visited;}
alpar@2
   133
    } visited;
alpar@2
   134
    
alpar@2
   135
    class 
alpar@2
   136
    {
alpar@2
   137
    public:
alpar@2
   138
      bfs_node_data<G> NodeType::*d;
alpar@2
   139
      typedef typename G::EdgeIterator value_type;
alpar@3
   140
      void Put(typename G::NodeIterator &i, const value_type &t)
alpar@2
   141
      {((*i).*d).tree=t;}
alpar@2
   142
      value_type Get(const typename G::NodeIterator &i) const
alpar@2
   143
      {return ((*i).*d).tree;}
alpar@2
   144
    } tree;
alpar@2
   145
    
alpar@2
   146
    class 
alpar@2
   147
    {
alpar@2
   148
    public:
alpar@2
   149
      bfs_node_data<G> NodeType::*d;
alpar@2
   150
      typedef int value_type;
alpar@3
   151
      void Put(typename G::NodeIterator &i, const value_type &t)
alpar@2
   152
      {((*i).*d).dist=t;}
alpar@2
   153
      value_type Get(const typename G::NodeIterator &i) const
alpar@2
   154
      {return ((*i).*d).dist;}
alpar@2
   155
    } dist;
alpar@2
   156
    
alpar@2
   157
    class 
alpar@2
   158
    {
alpar@2
   159
    public:
alpar@2
   160
      bfs_node_data<G> NodeType::*d;
alpar@2
   161
      typedef int value_type;
alpar@3
   162
      void Put(typename G::NodeIterator &i,  const value_type &t)
alpar@2
   163
      {((*i).*d).priority=t;}
alpar@2
   164
      value_type Get(const typename G::NodeIterator &i) const
alpar@2
   165
      {return ((*i).*d).priority;}
alpar@2
   166
    } priority;
alpar@2
   167
    
alpar@2
   168
    //do_nothing_map tree;   //node->EdgeIterator (W)
alpar@2
   169
    //    do_nothing_map dist;   //node->int (W)
alpar@2
   170
    //    do_nothing_map priority; //node->int (W)
alpar@2
   171
alpar@2
   172
    void SetDataField(const bfs_node_data<G> NodeType::*dd)
alpar@2
   173
    {
alpar@2
   174
      tree.d=visited.d=dist.d=priority.d=dd;
alpar@2
   175
    }
alpar@2
   176
    
alpar@2
   177
    bfs_static_maps(const bfs_node_data<G> NodeType::*dd) 
alpar@2
   178
    {
alpar@3
   179
      PutDataField(dd);
alpar@2
   180
    }
alpar@2
   181
    
alpar@2
   182
    bfs_static_maps(const bfs_static_maps<G> &B) 
alpar@2
   183
    {
alpar@2
   184
      tree.d=B.tree.d;visited.d=B.visited.d;
alpar@2
   185
      dist.d=B.dist.d;priority.d=B.priority.d;
alpar@2
   186
    }
alpar@2
   187
    
alpar@2
   188
  };
alpar@2
   189
  
alpar@2
   190
  template<typename I>
alpar@2
   191
  struct BFS_Q
alpar@2
   192
  {
alpar@2
   193
    I n;
alpar@2
   194
    int dist;
alpar@2
   195
    //  BFS_Q() {}
alpar@2
   196
    //  BFS_Q(BFS_Q<I> &b) {n=b.n;dist=b.dist;}
alpar@2
   197
  };
alpar@2
   198
  
alpar@2
   199
  template<class G,class M>
alpar@4
   200
  void bfs_fn(G &Gr,const typename G::NodeIterator &start_node,M &maps)
alpar@2
   201
  {
alpar@2
   202
    using namespace std;
alpar@2
   203
    
alpar@2
   204
    typedef BFS_Q<typename G::NodeIterator> Q_T;
alpar@2
   205
    
alpar@2
   206
    Q_T q;
alpar@2
   207
    
alpar@2
   208
    int pr=0;
alpar@2
   209
    typename G::NodeIterator n,m;
alpar@2
   210
    typename G::OutEdgeIterator e;
alpar@2
   211
    int d;
alpar@2
   212
    
alpar@2
   213
    for(Gr.GetFirst(n);n.isValid();++n)
alpar@3
   214
      Put(maps.visited,n,false);
alpar@2
   215
    
alpar@2
   216
    queue<Q_T> Q;
alpar@2
   217
    
alpar@2
   218
    q.n=start_node;
alpar@2
   219
    q.dist=0;
alpar@2
   220
    Q.push(q);
alpar@3
   221
    Put(maps.visited,start_node,true);
alpar@3
   222
    //      Put(maps::tree,start_node,?????);
alpar@3
   223
    Put(maps.dist,start_node,0);
alpar@3
   224
    Put(maps.priority,start_node,pr++);
alpar@2
   225
    
alpar@2
   226
    do {
alpar@2
   227
      n=Q.front().n;d=Q.front().dist+1;
alpar@2
   228
      Q.pop();
alpar@2
   229
      for(Gr.GetFirst(e,n);e.isValid();++e)
alpar@2
   230
	if(!Get(maps.visited,(m=e.Bnode()))) {
alpar@2
   231
	  q.n=m;
alpar@2
   232
	  q.dist=d;
alpar@2
   233
	  Q.push(q);
alpar@3
   234
	  Put(maps.visited,m,true);
alpar@3
   235
	  Put(maps.tree,m,e);
alpar@3
   236
	  Put(maps.dist,m,d);
alpar@3
   237
	  Put(maps.priority,m,pr++);
alpar@2
   238
	}
alpar@2
   239
    } while(!Q.empty());
alpar@2
   240
  };
alpar@4
   241
alpar@4
   242
  // bfs algorithm class
alpar@6
   243
alpar@6
   244
  template<class G>  //the default traits
alpar@6
   245
  class default_bfs_T
alpar@6
   246
  {
alpar@6
   247
  public:
alpar@6
   248
    
alpar@6
   249
    typedef G Graph;
alpar@6
   250
    typedef typename G::OutEdgeIterator SearchEdgeIterator;
alpar@6
   251
    
alpar@6
   252
    typedef typename G::NodeMap<bool> visited_map_t;
alpar@6
   253
    typedef typename G::NodeMap<typename G::EdgeIterator> tree_map_t;
alpar@6
   254
    
alpar@6
   255
    typedef typename G::NodeMap<int> dist_map_t;   //node->int (W)
alpar@6
   256
    typedef typename G::NodeMap<int> priority_map_t; //node->int (W)
alpar@6
   257
};
alpar@6
   258
alpar@4
   259
  template<class T>
alpar@4
   260
  class Bfs
alpar@4
   261
  {
alpar@4
   262
  public:
alpar@6
   263
    typedef typename T::Graph Graph;
alpar@4
   264
    typedef typename Graph::NodeIterator NodeIterator;
alpar@4
   265
    typedef typename Graph::EdgeIterator EdgeIterator;
alpar@4
   266
    
alpar@4
   267
    typedef typename T::SearchEdgeIterator SearchEdgeIterator;
alpar@4
   268
    
alpar@4
   269
    typename T::visited_map_t visited_map;
alpar@4
   270
    typename T::tree_map_t tree_map;
alpar@4
   271
    typename T::dist_map_t dist_map;
alpar@4
   272
    typename T::priority_map_t priority_map;
alpar@4
   273
    
alpar@4
   274
    struct bfs_queue_cont
alpar@4
   275
    {
alpar@4
   276
      NodeIterator n;
alpar@4
   277
      int dist;
alpar@4
   278
    };
alpar@4
   279
    
alpar@4
   280
    std::queue<bfs_queue_cont> bfs_queue;
alpar@4
   281
    
alpar@4
   282
    int priority;
alpar@4
   283
    Graph *G;
alpar@4
   284
    
alpar@8
   285
    //Bfs(int i): visited_map(G), tree_map(G), dist_map(G), priority_map(G) {}
alpar@8
   286
    Bfs() {}
alpar@8
   287
alpar@4
   288
    void SetG(Graph &Gr)
alpar@4
   289
    {
alpar@4
   290
      G=&Gr;
alpar@8
   291
      visited_map.SetG(Gr);
alpar@8
   292
      tree_map.SetG(Gr);
alpar@8
   293
      dist_map.SetG(Gr);
alpar@8
   294
      priority_map.SetG(Gr);
alpar@4
   295
    }
alpar@4
   296
    
alpar@4
   297
    void Init()
alpar@4
   298
    {
alpar@4
   299
      //There must be a better way to do this:
alpar@4
   300
      while(!bfs_queue.empty()) bfs_queue.pop();
alpar@4
   301
alpar@4
   302
      for(NodeIterator n(*G);n.isValid();++n)
alpar@4
   303
	Put(visited_map,n,false);
alpar@4
   304
      
alpar@4
   305
      priority=0;
alpar@4
   306
    }
alpar@4
   307
    
alpar@4
   308
    void AddStartNode(const NodeIterator &start_node,int dist=0)
alpar@4
   309
    {
alpar@4
   310
      bfs_queue_cont q;
alpar@4
   311
      q.n=start_node;
alpar@4
   312
      q.dist=dist;
alpar@4
   313
      bfs_queue.push(q);
alpar@4
   314
alpar@4
   315
      Put(visited_map,start_node,true);
alpar@4
   316
      //      Put(tree_map,start_node,?????);
alpar@4
   317
      Put(dist_map,start_node,dist);
alpar@4
   318
      Put(priority_map,start_node,priority++);    
alpar@4
   319
    }
alpar@4
   320
    
alpar@4
   321
    void Init(const NodeIterator &start_node,int dist=0)
alpar@4
   322
    {
alpar@4
   323
      Init();
alpar@4
   324
      AddStartNode(start_node,dist);
alpar@4
   325
    }
alpar@4
   326
alpar@4
   327
    void Run() 
alpar@4
   328
    {
alpar@4
   329
      NodeIterator n,m;
alpar@4
   330
      int d;
alpar@4
   331
alpar@4
   332
      bfs_queue_cont q;
alpar@4
   333
      while(!(bfs_queue.empty()/* && other stop conditions */)) {
alpar@4
   334
	n=bfs_queue.front().n;d=bfs_queue.front().dist+1;
alpar@4
   335
	bfs_queue.pop();
alpar@4
   336
	for(SearchEdgeIterator e(*G,n);e.isValid();++e)
alpar@4
   337
	  if(!Get(visited_map,(m=e.Bnode()))) {
alpar@4
   338
	    q.n=m;
alpar@4
   339
	    q.dist=d;
alpar@4
   340
	    bfs_queue.push(q);
alpar@4
   341
	    Put(visited_map,m,true);
alpar@4
   342
	    Put(tree_map,m,e);
alpar@4
   343
	    Put(dist_map,m,d);
alpar@4
   344
	    Put(priority_map,m,priority++);
alpar@4
   345
	  }
alpar@4
   346
      }
alpar@4
   347
    }
alpar@4
   348
  };
alpar@4
   349
  
alpar@2
   350
}
alpar@4
   351
  
alpar@2
   352
#endif