Depth-first search for graphs

Source: Internet
Author: User

The strategy used by the depth-first search is as implicit in its name: As far as possible, "in-depth" in the diagram.

The steps for depth-first search are as follows:

1. After accessing any of the nodes in the graph, access any of its adjacency nodes W1

2. From W1, Access node W2 (down-depth) adjacent to W1 but not yet visited

3. Repeat procedure 2 (if the node you are currently accessing cannot "drill down", fall back to the previous node and access its other adjacency node) until all nodes have been accessed.

Depth-First search algorithm

Like the breadth-first search algorithm, the depth-first search algorithm also paints nodes in the search process to indicate the state of the nodes.

Unlike breadth-first search, the precursor graph of breadth-first search forms a tree, and the precursor of depth-first search is a depth-priority forest made up of multi-lesson depth-priority trees.

Pseudo code

1 DFS (G)2  forEach vertex u∈white3X.color= White4u.π=NIL5Time=06  forEach vertex u∈g.v7      ifu.color== White8dfs-VISIT (g,u)9 Ten  Onedfs-VISIT (g,u) Atime=time+1 -U.d= Time -U.color=GRAY the  forEach V∈g.adj[u] -     ifV.color= White -v.π=u -dfs-VISIT (g,v) +U.color=black

Where V.D records the time the node V was first discovered (painted in gray)

V.F records the time that the search completed the neighboring list scan of V (painted black time)

The following is a process by which the depth-first search algorithm runs on a graph

Depth-first search for graphs

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