1 // (C) Copyright 2009 Eric Bose-Wolf
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)
7 #ifndef BOOST_GRAPH_TRANSITIVE_REDUCTION_HPP
8 #define BOOST_GRAPH_TRANSITIVE_REDUCTION_HPP
11 #include <algorithm> //std::find
12 #include <boost/concept/requires.hpp>
13 #include <boost/concept_check.hpp>
15 #include <boost/graph/graph_traits.hpp>
16 #include <boost/graph/topological_sort.hpp>
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?)
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
34 typename Graph
, typename GraphTR
, typename G_to_TR_VertexMap
,
35 typename VertexIndexMap
37 BOOST_CONCEPT_REQUIRES(
38 ((VertexListGraphConcept
< Graph
>))
39 ((IncidenceGraphConcept
< Graph
>))
40 ((MutableGraphConcept
< GraphTR
>))
41 ((ReadablePropertyMapConcept
< VertexIndexMap
,
42 typename graph_traits
<Graph
>::vertex_descriptor
>))
44 property_traits
< VertexIndexMap
>::value_type
>))
45 ((LvaluePropertyMapConcept
< G_to_TR_VertexMap
,
46 typename graph_traits
<Graph
>::vertex_descriptor
>)),
48 transitive_reduction(const Graph
& g
, GraphTR
& tr
,
49 G_to_TR_VertexMap g_to_tr_map
,
50 VertexIndexMap g_index_map
)
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
;
56 std::vector
<Vertex
> topo_order
;
57 topological_sort(g
, std::back_inserter(topo_order
));
59 std::vector
<size_type
> topo_number_storage(num_vertices(g
));
61 iterator_property_map
<size_type
*, VertexIndexMap
,
62 size_type
, size_type
&> topo_number( &topo_number_storage
[0], g_index_map
);
65 typename
std::vector
<Vertex
>::reverse_iterator it
= topo_order
.rbegin();
67 for(; it
!= topo_order
.rend(); ++it
,++n
) {
68 topo_number
[ *it
] = n
;
72 std::vector
< std::vector
< bool > > edge_in_closure(num_vertices(g
),
73 std::vector
<bool>( num_vertices(g
), false));
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
);
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
;
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
93 typename
Graph::out_edge_iterator oi
,oi_end
;
94 for( tie(oi
, oi_end
) = out_edges( *it
, g
); oi
!= oi_end
; ++oi
) {
95 neighbors
.push_back( target( *oi
, g
) );
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
];
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_
120 } //for( typename vector<Vertex>::reverse_iterator
123 } //for( typename vector<Vertex>::iterator
125 } //void transitive_reduction