Breadth First Search (BFS) Algorithm

In this part of the tutorial, we will discuss techniques that can be used to traverse all the vertices of a graph.
Traversing a graph means visiting all nodes and vertices of the graph. Graphs can be traversed using two standard methods. Each of them will be discussed in detail next.

  • breadth first search
  • depth first search

1. Breadth-first search (BFS) algorithm

Breadth-first search is a graph traversal algorithm that traverses the graph starting from the root node and exploring all adjacent nodes. Then, it selects the nearest node and browses all unexplored nodes. For each nearest node, the algorithm follows the same process until the target is found.

The algorithm for breadth-first search is given below. The algorithm starts by checking node A and all its neighbors. In the next step, the neighbors of the nearest node of A are searched, and processing continues in subsequent steps. The algorithm explores all neighbors of all nodes and ensures that each node is visited only once and no node is visited twice.

algorithm

第1步:设置状态 = 1(就绪状态)
    对于G中的每个节点
第2步:将起始节点A排队
    并设置其状态 = 2
    (等待状态)
第3步:重复第4步和第5步,直到
    队列是空的
第4步:使节点N出列。处理它
    并设置其状态 = 3
    (处理状态)。
第5步:将所有邻居排队
      N处于就绪状态
 (其STATUS = 1)并设置它们状态 = 2
 (等待状态)
  [循环结束]
第6步:退出

Example

Consider the graph G shown below and calculate the minimum path from node A to node E. The length of each side is given to be. p1

answer:

The minimum pathP can be found by applying a breadth-first search algorithm, which will start from node A and will end with EEnd. The algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 saves all nodes to be processed, while QUEUE2 saves all nodes processed and deleted from QUEUE1.

Let’s take a look at the graph in nodeA.

  1. GeneralAAdditionalQUEUE1, GeneralNULLAdditionalQUEUE2.
    QUEUE1 = {A}  
    QUEUE2 = {NULL}
    
  2. Remove node fromQUEUE1 and insert all its neighbors, insert node< a i=4>. AAQUEUE2

    QUEUE1 = {B, D}  
    QUEUE2 = {A}
    
  3. Remove node fromQUEUE1 and insert all its neighbors. Insert node into. BBQUEUE2

    QUEUE1 = {D, C, F}   
    QUEUE2 = {A, B}
    
  4. Remove node fromQUEUE1 and insert all its neighbors. Since is the only neighbor that has been inserted, it will not be inserted again. Insert node into. DFDQUEUE2

QUEUE1 = {C, F}  
QUEUE2 = { A, B, D}
  1. Remove node fromQUEUE1 and insert all its neighbors. Add node to . CCQUEUE2

    QUEUE1 = {F, E, G}  
    QUEUE2 = {A, B, D, C}
    
  2. removes fromQUEUE1 and adds all its neighbors. Since all neighbors have already been added, they will not be added again. Add node to . FFQUEUE2

QUEUE1 = {E, G}  
QUEUE2 = {A, B, D, C, F}
  1. was removed fromQUEUE1, and all neighbors of were added to, so they will not be added again. All nodes are visited, and the target node i.e. encounters. EEQUEUE1EQUEUE2
QUEUE1 = {G}  
QUEUE2 = {A, B, D, C, F,  E}

Now, use the nodes available in QUEUE2 to backtrack from E to A.

The minimum path is: A→B→C→E.

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/134822081