Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bellman_ford_test.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file bellman_ford_test.cpp
1 /* This software is distributed under the GNU Lesser General Public License */
2 //==========================================================================
3 //
4 // bellman_ford_test.cpp
5 //
6 //==========================================================================
7 // $Id: bellman_ford_test.cpp,v 1.2 2002/11/07 13:38:37 raitner Exp $
8 
9 #include <iostream>
10 
11 #include <GTL/graph.h>
12 #include <GTL/bellman_ford.h>
13 #include <GTL/edge_map.h>
14 
15 #ifdef __GTL_MSVCC
16 # ifdef _DEBUG
17 # define new DEBUG_NEW
18 # undef THIS_FILE
19  static char THIS_FILE[] = __FILE__;
20 # endif // _DEBUG
21 #endif // __GTL_MSVCC
22 
23 int main (int argc, char* argv[])
24 {
25  graph G;
26  G.make_directed();
27  node n1 = G.new_node();
28  node n2 = G.new_node();
29  node n3 = G.new_node();
30  node n4 = G.new_node();
31  node n5 = G.new_node();
32  node n6 = G.new_node();
33 
34  edge e1_2 = G.new_edge(n1, n2);
35  edge e2_3 = G.new_edge(n2, n3);
36  edge e1_6 = G.new_edge(n1, n6);
37  edge e3_6 = G.new_edge(n3, n6);
38  edge e4_3 = G.new_edge(n4, n3);
39  edge e6_4 = G.new_edge(n6, n4);
40  edge e6_5 = G.new_edge(n6, n5);
41  edge e5_4 = G.new_edge(n5, n4);
42  edge e5_1 = G.new_edge(n5, n1);
43 
44  edge_map<double> w(G);
45  w[e1_2] = 1;
46  w[e2_3] = 2;
47  w[e1_6] = 8;
48  w[e3_6] = 3;
49  w[e4_3] = 2;
50  w[e6_4] = 1;
51  w[e6_5] = 3;
52  w[e5_4] = 10;
53  w[e5_1] = 2;
54 
55  node_map<double> result(G);
56  result[n1] = 0;
57  result[n2] = 1;
58  result[n3] = 3;
59  result[n4] = 7;
60  result[n5] = 9;
61  result[n6] = 6;
62 
63  node_map<node> preds(G);
64  preds[n1] = node();
65  preds[n2] = n1;
66  preds[n3] = n2;
67  preds[n4] = n6;
68  preds[n5] = n6;
69  preds[n6] = n3;
70 
71  bellman_ford B;
72  B.store_preds(true);
73  B.source(n1);
74  B.weights(w);
75 
76  G.save("test.gml");
77 
78  cout << "Bellman Ford with positive edge weights" << endl;
79 
80  if (B.check(G) == algorithm::GTL_OK)
81  {
82  cout << "check OK" << endl;
83  }
84  else
85  {
86  cout << "check FAILED" << endl;
87  exit(1);
88  }
89 
90  if (B.run(G) == algorithm::GTL_OK)
91  {
92  cout << "run OK" << endl;
93  }
94  else
95  {
96  cout << "run FAILED" << endl;
97  exit(1);
98  }
99 
101 
102  for (it = G.nodes_begin(), end = G.nodes_end(); it != end; ++it)
103  {
104  if(result[*it] != B.distance(*it))
105  {
106  cout << "Distance for node " << *it << " FAILED" << endl;
107  exit(1);
108  }
109  }
110 
111  cout << "Distances OK" << endl;
112 
113  for (it = G.nodes_begin(), end = G.nodes_end(); it != end; ++it)
114  {
115  if(preds[*it] != B.predecessor_node(*it))
116  {
117  cout << "Predecessor for node " << *it << " FAILED" << endl;
118  exit(1);
119  }
120  }
121 
122  cout << "Predecessors OK" << endl;
123 
124  if (B.negative_cycle())
125  {
126  cout << "Negative Cycle FAILED" << endl;
127  }
128  else
129  {
130  cout << "Negative Cycle OK" << endl;
131  }
132 
133  //----------------------------------------------------------------------
134  // negative edge weights
135  //----------------------------------------------------------------------
136 
137  B.reset();
138 
139  cout << "Bellman Ford with some negative edge weights" << endl;
140 
141  w[e1_2] = 1;
142  w[e2_3] = 2;
143  w[e1_6] = -3;
144  w[e3_6] = 3;
145  w[e4_3] = 2;
146  w[e6_4] = 1;
147  w[e6_5] = 3;
148  w[e5_4] = 10;
149  w[e5_1] = 2;
150 
151  B.weights(w);
152 
153  result[n1] = 0;
154  result[n2] = 1;
155  result[n3] = 0;
156  result[n4] = -2;
157  result[n5] = 0;
158  result[n6] = -3;
159 
160  preds[n1] = node();
161  preds[n2] = n1;
162  preds[n3] = n4;
163  preds[n4] = n6;
164  preds[n5] = n6;
165  preds[n6] = n1;
166 
167  G.save("test2.gml");
168 
169 
170  if (B.check(G) == algorithm::GTL_OK)
171  {
172  cout << "check OK" << endl;
173  }
174  else
175  {
176  cout << "check FAILED" << endl;
177  exit(1);
178  }
179 
180  if (B.run(G) == algorithm::GTL_OK)
181  {
182  cout << "run OK" << endl;
183  }
184  else
185  {
186  cout << "run FAILED" << endl;
187  exit(1);
188  }
189 
190  for (it = G.nodes_begin(), end = G.nodes_end(); it != end; ++it)
191  {
192  if(result[*it] != B.distance(*it))
193  {
194  cout << "Distance for node " << *it << " FAILED" << endl;
195  exit(1);
196  }
197  }
198 
199  cout << "Distances OK" << endl;
200 
201  for (it = G.nodes_begin(), end = G.nodes_end(); it != end; ++it)
202  {
203  if(preds[*it] != B.predecessor_node(*it))
204  {
205  cout << "Predecessor for node " << *it << " FAILED" << endl;
206  exit(1);
207  }
208  }
209 
210  cout << "Predecessors OK" << endl;
211 
212  if (B.negative_cycle())
213  {
214  cout << "Negative Cycle FAILED" << endl;
215  }
216  else
217  {
218  cout << "Negative Cycle OK" << endl;
219  }
220 
221  //----------------------------------------------------------------------
222  // negative cycle
223  //----------------------------------------------------------------------
224 
225  B.reset();
226 
227  cout << "Bellman Ford with negative cycle" << endl;
228 
229  w[e1_2] = 1;
230  w[e2_3] = 2;
231  w[e1_6] = -8;
232  w[e3_6] = 3;
233  w[e4_3] = 2;
234  w[e6_4] = 1;
235  w[e6_5] = 3;
236  w[e5_4] = 10;
237  w[e5_1] = 2;
238 
239  B.weights(w);
240 
241  if (B.check(G) == algorithm::GTL_OK)
242  {
243  cout << "check OK" << endl;
244  }
245  else
246  {
247  cout << "check FAILED" << endl;
248  exit(1);
249  }
250 
251  if (B.run(G) == algorithm::GTL_OK)
252  {
253  cout << "run OK" << endl;
254  }
255  else
256  {
257  cout << "run FAILED" << endl;
258  exit(1);
259  }
260 
261  if (B.negative_cycle())
262  {
263  cout << "Negative Cycle OK" << endl;
264  }
265  else
266  {
267  cout << "Negative Cycle FAILED" << endl;
268  }
269 }