Detailed diagram of data structure
Figure in the computer program design is also very extensive use, the figure is a kind of tree similar to the data structure, from a mathematical point of view, the tree is also a graph.
Connected graphs: If there is at least one path that can connect all the vertices, then the graph is called the connected graph. You may now think of what data structure to use in the graph to represent Ah, vertex: With a vertex class to represent, vertex placed in the array and then marked with subscript, of course, the vertex can also be placed in a linked list or other data structure, the purpose of storage is to use convenience, which is not related to the edge how to connect points.
Edge: can be represented by adjacency matrix or adjacency table. The adjacency matrix is a two-dimensional array, and "table" in the adjacency table refers to the linked list (an array of linked lists).
On the code:
Vertex:
Public class Vertex { /** * This class is the point class */public Char Label // the name of the point Public boolean wasvisited; // flag bit Public Vertex (char label) { this. label = label; }}
Figure:
Public classGraph {/*** This class is a diagram representing the data structure*/ Private Final intMax_vertex = 20;//The maximum number of points PrivateVertex[]vertexs;//array of vertices Private intAdjmat[][];//represents an adjacency matrix Private intCurrentvertexs;//The actual number of the current vertex. PublicGraph () {Vertexs=NewVertex[max_vertex]; Adjmat=New int[Max_vertex][max_vertex]; Currentvertexs= 0;//the current number of 0//initializing adjacency matrices for(inti = 0;i<max_vertex;i++) { for(intj=0;j<max_vertex;j++) {Adjmat[i][j]= 0; } } } //defining operation classes for diagrams//ways to add vertices Public voidAddvertex (CharLab) {Vertexs[currentvertexs++] =NewVertex (Lab); } //ways to add edges Public voidAddedge (intStartintend) {Adjmat[start][end]= 1; Adjmat[end][start]= 1; } //in order to output the effect Public voidprintgraph () { for(inti=0;i<max_vertex;i++) { for(intj=0;j<max_vertex;j++) {System.out.print (Adjmat[i][j]+" "); } System.out.println (); } } }
Test:
Public classApp { Public Static voidMain (string[] args) {Graph thegraph=NewGraph (); Thegraph.addvertex (A); Thegraph.addvertex (B); Thegraph.addvertex (C); Thegraph.addvertex (D); Thegraph.addvertex (E); Thegraph.addedge (0, 1); Thegraph.addedge (1, 2); Thegraph.addedge (0, 3); Thegraph.addedge (3, 4); Thegraph.printgraph (); }}
Results of the test:
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Detailed diagram of data structure