OGDF GSoC Exercises 10


Problem Description

Write a program that

  1. generates a random series-parallel graph G (use OGDF’s generator, simply ignore the edge direction properties of the generated instance), and
  2. prints a readable description of how the edges/subgraphs can be combined via parallel or serial combinations, in order to obtain G.

E.g., the graph with the edges {ab,bd,ac,cd,ad} would give an output similar to (there are multiple possibilities):

1 := SER ab bd
2 := SER ac cd
3 := PAR 1 ad
G := PAR 3 2

(where the numbers just reference the lines where the corresponding subgraph was generated)

Find more details here.

Solution

/**
* This is the solution file for the Exercise 10
*
* @author Chameera Wijebandara
* @email chameerawijebandara@gmail.com
*
*	Windows 7
*	Vishual stuio 2010
*	OGDF Snapshot 2014-02-28
*
*/
#include <ogdf/basic/Graph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/SugiyamaLayout.h>
#include <ogdf/layered/OptimalRanking.h>
#include <ogdf/layered/FastHierarchyLayout.h>
#include <ogdf/layered/BarycenterHeuristic.h>
#include <map>

using namespace ogdf;
using namespace std;

// main
int main()
{

	// init the new grapth
	Graph G;

	// genarate random series paralle grapth
	randomSeriesParallelDAG(G, 6);

	// set grapth attributes
	GraphAttributes GA(G,
		GraphAttributes::nodeGraphics | GraphAttributes::edgeGraphics |
		GraphAttributes::nodeLabel | GraphAttributes::edgeStyle |
		GraphAttributes::nodeStyle );

	//maper to map node id
	map<int, int> maper;
	int nodeCount=0;
	node v;
	forall_nodes( v, G ){	// iterate through all	the node in the graph

		GA.fillColor( v ) = Color( "#FFFF00" );	// set node color to yellow
		GA.height( v ) = 20.0;	// set the height to 20.0
		GA.width( v ) = 40.0;	// set the width to 40.0

		char id = (char)(nodeCount +'a');
		GA.label( v ) = id;

		maper.insert(make_pair(v->index(), nodeCount));
		nodeCount++;
	}

	SugiyamaLayout SL;	 //init the SugiyamaLayout algorithm
	SL.setRanking(new OptimalRanking);
	SL.setCrossMin(new BarycenterHeuristic);

	FastHierarchyLayout *fhl = new FastHierarchyLayout;
	fhl->layerDistance(40.0);	// set layer distance to 40.0
	fhl->nodeDistance(20.0);	// set node distance to 20.0.
	SL.setLayout(fhl);

	SL.call(GA);
	GraphIO::drawSVG(GA, "execrise10.svg");

	int n = G.numberOfNodes(); // get number of nodes
	int m = G.numberOfEdges(); // get number of edges

	if(n==1) // if grapth only contain 1 node
	{
		cout<<"G := a";
		return 0;
	}
	if(n==2) // if grapth only contain 3 node
	{
		cout<<"G := ab";
		return 0;
	}

	// init variblaes.
	int* begin =new int[m];
	int* end =new int[m];
	int* state =new int[m];

	// subgrapth index
	int count =1;
	int countHold=1;

	// collect edeg data
	edge e;
	forall_edges(e,G){
		int temp =e->index();
		begin[temp] = maper.find(e->source()->index())->second ;
		end[temp] = maper.find(e->target()->index())->second;
		state[temp] = 0;
	}

	// count digree of the each node.
	int* inCount = new int[n];
	int* outCount = new int[n];

	//go untill complete the graph
	do{
		countHold =count;

		//init digree vaiables
		for(int i=0; i<n; i++)
		{
			inCount[i]=0;
			outCount[i]=0;
		}

		// calculate current degree
		for(int i = 0; i < m; i++)
		{
			if(state[i]!=-1)
			{
				inCount[end[i]]++;
				outCount[begin[i]]++;
			}
		}

		// gothrough the each node
		for(int i=0;i<n;i++)
		{
			// chaeck for seriall part
			if(inCount[i] == 1 && outCount[i]==1)
			{
				// go througth every edge
				for(int j=0;j<m;j++)
				{
					// check for second edge
					if(begin[j]==i && state[j]!=-1)
					{
						for(int k=0;k<m;k++)
						{
							// chcheck edge is not used
							if(end[k]==i && state[k]!=-1 && j!=k)
							{
								int tempCount=0;
								for(int l=0;l<m;l++)
								{
									if(state[l]!=-1)
									{
										tempCount++;
										if(tempCount>2)
										{
											break;
										}
									}
								}

								// print the output
								if(tempCount>2)
								{
									cout<< count;
								}
								else
								{
									cout<<"G";
								}
								cout<<" := SER ";

								if(state[k]==0)
								{
									cout<<(char)('a'+begin[k])<<(char)('a'+ end[k]);
								}
								else
								{
									cout<<state[k];

								}
								cout<<" ";
								if(state[j]==0)
								{
									cout<<(char)('a'+begin[j])<<(char)('a'+end[j]);
								}
								else
								{
									cout<<state[j];

								}
								state[j] = -1;

								cout<<endl;

								// set new edge data
								state[k] = count;
								end[k] = end[j];
								count++;

								break;
							}
						}
						break;
					}
				}
			}
		}

		// chech for pararell part
		for(int i=0;i<m;i++)
		{
			for(int j=i+1;j<m;j++)
			{
				// chaech for the  same sorse and traget
				if(begin[i] ==begin[j] &&  end[i]&& end[j] && state[i]!=-1 && state[j]!=-1)
				{
					int tempCount =0;
					for(int k=0;k<m;k++)
					{

						if(state[k]!=-1)
						{
							tempCount++;
							if(tempCount>2)
							{
								break;
							}
						}
					}

					// print the output
					if(tempCount>2)
					{
						cout<< count;
					}
					else
					{
						cout<<"G";
					}
					cout<<" := PAR ";

					if(state[i]==0)
					{
						cout<<(char)('a'+end[i])<<(char)('a'+begin[i]);
					}
					else
					{
						cout<<state[i];
					}

					cout<<" ";

					if(state[j]==0)
					{
						cout<<(char)('a'+end[j])<<(char)('a'+begin[j]);
					}
					else
					{
						cout<<state[j];
					}
					cout<<endl;

					// set new edge data
					state[j] = -1;
					state[i] = count;
					count++;

					break;
				}
			}
		}

	}while(count != countHold);

	getchar();

	// delet alocated memory

	delete(begin);
	delete(end);
	delete(state);
	delete(inCount);
	delete(outCount);

}

