Code for Random Graph Generation G(n,p) 1

Following is the Java code for generating a Random Graph G(n, p) where ‘n’ is the number of nodes/ vertices in the graph and ‘p’ is the probability that any edge will form or not.

The code makes use of JUNG API for graph visualization and creation. More About JUNG…

A sample run for n = 10 and p = 0.5 gives the following result. (Since the graph is random it may generate/ display differently on your machine.)

Random Graph Generated from running above program for n = 10 and p = 0.5

Random Graph Generated from running above program for n = 10 and p = 0.5

Though the code uses JUNG API for Graph as Object representation, the above code can be extended to have any other graph representation you may wish to choose. Please write a comment if you need help with some other graph representation.

Another code with custom Vertex representation is as listed below:

 

The Vertex class to be used is:

 

A sample output graph generated from above code is:

Input Graph

Rate this post

One comment on “Code for Random Graph Generation G(n,p)

  1. Reply amr May 2,2015 9:04 pm

    there’s an error stop me from running it


    java.lang.NoClassDefFoundError: org/apache/commons/collections/Predicate
    at RandomGraphGenerator.generateRandomGraph(graphjava.java:22)
    at RandomGraphGenerator.main(graphjava.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
    ?

Leave a Reply