Difference between revisions of "CPP/Boost/BGL/FilteredGraphVertices"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==FilteredGraph.cpp== <source lang="cpp"> #include <iostream> #include <boost/graph/adjacency_list.hpp> #include <boost/array.hpp> #include <boost/graph/filtered_graph.hpp> #inc…')
 
(Get a vertex_iterator)
 
(One intermediate revision by the same user not shown)
Line 19: Line 19:
 
int main()
 
int main()
 
{
 
{
  using namespace boost;
 
  
   typedef adjacency_list<vecS, vecS, directedS,
+
   typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
     no_property, property<edge_weight_t, int> > Graph;
+
     boost::no_property, boost::property<boost::edge_weight_t, int> > Graph;
   typedef property_map<Graph, edge_weight_t>::type EdgeWeightMap;
+
   typedef boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap;
  
 
   unsigned int numberOfVertices = 10;
 
   unsigned int numberOfVertices = 10;
Line 36: Line 35:
  
 
   vertex_id_filter<Graph> filter;
 
   vertex_id_filter<Graph> filter;
   filtered_graph<Graph, boost::keep_all, vertex_id_filter<Graph> > filteredGraph(g, boost::keep_all(), filter); // (graph, EdgePredicate, VertexPredicate)
+
   typedef boost::filtered_graph<Graph, boost::keep_all, vertex_id_filter<Graph> > FilteredGraphType;
 +
  FilteredGraphType filteredGraph(g, boost::keep_all(), filter); // (graph, EdgePredicate, VertexPredicate)
  
 
   std::cout << "Filtered graph:" << std::endl;
 
   std::cout << "Filtered graph:" << std::endl;
 
   boost::print_graph(filteredGraph);
 
   boost::print_graph(filteredGraph);
 +
 
 +
  FilteredGraphType::vertex_iterator ui,ui_end; tie(ui,ui_end) = vertices(filteredGraph);
 +
 
   return 0;
 
   return 0;
 
}
 
}
Line 49: Line 52:
 
cmake_minimum_required(VERSION 2.6)
 
cmake_minimum_required(VERSION 2.6)
  
Project(DepthFirstSearch)
+
Project(FilteredGraphVertices)
  
 
set(Boost_USE_MULTITHREADED ON)
 
set(Boost_USE_MULTITHREADED ON)
Line 57: Line 60:
 
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
 
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
  
ADD_EXECUTABLE(FilteredGraph FilteredGraph.cpp)
+
ADD_EXECUTABLE(FilteredGraphVertices FilteredGraphVertices.cpp)
  
 
</source>
 
</source>

Latest revision as of 11:25, 26 January 2012

FilteredGraph.cpp

#include <iostream>
 
#include <boost/graph/adjacency_list.hpp>
#include <boost/array.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/graph_utility.hpp>
 
template <typename TGraph>
struct vertex_id_filter
{
  bool operator()(const typename boost::graph_traits<TGraph>::vertex_descriptor& v) const
  {
    return 3 < v; // keep all vertx_descriptors greater than 3
  }
};
 
int main()
{
 
  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
    boost::no_property, boost::property<boost::edge_weight_t, int> > Graph;
  typedef boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap;
 
  unsigned int numberOfVertices = 10;
  Graph g(numberOfVertices);
  for(unsigned int i = 0; i < numberOfVertices - 1; ++i)
    {
    add_edge(i, i+1, g);
    }
 
  std::cout << "Original graph:" << std::endl;
  boost::print_graph(g);
 
  vertex_id_filter<Graph> filter;
  typedef boost::filtered_graph<Graph, boost::keep_all, vertex_id_filter<Graph> > FilteredGraphType;
  FilteredGraphType filteredGraph(g, boost::keep_all(), filter); // (graph, EdgePredicate, VertexPredicate)
 
  std::cout << "Filtered graph:" << std::endl;
  boost::print_graph(filteredGraph);
 
  FilteredGraphType::vertex_iterator ui,ui_end; tie(ui,ui_end) = vertices(filteredGraph);
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(FilteredGraphVertices)
 
set(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost 1.38 COMPONENTS required)
 
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
 
ADD_EXECUTABLE(FilteredGraphVertices FilteredGraphVertices.cpp)