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]
# 1
When you post code, use the [code][/code] tags so it stays legible, otherwise [i] turns your code italic. How are you representing a directed graph with an array of true/false variables? What does the array lack that would represent an undirected graph?
hunter9000a at 2007-7-10 11:32:17 > top of Java-index,Java Essentials,New To Java...
# 2

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...

Java_nooba at 2007-7-10 11:32:17 > top of Java-index,Java Essentials,New To Java...
# 3
**** missed the questioin completely... a 1 in the matrix represents a connection and a 0 represents no connection to the corresponding node.
Java_nooba at 2007-7-10 11:32:17 > top of Java-index,Java Essentials,New To Java...
# 4

> 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.

YAT_Archivista at 2007-7-10 11:32:17 > top of Java-index,Java Essentials,New To Java...
# 5

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();

}

}

}

DrLaszloJamfa at 2007-7-10 11:32:17 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks very much DrLaszloJamf. Your code is a great help.
Java_nooba at 2007-7-10 11:32:17 > top of Java-index,Java Essentials,New To Java...