Algorithm


do while( undiscovered edges )

	// Find serial parts
	Find node if(indeg ==1 && out outdeg==1)	// a -> b and b-> c
		Merge the sub graph // a -> c
		Print the sub graph details	 //  i := SER ab bc

	// Find Parallel parts
	Find edges which has same source and target  // a -> c  and a -> c
		Merge the sub graph // a -> c
		Print the sub graph details	 //  i := PAR 1 ac

	// State variable mange the state of the edge
	// -1 : finished
	//  0 : not yet discovered
	// >0 : already discovered( represent a sub graph)

Output

EX_10

OGDF GSoC Exercises 05


Problem Description

Compute the DFS-tree of a given graph, direct and color the edges according to the tree, and draw the resulting graph with a hierarchical layout algorithm. In particular, your program shall perform the following steps:

  1. Create a random undirected graph G using a suitable OGDF graph generator of your choice.
  2. Perform a depth-first search on G starting at a randomly selected vertex s, thereby computing the DFS-tree as well as the DFS-number of each node (this gives the order in which nodes are visited by the search).
  3. Direct the edges in G according to their DFS-number, so that they point from lower to higher number.
  4. Compute a hierarchical drawing of G (using SugiyamaLayout), coloring the edges of the DFS-tree with red and the other edges with blue, and write the drawing to an SVG file dfs.svg.

Find more details here.

Solution



/**
*This is the solution file for the Exercise 05
*
* @author Chameera Wijebandara
* @email chameerawijebandara@gmail.com
*
*	Windows 7
*	Vishual stuio 2010
*	OGDF Snapshot 2014-02-28
*
*/

#include <ogdf/basic/Graph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/SugiyamaLayout.h>
#include <ogdf/layered/OptimalRanking.h>
#include <ogdf/layered/MedianHeuristic.h>
#include <ogdf/layered/OptimalHierarchyLayout.h>

using namespace ogdf;

bool createDFTree( const int n, const int m );
void dfs( GraphAttributes *GA, Graph *G , Array<int> *state , node *current );

// main 
int main()
{

	cout<<"Pleace enter number n (n>0) : ";
	int n,m;
	cin >> n;
	cout<<"Pleace enter number m (m>0) : ";
	cin >> m;

	createDFTree( n, m );

	return 0;
}
//end of the main

/*
Create a random undirected graph G using a suitable OGDF graph generator of your choice.
Perform a depth-first search on G starting at a randomly selected vertex s, thereby computing the DFS-tree as well as the DFS-number of each node (this gives the order in which nodes are visited by the search).
Direct the edges in G according to their DFS-number, so that they point from lower to higher number.
Compute a hierarchical drawing of G (using SugiyamaLayout), coloring the edges of the DFS-tree with red and the other edges with blue, and write the drawing to an SVG file dfs.svg.
*/

