What is breadth-first search useful for?

前端 未结 10 1231
野趣味
野趣味 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 17:08

    When you need to get the shortest path to a vertex from a graph with no edge weight.

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

    Breadth-first search algorithm likes to stay as close as possible to the starting point. Some of the situations that I can think of are:

    1. Social networking websites can use it for finding the people in the specified distance.
    2. It can be useful in torrenting/peer-to-peer network to look for neighbouring computers.
    3. GPS navigation systems can use it to find nearby locations.
    0 讨论(0)
  • 2020-12-14 17:19

    Nobody has yet mentioned a key factor in cases where breadth-first search is useful: visiting a node one way may eliminate the requirement to visit the node some other way. In some cases, one will end up doing the same work regardless of the order in which nodes are visited, but BFS will have much fewer actions pending at a time than DFS. In other cases, visiting nodes in one sequence may require more work than others; various shortest-path algorithms are given as an example of that. If visiting a node requires visiting its neighbors unless the node is known to be reachable by a path shorter than the current one, visiting nodes in breadth-first order will typically result in nodes being assigned the shortest path--or something close to it--on their first visit. By contrast, a depth-first search would cause many nodes to be visited by very long paths the first time, then by slightly-shorter paths, then slightly-shorter paths, etc. requiring a truly monstrous total amount of work.

    BTW, one nice graphical illustration of the difference between depth-first and breadth-first algorithms is an area flood fill, where a white node is flood-filled by painting it black and flood-filling its neighbors. If one tries to flood-fill an NxN square area starting in a cornder, a depth-first operation would fill the square in a spiral or zig-zag sequence, with NxN-1 operations remaining on the stack. A breadth-first fill would "pour" out from the starting point, and have at most O(N) operations pending, regardless of the shape to be filled. BTW, the flood fill in IBM BASICA worked that way (I'm not sure about QBASIC).

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

    If your search domain is infinite, depth-first-search doesn't guarantee to terminate/find a solution, even if a finite solution does exist.

    Also you can see more elaborate algorithms like A* to be a special subtype of breadth-first-search.

    In general, bfs is both optimal and complete (with finite branching factor) while dfs isn't.

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