# The concept of depth-first search algorithm

Source: Internet
Author: User

The concept of depth-first search algorithm

Algorithm pseudo-code

DFS uses a recursive process, so this process requires a recursive work of the secondary stack, pseudo-code as follows:

``BOOL Visited[max_vertex_num];Access Tag Arrayvoid Dfstraverse (Graph G) {Depth-first traversal of figure G, Access function visit ()for (V=0;v<g.vexnum;++i) visited[v]= FALSE; //initializes data for all vertices, false indicates that for was never visited (v= 0;v<g.vexnum;++v) if (!visited[v]) DFS (G,V); //here from 0 to the last vertex is to prevent an extreme situation: there may be a vertex WI can not be traversed from the vertex w0, so you need to call it a DFS algorithm} void DFS (Graph g,int v) {//from Vertex v , using recursive idea, depth first traverse graph G visit (v); //access vertex v visited[v]=true; Set this vertex to have visited for (W=firstneighbor (g,v); W>=0;w= Nextneighbor (g,v,w)) if (!visited[w]) DFS (g,w);              ``
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
Examples and analysis

First access A, collocated A is already visited, and then access Vertex B, which is adjacent to a and not accessed, B is already accessed, and then accesses the vertex D, which is adjacent to B and is not accessed, and D is already visited. At this point, D has no adjacency point that has not been visited, this time returns the last visited Vertex B, accesses its adjacency and unreachable vertex E, E is already visited .... And so on, until all the vertices in the way are accessed once and only once, the traversal result is abdehcfg.

Performance analysis of DFS algorithm

The DFS algorithm is a recursive algorithm that requires a recursive work stack, so its spatial complexity is O (| v|).
The process of traversing a diagram is actually the process of finding its adjacency point for each vertex, and the time it takes depends on the storage structure used, and when represented in an adjacency matrix, the time required to find the critical point for each vertex is O (| v|), the total time complexity is O (| v|2). When represented in an adjacency table, the time required to find the adjacency point for all vertices is O (| e|), the time required to access the vertices is O (| v|), the total time complexity is O (| v|+| e|).

The concept of depth-first search algorithm

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.