Boost/BGL/CreateGraph

From ProgrammingExamples
< Boost‎ | BGL
Revision as of 15:28, 26 January 2011 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Timer.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 adjacency_list<vecS, vecS, bidirectionalS> Graph;
/*
void OutputVertices(const Graph &g);
void OutputEdges(const Graph &g);
*/
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 property_map<Graph, vertex_index_t>::type IndexMap;
  IndexMap index = get(vertex_index, g);
 
  std::cout << "vertices = ";
  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;
 
 
  // Get the edges
  std::cout << "edges = ";
  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)] << ") ";
  }
  std::cout << std::endl;
 
  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;
}
*/

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)