TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/host/graph/InternalGraph.tcc
Go to the documentation of this file.
00001 #include "GraphExporter.hpp"
00002 
00003 #include <boost/graph/graphviz.hpp>
00004 #include <boost/graph/connected_components.hpp>
00005 
00006 
00007 namespace tuttle {
00008 namespace host {
00009 namespace graph {
00010 
00011 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00012 void InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::toDominatorTree()
00013 {
00014         typedef typename boost::property_map<GraphContainer, boost::vertex_index_t>::type IndexMap;
00015         typedef typename std::vector<vertex_descriptor >::iterator VectorDescIter;
00016         typedef boost::iterator_property_map<VectorDescIter, IndexMap > PredMap;
00017 
00018         std::vector<vertex_descriptor> domTreePredVector;
00019         IndexMap indexMap( get( boost::vertex_index, _graph ) );
00020         vertex_iterator uItr;
00021         vertex_iterator uEnd;
00022         int j = 0;
00023         for( boost::tie( uItr, uEnd ) = vertices( _graph ); uItr != uEnd; ++uItr, ++j )
00024         {
00025                 put( indexMap, *uItr, j );
00026         }
00027 
00028         // Lengauer-Tarjan dominator tree algorithm
00029         domTreePredVector = std::vector<vertex_descriptor>( num_vertices( _graph ), boost::graph_traits<GraphContainer>::null_vertex() );
00030         PredMap domTreePredMap =
00031             boost::make_iterator_property_map( domTreePredVector.begin(), indexMap );
00032 
00033         boost::lengauer_tarjan_dominator_tree( _graph, vertex( 0, _graph ), domTreePredMap );
00034 
00035         for( boost::tie( uItr, uEnd ) = vertices( _graph ); uItr != uEnd; ++uItr )
00036                 boost::clear_vertex( *uItr, _graph );
00037 
00038         for( boost::tie( uItr, uEnd ) = vertices( _graph ); uItr != uEnd; ++uItr )
00039         {
00040                 if( get( domTreePredMap, *uItr ) != boost::graph_traits<GraphContainer>::null_vertex() )
00041                 {
00042                         add_edge( get( domTreePredMap, *uItr ), *uItr, _graph );
00043                         //get(indexMap, *uItr) indice du vertex courant
00044                         //get(domTreePredMap, *uItr) descriptor du dominator
00045                         //get(indexMap, get(domTreePredMap, *uItr)) indice du dominator
00046                 }
00047         }
00048 }
00049 
00050 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00051 std::vector<typename InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::vertex_descriptor>
00052 InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::rootVertices()
00053 {
00054         std::vector<vertex_descriptor> vroots;
00055         vertex_range_t vrange = getVertices();
00056         for( vertex_iterator it = vrange.first; it != vrange.second; ++it )
00057                 if( in_degree( *it, _graph ) == 0 )
00058                         vroots.push_back( *it );
00059 
00060         return vroots;
00061 }
00062 
00063 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00064 std::vector<typename InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::vertex_descriptor>
00065 InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::leafVertices()
00066 {
00067         std::vector<vertex_descriptor> vleafs;
00068         vertex_range_t vrange = getVertices();
00069         for( vertex_iterator it = vrange.first; it != vrange.second; ++it )
00070                 if( out_degree( *it, _graph ) == 0 )
00071                         vleafs.push_back( *it );
00072 
00073         return vleafs;
00074 }
00075 
00076 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00077 void InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::rebuildVertexDescriptorMap()
00078 {
00079         _vertexDescriptorMap.clear();
00080         BOOST_FOREACH( vertex_descriptor vd, getVertices() )
00081         {
00082                 _vertexDescriptorMap[instance( vd ).getKey()] = vd;
00083         }
00084 }
00085 
00086 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00087 std::vector<typename InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::vertex_descriptor>
00088 InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::getConnectedVertices( const vertex_descriptor& vroot )
00089 {
00090         std::vector<vertex_descriptor> connectedVertices;
00091         
00092     std::vector<int> componentId( getNbVertices() );
00093     boost::connected_components( _graph, &componentId[0] );
00094         
00095         int rootComponentId = componentId[vroot];
00096 
00097         for( size_t i = 0; i < componentId.size(); ++i )
00098         {
00099                 int id = componentId[i];
00100                 if( id == rootComponentId )
00101                 {
00102                         connectedVertices.push_back( i );
00103                 }
00104         }
00105 
00106         return connectedVertices;
00107 }
00108 
00109 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00110 std::vector<typename InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::vertex_descriptor>
00111 InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::getUnconnectedVertices( const vertex_descriptor& vroot )
00112 {
00113         std::vector<vertex_descriptor> unconnectedVertices;
00114         
00115     std::vector<int> componentId( getNbVertices() );
00116     boost::connected_components( _graph, &componentId[0] );
00117         
00118         int rootComponentId = componentId[vroot];
00119 
00120         for( size_t i = 0; i < componentId.size(); ++i )
00121         {
00122                 int id = componentId[i];
00123                 if( id != rootComponentId )
00124                 {
00125                         unconnectedVertices.push_back( i );
00126                 }
00127         }
00128         return unconnectedVertices;
00129 }
00130 
00131 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00132 std::size_t InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::removeUnconnectedVertices( const vertex_descriptor& vroot )
00133 {
00134         visitor::MarkUsed<This> vis( *this );
00135         this->depthFirstVisit( vis, vroot );
00136 
00137         std::list<std::string> toRemove;
00138         BOOST_FOREACH( const vertex_descriptor &vd, getVertices() )
00139         {
00140                 const Vertex& v = instance( vd );
00141 
00142                 if( !v.isUsed() )
00143                 {
00144                         toRemove.push_back( v.getName() );
00145                 }
00146         }
00147         BOOST_FOREACH( const std::string & vs, toRemove )
00148         {
00149                 //TUTTLE_TLOG( TUTTLE_TRACE, "removeVertex: " << vs );
00150                 this->removeVertex( getVertexDescriptor( vs ) );
00151         }
00152 
00153         return toRemove.size();
00154 }
00155 
00156 template< typename Vertex, typename Edge >
00157 std::ostream& operator<<( std::ostream& os, const InternalGraph<Vertex, Edge>& g )
00158 {
00159         os << "  vertex count: " << g.getVertexCount() << std::endl
00160            << "  edge count: " << g.getEdgeCount() << std::endl;
00161 
00162         exportSimple<Vertex, Edge>( os, g );
00163 
00164         return os;
00165 }
00166 
00167 }
00168 }
00169 }