fruchterman_reingold.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Copyright 2004, 2005 The Trustees of Indiana University.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_FRUCHTERMAN_REINGOLD_FORCE_DIRECTED_LAYOUT_HPP
  8. #define BOOST_GRAPH_FRUCHTERMAN_REINGOLD_FORCE_DIRECTED_LAYOUT_HPP
  9. #include <boost/config/no_tr1/cmath.hpp>
  10. #include <boost/graph/graph_traits.hpp>
  11. #include <boost/graph/named_function_params.hpp>
  12. #include <boost/graph/iteration_macros.hpp>
  13. #include <boost/graph/topology.hpp> // For topology concepts
  14. #include <boost/graph/detail/mpi_include.hpp>
  15. #include <vector>
  16. #include <list>
  17. #include <algorithm> // for std::min and std::max
  18. #include <numeric> // for std::accumulate
  19. #include <cmath> // for std::sqrt and std::fabs
  20. #include <functional>
  21. namespace boost
  22. {
  23. struct square_distance_attractive_force
  24. {
  25. template < typename Graph, typename T >
  26. T operator()(typename graph_traits< Graph >::edge_descriptor, T k, T d,
  27. const Graph&) const
  28. {
  29. return d * d / k;
  30. }
  31. };
  32. struct square_distance_repulsive_force
  33. {
  34. template < typename Graph, typename T >
  35. T operator()(typename graph_traits< Graph >::vertex_descriptor,
  36. typename graph_traits< Graph >::vertex_descriptor, T k, T d,
  37. const Graph&) const
  38. {
  39. return k * k / d;
  40. }
  41. };
  42. template < typename T > struct linear_cooling
  43. {
  44. typedef T result_type;
  45. linear_cooling(std::size_t iterations)
  46. : temp(T(iterations) / T(10)), step(0.1)
  47. {
  48. }
  49. linear_cooling(std::size_t iterations, T temp)
  50. : temp(temp), step(temp / T(iterations))
  51. {
  52. }
  53. T operator()()
  54. {
  55. T old_temp = temp;
  56. temp -= step;
  57. if (temp < T(0))
  58. temp = T(0);
  59. return old_temp;
  60. }
  61. private:
  62. T temp;
  63. T step;
  64. };
  65. struct all_force_pairs
  66. {
  67. template < typename Graph, typename ApplyForce >
  68. void operator()(const Graph& g, ApplyForce apply_force)
  69. {
  70. typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator;
  71. vertex_iterator v, end;
  72. for (boost::tie(v, end) = vertices(g); v != end; ++v)
  73. {
  74. vertex_iterator u = v;
  75. for (++u; u != end; ++u)
  76. {
  77. apply_force(*u, *v);
  78. apply_force(*v, *u);
  79. }
  80. }
  81. }
  82. };
  83. template < typename Topology, typename PositionMap > struct grid_force_pairs
  84. {
  85. typedef typename property_traits< PositionMap >::value_type Point;
  86. BOOST_STATIC_ASSERT(Point::dimensions == 2);
  87. typedef typename Topology::point_difference_type point_difference_type;
  88. template < typename Graph >
  89. explicit grid_force_pairs(
  90. const Topology& topology, PositionMap position, const Graph& g)
  91. : topology(topology), position(position)
  92. {
  93. two_k = 2. * this->topology.volume(this->topology.extent())
  94. / std::sqrt((double)num_vertices(g));
  95. }
  96. template < typename Graph, typename ApplyForce >
  97. void operator()(const Graph& g, ApplyForce apply_force)
  98. {
  99. typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator;
  100. typedef
  101. typename graph_traits< Graph >::vertex_descriptor vertex_descriptor;
  102. typedef std::list< vertex_descriptor > bucket_t;
  103. typedef std::vector< bucket_t > buckets_t;
  104. std::size_t columns = std::size_t(topology.extent()[0] / two_k + 1.);
  105. std::size_t rows = std::size_t(topology.extent()[1] / two_k + 1.);
  106. buckets_t buckets(rows * columns);
  107. vertex_iterator v, v_end;
  108. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v)
  109. {
  110. std::size_t column = std::size_t(
  111. (get(position, *v)[0] + topology.extent()[0] / 2) / two_k);
  112. std::size_t row = std::size_t(
  113. (get(position, *v)[1] + topology.extent()[1] / 2) / two_k);
  114. if (column >= columns)
  115. column = columns - 1;
  116. if (row >= rows)
  117. row = rows - 1;
  118. buckets[row * columns + column].push_back(*v);
  119. }
  120. for (std::size_t row = 0; row < rows; ++row)
  121. for (std::size_t column = 0; column < columns; ++column)
  122. {
  123. bucket_t& bucket = buckets[row * columns + column];
  124. typedef typename bucket_t::iterator bucket_iterator;
  125. for (bucket_iterator u = bucket.begin(); u != bucket.end(); ++u)
  126. {
  127. // Repulse vertices in this bucket
  128. bucket_iterator v = u;
  129. for (++v; v != bucket.end(); ++v)
  130. {
  131. apply_force(*u, *v);
  132. apply_force(*v, *u);
  133. }
  134. std::size_t adj_start_row = row == 0 ? 0 : row - 1;
  135. std::size_t adj_end_row = row == rows - 1 ? row : row + 1;
  136. std::size_t adj_start_column = column == 0 ? 0 : column - 1;
  137. std::size_t adj_end_column
  138. = column == columns - 1 ? column : column + 1;
  139. for (std::size_t other_row = adj_start_row;
  140. other_row <= adj_end_row; ++other_row)
  141. for (std::size_t other_column = adj_start_column;
  142. other_column <= adj_end_column; ++other_column)
  143. if (other_row != row || other_column != column)
  144. {
  145. // Repulse vertices in this bucket
  146. bucket_t& other_bucket
  147. = buckets[other_row * columns
  148. + other_column];
  149. for (v = other_bucket.begin();
  150. v != other_bucket.end(); ++v)
  151. {
  152. double dist = topology.distance(
  153. get(position, *u), get(position, *v));
  154. if (dist < two_k)
  155. apply_force(*u, *v);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. private:
  162. const Topology& topology;
  163. PositionMap position;
  164. double two_k;
  165. };
  166. template < typename PositionMap, typename Topology, typename Graph >
  167. inline grid_force_pairs< Topology, PositionMap > make_grid_force_pairs(
  168. const Topology& topology, const PositionMap& position, const Graph& g)
  169. {
  170. return grid_force_pairs< Topology, PositionMap >(topology, position, g);
  171. }
  172. template < typename Graph, typename PositionMap, typename Topology >
  173. void scale_graph(const Graph& g, PositionMap position, const Topology& topology,
  174. typename Topology::point_type upper_left,
  175. typename Topology::point_type lower_right)
  176. {
  177. if (num_vertices(g) == 0)
  178. return;
  179. typedef typename Topology::point_type Point;
  180. typedef typename Topology::point_difference_type point_difference_type;
  181. // Find min/max ranges
  182. Point min_point = get(position, *vertices(g).first), max_point = min_point;
  183. BGL_FORALL_VERTICES_T(v, g, Graph)
  184. {
  185. min_point = topology.pointwise_min(min_point, get(position, v));
  186. max_point = topology.pointwise_max(max_point, get(position, v));
  187. }
  188. Point old_origin = topology.move_position_toward(min_point, 0.5, max_point);
  189. Point new_origin
  190. = topology.move_position_toward(upper_left, 0.5, lower_right);
  191. point_difference_type old_size = topology.difference(max_point, min_point);
  192. point_difference_type new_size
  193. = topology.difference(lower_right, upper_left);
  194. // Scale to bounding box provided
  195. BGL_FORALL_VERTICES_T(v, g, Graph)
  196. {
  197. point_difference_type relative_loc
  198. = topology.difference(get(position, v), old_origin);
  199. relative_loc = (relative_loc / old_size) * new_size;
  200. put(position, v, topology.adjust(new_origin, relative_loc));
  201. }
  202. }
  203. namespace detail
  204. {
  205. template < typename Topology, typename PropMap, typename Vertex >
  206. void maybe_jitter_point(const Topology& topology, const PropMap& pm,
  207. Vertex v, const typename Topology::point_type& p2)
  208. {
  209. double too_close = topology.norm(topology.extent()) / 10000.;
  210. if (topology.distance(get(pm, v), p2) < too_close)
  211. {
  212. put(pm, v,
  213. topology.move_position_toward(
  214. get(pm, v), 1. / 200, topology.random_point()));
  215. }
  216. }
  217. template < typename Topology, typename PositionMap,
  218. typename DisplacementMap, typename RepulsiveForce, typename Graph >
  219. struct fr_apply_force
  220. {
  221. typedef
  222. typename graph_traits< Graph >::vertex_descriptor vertex_descriptor;
  223. typedef typename Topology::point_type Point;
  224. typedef typename Topology::point_difference_type PointDiff;
  225. fr_apply_force(const Topology& topology, const PositionMap& position,
  226. const DisplacementMap& displacement, RepulsiveForce repulsive_force,
  227. double k, const Graph& g)
  228. : topology(topology)
  229. , position(position)
  230. , displacement(displacement)
  231. , repulsive_force(repulsive_force)
  232. , k(k)
  233. , g(g)
  234. {
  235. }
  236. void operator()(vertex_descriptor u, vertex_descriptor v)
  237. {
  238. if (u != v)
  239. {
  240. // When the vertices land on top of each other, move the
  241. // first vertex away from the boundaries.
  242. maybe_jitter_point(topology, position, u, get(position, v));
  243. double dist
  244. = topology.distance(get(position, u), get(position, v));
  245. typename Topology::point_difference_type dispv
  246. = get(displacement, v);
  247. if (dist == 0.)
  248. {
  249. for (std::size_t i = 0; i < Point::dimensions; ++i)
  250. {
  251. dispv[i] += 0.01;
  252. }
  253. }
  254. else
  255. {
  256. double fr = repulsive_force(u, v, k, dist, g);
  257. dispv += (fr / dist)
  258. * topology.difference(
  259. get(position, v), get(position, u));
  260. }
  261. put(displacement, v, dispv);
  262. }
  263. }
  264. private:
  265. const Topology& topology;
  266. PositionMap position;
  267. DisplacementMap displacement;
  268. RepulsiveForce repulsive_force;
  269. double k;
  270. const Graph& g;
  271. };
  272. } // end namespace detail
  273. template < typename Topology, typename Graph, typename PositionMap,
  274. typename AttractiveForce, typename RepulsiveForce, typename ForcePairs,
  275. typename Cooling, typename DisplacementMap >
  276. void fruchterman_reingold_force_directed_layout(const Graph& g,
  277. PositionMap position, const Topology& topology,
  278. AttractiveForce attractive_force, RepulsiveForce repulsive_force,
  279. ForcePairs force_pairs, Cooling cool, DisplacementMap displacement)
  280. {
  281. typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator;
  282. typedef typename graph_traits< Graph >::vertex_descriptor vertex_descriptor;
  283. typedef typename graph_traits< Graph >::edge_iterator edge_iterator;
  284. double volume = topology.volume(topology.extent());
  285. // assume positions are initialized randomly
  286. double k = pow(volume / num_vertices(g),
  287. 1. / (double)(Topology::point_difference_type::dimensions));
  288. detail::fr_apply_force< Topology, PositionMap, DisplacementMap,
  289. RepulsiveForce, Graph >
  290. apply_force(topology, position, displacement, repulsive_force, k, g);
  291. do
  292. {
  293. // Calculate repulsive forces
  294. vertex_iterator v, v_end;
  295. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v)
  296. put(displacement, *v, typename Topology::point_difference_type());
  297. force_pairs(g, apply_force);
  298. // Calculate attractive forces
  299. edge_iterator e, e_end;
  300. for (boost::tie(e, e_end) = edges(g); e != e_end; ++e)
  301. {
  302. vertex_descriptor v = source(*e, g);
  303. vertex_descriptor u = target(*e, g);
  304. // When the vertices land on top of each other, move the
  305. // first vertex away from the boundaries.
  306. ::boost::detail::maybe_jitter_point(
  307. topology, position, u, get(position, v));
  308. typename Topology::point_difference_type delta
  309. = topology.difference(get(position, v), get(position, u));
  310. double dist = topology.distance(get(position, u), get(position, v));
  311. double fa = attractive_force(*e, k, dist, g);
  312. put(displacement, v, get(displacement, v) - (fa / dist) * delta);
  313. put(displacement, u, get(displacement, u) + (fa / dist) * delta);
  314. }
  315. if (double temp = cool())
  316. {
  317. // Update positions
  318. BGL_FORALL_VERTICES_T(v, g, Graph)
  319. {
  320. BOOST_USING_STD_MIN();
  321. BOOST_USING_STD_MAX();
  322. double disp_size = topology.norm(get(displacement, v));
  323. put(position, v,
  324. topology.adjust(get(position, v),
  325. get(displacement, v)
  326. * (min BOOST_PREVENT_MACRO_SUBSTITUTION(
  327. disp_size, temp)
  328. / disp_size)));
  329. put(position, v, topology.bound(get(position, v)));
  330. }
  331. }
  332. else
  333. {
  334. break;
  335. }
  336. } while (true);
  337. }
  338. namespace detail
  339. {
  340. template < typename DisplacementMap > struct fr_force_directed_layout
  341. {
  342. template < typename Topology, typename Graph, typename PositionMap,
  343. typename AttractiveForce, typename RepulsiveForce,
  344. typename ForcePairs, typename Cooling, typename Param, typename Tag,
  345. typename Rest >
  346. static void run(const Graph& g, PositionMap position,
  347. const Topology& topology, AttractiveForce attractive_force,
  348. RepulsiveForce repulsive_force, ForcePairs force_pairs,
  349. Cooling cool, DisplacementMap displacement,
  350. const bgl_named_params< Param, Tag, Rest >&)
  351. {
  352. fruchterman_reingold_force_directed_layout(g, position, topology,
  353. attractive_force, repulsive_force, force_pairs, cool,
  354. displacement);
  355. }
  356. };
  357. template <> struct fr_force_directed_layout< param_not_found >
  358. {
  359. template < typename Topology, typename Graph, typename PositionMap,
  360. typename AttractiveForce, typename RepulsiveForce,
  361. typename ForcePairs, typename Cooling, typename Param, typename Tag,
  362. typename Rest >
  363. static void run(const Graph& g, PositionMap position,
  364. const Topology& topology, AttractiveForce attractive_force,
  365. RepulsiveForce repulsive_force, ForcePairs force_pairs,
  366. Cooling cool, param_not_found,
  367. const bgl_named_params< Param, Tag, Rest >& params)
  368. {
  369. typedef typename Topology::point_difference_type PointDiff;
  370. std::vector< PointDiff > displacements(num_vertices(g));
  371. fruchterman_reingold_force_directed_layout(g, position, topology,
  372. attractive_force, repulsive_force, force_pairs, cool,
  373. make_iterator_property_map(displacements.begin(),
  374. choose_const_pmap(
  375. get_param(params, vertex_index), g, vertex_index),
  376. PointDiff()));
  377. }
  378. };
  379. } // end namespace detail
  380. template < typename Topology, typename Graph, typename PositionMap,
  381. typename Param, typename Tag, typename Rest >
  382. void fruchterman_reingold_force_directed_layout(const Graph& g,
  383. PositionMap position, const Topology& topology,
  384. const bgl_named_params< Param, Tag, Rest >& params)
  385. {
  386. typedef typename get_param_type< vertex_displacement_t,
  387. bgl_named_params< Param, Tag, Rest > >::type D;
  388. detail::fr_force_directed_layout< D >::run(g, position, topology,
  389. choose_param(get_param(params, attractive_force_t()),
  390. square_distance_attractive_force()),
  391. choose_param(get_param(params, repulsive_force_t()),
  392. square_distance_repulsive_force()),
  393. choose_param(get_param(params, force_pairs_t()),
  394. make_grid_force_pairs(topology, position, g)),
  395. choose_param(
  396. get_param(params, cooling_t()), linear_cooling< double >(100)),
  397. get_param(params, vertex_displacement_t()), params);
  398. }
  399. template < typename Topology, typename Graph, typename PositionMap >
  400. void fruchterman_reingold_force_directed_layout(
  401. const Graph& g, PositionMap position, const Topology& topology)
  402. {
  403. fruchterman_reingold_force_directed_layout(g, position, topology,
  404. attractive_force(square_distance_attractive_force()));
  405. }
  406. } // end namespace boost
  407. #include BOOST_GRAPH_MPI_INCLUDE(<boost/graph/distributed/fruchterman_reingold.hpp>)
  408. #endif // BOOST_GRAPH_FRUCHTERMAN_REINGOLD_FORCE_DIRECTED_LAYOUT_HPP