annotate DEPENDENCIES/generic/include/boost/graph/transitive_reduction.hpp @ 118:770eb830ec19 emscripten

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