transitive_closure.w 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. \documentclass[11pt]{report}
  2. \input{defs}
  3. \setlength\overfullrule{5pt}
  4. \tolerance=10000
  5. \sloppy
  6. \hfuzz=10pt
  7. \makeindex
  8. \begin{document}
  9. \title{A Generic Programming Implementation of Transitive Closure}
  10. \author{Jeremy G. Siek}
  11. \maketitle
  12. \section{Introduction}
  13. This paper documents the implementation of the
  14. \code{transitive\_closure()} function of the Boost Graph Library. The
  15. function was implemented by Vladimir Prus and some editing was done by
  16. Jeremy Siek.
  17. The algorithm used to implement the \code{transitive\_closure()}
  18. function is based on the detection of strong components
  19. \cite{nuutila95, purdom70}. The following discussion describes the
  20. main ideas of the algorithm and some relevant background theory.
  21. The \keyword{transitive closure} of a graph $G = (V,E)$ is a graph $G^+
  22. = (V,E^+)$ such that $E^+$ contains an edge $(u,v)$ if and only if $G$
  23. contains a path (of at least one edge) from $u$ to $v$. A
  24. \keyword{successor set} of a vertex $v$, denoted by $Succ(v)$, is the
  25. set of vertices that are reachable from vertex $v$. The set of
  26. vertices adjacent to $v$ in the transitive closure $G^+$ is the same as
  27. the successor set of $v$ in the original graph $G$. Computing the
  28. transitive closure is equivalent to computing the successor set for
  29. every vertex in $G$.
  30. All vertices in the same strong component have the same successor set
  31. (because every vertex is reachable from all the other vertices in the
  32. component). Therefore, it is redundant to compute the successor set
  33. for every vertex in a strong component; it suffices to compute it for
  34. just one vertex per component.
  35. A \keyword{condensation graph} is a a graph $G'=(V',E')$ based on the
  36. graph $G=(V,E)$ where each vertex in $V'$ corresponds to a strongly
  37. connected component in $G$ and the edge $(s,t)$ is in $E'$ if and only
  38. if there exists an edge in $E$ connecting any of the vertices in the
  39. component of $s$ to any of the vertices in the component of $t$.
  40. \section{The Implementation}
  41. The following is the interface and outline of the function:
  42. @d Transitive Closure Function
  43. @{
  44. template <typename Graph, typename GraphTC,
  45. typename G_to_TC_VertexMap,
  46. typename VertexIndexMap>
  47. void transitive_closure(const Graph& g, GraphTC& tc,
  48. G_to_TC_VertexMap g_to_tc_map,
  49. VertexIndexMap index_map)
  50. {
  51. if (num_vertices(g) == 0) return;
  52. @<Some type definitions@>
  53. @<Concept checking@>
  54. @<Compute strongly connected components of the graph@>
  55. @<Construct the condensation graph (version 2)@>
  56. @<Compute transitive closure on the condensation graph@>
  57. @<Build transitive closure of the original graph@>
  58. }
  59. @}
  60. The parameter \code{g} is the input graph and the parameter \code{tc}
  61. is the output graph that will contain the transitive closure of
  62. \code{g}. The \code{g\_to\_tc\_map} maps vertices in the input graph
  63. to the new vertices in the output transitive closure. The
  64. \code{index\_map} maps vertices in the input graph to the integers
  65. zero to \code{num\_vertices(g) - 1}.
  66. There are two alternate interfaces for the transitive closure
  67. function. The following is the version where defaults are used for
  68. both the \code{g\_to\_tc\_map} and the \code{index\_map}.
  69. @d The All Defaults Interface
  70. @{
  71. template <typename Graph, typename GraphTC>
  72. void transitive_closure(const Graph& g, GraphTC& tc)
  73. {
  74. if (num_vertices(g) == 0) return;
  75. typedef typename property_map<Graph, vertex_index_t>::const_type
  76. VertexIndexMap;
  77. VertexIndexMap index_map = get(vertex_index, g);
  78. typedef typename graph_traits<GraphTC>::vertex_descriptor tc_vertex;
  79. std::vector<tc_vertex> to_tc_vec(num_vertices(g));
  80. iterator_property_map<tc_vertex*, VertexIndexMap>
  81. g_to_tc_map(&to_tc_vec[0], index_map);
  82. transitive_closure(g, tc, g_to_tc_map, index_map);
  83. }
  84. @}
  85. \noindent The following alternate interface uses the named parameter
  86. trick for specifying the parameters. The named parameter functions to
  87. use in creating the \code{params} argument are
  88. \code{vertex\_index(VertexIndexMap index\_map)} and
  89. \code{orig\_to\_copy(G\_to\_TC\_VertexMap g\_to\_tc\_map)}.
  90. @d The Named Parameter Interface
  91. @{
  92. template <typename Graph, typename GraphTC,
  93. typename P, typename T, typename R>
  94. void transitive_closure(const Graph& g, GraphTC& tc,
  95. const bgl_named_params<P, T, R>& params)
  96. {
  97. if (num_vertices(g) == 0) return;
  98. detail::transitive_closure_dispatch(g, tc,
  99. get_param(params, orig_to_copy),
  100. choose_const_pmap(get_param(params, vertex_index), g, vertex_index)
  101. );
  102. }
  103. @}
  104. \noindent This dispatch function is used to handle the logic for
  105. deciding between a user-provided graph to transitive closure vertex
  106. mapping or to use the default, a vector, to map between the two.
  107. @d Construct Default G to TC Vertex Mapping
  108. @{
  109. namespace detail {
  110. template <typename Graph, typename GraphTC,
  111. typename G_to_TC_VertexMap,
  112. typename VertexIndexMap>
  113. void transitive_closure_dispatch
  114. (const Graph& g, GraphTC& tc,
  115. G_to_TC_VertexMap g_to_tc_map,
  116. VertexIndexMap index_map)
  117. {
  118. typedef typename graph_traits<GraphTC>::vertex_descriptor tc_vertex;
  119. typename std::vector<tc_vertex>::size_type
  120. n = is_default_param(g_to_tc_map) ? num_vertices(g) : 1;
  121. std::vector<tc_vertex> to_tc_vec(n);
  122. transitive_closure
  123. (g, tc,
  124. choose_param(g_to_tc_map, make_iterator_property_map
  125. (to_tc_vec.begin(), index_map, to_tc_vec[0])),
  126. index_map);
  127. }
  128. } // namespace detail
  129. @}
  130. The following statements check to make sure that the template
  131. parameters \emph{model} the concepts that are required for this
  132. algorithm.
  133. @d Concept checking
  134. @{
  135. BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph> ));
  136. BOOST_CONCEPT_ASSERT(( AdjacencyGraphConcept<Graph> ));
  137. BOOST_CONCEPT_ASSERT(( VertexMutableGraphConcept<GraphTC> ));
  138. BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept<GraphTC> ));
  139. BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept<VertexIndexMap, vertex> ));
  140. @}
  141. \noindent To simplify the code in the rest of the function we make the
  142. following typedefs.
  143. @d Some type definitions
  144. @{
  145. typedef typename graph_traits<Graph>::vertex_descriptor vertex;
  146. typedef typename graph_traits<Graph>::edge_descriptor edge;
  147. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  148. typedef typename property_traits<VertexIndexMap>::value_type size_type;
  149. typedef typename graph_traits<Graph>::adjacency_iterator adjacency_iterator;
  150. @}
  151. The first step of the algorithm is to compute which vertices are in
  152. each strongly connected component (SCC) of the graph. This is done
  153. with the \code{strong\_components()} function. The result of this
  154. function is stored in the \code{component\_number} array which maps
  155. each vertex to the number of the SCC to which it belongs (the
  156. components are numbered zero through \code{num\_scc}). We will use
  157. the SCC numbers for vertices in the condensation graph (CG), so we use
  158. the same integer type \code{cg\_vertex} for both.
  159. @d Compute strongly connected components of the graph
  160. @{
  161. typedef size_type cg_vertex;
  162. std::vector<cg_vertex> component_number_vec(num_vertices(g));
  163. iterator_property_map<cg_vertex*, VertexIndexMap>
  164. component_number(&component_number_vec[0], index_map);
  165. int num_scc = strong_components(g, component_number,
  166. vertex_index_map(index_map));
  167. std::vector< std::vector<vertex> > components;
  168. build_component_lists(g, num_scc, component_number, components);
  169. @}
  170. \noindent Later we will need efficient access to all vertices in the
  171. same SCC so we create a \code{std::vector} of vertices for each SCC
  172. and fill it in with the \code{build\_components\_lists()} function
  173. from \code{strong\_components.hpp}.
  174. The next step is to construct the condensation graph. There will be
  175. one vertex in the CG for every strongly connected component in the
  176. original graph. We will add an edge to the CG whenever there is one or
  177. more edges in the original graph that has its source in one SCC and
  178. its target in another SCC. The data structure we will use for the CG
  179. is an adjacency-list with a \code{std::set} for each out-edge list. We
  180. use \code{std::set} because it will automatically discard parallel
  181. edges. This makes the code simpler since we can just call
  182. \code{insert()} every time there is an edge connecting two SCCs in the
  183. original graph.
  184. @d Construct the condensation graph (version 1)
  185. @{
  186. typedef std::vector< std::set<cg_vertex> > CG_t;
  187. CG_t CG(num_scc);
  188. for (cg_vertex s = 0; s < components.size(); ++s) {
  189. for (size_type i = 0; i < components[s].size(); ++i) {
  190. vertex u = components[s][i];
  191. adjacency_iterator vi, vi_end;
  192. for (tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi) {
  193. cg_vertex t = component_number[*vi];
  194. if (s != t) // Avoid loops in the condensation graph
  195. CG[s].insert(t); // add edge (s,t) to the condensation graph
  196. }
  197. }
  198. }
  199. @}
  200. Inserting into a \code{std::set} and iterator traversal for
  201. \code{std::set} is a bit slow. We can get better performance if we use
  202. \code{std::vector} and then explicitly remove duplicated vertices from
  203. the out-edge lists. Here is the construction of the condensation graph
  204. rewritten to use \code{std::vector}.
  205. @d Construct the condensation graph (version 2)
  206. @{
  207. typedef std::vector< std::vector<cg_vertex> > CG_t;
  208. CG_t CG(num_scc);
  209. for (cg_vertex s = 0; s < components.size(); ++s) {
  210. std::vector<cg_vertex> adj;
  211. for (size_type i = 0; i < components[s].size(); ++i) {
  212. vertex u = components[s][i];
  213. adjacency_iterator v, v_end;
  214. for (tie(v, v_end) = adjacent_vertices(u, g); v != v_end; ++v) {
  215. cg_vertex t = component_number[*v];
  216. if (s != t) // Avoid loops in the condensation graph
  217. adj.push_back(t);
  218. }
  219. }
  220. std::sort(adj.begin(), adj.end());
  221. std::vector<cg_vertex>::iterator di = std::unique(adj.begin(), adj.end());
  222. if (di != adj.end())
  223. adj.erase(di, adj.end());
  224. CG[s] = adj;
  225. }
  226. @}
  227. Next we compute the transitive closure of the condensation graph. The
  228. basic outline of the algorithm is below. The vertices are considered
  229. in reverse topological order to ensure that the when computing the
  230. successor set for a vertex $u$, the successor set for each vertex in
  231. $Adj[u]$ has already been computed. The successor set for a vertex $u$
  232. can then be constructed by taking the union of the successor sets for
  233. all of its adjacent vertices together with the adjacent vertices
  234. themselves.
  235. \begin{tabbing}
  236. \textbf{for} \= ea\=ch \= vertex $u$ in $G'$ in reverse topological order \\
  237. \>\textbf{for} each vertex $v$ in $Adj[u]$ \\
  238. \>\>if ($v \notin Succ(u)$) \\
  239. \>\>\>$Succ(u)$ := $Succ(u) \cup \{ v \} \cup Succ(v)$
  240. \end{tabbing}
  241. An optimized implementation of the set union operation improves the
  242. performance of the algorithm. Therefore this implementation uses
  243. \keyword{chain decomposition}\cite{goral79,simon86}. The vertices of
  244. $G$ are partitioned into chains $Z_1, ..., Z_k$, where each chain
  245. $Z_i$ is a path in $G$ and the vertices in a chain have increasing
  246. topological number. A successor set $S$ is then represented by a
  247. collection of intersections with the chains, i.e., $S =
  248. \bigcup_{i=1 \ldots k} (Z_i \cap S)$. Each intersection can be represented
  249. by the first vertex in the path $Z_i$ that is also in $S$, since the
  250. rest of the path is guaranteed to also be in $S$. The collection of
  251. intersections is therefore represented by a vector of length $k$ where
  252. the $i$th element of the vector stores the first vertex in the
  253. intersection of $S$ with $Z_i$.
  254. Computing the union of two successor sets, $S_3 = S_1 \cup S_2$, can
  255. then be computed in $O(k)$ time with the below operation. We will
  256. represent the successor sets by vectors of integers where the integers
  257. are the topological numbers for the vertices in the set.
  258. @d Union of successor sets
  259. @{
  260. namespace detail {
  261. inline void
  262. union_successor_sets(const std::vector<std::size_t>& s1,
  263. const std::vector<std::size_t>& s2,
  264. std::vector<std::size_t>& s3)
  265. {
  266. for (std::size_t k = 0; k < s1.size(); ++k)
  267. s3[k] = std::min(s1[k], s2[k]);
  268. }
  269. } // namespace detail
  270. @}
  271. So to compute the transitive closure we must first sort the graph by
  272. topological number and then decompose the graph into chains. Once
  273. that is accomplished we can enter the main loop and begin computing
  274. the successor sets.
  275. @d Compute transitive closure on the condensation graph
  276. @{
  277. @<Compute topological number for each vertex@>
  278. @<Sort the out-edge lists by topological number@>
  279. @<Decompose the condensation graph into chains@>
  280. @<Compute successor sets@>
  281. @<Build the transitive closure of the condensation graph@>
  282. @}
  283. The \code{topological\_sort()} function is called to obtain a list of
  284. vertices in topological order and then we use this ordering to assign
  285. topological numbers to the vertices.
  286. @d Compute topological number for each vertex
  287. @{
  288. std::vector<cg_vertex> topo_order;
  289. std::vector<cg_vertex> topo_number(num_vertices(CG));
  290. topological_sort(CG, std::back_inserter(topo_order),
  291. vertex_index_map(identity_property_map()));
  292. std::reverse(topo_order.begin(), topo_order.end());
  293. size_type n = 0;
  294. for (std::vector<cg_vertex>::iterator i = topo_order.begin();
  295. i != topo_order.end(); ++i)
  296. topo_number[*i] = n++;
  297. @}
  298. Next we sort the out-edge lists of the condensation graph by
  299. topological number. This is needed for computing the chain
  300. decomposition, for each the vertices in a chain must be in topological
  301. order and we will be adding vertices to the chains from the out-edge
  302. lists. The \code{subscript()} function creates a function object that
  303. returns the topological number of its input argument.
  304. @d Sort the out-edge lists by topological number
  305. @{
  306. for (size_type i = 0; i < num_vertices(CG); ++i)
  307. std::sort(CG[i].begin(), CG[i].end(),
  308. compose_f_gx_hy(std::less<cg_vertex>(),
  309. detail::subscript(topo_number),
  310. detail::subscript(topo_number)));
  311. @}
  312. Here is the code that defines the \code{subscript\_t} function object
  313. and its associated helper object generation function.
  314. @d Subscript function object
  315. @{
  316. namespace detail {
  317. template <typename Container, typename ST = std::size_t,
  318. typename VT = typename Container::value_type>
  319. struct subscript_t : public std::unary_function<ST, VT> {
  320. subscript_t(Container& c) : container(&c) { }
  321. VT& operator()(const ST& i) const { return (*container)[i]; }
  322. protected:
  323. Container *container;
  324. };
  325. template <typename Container>
  326. subscript_t<Container> subscript(Container& c)
  327. { return subscript_t<Container>(c); }
  328. } // namespace detail
  329. @}
  330. Now we are ready to decompose the condensation graph into chains. The
  331. idea is that we want to form lists of vertices that are in a path and
  332. that the vertices in the list should be ordered by topological number.
  333. These lists will be stored in the \code{chains} vector below. To
  334. create the chains we consider each vertex in the graph in topological
  335. order. If the vertex is not already in a chain then it will be the
  336. start of a new chain. We then follow a path from this vertex to extend
  337. the chain.
  338. @d Decompose the condensation graph into chains
  339. @{
  340. std::vector< std::vector<cg_vertex> > chains;
  341. {
  342. std::vector<cg_vertex> in_a_chain(num_vertices(CG));
  343. for (std::vector<cg_vertex>::iterator i = topo_order.begin();
  344. i != topo_order.end(); ++i) {
  345. cg_vertex v = *i;
  346. if (!in_a_chain[v]) {
  347. chains.resize(chains.size() + 1);
  348. std::vector<cg_vertex>& chain = chains.back();
  349. for (;;) {
  350. @<Extend the chain until the path dead-ends@>
  351. }
  352. }
  353. }
  354. }
  355. @<Record the chain number and chain position for each vertex@>
  356. @}
  357. \noindent To extend the chain we pick an adjacent vertex that is not
  358. already in a chain. Also, the adjacent vertex chosen will be the one
  359. with lowest topological number since the out-edges of \code{CG} are in
  360. topological order.
  361. @d Extend the chain until the path dead-ends
  362. @{
  363. chain.push_back(v);
  364. in_a_chain[v] = true;
  365. graph_traits<CG_t>::adjacency_iterator adj_first, adj_last;
  366. tie(adj_first, adj_last) = adjacent_vertices(v, CG);
  367. graph_traits<CG_t>::adjacency_iterator next
  368. = std::find_if(adj_first, adj_last, not1(detail::subscript(in_a_chain)));
  369. if (next != adj_last)
  370. v = *next;
  371. else
  372. break; // end of chain, dead-end
  373. @}
  374. In the next steps of the algorithm we will need to efficiently find
  375. the chain for a vertex and the position in the chain for a vertex, so
  376. here we compute this information and store it in two vectors:
  377. \code{chain\_number} and \code{pos\_in\_chain}.
  378. @d Record the chain number and chain position for each vertex
  379. @{
  380. std::vector<size_type> chain_number(num_vertices(CG));
  381. std::vector<size_type> pos_in_chain(num_vertices(CG));
  382. for (size_type i = 0; i < chains.size(); ++i)
  383. for (size_type j = 0; j < chains[i].size(); ++j) {
  384. cg_vertex v = chains[i][j];
  385. chain_number[v] = i;
  386. pos_in_chain[v] = j;
  387. }
  388. @}
  389. Now that we have completed the chain decomposition we are ready to
  390. write the main loop for computing the transitive closure of the
  391. condensation graph. The output of this will be a successor set for
  392. each vertex. Remember that the successor set is stored as a collection
  393. of intersections with the chains. Each successor set is represented by
  394. a vector where the $i$th element is the representative vertex for the
  395. intersection of the set with the $i$th chain. We compute the successor
  396. sets for every vertex in decreasing topological order. The successor
  397. set for each vertex is the union of the successor sets of the adjacent
  398. vertex plus the adjacent vertices themselves.
  399. @d Compute successor sets
  400. @{
  401. cg_vertex inf = std::numeric_limits<cg_vertex>::max();
  402. std::vector< std::vector<cg_vertex> > successors(num_vertices(CG),
  403. std::vector<cg_vertex>(chains.size(), inf));
  404. for (std::vector<cg_vertex>::reverse_iterator i = topo_order.rbegin();
  405. i != topo_order.rend(); ++i) {
  406. cg_vertex u = *i;
  407. graph_traits<CG_t>::adjacency_iterator adj, adj_last;
  408. for (tie(adj, adj_last) = adjacent_vertices(u, CG);
  409. adj != adj_last; ++adj) {
  410. cg_vertex v = *adj;
  411. if (topo_number[v] < successors[u][chain_number[v]]) {
  412. // Succ(u) = Succ(u) U Succ(v)
  413. detail::union_successor_sets(successors[u], successors[v],
  414. successors[u]);
  415. // Succ(u) = Succ(u) U {v}
  416. successors[u][chain_number[v]] = topo_number[v];
  417. }
  418. }
  419. }
  420. @}
  421. We now rebuild the condensation graph, adding in edges to connect each
  422. vertex to every vertex in its successor set, thereby obtaining the
  423. transitive closure. The successor set vectors contain topological
  424. numbers, so we map back to vertices using the \code{topo\_order}
  425. vector.
  426. @d Build the transitive closure of the condensation graph
  427. @{
  428. for (size_type i = 0; i < CG.size(); ++i)
  429. CG[i].clear();
  430. for (size_type i = 0; i < CG.size(); ++i)
  431. for (size_type j = 0; j < chains.size(); ++j) {
  432. size_type topo_num = successors[i][j];
  433. if (topo_num < inf) {
  434. cg_vertex v = topo_order[topo_num];
  435. for (size_type k = pos_in_chain[v]; k < chains[j].size(); ++k)
  436. CG[i].push_back(chains[j][k]);
  437. }
  438. }
  439. @}
  440. The last stage is to create the transitive closure graph $G^+$ based on
  441. the transitive closure of the condensation graph $G'^+$. We do this in
  442. two steps. First we add edges between all the vertices in one SCC to
  443. all the vertices in another SCC when the two SCCs are adjacent in the
  444. condensation graph. Second we add edges to connect each vertex in a
  445. SCC to every other vertex in the SCC.
  446. @d Build transitive closure of the original graph
  447. @{
  448. // Add vertices to the transitive closure graph
  449. typedef typename graph_traits<GraphTC>::vertex_descriptor tc_vertex;
  450. {
  451. vertex_iterator i, i_end;
  452. for (tie(i, i_end) = vertices(g); i != i_end; ++i)
  453. g_to_tc_map[*i] = add_vertex(tc);
  454. }
  455. // Add edges between all the vertices in two adjacent SCCs
  456. graph_traits<CG_t>::vertex_iterator si, si_end;
  457. for (tie(si, si_end) = vertices(CG); si != si_end; ++si) {
  458. cg_vertex s = *si;
  459. graph_traits<CG_t>::adjacency_iterator i, i_end;
  460. for (tie(i, i_end) = adjacent_vertices(s, CG); i != i_end; ++i) {
  461. cg_vertex t = *i;
  462. for (size_type k = 0; k < components[s].size(); ++k)
  463. for (size_type l = 0; l < components[t].size(); ++l)
  464. add_edge(g_to_tc_map[components[s][k]],
  465. g_to_tc_map[components[t][l]], tc);
  466. }
  467. }
  468. // Add edges connecting all vertices in a SCC
  469. for (size_type i = 0; i < components.size(); ++i)
  470. if (components[i].size() > 1)
  471. for (size_type k = 0; k < components[i].size(); ++k)
  472. for (size_type l = 0; l < components[i].size(); ++l) {
  473. vertex u = components[i][k], v = components[i][l];
  474. add_edge(g_to_tc_map[u], g_to_tc_map[v], tc);
  475. }
  476. // Find loopbacks in the original graph.
  477. // Need to add it to transitive closure.
  478. {
  479. vertex_iterator i, i_end;
  480. for (tie(i, i_end) = vertices(g); i != i_end; ++i)
  481. {
  482. adjacency_iterator ab, ae;
  483. for (boost::tie(ab, ae) = adjacent_vertices(*i, g); ab != ae; ++ab)
  484. {
  485. if (*ab == *i)
  486. if (components[component_number[*i]].size() == 1)
  487. add_edge(g_to_tc_map[*i], g_to_tc_map[*i], tc);
  488. }
  489. }
  490. }
  491. @}
  492. \section{Appendix}
  493. @d Warshall Transitive Closure
  494. @{
  495. template <typename G>
  496. void warshall_transitive_closure(G& g)
  497. {
  498. typedef typename graph_traits<G>::vertex_descriptor vertex;
  499. typedef typename graph_traits<G>::vertex_iterator vertex_iterator;
  500. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<G> ));
  501. BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept<G> ));
  502. // Matrix form:
  503. // for k
  504. // for i
  505. // if A[i,k]
  506. // for j
  507. // A[i,j] = A[i,j] | A[k,j]
  508. vertex_iterator ki, ke, ii, ie, ji, je;
  509. for (tie(ki, ke) = vertices(g); ki != ke; ++ki)
  510. for (tie(ii, ie) = vertices(g); ii != ie; ++ii)
  511. if (edge(*ii, *ki, g).second)
  512. for (tie(ji, je) = vertices(g); ji != je; ++ji)
  513. if (!edge(*ii, *ji, g).second &&
  514. edge(*ki, *ji, g).second)
  515. {
  516. add_edge(*ii, *ji, g);
  517. }
  518. }
  519. @}
  520. @d Warren Transitive Closure
  521. @{
  522. template <typename G>
  523. void warren_transitive_closure(G& g)
  524. {
  525. using namespace boost;
  526. typedef typename graph_traits<G>::vertex_descriptor vertex;
  527. typedef typename graph_traits<G>::vertex_iterator vertex_iterator;
  528. BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<G> ));
  529. BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept<G> ));
  530. // Make sure second loop will work
  531. if (num_vertices(g) == 0)
  532. return;
  533. // for i = 2 to n
  534. // for k = 1 to i - 1
  535. // if A[i,k]
  536. // for j = 1 to n
  537. // A[i,j] = A[i,j] | A[k,j]
  538. vertex_iterator ic, ie, jc, je, kc, ke;
  539. for (tie(ic, ie) = vertices(g), ++ic; ic != ie; ++ic)
  540. for (tie(kc, ke) = vertices(g); *kc != *ic; ++kc)
  541. if (edge(*ic, *kc, g).second)
  542. for (tie(jc, je) = vertices(g); jc != je; ++jc)
  543. if (!edge(*ic, *jc, g).second &&
  544. edge(*kc, *jc, g).second)
  545. {
  546. add_edge(*ic, *jc, g);
  547. }
  548. // for i = 1 to n - 1
  549. // for k = i + 1 to n
  550. // if A[i,k]
  551. // for j = 1 to n
  552. // A[i,j] = A[i,j] | A[k,j]
  553. for (tie(ic, ie) = vertices(g), --ie; ic != ie; ++ic)
  554. for (kc = ic, ke = ie, ++kc; kc != ke; ++kc)
  555. if (edge(*ic, *kc, g).second)
  556. for (tie(jc, je) = vertices(g); jc != je; ++jc)
  557. if (!edge(*ic, *jc, g).second &&
  558. edge(*kc, *jc, g).second)
  559. {
  560. add_edge(*ic, *jc, g);
  561. }
  562. }
  563. @}
  564. The following indent command was run on the output files before
  565. they were checked into the Boost CVS repository.
  566. @e indentation
  567. @{
  568. indent -nut -npcs -i2 -br -cdw -ce transitive_closure.hpp
  569. @}
  570. @o transitive_closure.hpp
  571. @{
  572. // Copyright (C) 2001 Vladimir Prus <ghost@@cs.msu.su>
  573. // Copyright (C) 2001 Jeremy Siek <jsiek@@cs.indiana.edu>
  574. // Permission to copy, use, modify, sell and distribute this software is
  575. // granted, provided this copyright notice appears in all copies and
  576. // modified version are clearly marked as such. This software is provided
  577. // "as is" without express or implied warranty, and with no claim as to its
  578. // suitability for any purpose.
  579. // NOTE: this final is generated by libs/graph/doc/transitive_closure.w
  580. #ifndef BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
  581. #define BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
  582. #include <vector>
  583. #include <functional>
  584. #include <boost/compose.hpp>
  585. #include <boost/graph/vector_as_graph.hpp>
  586. #include <boost/graph/strong_components.hpp>
  587. #include <boost/graph/topological_sort.hpp>
  588. #include <boost/graph/graph_concepts.hpp>
  589. #include <boost/graph/named_function_params.hpp>
  590. #include <boost/concept/assert.hpp>
  591. namespace boost {
  592. @<Union of successor sets@>
  593. @<Subscript function object@>
  594. @<Transitive Closure Function@>
  595. @<The All Defaults Interface@>
  596. @<Construct Default G to TC Vertex Mapping@>
  597. @<The Named Parameter Interface@>
  598. @<Warshall Transitive Closure@>
  599. @<Warren Transitive Closure@>
  600. } // namespace boost
  601. #endif // BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
  602. @}
  603. @o transitive_closure.cpp
  604. @{
  605. // Copyright (c) Jeremy Siek 2001
  606. //
  607. // Permission to use, copy, modify, distribute and sell this software
  608. // and its documentation for any purpose is hereby granted without fee,
  609. // provided that the above copyright notice appears in all copies and
  610. // that both that copyright notice and this permission notice appear
  611. // in supporting documentation. Silicon Graphics makes no
  612. // representations about the suitability of this software for any
  613. // purpose. It is provided "as is" without express or implied warranty.
  614. // NOTE: this final is generated by libs/graph/doc/transitive_closure.w
  615. #include <boost/graph/transitive_closure.hpp>
  616. #include <boost/graph/graphviz.hpp>
  617. int main(int, char*[])
  618. {
  619. using namespace boost;
  620. typedef property<vertex_name_t, char> Name;
  621. typedef property<vertex_index_t, std::size_t,
  622. Name> Index;
  623. typedef adjacency_list<listS, listS, directedS, Index> graph_t;
  624. typedef graph_traits<graph_t>::vertex_descriptor vertex_t;
  625. graph_t G;
  626. std::vector<vertex_t> verts(4);
  627. for (int i = 0; i < 4; ++i)
  628. verts[i] = add_vertex(Index(i, Name('a' + i)), G);
  629. add_edge(verts[1], verts[2], G);
  630. add_edge(verts[1], verts[3], G);
  631. add_edge(verts[2], verts[1], G);
  632. add_edge(verts[3], verts[2], G);
  633. add_edge(verts[3], verts[0], G);
  634. std::cout << "Graph G:" << std::endl;
  635. print_graph(G, get(vertex_name, G));
  636. adjacency_list<> TC;
  637. transitive_closure(G, TC);
  638. std::cout << std::endl << "Graph G+:" << std::endl;
  639. char name[] = "abcd";
  640. print_graph(TC, name);
  641. std::cout << std::endl;
  642. std::ofstream out("tc-out.dot");
  643. write_graphviz(out, TC, make_label_writer(name));
  644. return 0;
  645. }
  646. @}
  647. \bibliographystyle{abbrv}
  648. \bibliography{jtran,ggcl,optimization,generic-programming,cad}
  649. \end{document}
  650. % LocalWords: Siek Prus Succ typename GraphTC VertexIndexMap const tc typedefs
  651. % LocalWords: typedef iterator adjacency SCC num scc CG cg resize SCCs di ch
  652. % LocalWords: traversal ith namespace topo inserter gx hy struct pos inf max
  653. % LocalWords: rbegin vec si hpp ifndef endif jtran ggcl