0

I want to generate network flow algorithm using random number of nodes and capacities using Java. Minimum nodes should be 4 and maximum is 10. So I used for loop and tried but it doesn't work for all number of node possibilities.

Following code work only for minimum random nodes (4). I created function to randomly generate nodes.

public static void main(String[] args) {
    int nodeNum = getRandomNumber();
    int[][] arr = new int[nodeNum][nodeNum];
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {

            if (i==0) {
                if (j==1 || j==2)
                   arr[i][j] = (int)(Math.random() * 10);
            }
            else if (i==1 && j==3){
                arr[i][j] = (int)(Math.random() * 10);
            }
            else if (i==2 && j==3){
                arr[i][j] = (int)(Math.random() * 10);
            }
            else {
                arr[i][j] = 0;
            }
            System.out.print(arr[i][j] + " ");
        }
        System.out.println("");
    }
}

//Generate random number of nodes between 4 and 10
private static int getRandomNumber() {
    int randomValue;
    do {
        randomValue = (int) (Math.random() * 10);
    } while(!(randomValue>=4 && randomValue<=10));
    return randomValue;
}
RAbey
  • 1
  • 2
  • Welcome to SO. Can you clarify your question by editing it please ? I don't know if you have an issue with the network flow implementation, or to build a random graph. – m.raynal Mar 28 '19 at 10:00
  • I have an issue with building a random graph using 2d array. – RAbey Mar 28 '19 at 11:50
  • Then [this answer](https://stackoverflow.com/a/20180154/7442673) can provide you with a method to generate random graphs. Because you need not also to choose the number of nodes, but also the number of edges. – m.raynal Mar 28 '19 at 12:16

0 Answers0