[Data structure] [Binary tree] 5. Depth-first traversal (DFS) of graphs && Breadth-first traversal (BFS)

1. Depth First Traversal (DFS)

1. Understand

1.1 Review the preorder traversal of the binary tree

Depth-first traversal is like pre-order traversal in a binary tree . One pole is inserted to the end, and it is found that it is impossible to move forward, and then back to the previous node to see if there is another fork to go. If there is, then one pole is inserted to the end ; if If no, just go back to the last node ... and continue to recurse until the left and right nodes have been traversed.

Let’s take a look at the preorder traversal of the binary tree first :
Insert picture description here
preorder traversal steps

: Starting from the root node A, brainlessly walk the left subtree, the left subtree, the left subtree ... until it reaches G , he has no left child node, that is, no left subtree to go, and then go to the right child Tree, because the binary tree in this figure is relatively simple, and G has no right subtree to go, then it will go back to the previous node D of G ;

: D’s left subtree has already been walked, now we will go to D’s right subtree;

: Take D's right subtree as a brand new tree, that is to say, after entering the root node H of D's right subtree , you can walk the left subtree without thinking . Because the binary tree in this figure is relatively simple, H has no left subtree, so look at the right subtree of H, and there is no, then fall back to D ;

: Now D left and right sub-trees are gone, the next D has no child nodes that are not traversed, so then passed back to D on one node B , find B right sub-tree, and found there is no, and then be returned To A ;

: A left subtree has gone through, and left A right subtree, the right subtree of A as a new tree to go, the first non-brain go left subtree , then returned to ......

1.2 Depth-first traversal (DFS) of graphs

The same is true for the graph. When you get the root node, first insert a pole until there is no node in front, then go back to the previous node and check if there is another fork road . If there is , treat the other fork road as a brand new graph. Come and go; if not , go back to the previous node to see if there is another fork in the road ...

the difference is:

: The graph may be a ring, that is, there is no head or tail. Therefore , if you encounter a node that has been traversed during the process of inserting a pole to the end , it means that you have reached the junction of the head and tail of the ring , and then go forward. They are all nodes that have been traversed, so don't go forward anymore, start to retreat;

: For a node in the binary tree , when going to its next step, there are only two choices: the left subtree and the right subtree, and the graph may have three, four, five, or even more forks to go down. Therefore, you need to Use while or for loops to ensure that each fork is passed.

The depth-first traversal (DFS) of a graph is as shown in the following figure:
Insert picture description here
first follow the order of 0 -> 1 -> 2 -> 3 -> 4 -> 5 to insert brainlessly, until the next of 5 is 0, and it has passed , So go back to 5 and see if there is another node in 5 that hasn't been walked through . G is found, so G goes down without thinking;

After G -> H , it is found that all the next nodes of H have also been passed, so go back to G to see if G has the next node that has not been passed . If there is no next node, then go back until G -> F -> E -> D , it is found that D has the next node I that has not been walked , so I go to I ;

After I walked, I found that there was no next node that I hadn't walked, so I continued to fall back, D -> C -> B -> A , until I returned to the root node , and the whole traversal was over.

2. Code

The following will post the depth-first traversal (DFS) algorithm based on the adjacency list . Before that, let's briefly introduce the adjacency list :

The adjacency table uses an array of linked list nodes to store all nodes, and uses a linked list to store all nodes adjacent to the current node, as shown below:
Insert picture description here
Among them, ABCDEFGHI in the first row is an array, and the array type is ListNode (linked list node) , And then each element in the array points to its own adjacency point, which constitutes the adjacency list in the figure above .

Based on the storage method of the adjacency table above , the procedure for depth traversal is given as follows:

The program is adapted from the program in another blog post.
The original program comes from: The blog post data structure of CSDN blogger [Misty Rain Blurred Half World War] : Graph traversal (1: Depth-first traversal)

int visited[G_numVertexes];//用一个visited数组来记录某个节点是否被遍历过:1表示遍历过;0表示没有遍历过。其中 ,G_numVertexes是图中节点的个数,也是邻接表中第一行那个数组的元素个数

void DFSTraverse(ListNode *G) {
    
    
    for (int i = 0; i < G_numVertexes; i++)
    {
    
    
        visited[i] = 0; //把visited数组的所有元素都初始化未0,表示都没遍历过
    }
    for (int i = 0; i < G_numVertexes; i++) //为了防止不连通图的特殊情况,在这里对每个节点都进行遍历
    {
    
    
        if (visited[i] == 0)
        {
    
    
            DFS(G, i); //对未遍历过的节点,调用DFS函数
        }
    }
}


void DFS(ListNode *G, int i) {
    
    
    EdgeNode *p;
    visited[i] = 1; //把这个节点的访问标志位设为1,表示已遍历过
    cout << G[i]->data << endl; //打印当前节点
    
    p = G[i]->adjvex; //根据邻接表的存储方式,把p设为G[i]的第一个邻接点
    while (p)
    {
    
    
    	cout << p->data << endl;
        if (!visted[p->adjvex]) //如果G[i]的另一个邻接点未遍历过,则遍历它
        {
    
    
            DFS(G, p->adjvex); //递归访问
        }
        p = p->next;
    }
}

Second, breadth first traversal (BFS)

1. Understand

2. Code

Guess you like

Origin blog.csdn.net/qq_39642978/article/details/112061940
Recommended