bool createDFTree( const int n, const int m )//number of nodes n and the number of edges m of the graph to be generated
{
	Graph G;
	randomSimpleGraph( G, n, m); // Create a random DAG using OGDF's random_hierarchy graph generator

	GraphAttributes GA( G, GraphAttributes::nodeGraphics |
		GraphAttributes::edgeGraphics |
		GraphAttributes::nodeLabel |
		GraphAttributes::nodeStyle |
		GraphAttributes::edgeType |
		GraphAttributes::edgeArrow |
		GraphAttributes::edgeStyle ); // Create graph attributes for this graph
	GA.setDirected(false); // set to undirected

	node v;
	forall_nodes( v, G ){	// iterate through all	the node in the graph
		GA.fillColor( v ) = Color( "#FFFF00" );	// set node color to yellow
		GA.height( v ) = 20.0;	// set the height to 20.0
		GA.width( v ) = 40.0;	// set the width to 40.0

		string s = to_string(v->index());	
		char const *pchar = s.c_str();  //use char const* as target type

		GA.label( v ) = pchar;
	}

	edge e;
	forall_edges(e ,G)	// set default edge color and type
	{
		GA.bends(e);
		GA.arrowType(e) =  ogdf::EdgeArrow::eaNone;
		GA.strokeColor(e) = Color("#0000FF");
	}

	Array<int> state(G.numberOfNodes()); // create state array to store the current states of the nodes	

	for(int i=0;i<state.size();i++)  
	{
		state[i] = -1;	// init the state vaiables
	}

	v = G.chooseNode();	 // select random nodes for source

	state[ v->index() ]=0;

	dfs( &GA, &G ,&state, &v); // start recurtion

	SugiyamaLayout SL;	 //Compute a hierarchical drawing of G (using SugiyamaLayout)
	SL.setRanking( new OptimalRanking );
	SL.setCrossMin( new MedianHeuristic );

	OptimalHierarchyLayout *ohl = new OptimalHierarchyLayout;
	ohl->layerDistance( 30.0 );
	ohl->nodeDistance( 25.0 );
	ohl->weightBalancing( 0.8 );
	SL.setLayout( ohl );
	SL.call( GA );

	GraphIO::drawSVG( GA, "dfs.svg" );

	return true;
}

void dfs( GraphAttributes *GA, Graph *G , Array<int> *state , node *current )
{
	edge e;
	forall_edges( e, *G ){	// iterate through all the node in the graph

		node u = e->target();
		if( e->source()->index() == (*current)->index() && (*state)[ u->index() ]==-1 )	 // 
		{	
			(*state)[ u->index() ]=0;	 // set states	

			GA->bends( e );
			GA->arrowType( e ) =  ogdf::EdgeArrow::eaLast;	 // add red colure and arrow type
			GA->strokeColor( e ) = Color( "#FF0000" );

			dfs( GA, G, state, &u );	
		}

		u = e->source();
		if( e->target()->index() == (*current)->index() && (*state)[ u->index() ]==-1)	 //
		{	
			(*state)[ u->index() ]=0;	 // set states	

			GA->bends( e );
			GA->arrowType( e ) =  ogdf::EdgeArrow::eaFirst;
			GA->strokeColor( e ) = Color( "#FF0000" );

			dfs( GA, G, state, &u );	
		}
	}
}

Output

EX_05

OGDF GSoC Exercises 04


Problem Description

Compute the BFS-tree of a given graph and draw it nicely. In particular, you shall write a program that performs the following steps:

  1. Read two parameters n (number of nodes) and m (number of edges) from the command line.
  2. Create a random planar and biconnected graph G with n nodes and m edges and select a random node s in G.
  3. Perform a breadth-first search on G starting at s and compute G‘s BFS-tree.
  4. Create a new graph T representing this tree, i.e., the root shall be the node corresponding to s and edges are directed from the root to the leaves.
  5. Compute a nice drawing of T and write it to a SVG file bfs-tree.svg; make sure that nodes are labelled with the index of the corresponding node in G.

Hint: OGDF provides many functions / modules you can use in this program.

Find more details here.

Solution

/**
*This is the solution file for the Exercise 04
*
* @author Chameera Wijebandara
* @email chameerawijebandara@gmail.com
*
*	Windows 7
*	Vishual stuio 2010
*	OGDF Snapshot 2014-02-28
*
*/

#include <ogdf/basic/Graph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/basic/Queue.h>
#include <ogdf/tree/TreeLayout.h>

using namespace ogdf;

bool createBFTree( const int n, const int m );

// main 
int main()
{

	cout<<"Pleace enter number n (n>0) : ";
	int n,m;
	cin >> n;
	cout<<"Pleace enter number m (m>0) : ";
	cin >> m;

	createBFTree( n, m );

	return 0;
}
//end of the main

/*
Create a random planar and biconnected graph G with n nodes and m edges and select a random node s in G.
Perform a breadth-first search on G starting at s and compute G's BFS-tree.
Create a new graph T representing this tree, i.e., the root shall be the node corresponding to s and edges are directed from the root to the leaves.
Compute a nice drawing of T and write it to a SVG file bfs-tree.svg; make sure that nodes are labelled with the index of the corresponding node in G.
*/

