The adjacency matrix method is used to establish a simple graph, then the breadth-first search (BFS) and the depth-first search (DFS) are used to test the code, and the non-recursive form of depth-first search is realized. It is important to note that each method can only be tested individually because the diagram is initialized before each test.
ImportJava.util.linkedlist;importJava.util.queue;importJava.util.stack;class the vertices of the Graphvertex {//graph. CharVertex Booleanisvisited; Public Graphvertex (CharVT) {vertex = VT; isvisited = False; } @Override Public BooleanEquals (Object obj) {return This.vertex = =((Graphvertex) obj). Vertex; } @Override public intHashcode () {return (This.vertex-' 0 ')); } @Override PublicString toString () {return ' "+ vertex + ' \ t ' +isvisited; }}classGraph {intVertexnum; Graphvertex[] Gvertex; Vertex int[][] Adjmat; Adjacency matrix public Graph (intVertexnum) {This.vertexnum =Vertexnum; Gvertex = newGraphvertex[vertexnum]; Adjmat = new Int[vertexnum][vertexnum]; Initialize all elements to 0} public void Addvertexs (graphvertex[] nodes) {//Add vertex system.arraycopy (nodes, 0, Gvertex, 0, nodes.length); } public void Addedge (Graphvertex ch1, Graphvertex CH2) {//constructs adjacency matrix int index1 =GetIndex (CH1); int index2 =GetIndex (CH2); ADJMAT[INDEX1][INDEX2] = 1; ADJMAT[INDEX2][INDEX1] = 1; } public void BFS (Graphvertex ch) {//Breadth First search queue<graphvertex> Queue = new linkedlist<graphvertex>(); int index =GetIndex (CH); Graphvertex GV =Gvertex[index]; System.out.print (Gv.vertex); Gv.isvisited = True; Queue.offer (GV); while (!Queue.isempty ()) {Graphvertex TMP =Queue.poll (); int i =GetIndex (TMP); for (int j = 0; J < Adjmat[i].length; J + +) {if (adjmat[i][j] = = 1 && gvertex[j].isvisited = = False) {System.out.println (Gvertex[j].vertex); gvertex[j].isvisited = True; Queue.offer (Gvertex[j]); }}}} public void DFS (Graphvertex ch) {//Depth-first search (recursive implementation) INT index =GetIndex (CH); Graphvertex GV =Gvertex[index]; System.out.println (Gv.vertex); Gv.isvisited = True; for (int j = 0; J < Adjmat[index].length; J + +) {if (adjmat[index][j] = = 1 && gvertex[j].isvisited = = False) {DFS (gvertex[j]);}} } public void Nondfs (Graphvertex ch) {//Depth-first search (non-recursive implementation) stack<graphvertex> Stack = new stack<graphvertex>(); int index =GetIndex (CH); Graphvertex GV =Gvertex[index]; System.out.println (Gv.vertex); Gv.isvisited = True; Stack.push (GV); while (!Stack.isempty ()) {Graphvertex TMP =Stack.peek (); int nextindex =Nextnotvisitvertex (TMP); if (Nextindex = =-1) Stack.pop (); Else{Graphvertex Vtex =Gvertex[nextindex]; System.out.println (Vtex.vertex); Vtex.isvisited = True; Stack.push (Vtex); }}} public intNextnotvisitvertex (Graphvertex tmp) {int index =GetIndex (TMP); for (int j = 0; J < Adjmat[index].length; J + +) {if (adjmat[index][j] = = 1 && gvertex[j].isvisited = = False) {returnJ }} return-1; } public intGetIndex (Graphvertex GV) {for (int i = 0; i < gvertex.length; i++) {if(Gv.equals (Gvertex[i])) return i;} return Integer.max_value;}} public class TestClass {//test class public static void addedges (Graph graph, string[] edges) {for (int i = 0; i < EDG Es.length; i++ ) {char ch1 = Edges[i].charat (0 ), char CH2 = Edges[i].charat (1 ); Graph.addedge (new Graphvertex (CH1) , New Graphvertex ((CH2)); }} public static void main (string[] args) {char[] Vertexs = {' A ', ' B ', ' C ', ' D ', ' E ' }; String[] edges = {"AB", "AE", "BC", "BD", "Be", "CD", "DE" }; Graph graph = new graph (vertexs.length); graphvertex[] nodes = new graphvertex[vertexs.length]; for (int i = 0; i < nodes.length; i++ ) {Nodes[i] = New Graphvertex (Vertexs[i]);} graph.addvertexs (nodes); Addedges (graph, edges); Graph. BFS (New Graphvertex (' A '));//graph. DFS (New Graphvertex (' A ')); Graph. Nondfs (New Graphvertex (' A ' ))}}
The establishment of graphs and the realization of two kinds of priority search