Search --- BFS

search for

  Depth-first search and breadth-first search tree and widely used in the figure, but their application is far more than that.

BFS

  BFS traverse layer by layer, each layer is traversed more than one traverse the results as a starting point, a distance to traverse all nodes have access to, traversed nodes can not be traversed again.

  level one:

  0->{6.2.1.5}

  Second floor:

  6->{4}

  2->{}

  1->{}

  5->{3}

  the third floor:

  4->{}

  3->{}

  Nodes in each layer are the same distance from the root, provided the distance di denotes an i-th node and the root node, a conclusion is derived: first traversal for node i after traversing the node j, there di <= dj . With this conclusion, the shortest path can be solved such as the optimal solution problem: The first time through to the destination node, it is the shortest path through the path. It should be noted that the use of BFS only the shortest path graph has no right, no right chart refers to the price from one node to another node are denoted by 1.

When the program achieved BFS need to consider the following questions:

  • Queues: one for each storage node traversal obtained
  • Tag: For traversed node should be marked to prevent repeated traversal.

Guess you like

Origin www.cnblogs.com/yjxyy/p/11109543.html