DFS&BFS

DFS&BFS

1. Depth first search

What problem does DFS solve

DFS solves the problem of connectivity, that is, given a starting point (or a certain starting state) and an end point (or some final state), it is judged whether there is a path connecting from the starting point to the ending point.

In many cases, there are many connected paths, and you only need to find one. DFS only cares about the existence of the path, not its length.

Algorithm idea

Starting from the starting point, choose an optional direction and keep moving forward until it is impossible to continue

Then try another direction, until the end,
depth-first search. The data structure is
Insert picture description here
the recursive implementation of stack DFS.
Using recursion to implement DFS can make the code look concise
. When recursive, you need to compress the variables and state of the current program. into the system inside the stack
push and pop the stack needs more time, if necessary pushed deep stacks, can cause operational inefficiencies
DFS non-recursive
data structure stack also supports push and pop operations
can Using stacks to improve operating efficiency
DFS complexity analysis
Since DFS is an algorithm in graph theory, to analyze the complexity of using DFS to solve problems, the idea of ​​graph theory should be borrowed. There are two ways to express graphs:

  1. The adjacency table (there are V vertices and E edges in the graph)
    The time to visit all vertices is O(V), and the time to find the neighbors of all vertices is O(E), so the total time complexity is O(V+E) )

  2. The adjacency matrix (there are V vertices and E edges in the graph) It
    takes O(V) time to find the neighbors of each vertex, and it takes O( V 2 V^2 to find the entire matrixV2 ) time

2. Breadth first search

The breadth-first search is generally used to solve the shortest path problem. The
breadth-first search starts from the starting point and proceeds
layer by layer. The distance from the starting point to the starting point in each layer is the same.
Double-ended BFS
starts from the starting point and the ending point at the same time. The breadth-first search is called double-ended BFS.
Double-ended BFS can greatly improve the efficiency of the search.
For example, if you want to judge how many friends you need to go through in a social application to get to know each other, the
breadth-first search uses a queue.
Insert picture description here
Analysis of BFS Complexity
Since BFS is an algorithm in graph theory, when analyzing the complexity of using BFS to solve problems, the idea of ​​graph theory should be borrowed. There are two ways to express graphs.

  1. In the adjacency list (there are V vertices and E edges in the graph),
    each vertex needs to be visited once, so the time complexity is O(V). When each vertex is visited, the vertices connected to it (that is, every All edges) are also visited once, so they add up to O(E), so the overall time complexity is O(V+E)

  2. The adjacency matrix (there are V vertices and E edges in the graph).
    Since there are V vertices, each vertex must be checked every time whether it is connected with other vertices, so the time complexity is O( V 2 V^2V2)

Guess you like

Origin blog.csdn.net/wqs12345612/article/details/114108153