Introduction to Algorithms: Do not hit the brick wall does not look back ---- depth-first search algorithm (DFS)

Introduction to Algorithms: Do not hit the brick wall does not look back ---- depth-first search algorithm (DFS)

Introduction to Algorithms

Introduction to Algorithms

About DFS algorithm

slightly

DFS algorithm thought

  1. First, a vertex is not visited as the starting vertex, go unvisited vertex along the edge of the current vertex; when there is no unvisited vertex, then back to a vertex, continue to test access to other vertices until all vertices have been visited.
    clearly, a depth-first traversal along a branch traversed until the end, and then back again the same manner traversing along one another, until all vertices have been visited so far.
  2. The key to understanding of DFS: DFS is to solve the "what to do the moment", as "the next step to do" and "what to do the moment" is the same
  3. Implemented method generally calls itself recursively

DFS algorithm model

  void dfs(int step){
      //判断边界
      //满足条件则返回

       //尝试每一种可能
      for(int i=1;i<n;i++){
           //继续下一步
           dfs(step+1)
      }
     //返回
 }

DFS time complexity of algorithm:

1. Although the way of implementation is recursive, but be aware that the time complexity. Under is for reference only:
Here Insert Picture Description

DFS algorithm application:

  1. Full array series and variants of the problem
  2. Maze (FIG communication problem) high complexity -----
  3. FIG number of independent sub-graphs (seed filling method)
  4. Graph traversal

Specific description

1. The full array series and variants of the problem

Sample Code full array series of problems

2. maze (FIG communication problem)

Maze (FIG communication problem)

FIG. 3. FIG number of independent sub (seed filling method)

FIG number of independent subgraphs

4. FIG traversal

Graph traversal

Published 155 original articles · won praise 15 · views 160 000 +

Guess you like

Origin blog.csdn.net/wangdamingll/article/details/105298086