bool createBFTree( const int n, const int m )//number of nodes n and the number of edges m of the graph to be generated
{
	Graph G;
	planarBiconnectedGraph( G, n, m, true); // Create a random DAG using OGDF's random_hierarchy graph generator

	GraphAttributes GA( G, GraphAttributes::nodeGraphics |
		GraphAttributes::edgeGraphics |
		GraphAttributes::nodeLabel |
		GraphAttributes::nodeStyle |
		GraphAttributes::edgeStyle ); // Create graph attributes for this graph

	Graph T;
	GraphAttributes TA( T, GraphAttributes::nodeGraphics |
		GraphAttributes::edgeGraphics |
		GraphAttributes::nodeLabel |
		GraphAttributes::nodeId |
		GraphAttributes::nodeStyle |
		GraphAttributes::edgeStyle ); // Create graph attributes for the tree


	Array<int> state(G.numberOfNodes()); // create state array to store the current states of the nodes	
	Array<node> nodes(G.numberOfNodes());  // create node array
	Queue<node> q;  // init q for store currently discovered nodes.

	for(int i=0;i<state.size();i++)  
	{
		state[i] = -1;	// init the state vaiables
		nodes[i] = T.newNode(); // create new nodes

		string s = to_string(i);	
		char const *pchar = s.c_str();  //use char const* as target type
		TA.label(nodes[i]) = pchar;	 // set lable	

		TA.fillColor( nodes[i] ) = Color( "#00FF00" );	// set node color to yellow
		TA.height( nodes[i] ) = 20.0;	// set the height to 20.0
		TA.width( nodes[i] ) = 40.0;	// set the width to 20.0


	}

	node v = G.chooseNode();	 // select random nodes for source
	q.append(v);	
	state[v->index()]=0;
	while(q.size()!=0)
	{
		v  = q.pop();	 //pop elements from the q
		edge e;

		forall_edges( e, G ){	// iterate through all the node in the graph

			node u = e->target();
			if(e->source()->index() == v->index() && state[u->index()]==-1)	 //
			{	
				q.append(u);
				state[u->index()]=0;	 // set states

				T.newEdge(nodes[v->index()],nodes[u->index()]);	 // add new edge

			}
		}
	}

	TreeLayout TL;	 // tree layout for virtualization
	TL.call(TA);

	GraphIO::drawSVG(TA,"bfs-tree.svg");

	return true;
}

Output

EX_04

OGDF GSoC Exercises 02


Problem Description

You shall write a graph generator for Sierpinski graphs, create such graphs and compute a drawing for them. In particular, you have to implement the following:

  1. A function sierpinksiGraph that creates a Sierpinksi graph of order n, where n≥1 is a function parameter.
  2. A main program that receives one parameter n from the command line.
  3. The main program shall then
    • create a Sierpinksi graph G of order n,
    • create a layout of G using a layout algorithm (choose a suitable one from OGDF’s layout algorithms, e.g., a multilevel force-directed algorithm),
    • and write the resulting layout to a SVG file sierpinski.svg (hint: see class GraphIO).

Your goal should be to have a nice looking SVG at the end (size and formatting of nodes and edges, overall layout).

Find more details here.

Solution

/**
*This is the solution file for the Exercise 02
*
* @author Chameera Wijebandara
* @email chameerawijebandara@gmail.com
*
*	Windows 7
*	Vishual stuio 2010
*	OGDF Snapshot 2014-02-28
*
*/

#include <ogdf/basic/Graph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/SugiyamaLayout.h>
#include <ogdf/layered/OptimalRanking.h>
#include <ogdf/layered/FastHierarchyLayout.h>
#include <ogdf/layered/BarycenterHeuristic.h>

# define NODESIZE 10
using namespace ogdf;

bool sierpinksiGraph ( const int n );
void initSierpinksiGraph( Graph *G, GraphAttributes *GA, float points [3][2] );
void genarateSierpinksiGraph( Graph *G, GraphAttributes *GA, float parent[3][2], int count );

// main
int main()
{
	cout<<"Pleace enter number n (n>0) : ";
	int n;
	cin >> n;
	sierpinksiGraph(n);
	return 0;
}
//end of the main

/*
A function sierpinksiGraph that creates a Sierpinksi graph of order n, where n is a function parameter.
and writes the final drawing to a SVG file
graph generator for Sierpinski graphs
*/

bool sierpinksiGraph ( const int n )//number of nodes n and the number of edges m of the graph to be generated
{
	if( n < 1 ) // if input is invalid
	{
		return false;
	}

	Graph G; // inti garaph
	GraphAttributes GA(G, GraphAttributes::nodeGraphics |
		GraphAttributes::nodeStyle |
		GraphAttributes::nodeLabel |
		GraphAttributes::edgeGraphics |
		GraphAttributes::edgeArrow |
		GraphAttributes::edgeStyle );

	float points [ 3 ][ 2 ] = {
		{ 0, 0 },
		{ 50*(n+3), 87*(n+3) },
		{ -50*(n+3), 87*(n+3) }}; //  points  of the outer triangerl

		initSierpinksiGraph( &G, &GA, points );	 // init the  Sierpinski graphs
		genarateSierpinksiGraph( &G, &GA, points, n-1 ); // recursively call pirates of the graph

		GraphIO::drawSVG( GA, "<code>mst.svg</code>" ); // write in to file

		return true;
}

