Depth first search and breadth first search of Graphs

Keywords: Algorithm

1, Depth first search (DFS)
Each time, after accessing the current node, first access the first adjacent node of the current node. You can see that this is a recursive process.
Depth first traversal algorithm steps:
1. Access the initial tail node v and mark that node v has been accessed
2. Find the first collar node of node v
3. If w exists, continue to execute 4. If w does not exist, return to the first step and continue from the next node of v
4. If W is not accessed, perform depth first traversal recursion on w (that is, treat w as another v, and then proceed to step 123)
5. Find the w adjacent node of node v, write an adjacent node, and go to step 3
Analysis of specific problems:

 A B C D E A 0 1 1 0 0 B 1 0 1 1 1 C 1 1 0 0 0 D 0 1 0 0 0 E 0 1 0 0 0

//   explain

//1 indicates that it can be connected directly, and 0 indicates that it cannot be connected directly

Depth first search process:

A-B-C-D-E,A, B and C can be connected, and the connection is "1"; B is connected with a, C, D and E; C is connected with a and B; D and B connect, e and B connect.

A connects to B. after finding B, B connects to C. C can't connect to other nodes, so it returns to B, B connects to D and B connects to E. (when you find a point, it doesn't change, and one road goes to black).

code:

```/Get the subscript of the first node
public int getfirstNeighbor(int index){
for(int j=0;j<vertexList.size();j++){
if(edges[index][j]>0){
return j;
}
}
return -1;
}

//The next adjacent node is obtained according to the subscript of the previous adjacent node
public int getNextNeighbor(int v1,int v2){
for(int j=v2+1;j<vertexList.size();j++){
if(edges[v1][j]>0){
return j;
}
}
return -1;
}

//Depth first traversal
/* isVisited[] An array that records whether a node accesses
* i The first time is 0
*/
public void dfs(boolean[] isVisited,int i){
//First access the node and output
System.out.print(getValueByIndex(i)+"->");
isVisited[i]=true;
int w=getfirstNeighbor(i);
while(w!=-1){
//Indicates that there are adjacent nodes
if(!isVisited[w]){
dfs(isVisited,w);
}
//If the node is accessed
w=getNextNeighbor(i,w);
}
}
public void dfs(){
isVisited=new boolean[vertexList.size()];
//Traverse all nodes for dfs backtracking
for(int i=0;i<vertexList.size();i++){
if(!isVisited[i]){
dfs(isVisited,i);
}
}
}```

2, Breadth first traversal of graphs (BFS)

The basic idea of breadth first traversal is similar to a hierarchical search process. A queue needs to be used to maintain the order of visited nodes, so as to access the leading nodes of these nodes in this order.

Algorithm steps:

1 access the initial node v and mark that node v has been accessed

2 node v in queue

3 when the queue is not empty, continue to execute, otherwise the algorithm will end

4. Get out of the queue and get the queue head node u

5 find the first collar node v of node u

6 if the collar node w of node u does not exist, move to step 3, otherwise cycle through the following three steps

6.1 if node w has not been accessed, access node W and mark it as accessed

6.2 node w in queue

6.3 find the next collar node after the w collar node of node u and transfer to step 6

The search process is as follows:

A finds B and then finds the successor node C of B. A can find C. at this time, B pops up B in the queue and finds it from node B. at this time, node a has been accessed, and C has also been accessed. At this time, D has been accessed, and E can also be accessed

That is, B and C are accessed by A, and D and E are accessed by A node.

```//breadth-first search
public void bfs(boolean isVisited[],int i){
int u; //Indicates the subscript corresponding to the head node of the queue
//Queue, which records the order of accessing nodes
//Output access node information
System.out.println(getValueByIndex(i)+"->");
//Mark as accessed
isVisited[i]=true;
//Add an accessed node to the queue
while(!queue.isEmpty()){
//Fetch the subscript of the header node of the queue
u=(Integer)queue.removeFirst();
//Get the subscript of the first adjacent node
w=getfirstNeighbor(u);
while(w!=-1){
//Have you visited
if(!isVisited[w]){
System.out.println(getValueByIndex(w)+"->");
//Mark as accessed
isVisited[w]=true;
//Join the team
}
//Take u as the leading point and find the next node after w
w=getNextNeighbor(u,w);
}
}
}

//Traverse all nodes and perform breadth first search
public void bfs(){
isVisited=new boolean[vertexList.size()];
for(int i=0;i<vertexList.size();i++){
if(!isVisited[i]){
bfs(isVisited,i);
}
}
}```

get!

Posted by TechGnome on Tue, 02 Nov 2021 03:23:43 -0700