Breadth first search: the timing of checking visitation status

后端 未结 2 1613
北荒
北荒 2021-02-01 07:43

In a breadth first search of a directed graph (cycles possible), when a node is dequeued, all its children that has not yet been visited are enqueued, and the process continues

2条回答
  •  醉话见心
    2021-02-01 08:09

    DFS

    Suppose you have a graph:

     A---B---E
     |   |
     |   |
     C---D
    

    And you search DFS from A.

    You would expect it to search the nodes A,B,D,C,E if using a depth first search (assuming a certain ordering of the children).

    However, if you mark nodes as visited before placing them on the stack, then you will visit A,B,D,E,C because C was marked as visited when we examined A.

    In some applications where you just want to visit all connected nodes this is a perfectly valid thing to do, but it is not technically a depth first search.

    BFS

    In breadth first search you can mark the nodes as visited either before or after pushing to the queue. However, it is more efficient to check before as you do not end up with lots of duplicate nodes in the queue.

    I don't understand why your BFS code failed in this case, perhaps if you post the code it will become clearer?

提交回复
热议问题