/*
initialize the outer triangle
*/
void initSierpinksiGraph(Graph *G, GraphAttributes *GA,float points [3][2])
{

	for(int i=0;i<3;i++) // 3 points in thetriangle
	{
		node v  = G->newNode();
		GA->x(v) = points[i][0];
		GA->y(v) = points[i][1];
		GA->width(v) = NODESIZE;
		GA->height(v) = NODESIZE;
		GA->shape(v) = ogdf::Shape::shEllipse;
		GA->fillColor(v) = Color("#FF0000"); // set colour
		GA->strokeColor(v) = Color("#FF0000");

		node u;
		forall_nodes(u, *G){
			edge e = G->newEdge(v,u);
			GA->bends(e);
			GA->arrowType(e) =  ogdf::EdgeArrow::eaNone;
			GA->strokeColor(e) = Color("#FF0000");
		}
	}

}

/*
recursively grow the  graph
*/
void genarateSierpinksiGraph(Graph *G, GraphAttributes *GA,float parent[3][2],int count)
{
	if(count==0)
	{
		return;
	}

	node list[3];
	for(int i=0;i<3;i++)
	{
		// create new node
		node v  = G->newNode();
		GA->x(v) = (parent[i][0] + parent[(i+1)%3][0])/2;
		GA->y(v) = (parent[i][1] + parent[(i+1)%3][1])/2;
		GA->width(v) = NODESIZE;
		GA->height(v) = NODESIZE;
		GA->shape(v) = ogdf::Shape::shEllipse;
		GA->fillColor(v) = Color("#FF0000");

		list[i] = v;

		// create new edge
		for(int j=0;j<i;j++){

			edge e = G->newEdge(v,list[j]);
			GA->bends(e);
			GA->arrowType(e) =  ogdf::EdgeArrow::eaNone;
			GA->strokeColor(e) = Color("#FF0000");
		}

		float child[3][2] = {
			{parent[i][0],parent[i][1]},
			{(parent[i][0] + parent[(i+1)%3][0])/2,(parent[i][1] + parent[(i+1)%3][1])/2},
			{(parent[i][0] + parent[(i+2)%3][0])/2,(parent[i][1] + parent[(i+2)%3][1])/2}};

			// call for next iteration
			genarateSierpinksiGraph(G,GA,child,count-1);
	}
}

Output

EX_02_n=1EX_02_n=2EX_02_n=5EX_02_n=10

OGDF GSoC Exercises 01


Problem Description

You shall write a program that generates a DAG, computes a layout with the Sugiyama layout algorithms, and writes the final drawing to a SVG file. In particular, your program has to perform the following tasks:

  1. Your program shall be called with two (integer) parameters, which are the number of nodes n and the number of edges m of the graph to be generated.
  2. Create a random DAG using OGDF’s random_hierarchy graph generator (set the parameters planarsingleSource, and longEdges to true).
  3. Create graph attributes for this graph, including color and style for nodes and edges.
  4. For each node, set the width and height to 20.0 and the color to yellow.
  5. Call the SugiyamaLayout algorithm with the following options:
    • Use FastHierarchyLayout with a layer distance of 40.0 and a node distance of 20.0.
    • Use OptimalRanking and BarycenterHeuristic
  6. Write the computed layout to a SVG file layout.svg (hint: see class GraphIO)

Find more details here.

Solution

/**
*This is the solution file for the Exercise 01
*
* @author Chameera Wijebandara
* @email chameerawijebandara@gmail.com
*
*	Windows 7
*	Vishual stuio 2010
*	OGDF Snapshot 2014-02-28
*
*/

#include <ogdf/basic/Graph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/SugiyamaLayout.h>
#include <ogdf/layered/OptimalRanking.h>
#include <ogdf/layered/FastHierarchyLayout.h>
#include <ogdf/layered/BarycenterHeuristic.h>

using namespace ogdf;

bool createDirectedAcyclicGraph( const int n, const int m );


// main 
int main()
{
	createDirectedAcyclicGraph( 12, 10 );
	return 0;
}
//end of the main

/*
generates a DAG, computes a layout with the Sugiyama layout algorithms, 
and writes the final drawing to a SVG file
*/

