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;
}