About BFS, DFS some new understanding

About BFS, DFS some new understanding

What is the "search algorithm"?

  • Algorithm is applied to data on the particular structure of the depth-first search and breadth first search algorithms algorithms are based on the "map" of such a data structure. This is because the ability to express such a data structure diagram is very strong, most of the design scene search can be abstracted into a "map."
  • Search algorithm on the graph, the most direct understanding is that, in the figure can be found starting from a vertex to another vertex of the path. There are many specific methods, the simplest, most "violent" depth-first, breadth-first search, as well as A *, IDA * and other heuristic search algorithm.

Breadth-first search (BFS)

  • BFS (Breadth-Firsr-Search) referred to: BFS
  • It is actually a kind of "carpet-style" search strategy, which is to look away from the nearest vertex, then the next nearest, turn out the search.
  • Breadth-first search algorithm of time, space complexity:
  • The worst case, the termination vertex t from the start vertex s very far away, you need a map to find complete traversal. This time, each vertex queue to be out again, each side will be visited once, so the time complexity of BFS is O (V + E), V represents the number of vertices, E is the number of edges . Of course, for a connected graph, it means that all the vertices of a graph is communication, E certainly greater than or equal to V-1, so that the time complexity of breadth-first search may be abbreviated as O (E)
  • Breadth-first search space consumed mainly visited several auxiliary variables in an array, the queue queue, prev arrays. Three storage space size will not exceed the number of vertices, the space complexity is O (V).

Depth-first search (DFS)

  • Depth-first search (Depth-First-Search), referred to DFS
  • The most intuitive example is the "Maze", chosen at random to a fork in the road, walked discovered when a dead end, you can fall back on a fork in the road, re-select a way to continue going until eventually find the exit. Such moves is a kind of depth-first search strategy.
  • Time depth-first search algorithm, space complexity:
  • Each side will be the most visited twice, once traversed, once rolled back. Therefore, the time complexity of the depth-first search algorithm is O (E), E denotes the number of edges.
  • Consume memory depth-first search algorithm is mainly visited, prev arrays and recursive call stack. Maximum depth visited, prev size of the array is proportional with the number of vertices V, the recursive call stack does not exceed the number of vertices, so the total space complexity is O (V).

to sum up

  1. Breadth-first search and depth-first search on the graph are the two most common and basic search algorithm, compared to other advanced search algorithms such as A *, IDA *, etc., to be simple and crude, no optimization, so, also search algorithm called the violence.
  2. These two search algorithms not only for the state space, that little map search.
  3. BFS, popular understanding, carpet layers of advancing from the starting vertex Start, out traversal. BFS needed to achieve by means of queues, the resulting path is traversed, the shortest path starting vertex to vertex terminated.
  4. Depth-first search with backtracking is thought, it is well suited for use recursion. Put another way, depth-first search is achieved by means of a stack.
  5. In terms of efficiency, the time complexity of depth-first and breadth-first search is O (E), space complexity is O (V).
Published 134 original articles · won praise 59 · Views 140,000 +

Guess you like

Origin blog.csdn.net/Code_star_one/article/details/104593205