Depth-first search algorithm and breadth-first search algorithm

(BFS)

DFS (depth-first algorithm)

Here Insert Picture Description

[Pictures of foreign chains dump fails, the source station may have a security chain mechanism, it is recommended to save the picture down directly upload (img-91x59Mwb-1585482698507) (C: \ Users \ Administrator \ AppData \ Roaming \ Typora \ typora-user-images \ 1585475182196.png)]

topic

Here Insert Picture Description

DFS is recursive

  • Recursive to realize DFS makes the code look simple
  • Recursive when needed the current state of the process variables and the stack is pressed into the inside of the system
  • Push and pop the stack needs more time, if necessary pushed deep stacks, can cause inefficiencies

DFS non-recursive

Stack data structure also supports push and pop operations

You can use the stack to improve operational efficiency

Here Insert Picture Description

Using the shortest path selecting DFS

[Pictures of foreign chains dump fails, the source station may have a security chain mechanism, it is recommended to save the picture down directly upload (img-X6hwg0Oh-1585482698513) (C: \ Users \ Administrator \ AppData \ Roaming \ Typora \ typora-user-images \ 1585475857838.png)]

Breadth-first search (BFS) (Shortest Path)

  • BFS is generally used to solve the shortest path problem
  • Breadth-first search is from the starting point, layer by layer be
  • Point distance among the number of steps the starting point of each layer are the same
  • Binary tree traversal sequence

Here Insert Picture Description

Here Insert Picture Description

Title: Labyrinth shortest

Here Insert Picture Description

Here Insert Picture Description




Published 222 original articles · won praise 22 · views 20000 +

Guess you like

Origin blog.csdn.net/ZHOUJIAN_TANK/article/details/105184778