Monday, March 28, 2011

A Lightweight (pseudo)Random Directed Graph Generator

I recently needed to generate some random graphs for a project I'm working on at school. I wrote a quick java program that takes the number of vertices and the average outdegree as inputs, and outputs the graph as a CSV file representing each of the edges.

It is run as follows:

java RandomGraphGenerator NUMBER_OF_VERTICES AVERAGE_OUTDEGREE

When called like this, it assumes a graph with vertices numbered from 0 to (NUMBER_OF_VERTICES - 1). It then generates floor((NUMBER_OF_VERTICES * AVERAGE_OUTDEGREE)) random edges, by randomly selecting a source vertex, then a target vertex, and repeating that process if that given edge already exists in the graph.


Here's the source: RandomGraphGenerator.java
Please feel free to use this in any way you see fit.

No comments:

Post a Comment