I have a 2D array called conArray, which holds values that tell me which nodes are connected to which other nodes.

The format of the array is as follows:

0 1 0 A B 1 B C 2 D E 3 C E

With the numbers representing the location of the characters inside the array.

For example, the first line means that Node A is connected to Node B.

I have been trying to work out of a way of getting these values into an adjacency matrix so that the rows and columns of the matrix represent the characters, and a 1 or 0 is stored in the place when indexed by both of them.

For example, with the array above i would end up with a boolean adjacency matrix that looks like this:

0 1 2 3 4 0 F T F F F 1 T F T F F 2 F T F F T 3 F F F F T 4 F F T T F

where the numbers around the outside represent the characters..(e.g 0=A,1=B)

I can work out the conditional bit of asking whether they connect or not but I have no idea how to put the answer in the right place in the adjacency matrix, if you understand?

Sorry my explaining is a bit poor but many many thanks for any help.

Well the matrix would just be an array of arrays, right? Then there are two simple ways of doing this:

1. Initialize the matrix to be all "F". Then go through every single connection and populate the correct places with "T".

2. The matrix starts empty. Have a nested for-loop go through every space in the matrix and check the connections array to see whether or not that spot should be populated, and put the correct value in the space.

Hope this helps, Yuriy

Darren Jacobs
Greenhorn

Joined: May 02, 2006
Posts: 6

posted

0

Thanks for all your help!

My main problem however is how to place the T in the right place.

For example, if A is connected to B. I need to put a T in the adjacency matrix at [0][1] or [1][0]

How do i convert these letters into places without a long winded method?

Thanks again!

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429

posted

0

For example, if A is connected to B. I need to put a T in the adjacency matrix at [0][1] or [1][0]

If you mean "which one to use" you have to decide that for yourself, just be consistent. Do you want [0][1] to represent "1 across, 0 down" or "0 down, 1 across"?

If you mean "what number represents B", just subtract the number 65 from the character 'B'. Make sure you use capital letters. I'd put the conversion code into a seperate method.