I'm a newer in Java and now about to use adjacency matrix(2D array) to implement a graph, but encountered some problems, pls help me,thanks. following are partly codes.
// following codes create a empty graph
public AdjMatrix() {
private boolean[][] adjacencymatrix; /*Define a 2D array to store adjacency matrix*/
private int vertices_numb; /*Number of vertices in the graph */
T[] vertices; /*To store the values of vertices*/
vertices_numb=0 /*set the graph to empty*/
} // end of AdjMatrix()
//Following code is for adding a vertex.
public void addVertex(T vertLabel) {
if (vertices_numb == 0) //If the graph is empty, print related information and assign value 0 to adjacency matrix[0][0]
System.out.println("Graph is empty!");
adjacencymatrix[0][0]=0;
vertices[0]=vertLabel; //To save the vertex
vertices_numb=vertices_numb+1; //to count the number of vertex
else
for(int i=0;i<=vertices_numb;i++) {
vertices[vertices_numb]=vertLabel;
adjacencymatrix[i][vertices_num]= how does program know they are connected between 2 nodes?
vertices_numb=vertices_numb+1;
}
} // end of addVertex()
My problems are:
1) How does program know if they are connected between two nodes?
2) When adding a vertex, do we need to consider the connection between this one with the nodes exist?
Many thanks.