Matrix programming
Hi there! I'm new to the SDN forums. Just wondering if anyone can help me with a little problem that I am having...
I'm writing a program that wil generate a random matrix of 0s and 1s. This will be used to represent a graph of some sort. The problem that I am having now is that it is ok for representing a directed graph but it is not so good for representing an undirected graph. Can anyone give me some suggestions to how this problem can be solved? Any help will be much appreciated!
Here's a bit of the code. Right now the program generates a directed graph but I want an undirected graph...
user inputs number of nodes then the following code is executed:
networkA = new int[x][y];
// Random function integrated to enter (0,1) into network
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
networkA[j] = zeroOne.nextInt(2);
}// Close i loop
}// Close j loop
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(networkA[j] + " ");
}
System.out.println();
Thanks in advance!
[1146 byte] By [
Java_nooba] at [2007-11-26 22:28:51]

Oops... sorry about the code thing, will remember for future posts.
Right now the For loop generates a Matrix like:
1 1 0 1
0 1 1 1
0 0 0 1
0 1 0 1
This can't represent an undirected graph cos the upper diagonal half of the matrix doesn't match the bottom half. Just wondering if the code I am using now can be changed in some way that will let me show an undirected graph.
The last method I tried was to do an array of arrays which incremented a row of numbers n, then n-1, and so on down to 1. This represented the half matrix. But then I didn't know how to get the bottom half to be the same...
The answer is probably jumping at me but I can't see it...
> But then I didn't know how to get the bottom half to be the same...
> The answer is probably jumping at me but I can't see it...
So there's a link from A to B if M_AB, you want M_AB <=> M_BA to make it undirected, and you have one half. I suppose the question is whether the half you have includes M_AB or M_BA.
import java.util.Random;
public class RandomExample {
public static void main(String[] args) {
Random rnd = new Random();
final int size = 10;
int [][] network = new int[size][size];
// Random function integrated to enter (0,1) into network
for (int i = 0; i < size; i++) {
for (int j = 0; j <= i; j++) {
network[j][i] = network[i][j] = rnd.nextInt(2);
}
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(network[i][j] + " ");
}
System.out.println();
}
}
}