All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gr.james.simplegraph.examples.GenerateRandomGraph Maven / Gradle / Ivy

Go to download

Simple Graph is a graph interface for Java 6 that is designed to expose a very simple API to support working with graphs

The newest version!
package gr.james.simplegraph.examples;

import gr.james.simplegraph.MutableGraph;

import java.util.Random;

/**
 * Generate a random {@link MutableGraph}.
 */
public final class GenerateRandomGraph {
    private GenerateRandomGraph() {
    }

    /**
     * Generates a {@link MutableGraph} according to the G(n,p) model.
     * 

* This method does not generate self loops. *

* Complexity: O(n^2) * * @param n the number of vertices * @param p the connection probability * @param r the {@link Random} instance to use * @return a new {@link MutableGraph} complying with the G(n,p) model * @throws IllegalArgumentException if {@code n < 0} or {@code p} is not in [0,1] * @throws NullPointerException if {@code r} is {@code null} */ public static MutableGraph randomGraph(int n, double p, Random r) { if (p < 0 && p > 1) { throw new IllegalArgumentException(); } final MutableGraph g = new MutableGraph(n); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (r.nextDouble() < p) { g.putEdge(i, j); } } } return g; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy