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

com.salesforce.jgrapht.generate.ScaleFreeGraphGenerator Maven / Gradle / Ivy

Go to download

This project contains the apt processor that implements all the checks enumerated in @Verify. It is a self contained, and shaded jar.

There is a newer version: 2.0.7
Show newest version
/*
 * (C) Copyright 2008-2017, by Ilya Razenshteyn and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * This program and the accompanying materials are dual-licensed under
 * either
 *
 * (a) the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation, or (at your option) any
 * later version.
 *
 * or (per the licensee's choosing)
 *
 * (b) the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation.
 */
package com.salesforce.jgrapht.generate;

import java.util.*;

import com.salesforce.jgrapht.*;

/**
 * Generates directed or undirected
 * scale-free network of any
 * size. Scale-free network is a connected graph, where degrees of vertices are distributed in
 * unusual way. There are many vertices with small degrees and only small amount of vertices with
 * big degrees.
 * 
 * @param  the graph vertex type
 * @param  the graph edge type
 *
 * @author Ilya Razenshteyn
 */
public class ScaleFreeGraphGenerator
    implements GraphGenerator
{
    private int size; // size of graphs, generated by this instance of generator
    private long seed; // initial seed
    private Random random; // the source of randomness

    /**
     * Constructs a new ScaleFreeGraphGenerator.
     *
     * @param size number of vertices to be generated
     */
    public ScaleFreeGraphGenerator(int size)
    {
        if (size < 0) {
            throw new IllegalArgumentException("invalid size: " + size + " (must be non-negative)");
        }
        this.size = size;
        random = new Random();
        seed = random.nextLong();
    }

    /**
     * Constructs a new ScaleFreeGraphGenerator using fixed 
     * seed for the random generator.
     *
     * @param size number of vertices to be generated
     * @param seed initial seed for the random generator
     */
    public ScaleFreeGraphGenerator(int size, long seed)
    {
        if (size < 0) {
            throw new IllegalArgumentException("invalid size: " + size + " (must be non-negative)");
        }
        this.size = size;
        random = new Random();
        this.seed = seed;
    }

    /**
     * Generates scale-free network with size passed to the constructor. Each call of this
     * method produces identical output (but if target is an undirected graph, the
     * directions of edges will be lost).
     *
     * @param target receives the generated edges and vertices; if this is non-empty on entry, the
     *        result will be a disconnected graph since generated elements will not be connected to
     *        existing elements
     * @param vertexFactory called to produce new vertices
     * @param resultMap unused parameter
     */
    @Override
    public void generateGraph(
        Graph target, VertexFactory vertexFactory, Map resultMap)
    {
        random.setSeed(seed);
        List vertexList = new ArrayList<>();
        List degrees = new ArrayList<>();
        int degreeSum = 0;
        for (int i = 0; i < size; i++) {
            V newVertex = vertexFactory.createVertex();
            target.addVertex(newVertex);
            int newDegree = 0;
            while ((newDegree == 0) && (i != 0)) // we want our graph to be
                                                 // connected

            {
                for (int j = 0; j < vertexList.size(); j++) {
                    if ((degreeSum == 0) || (random.nextInt(degreeSum) < degrees.get(j))) {
                        degrees.set(j, degrees.get(j) + 1);
                        newDegree++;
                        degreeSum += 2;
                        if (random.nextInt(2) == 0) {
                            target.addEdge(vertexList.get(j), newVertex);
                        } else {
                            target.addEdge(newVertex, vertexList.get(j));
                        }
                    }
                }
            }
            vertexList.add(newVertex);
            degrees.add(newDegree);
        }
    }
}

// End ScaleFreeGraphGenerator.java




© 2015 - 2025 Weber Informatics LLC | Privacy Policy