bool createDirectedAcyclicGraph( const int n, const int m )//number of nodes n and the number of edges m of the graph to be generated
{
	Graph G;
	randomHierarchy( G, n, m, true, true, true ); // Create a random DAG using OGDF's random_hierarchy graph generator

	GraphAttributes GA( G, GraphAttributes::nodeGraphics |
		GraphAttributes::edgeGraphics |
		GraphAttributes::nodeLabel |
		GraphAttributes::nodeStyle |
		GraphAttributes::edgeStyle ); // Create graph attributes for this graph

	node v;
	forall_nodes( v, G ){	// iterate through all the node in the graph
		GA.fillColor( v ) = Color( "#FFFF00" );	// set node color to yellow
		GA.height( v ) = 20.0;	// set the height to 20.0
		GA.width( v ) = 20.0;	// set the width to 20.0
	}

	SugiyamaLayout SL;	 //init the SugiyamaLayout algorithm
	SL.setRanking(new OptimalRanking);
	SL.setCrossMin(new BarycenterHeuristic);

	FastHierarchyLayout *fhl = new FastHierarchyLayout;
	fhl->layerDistance(40.0);	// set layer distance to 40.0 
	fhl->nodeDistance(20.0);	// set node distance to 20.0.
	SL.setLayout(fhl);

	SL.call(GA);

	GraphIO::drawSVG(GA,"layout.svg");

	return true;
}

Output

Ex_01

Just submitted GSoC Project Proposals : OGDF


I have submitted four proposals for project ideas given by OGDF.

  1. Computation of Treewidth
  2. Basic Linear Algebra Support
  3. Generators for Different Random Graph Models
  4. Search Trees and Priority Queues

Computation of Treewidth

Short description: The treewidth measures how similar a given graph is to a tree. Tree width is widely used in various areas in Graph theory. However, already computing the treewidth of a graph is NP-complete. There exist, however, practically good heuristics which construct a tree decomposition close to the optimum.

Find the complete proposal here.

Basic Linear Algebra Support

Short description: Basic data structures for vectors and matrices are used in many graph layout algorithms that apply some kind of numerical optimization. But OGDF does not provide vector/matrix classes out-of-the-box. So the goal of this project is to provide powerful and efficient vector and matrix classes, that provide the obvious basic functionality as well as more complex operations and different storage models.

Find the complete proposal here.

Generators for Different Random Graph Models

Short description: Every good graph framework should have a standard set of random graph generators with known properties. There are many different random graph models that are used throughout theory. The task is to rework and extend the set of efficient random graph generators in the OGDF. Our target of this project is to build different random graph generators and documentation about them. Every generator should come with a documentation including a brief summary of the properties of the graph model.

Find the complete proposal here.

Search Trees and Priority Queues

Short description: The goal of this project is clearly separate abstract data types from implementing data structures, and identifying and providing various implementing data structures. An important design goal is to focus on efficiency. Implementation of data types PriorityQueue and Dictionary which can use different implementing data structures; implementation of data structures that can be used as implementing data structures for priority queues and dictionaries.

Find the complete proposal here.

Hope I will get the chance to spend summer with OGDF and make good contribution to the open source world.

Sample layout in OGDF


Introduction

I have write his article while exploring OGDF features and capabilities. This will demonstrate and explain same basic layouts we can use with OGDF.

This is the source code I am going to demonstrate below graphs.


/**
*
* @author Chameera Wijebandara
* @email chameerawijebandara@gmail.com
*
* Windows 7
* Vishual stuio 2010
* OGDF Snapshot 2014-02-28
*
*/

#include <ogdf/basic/Graph.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/DfsAcyclicSubgraph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/layered/SugiyamaLayout.h>
#include <ogdf/layered/OptimalRanking.h>
#include <ogdf/layered/MedianHeuristic.h>
#include <ogdf/layered/OptimalHierarchyLayout.h>

using namespace ogdf;

int main()
{
	Graph G;

	randomGraph(G,10,35); // grapth genarate.

	GraphAttributes GA( G, GraphAttributes::nodeGraphics |
		GraphAttributes::edgeGraphics |
		GraphAttributes::nodeLabel |
		GraphAttributes::nodeStyle |
		GraphAttributes::edgeType |
		GraphAttributes::edgeArrow |
		GraphAttributes::edgeStyle ); // Create graph attributes for this graph

	node v;
	forall_nodes( v, G ){ // iterate through all the node in the graph
		GA.fillColor( v ) = Color( "#FFFF00" ); // set node color to yellow

		GA.height( v ) = 20.0; // set the height to 20.0
		GA.width( v ) = 20.0; // set the width to 40.0
		GA.shape(v) = ogdf::Shape::shEllipse;

		string s = to_string(v->index());
		char const *pchar = s.c_str(); //use char const* as target type
		GA.label( v ) = pchar;
	}

	edge e;
	forall_edges(e ,G) // set default edge color and type
	{
		GA.bends(e);
		//GA.arrowType(e) = ogdf::EdgeArrow::eaNone;
		GA.strokeColor(e) = Color("#0000FF");
	}

	SugiyamaLayout SL; //Compute a hierarchical drawing of G (using SugiyamaLayout)
	SL.setRanking( new OptimalRanking );
	SL.setCrossMin( new MedianHeuristic );

	OptimalHierarchyLayout *ohl = new OptimalHierarchyLayout;

	SL.setLayout( ohl );
	SL.call( GA );

	GraphIO::drawSVG( GA, "C:\\Users\\Chameera\\Desktop\\Output.svg" );

	return 0;
}
//end of the main

Mixed Model Layout


randomTriconnectedGraph(G,10,1,2);

....

MixedModelLayout MML;

MML.call( GA );

Capture

Planar Straight Layout


randomTriconnectedGraph(G,10,2,3);

....
PlanarStraightLayout PSL;
PSL.call( GA );

Capture

Planar Draw Layout


randomTriconnectedGraph(G,10,2,3);

....
PlanarDrawLayout PDL;
PDL.call( GA );

Capture

Planarization Grid Layout


randomTriconnectedGraph(G,10,2,3);

....
PlanarizationGridLayout PGL;
PGL.call( GA );

Capture

FMMM Layout


randomGraph(G,12,23);

....
FMMMLayout FL;
FL.call( GA );

Capture

Sugiyama Layout


randomGraph(G,10,23);

....</pre>
SugiyamaLayout SL; //Compute a hierarchical drawing of G (using SugiyamaLayout)
 SL.setRanking( new OptimalRanking );
 SL.setCrossMin( new MedianHeuristic );

OptimalHierarchyLayout *ohl = new OptimalHierarchyLayout;

SL.setLayout( ohl );
 SL.call( GA );
<pre>

Capture

Tree Layout


randomTree(G,10);

....</pre>
TreeLayout TL;

TL.call( GA );
<pre>

Capture

Radial Tree Layout


randomTree(G,10);

....</pre>
RadialTreeLayout RTL;

RTL.call( GA );
<pre>

Capture

Circular Layout


wheelGraph(G,15);

....</pre>
CircularLayout CL;

CL.call( GA );
<pre>

Capture

Balloon Layout


wheelGraph(G,15);

....</pre>
BalloonLayout BL;

BL.call( GA );
<pre>

Capture
Hope this will help you to understand how work with OGDF graph layouts.

Basic Graph Examples with OGDF


Introduction

I have write his article while exploring OGDF features. This will demonstrate and explain same basic graph genarators and layout contain in the OGDF.

This is the source code I am going to demonstrate below graphs.

 


/**
*
* @author Chameera Wijebandara
* @email chameerawijebandara@gmail.com
*
* Windows 7
* Vishual stuio 2010
* OGDF Snapshot 2014-02-28
*
*/

#include <ogdf/basic/Graph.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/DfsAcyclicSubgraph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/layered/SugiyamaLayout.h>
#include <ogdf/layered/OptimalRanking.h>
#include <ogdf/layered/MedianHeuristic.h>
#include <ogdf/layered/OptimalHierarchyLayout.h>

using namespace ogdf;

int main()
{
	Graph G;

	randomSimpleGraph(G, 10, 20); // grapth genarate.

	GraphAttributes GA( G, GraphAttributes::nodeGraphics |
		GraphAttributes::edgeGraphics |
		GraphAttributes::nodeLabel |
		GraphAttributes::nodeStyle |
		GraphAttributes::edgeType |
		GraphAttributes::edgeArrow |
		GraphAttributes::edgeStyle ); // Create graph attributes for this graph

	node v;
	forall_nodes( v, G ){ // iterate through all the node in the graph
		GA.fillColor( v ) = Color( "#FFFF00" ); // set node color to yellow

		GA.height( v ) = 20.0; // set the height to 20.0
		GA.width( v ) = 20.0; // set the width to 40.0
		GA.shape(v) = ogdf::Shape::shEllipse;

		string s = to_string(v->index());
		char const *pchar = s.c_str(); //use char const* as target type
		GA.label( v ) = pchar;
	}

	edge e;
	forall_edges(e ,G) // set default edge color and type
	{
		GA.bends(e);
		GA.arrowType(e) = ogdf::EdgeArrow::eaNone;
		GA.strokeColor(e) = Color("#0000FF");
	}

	SugiyamaLayout SL; //Compute a hierarchical drawing of G (using SugiyamaLayout)
	SL.setRanking( new OptimalRanking );
	SL.setCrossMin( new MedianHeuristic );

	OptimalHierarchyLayout *ohl = new OptimalHierarchyLayout;

	SL.setLayout( ohl );
	SL.call( GA );

	GraphIO::drawSVG( GA, "C:\\Users\\Chameera\\Desktop\\Output.svg" );

	return 0;
}
//end of the main

Complete Bipartite Graph

Creates t complete bipartite graph Kn,m.

For the demostration i have use completeBipartiteGraph with SugiyamaLayout,OptimalRanking, MedianHeuristic and OptimalHierarchyLayout.


completeBipartiteGraph( G, 8, 10 );

Capture

Complete Graph

