My Experiments with Boost Graph Library


Recently I picked up “An Introduction to Graph Theory” by Robin J. Wilson. After going through a couple of chapters I felt the sudden urge to try out a few of graph theory problems through programming. To try out the algorithms I first need to create the graphs themselves, which seemed a bit of a tedious task. But then thats what the boost graph Library is there for.

The BGL has made it very easy to create graphs and perform any operations on them.

Below is a simple program that lets you create a graph using the BGL.

#include<iostream>
 #include<boost/graph/adjacency_list.hpp>
 #include<boost/graph/graphviz.hpp>

int main(){
 boost::adjacency_list<> graph;
 int noVert = 0;
 int start=0,end=0;
 char choice = 'y';

 while(choice == 'y') {
 std::cout<<"Enter the start and end vertex of the edge: ";
 std::cin>>start>>end;
 boost::add_edge(start,end,graph);
 std::cout<<"Do you want to add another edge(y/n): ";
 std::cin>>choice;
 }

boost::write_graphviz(std::cout,graph);
 return 0;
 }

The coolest feature of BGL is that it allows you to write your graph as ‘dot’ file, which can be used to by Graphviz to visualize. Checkout my github repo for more BGL based programs.

Advertisements

About vineethkartha

Hi, I am Vineeth Kartha, I hold a Masters degree in Embedded Systems from BITS Pilani, Goa Campus. I currently work as a Software Developer. I am A free software promoter and electronics hobbyist. In my free time I do a little bit of birding and Wildlife photography. I love travelling, reading and blogging.

Posted on May 29, 2016, in Technical. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: