The concept of depth-first search algorithm

Source: Internet
Author: User

The concept of depth-first search algorithm

Unlike the breadth-first search algorithm, the depth-first search algorithm resembles the first-order traversal of the tree. The search strategy followed by this search algorithm is to search for a graph as "deep" as possible. Its basic idea is as follows: first, access one of the starting vertex v in the graph, then from V, access any vertex adjacent to V that is not accessed, and then access any vertex that is contiguous to the W1 and is not W1 W2 .... Repeat the process. When it is no longer possible to continue down the access, then go back to the recently accessed vertex, and if it has an adjacent vertex that has not been accessed, proceed from that point until all the vertices in the graph have been accessed (or the same example, from where you begin to traverse all your relatives, for example: first visit your son, Continue to visit your son's son from your son until your son is the last vertex, return to the previous level, visit your son's daughter, and visit the son of your son's daughter ... and so on, until all of your relatives have been visited once, this is different from the breadth-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

Contact Us

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: 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.