Difference between revisions of "Boost/BGL/IO/ReadGraph"

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search
(Created page with '==ReadGraph.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> #include…')
 
(Simplified)
Line 16: Line 16:
 
     return -1;
 
     return -1;
 
   }
 
   }
  // Vertex properties
 
  typedef boost::property < boost::vertex_name_t, std::string,
 
            boost::property < boost::vertex_color_t, float > > vertex_p;
 
  // Edge properties
 
  typedef boost::property < boost::edge_weight_t, double > edge_p;
 
  // Graph properties
 
  typedef boost::property < boost::graph_name_t, std::string > graph_p;
 
  // adjacency_list-based type
 
  typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS,
 
    vertex_p, edge_p, graph_p > graph_t;
 
  
   // Construct an empty graph and prepare the dynamic_property_maps.
+
  typedef boost::property < boost::vertex_name_t, std::string> VertexProperty;
   graph_t graph(0);
+
  typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, VertexProperty> graph_t;
 +
   //typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::no_property> graph_t;
 +
  //typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::null_property_map> graph_t;
 +
   graph_t graph;
 
   boost::dynamic_properties dp;
 
   boost::dynamic_properties dp;
  
Line 34: Line 27:
 
     get(boost::vertex_name, graph);
 
     get(boost::vertex_name, graph);
 
   dp.property("node_id",name);
 
   dp.property("node_id",name);
 
  boost::property_map<graph_t, boost::vertex_color_t>::type mass =
 
    get(boost::vertex_color, graph);
 
  dp.property("mass",mass);
 
 
  boost::property_map<graph_t, boost::edge_weight_t>::type weight =
 
    get(boost::edge_weight, graph);
 
  dp.property("weight",weight);
 
 
  // Use ref_property_map to turn a graph property into a property map
 
  boost::ref_property_map<graph_t*,std::string>
 
    gname(get_property(graph,boost::graph_name));
 
  dp.property("name",gname);
 
 
  /*
 
  // Sample graph as an std::istream;
 
  std::istringstream
 
    gvgraph("digraph { graph [name=\"graphname\"]  a  c e [mass = 6.66] }");
 
 
  bool status = boost::read_graphviz(gvgraph,graph,dp,"node_id");
 
  */
 
  
 
   std::string filename = argv[1];
 
   std::string filename = argv[1];
Line 65: Line 37:
 
   return 0;
 
   return 0;
 
}
 
}
 +
 +
</source>
 +
 +
==HelloWorld.dot==
 +
<source lang="text">
 +
digraph G {Hello->World}
 
</source>
 
</source>
  
Line 73: Line 51:
 
Project(ReadGraph)
 
Project(ReadGraph)
  
set(Boost_USE_MULTITHREADED ON)
+
FIND_PACKAGE(Boost)
FIND_PACKAGE(Boost 1.38 COMPONENTS program_options required)
+
  
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
 
 
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
 
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
  
Line 83: Line 59:
  
  
</source>
 
 
==HelloWorld.dot==
 
<source lang="text">
 
digraph G {Hello->World}
 
 
</source>
 
</source>

Revision as of 19:36, 22 June 2011

ReadGraph.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>
 
int main(int argc, char*argv[])
{
  if(argc < 2)
  {
    std::cerr << "Required: filename.dot" << std::endl;
    return -1;
  }
 
  typedef boost::property < boost::vertex_name_t, std::string> VertexProperty;
  typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, VertexProperty> graph_t;
  //typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::no_property> graph_t;
  //typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::null_property_map> graph_t;
  graph_t graph;
  boost::dynamic_properties dp;
 
  boost::property_map<graph_t, boost::vertex_name_t>::type name =
    get(boost::vertex_name, graph);
  dp.property("node_id",name);
 
  std::string filename = argv[1];
  std::ifstream fin(filename.c_str());
 
  bool status = boost::read_graphviz(fin,graph,dp,"node_id");
 
  std::cout << "There are " << boost::num_vertices(graph) << " vertices." << std::endl;
 
  return 0;
}

HelloWorld.dot

digraph G {Hello->World}

CMakeLists.txt

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