Difference between revisions of "Boost/BGL/RelabelInputVertices"

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search
(Created page with '==RelabelInputVertices.cpp== <source lang="cpp"> // http://www.boost.org/doc/libs/1_37_0/libs/graph/doc/read_graphviz.html #include <iostream> #include <string> #include <fstream…')
 
Line 9: Line 9:
 
#include <boost/graph/graphviz.hpp>
 
#include <boost/graph/graphviz.hpp>
  
typedef boost::property < boost::vertex_name_t, std::string> VertexProperty;
+
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS> Graph;
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> Graph;
+
 
 
+
 
void OutputEdges(const Graph& g);
 
void OutputEdges(const Graph& g);
  
Line 109: Line 108:
 
}");
 
}");
  
 
+
   // Create a graph type with a vertex property to store the id of the vertices in the graphviz file
   Graph graph;
+
  typedef boost::property < boost::vertex_name_t, std::string> VertexProperty;
   boost::dynamic_properties dp; // not used
+
   typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> GraphFromFile;
 
    
 
    
   boost::property_map<Graph, boost::vertex_name_t>::type name =
+
  // Read the graphviz file
     get(boost::vertex_name, graph);
+
  GraphFromFile graphFromFile;
 +
  boost::dynamic_properties dp;
 +
 
 +
   boost::property_map<GraphFromFile, boost::vertex_name_t>::type name =
 +
     get(boost::vertex_name, graphFromFile);
 
   dp.property("node_id",name);
 
   dp.property("node_id",name);
 
    
 
    
   boost::read_graphviz(ss,graph,dp);
+
   boost::read_graphviz(ss,graphFromFile,dp);
  
   boost::property_map<Graph, boost::vertex_name_t>::type value = boost::get(boost::vertex_name_t(), graph);
+
  // Create a property_map of the input vertex ids
 +
   boost::property_map<GraphFromFile, boost::vertex_name_t>::type value = boost::get(boost::vertex_name_t(), graphFromFile);
 
    
 
    
   typedef Graph::vertex_iterator vertex_iter;
+
   // Create a new graph of the desired type
   std::pair<vertex_iter, vertex_iter> vertexPair;
+
  Graph graph(boost::num_vertices(graphFromFile));
   for (vertexPair = boost::vertices(graph); vertexPair.first != vertexPair.second; ++vertexPair.first)
+
 
    {
+
  // Iterate over the edges of the input graph and create edges in the output graph on the corresponding vertices
     std::cout << *vertexPair.first <<  " : " << value[*vertexPair.first] << std::endl;
+
   std::pair<GraphFromFile::edge_iterator, GraphFromFile::edge_iterator> edgePair;
    }
+
   for(edgePair = boost::edges(graphFromFile); edgePair.first != edgePair.second; ++edgePair.first)
 +
  {
 +
     std::stringstream ssSource(value[boost::source(*edgePair.first, graphFromFile)]);
 +
    std::stringstream ssTarget(value[boost::target(*edgePair.first, graphFromFile)]);
 +
    unsigned int source = 0;
 +
    unsigned int target = 0;
 +
    ssSource >> source;
 +
    ssTarget >> target;
 +
    boost::add_edge(source, target, graph);
 +
  }
 
   return graph;
 
   return graph;
 
}
 
}

Revision as of 15:16, 26 June 2011

RelabelInputVertices.cpp

// http://www.boost.org/doc/libs/1_37_0/libs/graph/doc/read_graphviz.html
#include <iostream>
#include <string>
#include <fstream>
 
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
 
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS> Graph;
 
void OutputEdges(const Graph& g);
 
Graph CreateGraph();
Graph ReadGraph();
 
int main(int argc, char*argv[])
{
  std::cout << "Created" << std::endl;
 
  OutputEdges(CreateGraph());
 
  std::cout << std::endl << "Read" << std::endl;
 
  OutputEdges(ReadGraph());
 
  return 0;
}
 
void OutputEdges(const Graph& g)
{
  std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g);
  for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator)
    {
      std::cout << "Edge exists between " << boost::target(*edgeIterator, g) << " and " 
                <<  boost::source(*edgeIterator, g) << std::endl;
    }  
}
 
Graph CreateGraph()
{
  Graph g(19);
 
  boost::add_edge(0,1,g);
  boost::add_edge(1,2,g);
  boost::add_edge(2,3,g);
  boost::add_edge(3,4,g);
  boost::add_edge(4,5,g);
  boost::add_edge(5,6,g);
  boost::add_edge(6,7,g);
  boost::add_edge(7,8,g);
  boost::add_edge(7,9,g);
  boost::add_edge(9,10,g);
  boost::add_edge(7,11,g);
  boost::add_edge(11,12,g);
  boost::add_edge(12,13,g);
  boost::add_edge(13,14,g);
  boost::add_edge(13,15,g);
  boost::add_edge(15,16,g);
  boost::add_edge(16,17,g);
  boost::add_edge(17,18,g);
 
  return g;
}
 
 
Graph ReadGraph()
{
  std::stringstream ss("graph G { \
0; \
1; \
2; \
3; \
4; \
5; \
6; \
7; \
8; \
9; \
10; \
11; \
12; \
13; \
14; \
15; \
16; \
17; \
18; \
0--1 ; \
1--2 ; \
2--3 ; \
3--4 ; \
4--5 ; \
5--6 ; \
6--7 ; \
7--8 ; \
7--9 ; \
9--10 ; \
7--11 ; \
11--12 ; \
12--13 ; \
13--14 ; \
13--15 ; \
15--16 ; \
16--17 ; \
17--18 ; \
}");
 
  // Create a graph type with a vertex property to store the id of the vertices in the graphviz file
  typedef boost::property < boost::vertex_name_t, std::string> VertexProperty;
  typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> GraphFromFile;
 
  // Read the graphviz file
  GraphFromFile graphFromFile;
  boost::dynamic_properties dp;
 
  boost::property_map<GraphFromFile, boost::vertex_name_t>::type name =
    get(boost::vertex_name, graphFromFile);
  dp.property("node_id",name);
 
  boost::read_graphviz(ss,graphFromFile,dp);
 
  // Create a property_map of the input vertex ids
  boost::property_map<GraphFromFile, boost::vertex_name_t>::type value = boost::get(boost::vertex_name_t(), graphFromFile);
 
  // Create a new graph of the desired type
  Graph graph(boost::num_vertices(graphFromFile));
 
  // Iterate over the edges of the input graph and create edges in the output graph on the corresponding vertices
  std::pair<GraphFromFile::edge_iterator, GraphFromFile::edge_iterator> edgePair;
  for(edgePair = boost::edges(graphFromFile); edgePair.first != edgePair.second; ++edgePair.first)
  {
    std::stringstream ssSource(value[boost::source(*edgePair.first, graphFromFile)]);
    std::stringstream ssTarget(value[boost::target(*edgePair.first, graphFromFile)]);
    unsigned int source = 0;
    unsigned int target = 0;
    ssSource >> source;
    ssTarget >> target;
    boost::add_edge(source, target, graph);
  }
  return graph;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(RelabelInputVertices)
 
FIND_PACKAGE(Boost)
 
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
 
ADD_EXECUTABLE(RelabelInputVertices RelabelInputVertices.cpp)
target_link_libraries(RelabelInputVertices boost_graph)