bfs.h

00001 //==========================================================================
00002 //
00003 //   bfs.h
00004 //
00005 //==========================================================================
00006 // $Id: bfs.h,v 1.14 2003/03/24 15:58:54 raitner Exp $
00007 
00008 #ifndef GTL_BFS_H
00009 #define GTL_BFS_H
00010 
00011 #include <GTL/GTL.h>
00012 #include <GTL/algorithm.h>
00013 #include <GTL/node_map.h>
00014 
00015 #include <deque>
00016 
00017 __GTL_BEGIN_NAMESPACE
00018 
00086 class GTL_EXTERN bfs : public algorithm
00087 {
00088 public:
00089     
00093     bfs ();
00094 
00098     virtual ~bfs ();
00099 
00100     int run (graph& G);
00101 
00111     virtual int check (graph& G) { return GTL_OK; }
00112 
00113     virtual void reset ();
00114     
00115     //-----------------------------------------------------------------------
00116     //  Parameters
00117     //-----------------------------------------------------------------------
00118 
00128     void start_node (const node& n) {start = n;}
00129 
00135     node start_node () const {return start;}
00136 
00154     void scan_whole_graph (bool set) {whole_graph = set;}
00155     
00162     bool scan_whole_graph () const {return whole_graph;}
00163 
00174     void calc_level (bool set);
00175     
00182     bool calc_level () const {return level_number != 0;}
00183 
00193     void store_non_tree_edges (bool set);
00194 
00202     bool store_non_tree_edges () const {return non_tree != 0;}
00203 
00204 
00214     void store_preds (bool set);
00215 
00222     bool store_preds () const {return preds != 0;}
00223 
00230     bool reached (const node& n) const
00231         {return bfs_number[n] != 0;}
00232 
00242     int bfs_num (const node& n) const 
00243         {return bfs_number[n];}
00244 
00254     int operator[] (const node& n) const 
00255         {return bfs_number[n];}
00256 
00267     int level (const node& n) const
00268         {assert (level_number); return (*level_number)[n];}
00269 
00283     node father (const node& n) const
00284         {assert (preds); return (*preds)[n];}
00285 
00289     typedef list<edge>::const_iterator tree_edges_iterator;
00290 
00300     tree_edges_iterator tree_edges_begin () const 
00301         {return tree.begin();}
00302 
00309     tree_edges_iterator tree_edges_end () const
00310         {return tree.end();}
00311    
00315     typedef list<node>::const_iterator bfs_iterator; 
00316 
00322     bfs_iterator begin () const 
00323         {return bfs_order.begin();}
00324 
00331     bfs_iterator end () const 
00332         {return bfs_order.end();}
00333 
00337     typedef list<edge>::const_iterator non_tree_edges_iterator;
00338 
00345     non_tree_edges_iterator non_tree_edges_begin () const 
00346         {assert (non_tree);  return non_tree->begin(); }
00347 
00355     non_tree_edges_iterator non_tree_edges_end () const 
00356         {assert (non_tree); return non_tree->end(); }
00357     
00361     typedef list<bfs_iterator>::const_iterator roots_iterator;
00362 
00392     roots_iterator roots_begin () const 
00393         {return roots.begin();}
00394 
00401     roots_iterator roots_end () const 
00402         {return roots.end();}
00403 
00410     int number_of_reached_nodes () const
00411         {return reached_nodes;}
00412 
00413     //-----------------------------------------------------------------------
00414     //   Handler
00415     //-----------------------------------------------------------------------
00416 
00422     virtual void init_handler (graph& G) { };
00423 
00429     virtual void end_handler (graph& G) { };
00430 
00437     virtual void popped_node_handler (graph& G, node& n) { };
00438 
00448     virtual void finished_node_handler (graph& G, node& n) { };
00449 
00460     virtual void unused_node_handler (graph& G, node& n, node& f) { };
00461 
00472     virtual void used_node_handler (graph& G, node& n, node& f) { };
00473 
00485     virtual void new_start_handler (graph& G, node& n) { };
00486 
00487 private:
00488 
00489     void bfs_sub (graph&, const node&, edge_map<int>*);
00490 
00491 protected:
00492 
00493     //-----------------------------------------------------------------------
00494     //   Data
00495     //-----------------------------------------------------------------------
00496     
00500     int act_bfs_num;
00501 
00505     deque<node> qu;
00506 
00512     list<node> bfs_order;
00513 
00519     list<edge> tree;
00520 
00524     node_map<int> bfs_number;
00525 
00529     int reached_nodes;
00530     
00536     list<bfs_iterator> roots;
00537 
00538     //-----------------------------------------------------------------------
00539     //   Optional
00540     //-----------------------------------------------------------------------
00541 
00547     bool whole_graph;
00548 
00554     node start;
00555 
00561     node_map<int>* level_number;
00562 
00568     list<edge>* non_tree;
00569 
00575     node_map<node>* preds;
00576 };
00577 
00578 __GTL_END_NAMESPACE
00579 
00580 #endif // GTL_BFS_H
00581 
00582 //--------------------------------------------------------------------------
00583 //   end of file
00584 //--------------------------------------------------------------------------