Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bfs.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file bfs.cpp
1 /* This software is distributed under the GNU Lesser General Public License */
2 //==========================================================================
3 //
4 // bfs.cpp
5 //
6 //==========================================================================
7 // $Id: bfs.cpp,v 1.11 2001/11/07 13:58:09 pick Exp $
8 
9 #include <GTL/bfs.h>
10 #include <GTL/edge_map.h>
11 
12 #ifdef __GTL_MSVCC
13 # ifdef _DEBUG
14 # ifndef SEARCH_MEMORY_LEAKS_ENABLED
15 # error SEARCH NOT ENABLED
16 # endif
17 # define new DEBUG_NEW
18 # undef THIS_FILE
19  static char THIS_FILE[] = __FILE__;
20 # endif // _DEBUG
21 #endif // __GTL_MSVCC
22 
24 
25 //--------------------------------------------------------------------------
26 // Con-/Destructors
27 //--------------------------------------------------------------------------
28 
30 {
31  level_number = 0;
32  preds = 0;
33  non_tree = 0;
34  act_bfs_num = 1;
35  reached_nodes = 0;
36  whole_graph = false;
37 }
38 
40 {
41  if (level_number) delete level_number;
42  if (preds) delete preds;
43  if (non_tree) delete non_tree;
44 }
45 
46 
47 //--------------------------------------------------------------------------
48 // Parameters
49 //--------------------------------------------------------------------------
50 
51 void bfs::calc_level (bool set)
52 {
53  if (set && !level_number) {
55  } else if (!set && level_number) {
56  delete level_number;
57  level_number = 0;
58  }
59 }
60 
61 void bfs::store_preds (bool set)
62 {
63  if (set && !preds) {
64  preds = new node_map<node>;
65  } else if (!set && preds) {
66  delete preds;
67  preds = 0;
68  }
69 }
70 
71 void bfs::store_non_tree_edges (bool set)
72 {
73  if (set && !non_tree) {
74  non_tree = new list<edge>;
75  } else if (!set && non_tree) {
76  delete non_tree;
77  non_tree = 0;
78  }
79 }
80 
81 //--------------------------------------------------------------------------
82 // GTL_Algorithm - Interface
83 //--------------------------------------------------------------------------
84 
85 void bfs::reset ()
86 {
87  act_bfs_num = 1;
88  tree.erase (tree.begin(), tree.end());
89  bfs_order.erase (bfs_order.begin(), bfs_order.end());
90  roots.erase (roots.begin(), roots.end());
91  reached_nodes = 0;
92  if (non_tree) {
93  non_tree->erase (non_tree->begin(), non_tree->end());
94  }
95 }
96 
97 
98 int bfs::run (graph& G) {
99 
100  bfs_number.init (G, 0);
101 
102  if (level_number) {
103  level_number->init (G);
104  }
105 
106  if (preds) {
107  preds->init (G, node());
108  }
109 
110  edge_map<int> *used = 0;
111 
112  if (non_tree) {
113  used = new edge_map<int> (G, 0);
114  }
115 
116  init_handler (G);
117 
118  //
119  // Set start-node
120  //
121 
122  if (start == node()) {
123  start = G.choose_node();
124  }
125 
127 
128  bfs_sub (G, start, used);
129 
130  node curr;
131 
133  forall_nodes (curr, G) {
134  if (bfs_number[curr] == 0) {
135 
136  new_start_handler (G, curr);
137 
138  bfs_sub (G, curr, used);
139  }
140  }
141  }
142 
143  if (non_tree) {
144  delete used;
145  }
146 
147  end_handler (G);
148 
149  return 1;
150 }
151 
152 
153 
154 //--------------------------------------------------------------------------
155 // PRIVATE
156 //--------------------------------------------------------------------------
157 
158 
159 void bfs::bfs_sub (graph& G, const node& st, edge_map<int>* used)
160 {
161  qu.push_back (st);
162  bfs_number[st] = act_bfs_num;
163  ++act_bfs_num;
164 
165  if (level_number) {
166  (*level_number)[st] = 0;
167  }
168 
169  while (!qu.empty()) {
170  node tmp = qu.front();
171  qu.pop_front();
172  ++reached_nodes;
173 
174  if (tmp == st) {
175  roots.push_back (bfs_order.insert (bfs_order.end(), tmp));
176  } else {
177  bfs_order.push_back (tmp);
178  }
179 
180  popped_node_handler (G, tmp);
181 
184 
185  for (; it != end; ++it) {
186  edge curr = *it;
187  node opp = tmp.opposite (curr);
188 
189  if (bfs_number[opp] == 0) {
190  bfs_number[opp] = act_bfs_num;
191  ++act_bfs_num;
192  tree.push_back (curr);
193 
194  if (non_tree) {
195  (*used)[curr] = 1;
196  }
197 
198  if (level_number) {
199  (*level_number)[opp] = (*level_number)[tmp] + 1;
200  }
201 
202  if (preds) {
203  (*preds)[opp] = tmp;
204  }
205 
206  qu.push_back (opp);
207 
208  unused_node_handler (G, opp, tmp);
209 
210  } else {
211  if (non_tree && !(*used)[curr]) {
212  (*used)[curr] = 1;
213  non_tree->push_back(curr);
214  }
215 
216  used_node_handler (G, opp, tmp);
217  }
218  }
219 
220  finished_node_handler (G, tmp);
221  }
222 }
223 
225 
226 //--------------------------------------------------------------------------
227 // end of file
228 //--------------------------------------------------------------------------