Package util

Source Code of util.CircleGraphGenerator

package util;

import core.Graphs.Graph;

import java.util.*;

/**
* Nothing software.
*/
public class CircleGraphGenerator implements GraphGenerator{
    @Override
    public Object generate(int numberOfNodes) {
        int listCapacity = numberOfNodes * 2;
        ArrayList<Integer> arr = new ArrayList<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);

        Map<Integer, Boolean> isStartingFromFirstOccurance = new HashMap<Integer, Boolean>();

        Random random = new Random();
        for (int i = 0; i < numberOfNodes; i++){
            isStartingFromFirstOccurance.put(i, random.nextBoolean());
        }

        for (int i = 0; i < numberOfNodes; i++){
            int indexOfFirstOccurance = arr.indexOf(i);
            int indexOfLastOccurance = arr.lastIndexOf(i);
            if (isStartingFromFirstOccurance.get(i)){
                for (int j = indexOfFirstOccurance; j < indexOfLastOccurance; j++){
                    Integer jIndex = arr.get(j);
                    adjacencyMatrix[i][jIndex] = 1;
                    adjacencyMatrix[jIndex][i] = 1;
                }
            } else {
                for (int j = indexOfLastOccurance; j < listCapacity; j++){
                    Integer jIndex = arr.get(j);
                    adjacencyMatrix[i][jIndex] = 1;
                    adjacencyMatrix[jIndex][i] = 1;
                }

                for (int j = 0; j < indexOfFirstOccurance; j++){
                    Integer jIndex = arr.get(j);
                    adjacencyMatrix[i][jIndex] = 1;
                    adjacencyMatrix[jIndex][i] = 1;
                }
            }
            adjacencyMatrix[i][i] = 0;
        }

        return new Graph(adjacencyMatrix);
    }
}
TOP

Related Classes of util.CircleGraphGenerator

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.