简体   繁体   中英

Breadth First Search (BFS)

I am studying the BFS algorithm and I just had a question regarding how neighboring nodes are inserted into the queue.

For example, let's say we're dealing with an undirected graph and we want to perform BFS to output the contents of the graph, then how would we know which order the neighboring nodes are inserted into the queue after an initial node had been pulled from the queue? Also, is there any way to modify how neighboring nodes are inserted into the queue?

Any help would be much appreciated, thank you.

The order of insertion of siblings (neighbors) is entirely determined by the code that inserts them - there is no requirement from a theoretical standpoint. The requirement of BFS is that all nodes at depth k are traversed before any nodes at depth k+1 .

For example, given queue q and root node root :

q.enqueue(root);

while(!q.isEmpty()) {
     Node n = q.dequeue();

     <process n>

     // add children to queue
     for (Node child : n.getChildren()) {
          q.enqueue(child);
     }
}

so if this were to start with n as the root of a tree, it would walk through the whole tree in level order, that is, breadth-first. So you ask, what order are the children inserted? Well, that just depends on what order getChildren() iterates through the nodes (in this example). Other implementations might sort them and add them in that order. Or have a linked-list order for children under a parent. Or pick at random.

If nodes had number values and the tree had the format

1
1.1 1.2          1.3
    1.2.1 1.2.2  1.3.1

the code might be set up to iterate through children in number order. It would process 1, add its children (1.1, 1.2, 1.3) to the queue, process 1.1, add its children (none) to the queue, process 1.2, add its children (1.2.1, 1.2.2) to the queue, process 1.3, and its children (1.3.1) to the queue, and then move on to the third level.

If you wanted to modify the order, you could (A) change the code logic where the nodes get added to the queue, specifying a particular way to choose the next child to push rather than just blindly iterating, (B) change/override the iterating function getChildren() that the enquque block calls, or (C) if you know the method of iteration but cannot change the code, force the tree to have a setup that will be traversed by the iterating function in the way that you want, for example by renaming the nodes or linking them in the structure in a particular way. Option (B) is probably preferred.

Since you say the graph is "undirected", it sounds like perhaps you cannot control the order of the graph itself, so option (C) would not work anyway. Therefore, if you want to control the order of the children, you would need to make the iterating code sort the nodes in some manner so you get a consistent result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM