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

