graph_utility.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. //
  11. #ifndef BOOST_GRAPH_UTILITY_HPP
  12. #define BOOST_GRAPH_UTILITY_HPP
  13. #include <stdlib.h>
  14. #include <iostream>
  15. #include <algorithm>
  16. #include <assert.h>
  17. #include <boost/config.hpp>
  18. #include <boost/tuple/tuple.hpp>
  19. #include <boost/graph/graph_traits.hpp>
  20. #include <boost/graph/iteration_macros.hpp>
  21. #include <boost/graph/properties.hpp>
  22. #include <boost/pending/container_traits.hpp>
  23. #include <boost/graph/depth_first_search.hpp>
  24. // iota moved to detail/algorithm.hpp
  25. #include <boost/detail/algorithm.hpp>
  26. namespace boost
  27. {
  28. // Provide an undirected graph interface alternative to the
  29. // the source() and target() edge functions.
  30. template < class UndirectedGraph >
  31. inline std::pair< typename graph_traits< UndirectedGraph >::vertex_descriptor,
  32. typename graph_traits< UndirectedGraph >::vertex_descriptor >
  33. incident(typename graph_traits< UndirectedGraph >::edge_descriptor e,
  34. UndirectedGraph& g)
  35. {
  36. return std::make_pair(source(e, g), target(e, g));
  37. }
  38. // Provide an undirected graph interface alternative
  39. // to the out_edges() function.
  40. template < class Graph >
  41. inline std::pair< typename graph_traits< Graph >::out_edge_iterator,
  42. typename graph_traits< Graph >::out_edge_iterator >
  43. incident_edges(typename graph_traits< Graph >::vertex_descriptor u, Graph& g)
  44. {
  45. return out_edges(u, g);
  46. }
  47. template < class Graph >
  48. inline typename graph_traits< Graph >::vertex_descriptor opposite(
  49. typename graph_traits< Graph >::edge_descriptor e,
  50. typename graph_traits< Graph >::vertex_descriptor v, const Graph& g)
  51. {
  52. typedef typename graph_traits< Graph >::vertex_descriptor vertex_descriptor;
  53. if (v == source(e, g))
  54. return target(e, g);
  55. else if (v == target(e, g))
  56. return source(e, g);
  57. else
  58. return vertex_descriptor();
  59. }
  60. //===========================================================================
  61. // Some handy predicates
  62. template < typename Vertex, typename Graph > struct incident_from_predicate
  63. {
  64. incident_from_predicate(Vertex u, const Graph& g) : m_u(u), m_g(g) {}
  65. template < class Edge > bool operator()(const Edge& e) const
  66. {
  67. return source(e, m_g) == m_u;
  68. }
  69. Vertex m_u;
  70. const Graph& m_g;
  71. };
  72. template < typename Vertex, typename Graph >
  73. inline incident_from_predicate< Vertex, Graph > incident_from(
  74. Vertex u, const Graph& g)
  75. {
  76. return incident_from_predicate< Vertex, Graph >(u, g);
  77. }
  78. template < typename Vertex, typename Graph > struct incident_to_predicate
  79. {
  80. incident_to_predicate(Vertex u, const Graph& g) : m_u(u), m_g(g) {}
  81. template < class Edge > bool operator()(const Edge& e) const
  82. {
  83. return target(e, m_g) == m_u;
  84. }
  85. Vertex m_u;
  86. const Graph& m_g;
  87. };
  88. template < typename Vertex, typename Graph >
  89. inline incident_to_predicate< Vertex, Graph > incident_to(
  90. Vertex u, const Graph& g)
  91. {
  92. return incident_to_predicate< Vertex, Graph >(u, g);
  93. }
  94. template < typename Vertex, typename Graph > struct incident_on_predicate
  95. {
  96. incident_on_predicate(Vertex u, const Graph& g) : m_u(u), m_g(g) {}
  97. template < class Edge > bool operator()(const Edge& e) const
  98. {
  99. return source(e, m_g) == m_u || target(e, m_g) == m_u;
  100. }
  101. Vertex m_u;
  102. const Graph& m_g;
  103. };
  104. template < typename Vertex, typename Graph >
  105. inline incident_on_predicate< Vertex, Graph > incident_on(
  106. Vertex u, const Graph& g)
  107. {
  108. return incident_on_predicate< Vertex, Graph >(u, g);
  109. }
  110. template < typename Vertex, typename Graph > struct connects_predicate
  111. {
  112. connects_predicate(Vertex u, Vertex v, const Graph& g)
  113. : m_u(u), m_v(v), m_g(g)
  114. {
  115. }
  116. template < class Edge > bool operator()(const Edge& e) const
  117. {
  118. if (is_directed(m_g))
  119. return source(e, m_g) == m_u && target(e, m_g) == m_v;
  120. else
  121. return (source(e, m_g) == m_u && target(e, m_g) == m_v)
  122. || (source(e, m_g) == m_v && target(e, m_g) == m_u);
  123. }
  124. Vertex m_u, m_v;
  125. const Graph& m_g;
  126. };
  127. template < typename Vertex, typename Graph >
  128. inline connects_predicate< Vertex, Graph > connects(
  129. Vertex u, Vertex v, const Graph& g)
  130. {
  131. return connects_predicate< Vertex, Graph >(u, v, g);
  132. }
  133. // Need to convert all of these printing functions to take an ostream object
  134. // -JGS
  135. template < class IncidenceGraph, class Name >
  136. void print_in_edges(
  137. const IncidenceGraph& G, Name name, std::ostream& os = std::cout)
  138. {
  139. typename graph_traits< IncidenceGraph >::vertex_iterator ui, ui_end;
  140. for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
  141. {
  142. os << get(name, *ui) << " <-- ";
  143. typename graph_traits< IncidenceGraph >::in_edge_iterator ei, ei_end;
  144. for (boost::tie(ei, ei_end) = in_edges(*ui, G); ei != ei_end; ++ei)
  145. os << get(name, source(*ei, G)) << " ";
  146. os << '\n';
  147. }
  148. }
  149. template < class IncidenceGraph, class Name >
  150. void print_graph_dispatch(const IncidenceGraph& G, Name name, directed_tag,
  151. std::ostream& os = std::cout)
  152. {
  153. typename graph_traits< IncidenceGraph >::vertex_iterator ui, ui_end;
  154. for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
  155. {
  156. os << get(name, *ui) << " --> ";
  157. typename graph_traits< IncidenceGraph >::out_edge_iterator ei, ei_end;
  158. for (boost::tie(ei, ei_end) = out_edges(*ui, G); ei != ei_end; ++ei)
  159. os << get(name, target(*ei, G)) << " ";
  160. os << '\n';
  161. }
  162. }
  163. template < class IncidenceGraph, class Name >
  164. void print_graph_dispatch(const IncidenceGraph& G, Name name, undirected_tag,
  165. std::ostream& os = std::cout)
  166. {
  167. typename graph_traits< IncidenceGraph >::vertex_iterator ui, ui_end;
  168. for (boost::tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
  169. {
  170. os << get(name, *ui) << " <--> ";
  171. typename graph_traits< IncidenceGraph >::out_edge_iterator ei, ei_end;
  172. for (boost::tie(ei, ei_end) = out_edges(*ui, G); ei != ei_end; ++ei)
  173. os << get(name, target(*ei, G)) << " ";
  174. os << '\n';
  175. }
  176. }
  177. template < class IncidenceGraph, class Name >
  178. void print_graph(
  179. const IncidenceGraph& G, Name name, std::ostream& os = std::cout)
  180. {
  181. typedef typename graph_traits< IncidenceGraph >::directed_category Cat;
  182. print_graph_dispatch(G, name, Cat(), os);
  183. }
  184. template < class IncidenceGraph >
  185. void print_graph(const IncidenceGraph& G, std::ostream& os = std::cout)
  186. {
  187. print_graph(G, get(vertex_index, G), os);
  188. }
  189. template < class EdgeListGraph, class Name >
  190. void print_edges(
  191. const EdgeListGraph& G, Name name, std::ostream& os = std::cout)
  192. {
  193. typename graph_traits< EdgeListGraph >::edge_iterator ei, ei_end;
  194. for (boost::tie(ei, ei_end) = edges(G); ei != ei_end; ++ei)
  195. os << "(" << get(name, source(*ei, G)) << ","
  196. << get(name, target(*ei, G)) << ") ";
  197. os << '\n';
  198. }
  199. template < class EdgeListGraph, class VertexName, class EdgeName >
  200. void print_edges2(const EdgeListGraph& G, VertexName vname, EdgeName ename,
  201. std::ostream& os = std::cout)
  202. {
  203. typename graph_traits< EdgeListGraph >::edge_iterator ei, ei_end;
  204. for (boost::tie(ei, ei_end) = edges(G); ei != ei_end; ++ei)
  205. os << get(ename, *ei) << "(" << get(vname, source(*ei, G)) << ","
  206. << get(vname, target(*ei, G)) << ") ";
  207. os << '\n';
  208. }
  209. template < class VertexListGraph, class Name >
  210. void print_vertices(
  211. const VertexListGraph& G, Name name, std::ostream& os = std::cout)
  212. {
  213. typename graph_traits< VertexListGraph >::vertex_iterator vi, vi_end;
  214. for (boost::tie(vi, vi_end) = vertices(G); vi != vi_end; ++vi)
  215. os << get(name, *vi) << " ";
  216. os << '\n';
  217. }
  218. template < class Graph, class Vertex >
  219. bool is_adj_dispatch(Graph& g, Vertex a, Vertex b, bidirectional_tag)
  220. {
  221. typename graph_traits< Graph >::adjacency_iterator vi, viend, adj_found;
  222. boost::tie(vi, viend) = adjacent_vertices(a, g);
  223. adj_found = std::find(vi, viend, b);
  224. if (adj_found == viend)
  225. return false;
  226. typename graph_traits< Graph >::out_edge_iterator oi, oiend, out_found;
  227. boost::tie(oi, oiend) = out_edges(a, g);
  228. out_found = std::find_if(oi, oiend, incident_to(b, g));
  229. if (out_found == oiend)
  230. return false;
  231. typename graph_traits< Graph >::in_edge_iterator ii, iiend, in_found;
  232. boost::tie(ii, iiend) = in_edges(b, g);
  233. in_found = std::find_if(ii, iiend, incident_from(a, g));
  234. if (in_found == iiend)
  235. return false;
  236. return true;
  237. }
  238. template < class Graph, class Vertex >
  239. bool is_adj_dispatch(Graph& g, Vertex a, Vertex b, directed_tag)
  240. {
  241. typename graph_traits< Graph >::adjacency_iterator vi, viend, found;
  242. boost::tie(vi, viend) = adjacent_vertices(a, g);
  243. found = std::find(vi, viend, b);
  244. if (found == viend)
  245. return false;
  246. typename graph_traits< Graph >::out_edge_iterator oi, oiend, out_found;
  247. boost::tie(oi, oiend) = out_edges(a, g);
  248. out_found = std::find_if(oi, oiend, incident_to(b, g));
  249. if (out_found == oiend)
  250. return false;
  251. return true;
  252. }
  253. template < class Graph, class Vertex >
  254. bool is_adj_dispatch(Graph& g, Vertex a, Vertex b, undirected_tag)
  255. {
  256. return is_adj_dispatch(g, a, b, directed_tag());
  257. }
  258. template < class Graph, class Vertex >
  259. bool is_adjacent(Graph& g, Vertex a, Vertex b)
  260. {
  261. typedef typename graph_traits< Graph >::directed_category Cat;
  262. return is_adj_dispatch(g, a, b, Cat());
  263. }
  264. template < class Graph, class Edge > bool in_edge_set(Graph& g, Edge e)
  265. {
  266. typename Graph::edge_iterator ei, ei_end, found;
  267. boost::tie(ei, ei_end) = edges(g);
  268. found = std::find(ei, ei_end, e);
  269. return found != ei_end;
  270. }
  271. template < class Graph, class Vertex > bool in_vertex_set(Graph& g, Vertex v)
  272. {
  273. typename Graph::vertex_iterator vi, vi_end, found;
  274. boost::tie(vi, vi_end) = vertices(g);
  275. found = std::find(vi, vi_end, v);
  276. return found != vi_end;
  277. }
  278. template < class Graph, class Vertex >
  279. bool in_edge_set(Graph& g, Vertex u, Vertex v)
  280. {
  281. typename Graph::edge_iterator ei, ei_end;
  282. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  283. if (source(*ei, g) == u && target(*ei, g) == v)
  284. return true;
  285. return false;
  286. }
  287. // is x a descendant of y?
  288. template < typename ParentMap >
  289. inline bool is_descendant(typename property_traits< ParentMap >::value_type x,
  290. typename property_traits< ParentMap >::value_type y, ParentMap parent)
  291. {
  292. if (get(parent, x) == x) // x is the root of the tree
  293. return false;
  294. else if (get(parent, x) == y)
  295. return true;
  296. else
  297. return is_descendant(get(parent, x), y, parent);
  298. }
  299. // is y reachable from x?
  300. template < typename IncidenceGraph, typename VertexColorMap >
  301. inline bool is_reachable(
  302. typename graph_traits< IncidenceGraph >::vertex_descriptor x,
  303. typename graph_traits< IncidenceGraph >::vertex_descriptor y,
  304. const IncidenceGraph& g,
  305. VertexColorMap color) // should start out white for every vertex
  306. {
  307. typedef typename property_traits< VertexColorMap >::value_type ColorValue;
  308. dfs_visitor<> vis;
  309. depth_first_visit(g, x, vis, color);
  310. return get(color, y) != color_traits< ColorValue >::white();
  311. }
  312. // Is the undirected graph connected?
  313. // Is the directed graph strongly connected?
  314. template < typename VertexListGraph, typename VertexColorMap >
  315. inline bool is_connected(const VertexListGraph& g, VertexColorMap color)
  316. {
  317. typedef typename property_traits< VertexColorMap >::value_type ColorValue;
  318. typedef color_traits< ColorValue > Color;
  319. typename graph_traits< VertexListGraph >::vertex_iterator ui, ui_end, vi,
  320. vi_end, ci, ci_end;
  321. for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
  322. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  323. if (*ui != *vi)
  324. {
  325. for (boost::tie(ci, ci_end) = vertices(g); ci != ci_end; ++ci)
  326. put(color, *ci, Color::white());
  327. if (!is_reachable(*ui, *vi, g, color))
  328. return false;
  329. }
  330. return true;
  331. }
  332. template < typename Graph >
  333. bool is_self_loop(
  334. typename graph_traits< Graph >::edge_descriptor e, const Graph& g)
  335. {
  336. return source(e, g) == target(e, g);
  337. }
  338. template < class T1, class T2 >
  339. std::pair< T1, T2 > make_list(const T1& t1, const T2& t2)
  340. {
  341. return std::make_pair(t1, t2);
  342. }
  343. template < class T1, class T2, class T3 >
  344. std::pair< T1, std::pair< T2, T3 > > make_list(
  345. const T1& t1, const T2& t2, const T3& t3)
  346. {
  347. return std::make_pair(t1, std::make_pair(t2, t3));
  348. }
  349. template < class T1, class T2, class T3, class T4 >
  350. std::pair< T1, std::pair< T2, std::pair< T3, T4 > > > make_list(
  351. const T1& t1, const T2& t2, const T3& t3, const T4& t4)
  352. {
  353. return std::make_pair(t1, std::make_pair(t2, std::make_pair(t3, t4)));
  354. }
  355. template < class T1, class T2, class T3, class T4, class T5 >
  356. std::pair< T1, std::pair< T2, std::pair< T3, std::pair< T4, T5 > > > >
  357. make_list(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5)
  358. {
  359. return std::make_pair(
  360. t1, std::make_pair(t2, std::make_pair(t3, std::make_pair(t4, t5))));
  361. }
  362. namespace graph
  363. {
  364. // Functor for remove_parallel_edges: edge property of the removed edge is
  365. // added to the remaining
  366. template < typename EdgeProperty > struct add_removed_edge_property
  367. {
  368. add_removed_edge_property(EdgeProperty ep) : ep(ep) {}
  369. template < typename Edge > void operator()(Edge stay, Edge away)
  370. {
  371. put(ep, stay, get(ep, stay) + get(ep, away));
  372. }
  373. EdgeProperty ep;
  374. };
  375. // Same as above: edge property is capacity here
  376. template < typename Graph >
  377. struct add_removed_edge_capacity
  378. : add_removed_edge_property<
  379. typename property_map< Graph, edge_capacity_t >::type >
  380. {
  381. typedef add_removed_edge_property<
  382. typename property_map< Graph, edge_capacity_t >::type >
  383. base;
  384. add_removed_edge_capacity(Graph& g) : base(get(edge_capacity, g)) {}
  385. };
  386. template < typename Graph > bool has_no_vertices(const Graph& g)
  387. {
  388. typedef typename boost::graph_traits< Graph >::vertex_iterator vi;
  389. std::pair< vi, vi > p = vertices(g);
  390. return (p.first == p.second);
  391. }
  392. template < typename Graph > bool has_no_edges(const Graph& g)
  393. {
  394. typedef typename boost::graph_traits< Graph >::edge_iterator ei;
  395. std::pair< ei, ei > p = edges(g);
  396. return (p.first == p.second);
  397. }
  398. template < typename Graph >
  399. bool has_no_out_edges(
  400. const typename boost::graph_traits< Graph >::vertex_descriptor& v,
  401. const Graph& g)
  402. {
  403. typedef typename boost::graph_traits< Graph >::out_edge_iterator ei;
  404. std::pair< ei, ei > p = out_edges(v, g);
  405. return (p.first == p.second);
  406. }
  407. } // namespace graph
  408. template < class PropertyIn, class PropertyOut, class Graph >
  409. void copy_vertex_property(PropertyIn p_in, PropertyOut p_out, Graph& g)
  410. {
  411. BGL_FORALL_VERTICES_T(u, g, Graph)
  412. put(p_out, u, get(p_in, g));
  413. }
  414. template < class PropertyIn, class PropertyOut, class Graph >
  415. void copy_edge_property(PropertyIn p_in, PropertyOut p_out, Graph& g)
  416. {
  417. BGL_FORALL_EDGES_T(e, g, Graph)
  418. put(p_out, e, get(p_in, g));
  419. }
  420. // Return true if property_map1 and property_map2 differ
  421. // for any of the vertices in graph.
  422. template < typename PropertyMapFirst, typename PropertyMapSecond,
  423. typename Graph >
  424. bool are_property_maps_different(const PropertyMapFirst property_map1,
  425. const PropertyMapSecond property_map2, const Graph& graph)
  426. {
  427. BGL_FORALL_VERTICES_T(vertex, graph, Graph)
  428. {
  429. if (get(property_map1, vertex) != get(property_map2, vertex))
  430. {
  431. return (true);
  432. }
  433. }
  434. return (false);
  435. }
  436. } /* namespace boost */
  437. #endif /* BOOST_GRAPH_UTILITY_HPP*/