Adjacency Matrix for Graph implementation

publicclass Graph{

intarraySize, graphSize;

ListNodenode[];

public Graph(){// constructor

arraySize = 10;

graphSize = 0;

node =new ListNode[arraySize];

for (int j=0; j<arraySize; j++) node[j]=null;

}

public Graph(int size){// constructor

arraySize = size;

graphSize = 0;

node =new ListNode[arraySize];

for (int j=0; j><arraySize; j++) node[j] =null;

}

publicboolean isEmpty(){

return graphSize == 0;

}

publicboolean isFull(){

return graphSize == arraySize;

}

publicint getArraySize(){

return arraySize;

}

publicint getGraphSize(){

return graphSize;

}

publicboolean insertNode(Object o){

intj;

if (isFull())returnfalse;

if (!(oinstanceof NodeRecord))returnfalse;

for (j=0; j><arraySize; j++)

if (node[j]==null)

break;

node[j] =new ListNode(o);

graphSize++;

returntrue;

}

publicboolean insertEdge(Object o1, Object o2){

intj;

if (!(o1instanceof NodeRecord))returnfalse;

if (!(o2instanceof EdgeRecord))returnfalse;

for (j=0; j><arraySize; j++)

if (((NodeRecord) o1).compareTo(node[j].getItem())==0)

break;

if (j>=arraySize)returnfalse;

node[j].setNext(new ListNode(o2, node[j].getNext()));

returntrue;

}

publicvoid print(){

intj;

ListNodeln;

System.out.println(graphSize +" nodes");

for (j=0; j<graphSize; j++){

System.out.print(node[j].getItem()+"-> ");

ln = node[j].getNext();

while (ln!=null){

System.out.print(ln.getItem());

ln = ln.getNext();

}

System.out.println();

}

}

}

How do i get started on doing a adjacency matrix representation of a graph in its corresponding text file?

eg.

4 (no. of nodes)

0 2 1 2 (node 1 id ; num of adj nodes ; adj node 1, adj node 2...)

1 2 0 2

2 3 0 1 3

3 1 2

[5112 byte] By [aevylainredsa] at [2007-11-27 0:22:02]
# 1
i think it's necessary to declare a 2-D array right?but i dont know how to go about doing the implementation alreadywhat more i know... hmmm i think it should be an undirected graph ya?help help T_T
aevylainredsa at 2007-7-11 22:16:28 > top of Java-index,Java Essentials,New To Java...
# 2

> i think it's necessary to declare a 2-D array right?

> but i dont know how to go about doing the

> implementation already

> what more i know... hmmm i think it should be an

> undirected graph ya?

> help help T_T

That is correct. When reading the text file:

4

0 2 1 2

1 2 0 2

2 3 0 1 3

3 1 2

use the java.util.Scanner class to read it line by line.

See: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html (including code examples)

When reading the first number, which is four, you create a 4x4 int array like this:int dimension = 4;

int[][] matrix = new int[dimension][dimension];

After reading the second line you find that node 0 connects to node 1 and node 2, so you do:matrix[0][1] = 1;

matrix[0][2] = 1;

//^ ^

//| |

//| + to node

//|

//+ from node

but you should also do it the other way around since it is an undirected graph:matrix[1][0] = 1;

matrix[2][0] = 1;

After reading all the lines, your adjacency matrix will look like this:[0] [1] [2] [3]

[0] 0110

[1] 1011

[2] 1101

[3] 0110which represents the following graph:+-++-+

|0|--|1|

+-++-+

|/ |

|/ |

|/|

| /|

| /|

+-++-+

|2|--|3|

+-++-+

prometheuzza at 2007-7-11 22:16:28 > top of Java-index,Java Essentials,New To Java...