Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfs.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file dfs.cpp
1 /* This software is distributed under the GNU Lesser General Public License */
2 //==========================================================================
3 //
4 // dfs.cpp
5 //
6 //==========================================================================
7 // $Id: dfs.cpp,v 1.18 2001/11/07 13:58:09 pick Exp $
8 
9 #include <GTL/dfs.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  act_dfs_num = 1;
32  act_comp_num = 1;
33  reached_nodes = 0;
34  whole_graph = false;
35  comp_number = 0;
36  preds = 0;
37  used = 0;
38  back_edges = 0;
39 }
40 
42 {
43  if (comp_number) delete comp_number;
44  if (preds) delete preds;
45  if (back_edges) {
46  delete back_edges;
47  delete used;
48  }
49 }
50 
51 //--------------------------------------------------------------------------
52 // GTL_Algorithm - Interface
53 //--------------------------------------------------------------------------
54 
55 
56 void dfs::reset ()
57 {
58  act_dfs_num = 1;
59  act_comp_num = 1;
60  reached_nodes = 0;
61  tree.erase (tree.begin(), tree.end());
62  dfs_order.erase (dfs_order.begin(), dfs_order.end());
63  roots.erase (roots.begin(), roots.end());
64  start = node();
65 
66  if (back_edges) {
67  back_edges->erase (back_edges->begin(), back_edges->end());
68  }
69 }
70 
71 
72 int dfs::check (graph& G)
73 {
74  return GTL_OK;
75 }
76 
77 int dfs::run (graph& G)
78 {
79  //
80  // initialization
81  //
82 
83  node curr;
84  node dummy;
85 
86  dfs_number.init (G, 0);
87 
88  if (comp_number) {
89  comp_number->init (G);
90  }
91 
92  if (preds) {
93  preds->init (G, node());
94  }
95 
96  if (back_edges) {
97  used = new edge_map<int> (G, 0);
98  }
99 
100  init_handler (G);
101 
102  //
103  // Set start-node
104  //
105 
106  if (G.number_of_nodes() == 0) {
107  return GTL_OK;
108  }
109 
110  if (start == node()) {
111  start = G.choose_node();
112  }
113 
115 
116  dfs_sub (G, start, dummy);
117 
119 
120  //
121  // Continue DFS with next unused node.
122  //
123 
124  forall_nodes (curr, G) {
125  if (dfs_number[curr] == 0) {
126  new_start_handler (G, curr);
127  dfs_sub (G, curr, dummy);
128  }
129  }
130  }
131 
132  if (back_edges) {
133  delete used;
134  used = 0;
135  }
136 
137  end_handler(G);
138 
139  return GTL_OK;
140 }
141 
142 
143 //--------------------------------------------------------------------------
144 // PRIVATE
145 //--------------------------------------------------------------------------
146 
147 
148 void dfs::dfs_sub (graph& G, node& curr, node& father)
149 {
150  node opp;
151  edge adj;
152 
153  if (father == node()) {
154  roots.push_back (dfs_order.insert (dfs_order.end(), curr));
155  } else {
156  dfs_order.push_back (curr);
157  }
158 
159  dfs_number[curr] = act_dfs_num;
160  reached_nodes++;
161 
162  if (preds) {
163  (*preds)[curr] = father;
164  }
165 
166  entry_handler (G, curr, father);
167 
168  ++act_dfs_num;
171 
172  while (it != end) {
173  adj = *it;
174  opp = curr.opposite(adj);
175 
176  if (dfs_number[opp] == 0) {
177  tree.push_back (adj);
178 
179  if (back_edges) {
180  (*used)[adj] = 1;
181  }
182 
183  before_recursive_call_handler (G, adj, opp);
184  dfs_sub (G, opp, curr);
185  after_recursive_call_handler (G, adj, opp);
186 
187  } else {
188  if (back_edges && !(*used)[adj]) {
189  (*used)[adj] = 1;
190  back_edges->push_back (adj);
191  }
192 
193  old_adj_node_handler (G, adj, opp);
194  }
195 
196  ++it;
197  }
198 
199  leave_handler (G, curr, father);
200 
201  if (comp_number) {
202  (*comp_number)[curr] = act_comp_num;
203  ++act_comp_num;
204  }
205 }
206 
207 //--------------------------------------------------------------------------
208 // Parameters
209 //--------------------------------------------------------------------------
210 
211 void dfs::calc_comp_num (bool set)
212 {
213  if (set && !comp_number) {
215  } else if (!set && comp_number) {
216  delete comp_number;
217  comp_number = 0;
218  }
219 }
220 
221 void dfs::store_preds (bool set)
222 {
223  if (set && !preds) {
224  preds = new node_map<node>;
225  } else if (!set && preds) {
226  delete preds;
227  preds = 0;
228  }
229 }
230 
232 {
233  if (set && !back_edges) {
234  back_edges = new list<edge>;
235  } else if (!set && back_edges) {
236  delete back_edges;
237  back_edges = 0;
238  }
239 }
240 
242 
243 //--------------------------------------------------------------------------
244 // end of file
245 //--------------------------------------------------------------------------