Difference between revisions of "Boost/BGL/CreateGraph"

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search
(Created page with '==Timer.cpp== <source lang="cpp"> #include <iostream> // for std::cout #include <utility> // for std::pair #include <algorithm> …')
 
Line 1: Line 1:
==Timer.cpp==
+
==CreateGraph.cpp==
 
<source lang="cpp">
 
<source lang="cpp">
 
#include <iostream>                  // for std::cout
 
#include <iostream>                  // for std::cout
Line 7: Line 7:
 
#include <boost/graph/adjacency_list.hpp>
 
#include <boost/graph/adjacency_list.hpp>
  
using namespace boost;
+
//using namespace boost;
 +
 
 +
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> Graph;
  
typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;
 
/*
 
void OutputVertices(const Graph &g);
 
void OutputEdges(const Graph &g);
 
*/
 
 
int main(int,char*[])
 
int main(int,char*[])
 
{
 
{
Line 23: Line 20:
  
 
   // Get the vertices
 
   // Get the vertices
   typedef property_map<Graph, vertex_index_t>::type IndexMap;
+
   typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap;
  IndexMap index = get(vertex_index, g);
+
  
 +
  // "Ask a question" of a graph. The IndexMap is the solution set.
 +
  IndexMap index = get(boost::vertex_index, g);
 +
 
 
   std::cout << "vertices = ";
 
   std::cout << "vertices = ";
   typedef graph_traits<Graph>::vertex_iterator vertex_iter;
+
   typedef boost::graph_traits<Graph>::vertex_iterator vertex_iter;
   std::pair<vertex_iter, vertex_iter> vp;
+
   std::pair<vertex_iter, vertex_iter> vertexPair;
   for (vp = vertices(g); vp.first != vp.second; ++vp.first)
+
   // The call to vertices(g) sets vp to (0, numberOfElements)
 +
  // The loop, therefore, starts at 0, and increments the 'first' of the vertexPair
 +
  // until it equals the 'second', which again is the numberOfElements.
 +
  for (vertexPair = vertices(g); vertexPair.first != vertexPair.second; ++vertexPair.first)
 
   {
 
   {
  std::cout << index[*vp.first] <<  " ";
+
    // You can see what is going on with this:
 +
//std::cout << "first: " << index[*vertexPair.first] <<  " second: " << index[*vp.second] << std::endl; // the vertex iterator
 +
 
 +
// Or just output the vertex ids
 +
std::cout << index[*vertexPair.first] <<  " ";
 
   }
 
   }
 
   std::cout << std::endl;
 
   std::cout << std::endl;
  
 
  // Get the edges
 
 
   std::cout << "edges = ";
 
   std::cout << "edges = ";
   graph_traits<Graph>::edge_iterator ei, ei_end;
+
   typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
   for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
+
  std::pair<edge_iter, edge_iter> edgePair;
 +
   for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first)
 
   {
 
   {
  std::cout << "(" << index[source(*ei, g)]
+
      std::cout << "(" << index[source(*edgePair.first, g)]
  << "," << index[target(*ei, g)] << ") ";
+
              << "," << index[target(*edgePair.first, g)] << ") ";
 
   }
 
   }
 
   std::cout << std::endl;
 
   std::cout << std::endl;
 
 
   return 0;
 
   return 0;
 
}
 
}
  
 
/*
 
void OutputVertices(const Graph &g)
 
{
 
  // get the vertices
 
  typedef property_map<Graph, vertex_index_t>::type IndexMap;
 
  IndexMap index = get(vertex_index, g);
 
 
  std::cout << "vertices(g) = ";
 
  typedef graph_traits<Graph>::vertex_iterator vertex_iter;
 
  std::pair<vertex_iter, vertex_iter> vp;
 
  for (vp = vertices(g); vp.first != vp.second; ++vp.first)
 
  {
 
  std::cout << index[*vp.first] <<  " ";
 
  }
 
  std::cout << std::endl;
 
}
 
 
void OutputEdges(const Graph &g)
 
{
 
  typedef property_map<Graph, vertex_index_t>::type IndexMap;
 
  IndexMap index = get(vertex_index, g);
 
 
  //get the edges
 
  std::cout << "edges(g) = ";
 
  graph_traits<Graph>::edge_iterator ei, ei_end;
 
  for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
 
  {
 
  std::cout << "(" << index[source(*ei, g)]
 
  << "," << index[target(*ei, g)] << ") ";
 
 
  //property_map<Graph, edge_weight_t>::type edge_weight = get(edge_weight_t(), g);
 
  }
 
  std::cout << std::endl;
 
}
 
*/
 
 
</source>
 
</source>
  

Revision as of 16:16, 26 January 2011

CreateGraph.cpp

#include <iostream>                  // for std::cout
#include <utility>                   // for std::pair
#include <algorithm>                 // for std::for_each
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
 
//using namespace boost;
 
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> Graph;
 
int main(int,char*[])
{
  // Construct a graph
  Graph g(3); // 3 vertices
 
  add_edge(0, 1, g);
  add_edge(1, 2, g);
 
  // Get the vertices
  typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap;
 
  // "Ask a question" of a graph. The IndexMap is the solution set.
  IndexMap index = get(boost::vertex_index, g);
 
  std::cout << "vertices = ";
  typedef boost::graph_traits<Graph>::vertex_iterator vertex_iter;
  std::pair<vertex_iter, vertex_iter> vertexPair;
  // The call to vertices(g) sets vp to (0, numberOfElements)
  // The loop, therefore, starts at 0, and increments the 'first' of the vertexPair
  // until it equals the 'second', which again is the numberOfElements.
  for (vertexPair = vertices(g); vertexPair.first != vertexPair.second; ++vertexPair.first)
  {
    // You can see what is going on with this:
	//std::cout << "first: " << index[*vertexPair.first] <<  " second: " << index[*vp.second] << std::endl; // the vertex iterator
 
	// Or just output the vertex ids
	std::cout << index[*vertexPair.first] <<  " ";
  }
  std::cout << std::endl;
 
  std::cout << "edges = ";
  typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
  std::pair<edge_iter, edge_iter> edgePair;
  for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first)
  {
      std::cout << "(" << index[source(*edgePair.first, g)]
              << "," << index[target(*edgePair.first, g)] << ") ";
  }
  std::cout << std::endl;
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(ConstructGraph)
 
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(ConstructGraph ConstructGraph.cpp)
target_link_libraries(ConstructGraph)