Package util

Source Code of util.RandomIntervalGraphGenerator

package util;

import core.Graphs.Graph;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
* Nothing software.
*/
public class RandomIntervalGraphGenerator implements GraphGenerator {
    @Override
    public Object generate(int numberOfNodes) {

        int listCapacity = numberOfNodes * 2;
        List<Integer> arr = new LinkedList<Integer>();
        int[][] adjacencyMatrix = new int[numberOfNodes][numberOfNodes];
        Integer nodeNumber = 0;
        for (int i = 0; i < listCapacity; i = i + 2) {
            arr.add(nodeNumber);
            arr.add(nodeNumber);
            nodeNumber++;
        }

        Collections.shuffle(arr);

        List<Integer> arr1 = new ArrayList<Integer>(arr);

        for (int i = 0; i < numberOfNodes; i++) {
            for (int j = arr1.indexOf(i); j < arr1.lastIndexOf(i); j++) {
                Integer jIndex = arr1.get(j);
                adjacencyMatrix[i][jIndex] = 1;
                adjacencyMatrix[jIndex][i] = 1;
            }
            adjacencyMatrix[i][i] = 0;
        }

        return new Graph(adjacencyMatrix);
    }
}
TOP

Related Classes of util.RandomIntervalGraphGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.