Depth-first search and breadth-first search for graph traversal

Source: Internet
Author: User

Graphic Introduction to depth first search

1. Depth First Search Introduction

The depth-first search of the graph (Depth first) is similar to the tree's ordinal traversal.

Its idea: Assuming that the initial state is that all vertices in the diagram are not accessed, from a vertex V, the vertex is first accessed, and then the depth-first search traversal graph is started from each of its inaccessible adjacency points, until all vertices and V paths in the graph are accessed. If there are other vertices that are not being accessed at this time, select a different vertex to start from and repeat the process until all vertices in the diagram are accessed.

Obviously, depth-first search is a recursive process.

2. Depth First search diagram

2.1 Depth First search for non-direction graphs

The following is an example of a "no-direction graph" to illustrate depth-first search.

Depth-first traversal of the above figure G1, starting at vertex a.

Step 1th : Visit A.

Step 2nd : Access (adjacency point of a) C.

After step 1th access a, you should then visit the adjacency point of a, which is one of the "c,d,f". But in the implementation of this article, vertex abcdefg are stored sequentially, c in front of "D and F", so first visit C.

3rd Step : Visit (adjacent point C) B.

After step 2nd of Access C, you should then visit the adjacency point of C, which is one of the "B and D" (A has been visited, not counted). And because B before D, first access to B.

Step 4th : Access (adjacency point C) D.

After the 3rd step accesses the adjacency point B of C, B has no inaccessible adjacency point; therefore, return to another contiguous point D of Access C.

Step 5th : Access (adjacency point of a) F.

A has been accessed before, and all the adjacent points (including recursive adjacency points) of the adjacency point B of "a" are accessed), so that this is returned to another adjacency point F of access a.

6th Step : Visit (adjacent point f) G.

7th Step : Visit (adjacent point of G) E.

So the access order is:A-> C-> B-> D-> F-> G-> E

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.