breadth first or depth first search

后端 未结 4 561
抹茶落季
抹茶落季 2021-02-04 17:09

I know how this algorithm works, but cant decide when to use which algorithm ?

Are there some guidelines, where one better perform than other or any considerations ?

4条回答
  •  悲&欢浪女
    2021-02-04 17:41

    BFS is generally useful in cases where the graph has some meaningful "natural layering" (e.g., closer nodes represent "closer" results) and your goal result is likely to be located closer to the starting point or the starting points are "cheaper to search".

    When you want to find the shortest path, BFS is a natural choice.

    If your graph is infinite or pro grammatically generated, you would probably want to search closer layers before venturing afield, as the cost of exploring remote nodes before getting to the closer nodes is prohibitive.

    If accessing more remote nodes would be more expensive due to memory/disk/locality issues, BFS may again be better.

提交回复
热议问题