Application of graphs-the implementation of search
The search of the graph mainly includes two kinds of 1 are depth first search, one is breadth first search. Depth-First search as the name implies is always far away from the source search point, search down, depth-first search mechanism is a stack to achieve, breadth-first search algorithm is implemented by the queue, because the implementation of the mechanism is different, so the way to search is also different.
First, the depth-first search method, first find a starting point, and then do three things: 1: First access to the vertex, 2: And then put the point into the stack, in order to remember it 3: Mark the point so that no longer access to the point. The following can access any vertex connected to a, as long as it has not been accessed.
Rule 1: Access an unreachable vertex and tag it to add it to the stack.
Rule 2: When rule 1 cannot be executed, if the stack is not empty, a vertex is popped from the stack.
Rule 3: This is the end of the search when Rule 1 and rule 2 cannot be executed.
The key to depth-first search is how to find points that have not been visited with a vertex adjacency, the adjacency matrix is the key, find the row where the specified vertex is located, search backwards from the first column for a value of 1, the column number is the number of the adjacency vertex, check if the vertex has not been accessed, and if so, access the next vertex If the row has no vertices equal to 1 and has not been accessed, the vertices connected to the specified point are all accessed.
Code:
// returns a vertex that has not been visited Public int Getadjunvisitedvertex (int v) { for (int j = 0;j<= currentvertexs;j++) {iffalse) { return J; } } return -1; }
Implementation of the depth-first algorithm:
Public voidDfs () {//start at the No. 0 pointVertexs[0].wasvisited=true; //Print the vertexDisplayvertex (0); Mystack.push (0); while(!Mystack.isempty ()) { intv =Getadjunvisitedvertex (Mystack.peek ()); if(v = =-1) {mystack.pop (); }Else{ //same as the first point of processingvertexs[v].wasvisited =true; //Print this pointDisplayvertex (v); //into the stackMystack.push (v); } } //the stack is empty and it's over. for(intj=0;j<currentvertexs;j++) { //all vertex flag positions are false endvertexs[j].wasvisited =false; } }
The following breadth-first search:
The depth-first search algorithm behaves as far away from the starting point as possible, and the breadth is not, as close as possible to the starting point. First, access the adjacency point of the starting point, and then access the farther area.
Rule 1: Access the neighboring point of the next future visit (if present) the vertex must be the adjacency point of the current vertex, mark it, and insert it into the queue.
Rule 2: If rule 1 cannot be executed because no vertices have not been visited, remove a vertex from the head of the queue (if one exists) and make it the current vertex.
Rule 3: If the queue is empty, rule 2 cannot be executed, the search ends.
Code:
Importjava.util.LinkedList;ImportJava.util.Queue;ImportJava.util.Stack; Public classGudugraph {/*** 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. PrivateQueue<integer>queue; Publicgudugraph () {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; }} Queue=NewLinkedlist<integer>(); } //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 (); } } //the implementation of methods for printing vertices Public voidDisplayvertex (intv) {System.out.print (Vertexs[v].label); } //returns a vertex that has not been visited Public intGetadjunvisitedvertex (intv) { for(intj = 0;j<=currentvertexs;j++) { if(Adjmat[v][j] = = 1&& Vertexs[j].wasvisited = =false) { returnJ; } } return-1; } Public voidBFs () {//start at the No. 0 pointVertexs[0].wasvisited=true; //Print the vertexDisplayvertex (0); Queue.add (0); while(!Queue.isempty ()) { intV2 =Getadjunvisitedvertex (Queue.peek ()); if(v2 = =-1) {queue.poll (); }Else{ //same as the first point of processingvertexs[v2].wasvisited =true; //Print this pointDisplayvertex (v2); //into the stackQueue.add (v2); } } //the stack is empty and it's over. for(intj=0;j<currentvertexs;j++) { //all vertex flag positions are false endvertexs[j].wasvisited =false; } } }
Application of graphs-the implementation of search