graph_utility.hpp 16 KB

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