]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright 2009 Eric Bose-Wolf |
2 | // | |
3 | // Use, modification and distribution are subject to the | |
4 | // Boost Software License, Version 1.0 (See accompanying file | |
5 | // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #ifndef BOOST_GRAPH_TRANSITIVE_REDUCTION_HPP | |
8 | #define BOOST_GRAPH_TRANSITIVE_REDUCTION_HPP | |
9 | ||
10 | #include <vector> | |
11 | #include <algorithm> //std::find | |
12 | #include <boost/concept/requires.hpp> | |
13 | #include <boost/concept_check.hpp> | |
14 | ||
15 | #include <boost/graph/graph_traits.hpp> | |
16 | #include <boost/graph/topological_sort.hpp> | |
17 | ||
18 | // also I didn't got all of the concepts thin. Am I suppose to check | |
19 | // for all concepts, which are needed for functions I call? (As if I | |
20 | // wouldn't do that, the users would see the functions called by | |
21 | // complaining about missings concepts, which would be clearly an error | |
22 | // message revealing internal implementation and should therefore be avoided?) | |
23 | ||
24 | // the pseudocode which I followed implementing this algorithmn was taken | |
25 | // from the german book Algorithmische Graphentheorie by Volker Turau | |
26 | // it is proposed to be of O(n + nm_red ) where n is the number | |
27 | // of vertices and m_red is the number of edges in the transitive | |
28 | // reduction, but I think my implementation spoiled this up at some point | |
29 | // indicated below. | |
30 | ||
31 | namespace boost { | |
32 | ||
33 | template < | |
34 | typename Graph, typename GraphTR, typename G_to_TR_VertexMap, | |
35 | typename VertexIndexMap | |
36 | > | |
37 | BOOST_CONCEPT_REQUIRES( | |
38 | ((VertexListGraphConcept< Graph >)) | |
39 | ((IncidenceGraphConcept< Graph >)) | |
40 | ((MutableGraphConcept< GraphTR >)) | |
41 | ((ReadablePropertyMapConcept< VertexIndexMap, | |
42 | typename graph_traits<Graph>::vertex_descriptor >)) | |
43 | ((Integer< typename | |
44 | property_traits< VertexIndexMap >::value_type >)) | |
45 | ((LvaluePropertyMapConcept< G_to_TR_VertexMap, | |
46 | typename graph_traits<Graph>::vertex_descriptor >)), | |
47 | (void)) | |
48 | transitive_reduction(const Graph& g, GraphTR& tr, | |
49 | G_to_TR_VertexMap g_to_tr_map, | |
50 | VertexIndexMap g_index_map ) | |
51 | { | |
52 | typedef typename graph_traits<Graph>::vertex_descriptor Vertex; | |
53 | typedef typename graph_traits<Graph>::vertex_iterator VertexIterator; | |
54 | typedef typename std::vector<Vertex>::size_type size_type; | |
55 | ||
56 | std::vector<Vertex> topo_order; | |
57 | topological_sort(g, std::back_inserter(topo_order)); | |
58 | ||
59 | std::vector<size_type> topo_number_storage(num_vertices(g)); | |
60 | ||
61 | iterator_property_map<size_type*, VertexIndexMap, | |
62 | size_type, size_type&> topo_number( &topo_number_storage[0], g_index_map ); | |
63 | ||
64 | { | |
65 | typename std::vector<Vertex>::reverse_iterator it = topo_order.rbegin(); | |
66 | size_type n = 0; | |
67 | for(; it != topo_order.rend(); ++it,++n ) { | |
68 | topo_number[ *it ] = n; | |
69 | } | |
70 | } | |
71 | ||
72 | std::vector< std::vector< bool > > edge_in_closure(num_vertices(g), | |
73 | std::vector<bool>( num_vertices(g), false)); | |
74 | { | |
75 | typename std::vector<Vertex>::reverse_iterator it = topo_order.rbegin(); | |
76 | for( ; it != topo_order.rend(); ++it ) { | |
77 | g_to_tr_map[*it] = add_vertex(tr); | |
78 | } | |
79 | } | |
80 | ||
81 | typename std::vector<Vertex>::iterator | |
82 | it = topo_order.begin(), | |
83 | end = topo_order.end(); | |
84 | for( ; it != end; ++it ) { | |
85 | size_type i = topo_number[ *it ]; | |
86 | edge_in_closure[i][i] = true; | |
87 | std::vector<Vertex> neighbors; | |
88 | ||
89 | //I have to collect the successors of *it and traverse them in | |
90 | //ascending topological order. I didn't know a better way, how to | |
91 | //do that. So what I'm doint is, collection the successors of *it here | |
92 | { | |
93 | typename Graph::out_edge_iterator oi,oi_end; | |
94 | for( boost::tie(oi, oi_end) = out_edges( *it, g ); oi != oi_end; ++oi ) { | |
95 | neighbors.push_back( target( *oi, g ) ); | |
96 | } | |
97 | } | |
98 | ||
99 | { | |
100 | //and run through all vertices in topological order | |
101 | typename std::vector<Vertex>::reverse_iterator | |
102 | rit = topo_order.rbegin(), | |
103 | rend = topo_order.rend(); | |
104 | for(; rit != rend; ++rit ) { | |
105 | //looking if they are successors of *it | |
106 | if( std::find( neighbors.begin(), neighbors.end(), *rit) != neighbors.end() ) { | |
107 | size_type j = topo_number[ *rit ]; | |
108 | if( not edge_in_closure[i][j] ) { | |
109 | for(size_type k = j; k < num_vertices(g); ++k) { | |
110 | if( not edge_in_closure[i][k] ) { | |
111 | //here we need edge_in_closure to be in topological order, | |
112 | edge_in_closure[i][k] = edge_in_closure[j][k]; | |
113 | } | |
114 | } | |
115 | //therefore we only access edge_in_closure only through | |
116 | //topo_number property_map | |
117 | add_edge(g_to_tr_map[*it], g_to_tr_map[*rit], tr); | |
118 | } //if ( not edge_in_ | |
119 | } //if (find ( | |
120 | } //for( typename vector<Vertex>::reverse_iterator | |
121 | } // { | |
122 | ||
123 | } //for( typename vector<Vertex>::iterator | |
124 | ||
125 | } //void transitive_reduction | |
126 | ||
127 | } // namespace boost | |
128 | ||
129 | #endif | |
130 |