Creates the complete graph Kn.

For the demonstration I have use completeGraph with SugiyamaLayout,OptimalRanking, MedianHeuristic and OptimalHierarchyLayout.


completeGraph( G, 4);

Capture

Cube Graph

Creates the complete graph Kn.

For the demonstration I have use cubeGraph with SugiyamaLayout,OptimalRanking, MedianHeuristic and OptimalHierarchyLayout.


cubeGraph( G, 4);

Capture

Grid Graph

Creates a (toroidal) grid graph on n x m nodes.

For the demonstration I have use gridGraph with SugiyamaLayout,OptimalRanking, MedianHeuristic and OptimalHierarchyLayout.


gridGraph (G,4,2,false,false);

Capture

Petersen Graph

Creates a generalized Petersen graph.

For the demonstration I have use petersenGraph with SugiyamaLayout,OptimalRanking, MedianHeuristic and OptimalHierarchyLayout.


petersenGraph (G,3,4);

Capture
Planar Biconnected Graph

Creates a planar biconnected (embedded) graph.


planarBiconnectedGraph (G,5,10);

 

Capture

Planar CNB Graph

Creates a planar graph, that is connected, but not biconnected.


planarCNBGraph (G,5,10,3);

Capture

Planar Connected Graph

Creates a connected (simple) planar (embedded) graph.


planarConnectedGraph (G,6,10);

Capture

Planar Triconnected Graph

Creates a planar triconnected (and simple) graph.

This graph generator works in two steps.

  1. A planar triconnected 3-regular graph is constructed using successive splitting of pairs of nodes. The constructed graph has n nodes and 1.5n edges.
  2. The remaining edges are inserted by successive splitting of faces with degree four or greater. The resulting graph also represents a combinatorial embedding.

planarTriconnectedGraph(G,6,10);

Capture

Random Biconnected Graph

Creates a random biconnected graph.


randomBiconnectedGraph(G,4,7);

Capture

Random Tree

Creates a random tree.


randomTree(G,15);

Capture

Regular Tree

Creates a regular tree.


regularTree(G,18,3);

Capture

Wheel Graph

Creates the graph W(d)n: A wheel graph.


wheelGraph(G,4);

CaptureHope this will help you t understand how work with OGDF graph generators.

Beginning with OGDF


What is OGDF

Goals of the Open Graph Drawing Framework (OGDF) were to transfer essential design concepts of AGD and to overcome its main deficiencies for use in academic research.

Find more details here.

Download and Install Python

You can download windows installer(msi) here.

Download Python installer

The Windows version is provided as an MSI package. To install it manually, just double-click the file. The MSI package format allows Windows administrators to automate installation with their standard tools.

Download and Install OGDF

You can download the latest release or any snapshot release of OGDF here.

You can find complete build instruction here.

As an example i have demonstrate  installing OGDF Snapshot 2014-02-28 in visual studio 2010.

  • Extract the downloaded .zip file
    extract
  • Build OGDF (Visual Studio 2010):
    1. Execute the python script makeVCXProj.py to generate a Visual Studio 2010 project file ogdf.vcxproj.runthis
    2. Open this project with Visual Studio 2010 and build the project.openFile

Configure your first OGDF application

  • Open the Visual Studio 2010
  • Create new win 32 console applicationnewProject
  • Create an empty projectfinish
  • Add new C++ file in to the projectaddnewCfileaddfile
  • Add the path to the OGDF header files to the include path (project properties → C++ → Additional include directories). This is the directory of the OGDF installation which contains the directory ogdf.
    • Go to Project property window
      prpotes
    • Navigate to Additional include directories  window.
      1
    • Add Additional include directories
      1.1
  • Add the path to ogdf.lib to the library path (project properties → Linker → Additional library directories)2
  • Add ogdf.lib and coin.lib as ditional library for the linker (project properties → Linker → Input → Additional dependencies).
    3

Run Your First OGDF Application

Sample source code


#include <ogdf/basic/Graph.h>
#include <ogdf/basic/GraphAttributes.h>
#include <ogdf/basic/GraphAttributes.h>
#include <ogdf/fileformats/GraphIO.h>
using namespace ogdf;

int main()
{</pre>
#include <ogdf/basic/Graph.h>
#include <ogdf/basic/GraphAttributes.h>
#include <ogdf/basic/GraphAttributes.h>
#include <ogdf/fileformats/GraphIO.h>
using namespace ogdf;

#include <ogdf/basic/Graph.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/DfsAcyclicSubgraph.h>

using namespace ogdf;

int main()
{
 Graph G;
 randomSimpleGraph(G, 10, 20);
 DfsAcyclicSubgraph DAS;
 DAS.callAndReverse(G);
 cout<<G.numberOfEdges()<<endl;
 getchar();

 return 0;
}
<pre>

Just copy and past this code in to your main.cpp file.

If all the work we have don is correct it gives fallowing output.

output
If you got this we are done 😀