The adjacency matrix implementation diagram uses a matrix to subscript the matrix as a vertex if there is an edge between the vertex and the vertex. Then set the value to 1 on the point at which the matrix corresponds. (Default is 0)
Packagemygraph;Importjava.util.List;/*** adjacency Matrix representation graph *@authorRanter **/ Public classVertex {Private Charlable;//Matrix Element Private int[] List =New int[20] [20]; Private Char[] Vertexlist =New Char[20]; Private intNverts;//Current vertex subscriptVertex () { This. Nverts = 0; } //Add a vertex Public voidAddvertex (Charlable) {Vertexlist[nverts++] =lable; } //Add an edge Public voidAddedge (intStartintend) {List[start][end]= 1; List[end][start]= 1; } //Print Matrix Public voidPrintmatrix () { for(inti = 0; i < nverts; i + +) { for(intj = 0; J < Nverts; J + +) {System.out.print (list[i][j]); } System.out.println (); } }}
Test procedure:
Public Static voidMain (string[] args) {Vertex v=NewVertex (); V.addvertex (' A ');//0V.addvertex (' B ');//1V.addvertex (' C ');//2V.addvertex (' d ');//3V.addvertex (' e ');//4V.addedge (0, 2);//a-cV.addedge (1, 4);//b-eV.addedge (2, 4);//b-eV.addedge (0, 4);//a-eV.printmatrix (); }
Test results:
a b c d e a 0 0 1 0 1b 0 0 0 0 1C 1 0 0 0 1d 0 0 0 0 0
e 1 1 1 0 0
Data structure Java version of the adjacency matrix implementation diagram (11)