Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) BoostGraphExample2


BoostQt CreatorLubuntu


Boost.Graph example 2: four human names and their relationships + plotting is a Boost.Graph example. It defines a graph of person names and their relationships. Then the graph is written to .dot file and plotted using KGraphViewer.


Technical facts


Operating system(s) or programming environment(s)


Project type:

C++ standard:


Libraries used:






Qt project file: ./CppBoostGraphExample2/CppBoostGraphExample2.pro


include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
# }
# QMAKE_CXXFLAGS += -std=c++1y -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

# win32 {
#     ../../Libraries/boost_1_54_0
# }

SOURCES += main.cpp








#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#pragma GCC diagnostic pop
int main()
  //Define the type of graph:
  //boost::adjacency_list is the 'Swiss army knife' graph
  typedef boost::adjacency_list
    //Store all edges as a std::vector
    //Store all vertices in a std::vector
    //Relations are both ways (in this example)
    //(note: but you can freely change it to boost::directedS)
    //All vertices are person names of type std::string
    //All edges are relation of type std::string
  > Graph;

  Graph  g;

  std::vector<std::string> names;
  names.push_back("Mr. A");
  names.push_back("Mrs. B");
  names.push_back("Dr. C");
  names.push_back("Prof. D");

  const Graph::vertex_descriptor v0 = boost::add_vertex(names[0],g);
  const Graph::vertex_descriptor v1 = boost::add_vertex(names[1],g);
  const Graph::vertex_descriptor v2 = boost::add_vertex(names[2],g);
  const Graph::vertex_descriptor v3 = boost::add_vertex(names[3],g);

  std::vector<std::string> relations;


  //Writing graph to file
    std::ofstream f("test.dot");
    //- All relationships are omitted

  //View the output directly using KGraphViewerer
  std::system("kgraphviewer test.dot");






Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml