floyd_warshall_test.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // Copyright 2002 Rensselaer Polytechnic Institute
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Lauren Foutz
  6. // Scott Hill
  7. #include <boost/graph/floyd_warshall_shortest.hpp>
  8. #include <map>
  9. #include <algorithm>
  10. #include <iostream>
  11. #include <boost/random/linear_congruential.hpp>
  12. #include <boost/graph/graph_utility.hpp>
  13. #include <boost/graph/properties.hpp>
  14. #include <boost/graph/bellman_ford_shortest_paths.hpp>
  15. #include <boost/graph/random.hpp>
  16. #include <boost/graph/adjacency_list.hpp>
  17. #include <boost/graph/adjacency_matrix.hpp>
  18. #include <boost/test/minimal.hpp>
  19. #include <algorithm>
  20. using namespace boost;
  21. template<typename T>
  22. inline const T& my_min(const T& x, const T& y)
  23. { return x < y? x : y; }
  24. template<typename Graph>
  25. bool acceptance_test(Graph& g, int vec, int e)
  26. {
  27. boost::minstd_rand ran(vec);
  28. {
  29. typename boost::property_map<Graph, boost::vertex_name_t>::type index =
  30. boost::get(boost::vertex_name, g);
  31. typename boost::graph_traits<Graph>::vertex_iterator firstv, lastv,
  32. firstv2, lastv2;
  33. int x = 0;
  34. for(boost::tie(firstv, lastv) = boost::vertices(g); firstv != lastv;
  35. firstv++){
  36. boost::put(index, *firstv, x);
  37. x++;
  38. }
  39. for(int i = 0; i < e; i++){
  40. boost::add_edge(index[ran() % vec], index[ran() % vec], g);
  41. }
  42. typename boost::graph_traits<Graph>::edge_iterator first, last;
  43. typename boost::property_map<Graph, boost::edge_weight_t>::type
  44. local_edge_map = boost::get(boost::edge_weight, g);
  45. for(boost::tie(first, last) = boost::edges(g); first != last; first++){
  46. if (ran() % vec != 0){
  47. boost::put(local_edge_map, *first, ran() % 100);
  48. } else {
  49. boost::put(local_edge_map, *first, 0 - (ran() % 100));
  50. }
  51. }
  52. int int_inf =
  53. std::numeric_limits<int>::max BOOST_PREVENT_MACRO_SUBSTITUTION();
  54. typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_des;
  55. std::map<vertex_des,int> matrixRow;
  56. std::map<vertex_des, std::map<vertex_des ,int> > matrix;
  57. typedef typename boost::property_map<Graph, boost::vertex_distance_t>::type
  58. distance_type;
  59. distance_type distance_row = boost::get(boost::vertex_distance, g);
  60. for(boost::tie(firstv, lastv) = boost::vertices(g); firstv != lastv;
  61. firstv++){
  62. boost::put(distance_row, *firstv, int_inf);
  63. matrixRow[*firstv] = int_inf;
  64. }
  65. for(boost::tie(firstv, lastv) = boost::vertices(g); firstv != lastv;
  66. firstv++){
  67. matrix[*firstv] = matrixRow;
  68. }
  69. for(boost::tie(firstv, lastv) = boost::vertices(g); firstv != lastv;
  70. firstv++){
  71. matrix[*firstv][*firstv] = 0;
  72. }
  73. std::map<vertex_des, std::map<vertex_des, int> > matrix3(matrix);
  74. std::map<vertex_des, std::map<vertex_des, int> > matrix4(matrix);
  75. for(boost::tie(first, last) = boost::edges(g); first != last; first++){
  76. if (matrix[boost::source(*first, g)][boost::target(*first, g)] != int_inf)
  77. {
  78. matrix[boost::source(*first, g)][boost::target(*first, g)] =
  79. my_min
  80. (boost::get(local_edge_map, *first),
  81. matrix[boost::source(*first, g)][boost::target(*first, g)]);
  82. } else {
  83. matrix[boost::source(*first, g)][boost::target(*first, g)] =
  84. boost::get(local_edge_map, *first);
  85. }
  86. }
  87. bool is_undirected =
  88. boost::is_same<typename boost::graph_traits<Graph>::directed_category,
  89. boost::undirected_tag>::value;
  90. if (is_undirected){
  91. for(boost::tie(first, last) = boost::edges(g); first != last; first++){
  92. if (matrix[boost::target(*first, g)][boost::source(*first, g)] != int_inf)
  93. {
  94. matrix[boost::target(*first, g)][boost::source(*first, g)] =
  95. my_min
  96. (boost::get(local_edge_map, *first),
  97. matrix[boost::target(*first, g)][boost::source(*first, g)]);
  98. } else {
  99. matrix[boost::target(*first, g)][boost::source(*first, g)] =
  100. boost::get(local_edge_map, *first);
  101. }
  102. }
  103. }
  104. bool bellman, floyd1, floyd2, floyd3;
  105. floyd1 =
  106. boost::floyd_warshall_initialized_all_pairs_shortest_paths
  107. (g,
  108. matrix, weight_map(boost::get(boost::edge_weight, g)).
  109. distance_inf(int_inf). distance_zero(0));
  110. floyd2 =
  111. boost::floyd_warshall_all_pairs_shortest_paths
  112. (g, matrix3,
  113. weight_map(local_edge_map).
  114. distance_inf(int_inf). distance_zero(0));
  115. floyd3 = boost::floyd_warshall_all_pairs_shortest_paths(g, matrix4);
  116. boost::dummy_property_map dummy_map;
  117. std::map<vertex_des, std::map<vertex_des, int> > matrix2;
  118. for(boost::tie(firstv, lastv) = vertices(g); firstv != lastv; firstv++){
  119. boost::put(distance_row, *firstv, 0);
  120. bellman =
  121. boost::bellman_ford_shortest_paths
  122. (g, vec,
  123. weight_map(boost::get(boost::edge_weight, g)).
  124. distance_map(boost::get(boost::vertex_distance, g)).
  125. predecessor_map(dummy_map));
  126. distance_row = boost::get(boost::vertex_distance, g);
  127. for(boost::tie(firstv2, lastv2) = vertices(g); firstv2 != lastv2;
  128. firstv2++){
  129. matrix2[*firstv][*firstv2] = boost::get(distance_row, *firstv2);
  130. boost::put(distance_row, *firstv2, int_inf);
  131. }
  132. if(bellman == false){
  133. break;
  134. }
  135. }
  136. if (bellman != floyd1 || bellman != floyd2 || bellman != floyd3){
  137. std::cout <<
  138. "A negative cycle was detected in one algorithm but not the others. "
  139. << std::endl;
  140. return false;
  141. }
  142. else if (bellman == false && floyd1 == false && floyd2 == false &&
  143. floyd3 == false){
  144. return true;
  145. }
  146. else {
  147. typename boost::graph_traits<Graph>::vertex_iterator first1, first2,
  148. last1, last2;
  149. for (boost::tie(first1, last1) = boost::vertices(g); first1 != last1;
  150. first1++){
  151. for (boost::tie(first2, last2) = boost::vertices(g); first2 != last2;
  152. first2++){
  153. if (matrix2[*first1][*first2] != matrix[*first1][*first2]){
  154. std::cout << "Algorithms do not match at matrix point "
  155. << index[*first1] << " " << index[*first2]
  156. << " Bellman results: " << matrix2[*first1][*first2]
  157. << " floyd 1 results " << matrix[*first1][*first2]
  158. << std::endl;
  159. return false;
  160. }
  161. if (matrix2[*first1][*first2] != matrix3[*first1][*first2]){
  162. std::cout << "Algorithms do not match at matrix point "
  163. << index[*first1] << " " << index[*first2]
  164. << " Bellman results: " << matrix2[*first1][*first2]
  165. << " floyd 2 results " << matrix3[*first1][*first2]
  166. << std::endl;
  167. return false;
  168. }
  169. if (matrix2[*first1][*first2] != matrix4[*first1][*first2]){
  170. std::cout << "Algorithms do not match at matrix point "
  171. << index[*first1] << " " << index[*first2]
  172. << " Bellman results: " << matrix2[*first1][*first2]
  173. << " floyd 3 results " << matrix4[*first1][*first2]
  174. << std::endl;
  175. return false;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. return true;
  182. }
  183. template<typename Graph>
  184. bool acceptance_test2(Graph& g, int vec, int e)
  185. {
  186. boost::minstd_rand ran(vec);
  187. {
  188. typename boost::property_map<Graph, boost::vertex_name_t>::type index =
  189. boost::get(boost::vertex_name, g);
  190. typename boost::graph_traits<Graph>::vertex_iterator firstv, lastv,
  191. firstv2, lastv2;
  192. int x = 0;
  193. for(boost::tie(firstv, lastv) = boost::vertices(g); firstv != lastv;
  194. firstv++){
  195. boost::put(index, *firstv, x);
  196. x++;
  197. }
  198. boost::generate_random_graph(g, vec, e, ran, true);
  199. typename boost::graph_traits<Graph>::edge_iterator first, last;
  200. typename boost::property_map<Graph, boost::edge_weight_t>::type
  201. local_edge_map = boost::get(boost::edge_weight, g);
  202. for(boost::tie(first, last) = boost::edges(g); first != last; first++){
  203. if (ran() % vec != 0){
  204. boost::put(local_edge_map, *first, ran() % 100);
  205. } else {
  206. boost::put(local_edge_map, *first, 0 - (ran() % 100));
  207. }
  208. }
  209. int int_inf =
  210. std::numeric_limits<int>::max BOOST_PREVENT_MACRO_SUBSTITUTION();
  211. typedef typename boost::graph_traits<Graph>::vertex_descriptor vertex_des;
  212. std::map<vertex_des,int> matrixRow;
  213. std::map<vertex_des, std::map<vertex_des ,int> > matrix;
  214. typedef typename boost::property_map<Graph, boost::vertex_distance_t>::type
  215. distance_type;
  216. distance_type distance_row = boost::get(boost::vertex_distance, g);
  217. for(boost::tie(firstv, lastv) = boost::vertices(g); firstv != lastv;
  218. firstv++){
  219. boost::put(distance_row, *firstv, int_inf);
  220. matrixRow[*firstv] = int_inf;
  221. }
  222. for(boost::tie(firstv, lastv) = boost::vertices(g); firstv != lastv;
  223. firstv++){
  224. matrix[*firstv] = matrixRow;
  225. }
  226. for(boost::tie(firstv, lastv) = boost::vertices(g); firstv != lastv;
  227. firstv++){
  228. matrix[*firstv][*firstv] = 0;
  229. }
  230. std::map<vertex_des, std::map<vertex_des, int> > matrix3(matrix);
  231. std::map<vertex_des, std::map<vertex_des, int> > matrix4(matrix);
  232. for(boost::tie(first, last) = boost::edges(g); first != last; first++){
  233. if (matrix[boost::source(*first, g)][boost::target(*first, g)] != int_inf)
  234. {
  235. matrix[boost::source(*first, g)][boost::target(*first, g)] =
  236. my_min
  237. (boost::get(local_edge_map, *first),
  238. matrix[boost::source(*first, g)][boost::target(*first, g)]);
  239. } else {
  240. matrix[boost::source(*first, g)][boost::target(*first, g)] =
  241. boost::get(local_edge_map, *first);
  242. }
  243. }
  244. bool is_undirected =
  245. boost::is_same<typename boost::graph_traits<Graph>::directed_category,
  246. boost::undirected_tag>::value;
  247. if (is_undirected){
  248. for(boost::tie(first, last) = boost::edges(g); first != last; first++){
  249. if (matrix[boost::target(*first, g)][boost::source(*first, g)]
  250. != int_inf){
  251. matrix[boost::target(*first, g)][boost::source(*first, g)] =
  252. my_min
  253. (boost::get(local_edge_map, *first),
  254. matrix[boost::target(*first, g)][boost::source(*first, g)]);
  255. } else {
  256. matrix[boost::target(*first, g)][boost::source(*first, g)] =
  257. boost::get(local_edge_map, *first);
  258. }
  259. }
  260. }
  261. bool bellman, floyd1, floyd2, floyd3;
  262. floyd1 =
  263. boost::floyd_warshall_initialized_all_pairs_shortest_paths
  264. (g,
  265. matrix, weight_map(boost::get(boost::edge_weight, g)).
  266. distance_inf(int_inf). distance_zero(0));
  267. floyd2 =
  268. boost::floyd_warshall_all_pairs_shortest_paths
  269. (g, matrix3,
  270. weight_map(local_edge_map).
  271. distance_inf(int_inf). distance_zero(0));
  272. floyd3 = boost::floyd_warshall_all_pairs_shortest_paths(g, matrix4);
  273. boost::dummy_property_map dummy_map;
  274. std::map<vertex_des, std::map<vertex_des, int> > matrix2;
  275. for(boost::tie(firstv, lastv) = vertices(g); firstv != lastv; firstv++){
  276. boost::put(distance_row, *firstv, 0);
  277. bellman =
  278. boost::bellman_ford_shortest_paths
  279. (g, vec,
  280. weight_map(boost::get(boost::edge_weight, g)).
  281. distance_map(boost::get(boost::vertex_distance, g)).
  282. predecessor_map(dummy_map));
  283. distance_row = boost::get(boost::vertex_distance, g);
  284. for(boost::tie(firstv2, lastv2) = vertices(g); firstv2 != lastv2;
  285. firstv2++){
  286. matrix2[*firstv][*firstv2] = boost::get(distance_row, *firstv2);
  287. boost::put(distance_row, *firstv2, int_inf);
  288. }
  289. if(bellman == false){
  290. break;
  291. }
  292. }
  293. if (bellman != floyd1 || bellman != floyd2 || bellman != floyd3){
  294. std::cout <<
  295. "A negative cycle was detected in one algorithm but not the others. "
  296. << std::endl;
  297. return false;
  298. }
  299. else if (bellman == false && floyd1 == false && floyd2 == false &&
  300. floyd3 == false){
  301. return true;
  302. }
  303. else {
  304. typename boost::graph_traits<Graph>::vertex_iterator first1, first2,
  305. last1, last2;
  306. for (boost::tie(first1, last1) = boost::vertices(g); first1 != last1;
  307. first1++){
  308. for (boost::tie(first2, last2) = boost::vertices(g); first2 != last2;
  309. first2++){
  310. if (matrix2[*first1][*first2] != matrix[*first1][*first2]){
  311. std::cout << "Algorithms do not match at matrix point "
  312. << index[*first1] << " " << index[*first2]
  313. << " Bellman results: " << matrix2[*first1][*first2]
  314. << " floyd 1 results " << matrix[*first1][*first2]
  315. << std::endl;
  316. return false;
  317. }
  318. if (matrix2[*first1][*first2] != matrix3[*first1][*first2]){
  319. std::cout << "Algorithms do not match at matrix point "
  320. << index[*first1] << " " << index[*first2]
  321. << " Bellman results: " << matrix2[*first1][*first2]
  322. << " floyd 2 results " << matrix3[*first1][*first2]
  323. << std::endl;
  324. return false;
  325. }
  326. if (matrix2[*first1][*first2] != matrix4[*first1][*first2]){
  327. std::cout << "Algorithms do not match at matrix point "
  328. << index[*first1] << " " << index[*first2]
  329. << " Bellman results: " << matrix2[*first1][*first2]
  330. << " floyd 3 results " << matrix4[*first1][*first2]
  331. << std::endl;
  332. return false;
  333. }
  334. }
  335. }
  336. }
  337. }
  338. return true;
  339. }
  340. int test_main(int, char*[])
  341. {
  342. typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS,
  343. boost::property<boost::vertex_distance_t, int,
  344. boost::property<boost::vertex_name_t, int> > ,
  345. boost::property<boost::edge_weight_t, int> > Digraph;
  346. Digraph adjlist_digraph;
  347. BOOST_CHECK(acceptance_test2(adjlist_digraph, 100, 2000));
  348. typedef boost::adjacency_matrix<boost::undirectedS,
  349. boost::property<boost::vertex_distance_t, int,
  350. boost::property<boost::vertex_name_t, int> > ,
  351. boost::property<boost::edge_weight_t, int> > Graph;
  352. Graph matrix_graph(100);
  353. BOOST_CHECK(acceptance_test(matrix_graph, 100, 2000));
  354. return 0;
  355. }