What is breadth-first search useful for?

前端 未结 10 1230
野趣味
野趣味 2020-12-14 16:43

Usually when I\'ve had to walk a graph, I\'ve always used depth-first search because of the lower space complexity. I\'ve honestly never seen a situation that calls for a b

相关标签:
10条回答
  • 2020-12-14 16:59

    When does it make sense to use a breadth-first search?

    For example, when you need to find the shortest path in a graph - DFS just can't do that. There are some other applications, but, in general, DFS and BFS are the same algorithm working on different data structures (BFS uses queue and DFS works with stack).

    0 讨论(0)
  • 2020-12-14 16:59

    In depth first search you may find "local" solutions - to truly find a global solution you need to traverse the entire graph (or use a heuristic).

    0 讨论(0)
  • 2020-12-14 17:02

    When you want to reach a node by traversing as few edges as possible, i.e. when you want to find the shortest path in an unweighted graph.

    Also the space complexity of a depth first search can be higher than that of a breadth first search when e.g. each node has only one child node, i.e. when the graph is deep but not very broad.

    0 讨论(0)
  • 2020-12-14 17:04

    One example is traversing the filesystem (with limited recursive depth).

    According to wikipedia, it's also useful for certain graph algorithms (bipartiteness, connected components).

    0 讨论(0)
  • 2020-12-14 17:07

    BFS is sometimes really useful. Suppose you have a tree that represents let's say WBS. You may want to present to your user only 1 level of it.

    0 讨论(0)
  • 2020-12-14 17:08

    Could be used for solving a search problem with minimum number of steps. Going in depth first could lead (if not limited somehow) to infinite depth.

    Ex: finding the closest node to the root that satisfies a condition.

    0 讨论(0)
提交回复
热议问题