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

Source: Internet
Author: User

In this chapter, we introduce the depth first search and breadth first search, and then give the realization of C/c++/java.

Directory
1. Depth First search graphics and text introduction
1.1 Depth First Search introduction
1.2 Depth First search diagram
2. Breadth First search graphics and text introduction
2.1 Breadth First Search introduction
2.2 Breadth First search diagram
3. Search algorithm's source code

Reprint please indicate the source: http://www.cnblogs.com/skywang12345/

More content: Data structure and Algorithm series catalogue

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

2.2 Depth-first search of a forward graph

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

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

Step 1th: Visit A.
Step 2nd: Visit B.
After you have accessed a, you should then access another vertex of the out edge of a, that is, Vertex b.
3rd step: Visit C.
After you have accessed B, you should then access another vertex of the out side of B, the vertex c,e,f. In the diagram implemented in this article, vertex abcdefg are stored sequentially, so the C is accessed first.
4th step: Access to E.
Next, access to the other vertex of the out side of C, Vertex e.
5th step: Access D.
Next, the vertex b,d is accessed by another vertex of the out edge of E. Vertex b has been accessed, so vertex d is accessed.
6th step: Access to F.
You should then backtrack to "access another vertex F" of the out edge of a.
7th step: Access to G.

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

Introduction to Breadth-first search graphics and text

1. Breadth First Search Introduction

Breadth-First search (breadth), also known as "breadth First search" or "horizontal first search", referred to as BFS.

The idea is to start from a vertex v in the diagram, after accessing V, each of the inaccessible adjacent points of V is accessed sequentially, and then the adjacency points are accessed sequentially from these adjacency points, and the adjacency points of the first accessed vertex are accessed before the adjacent points of the vertex to be accessed. The adjacent points of all the vertices that have been accessed in the diagram are accessed. If there are still vertices in the diagram that are not being accessed, you need to select a node that has not been accessed as the new starting point, and repeat the process until all the vertices in the diagram are accessed.

In other words, the breadth-first search traversal graph process is a V-starting point, from near to far, sequentially access and V have path-connected and path length of 1,2 ....

2. Breadth First search diagram

2.1 Breadth-First search for non-direction graphs

The following is an example of a "no map" to demonstrate breadth-first search. Or the above figure G1 as an example to illustrate.

Step 1th: Visit A.
Step 2nd: Access C,d,f in turn.
After you have accessed a, you then visit the adjacency point of a. As has been said before, in this implementation of this article, vertex abcdefg are stored sequentially, c in front of "D and F", so first access to C. After you have accessed the C, then visit D,f again.
Step 3rd: Access B,g in turn.
After the 2nd step has accessed the c,d,f, then access their adjacent points in turn. First, the adjacency point B of C is accessed, and then the adjacent point G of F is accessed.
4th step: Access to E.
After the 3rd step has accessed the b,g, then access their adjacent points in turn. Only G has adjacency point E, so the adjacency point E of G is accessed.

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

2.2 Breadth-first search of a forward graph

The following is an example of a "direction graph" to illustrate breadth-first search. Or the above figure G2 as an example to illustrate.

Step 1th: Visit A.
Step 2nd: Visit B.
Step 3rd: Access C,e,f in turn.
After you have accessed B, you will then access another vertex of the out side of B, that is, c,e,f. As already mentioned, vertex abcdefg are stored sequentially in the implementation of this article, so the C is accessed first and then the e,f is accessed sequentially.
Step 4th: Access D,g in turn.
After you have accessed the c,e,f, you can then access another vertex of their out edges in turn. or in accordance with the order of C,E,F access, C has been all visited, then only the e,f, first access to the adjacent point of E D, and then access to the adjacent point G F.

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

The source of the search algorithm

This paper gives the C/c++/java search algorithm source code of "adjacency matrix non-direction graph", "adjacency table non-direction graph", "adjacency Matrix Direction Graph", "adjacency Table Direction graph". Here will no longer explain the source code, please RTFSC, reference source in the comments to understand.

1. C Language Source
1.1 Non-direction graph implemented by adjacency matrix (MATRIXUDG.C)
1.2 Non-direction graph implemented by adjacency table (LISTUDG.C)
1.3 The direction graph implemented by adjacency matrix (MATRIXDG.C)
1.4 The forward graph implemented by adjacency table (LISTDG.C)

2. C + + source code
2.1 Non-direction graph implemented by adjacency matrix (MatrixUDG.cpp)
2.2 Non-direction graph implemented by adjacency table (ListUDG.cpp)
2.3 The direction graph implemented by adjacency matrix (MatrixDG.cpp)
2.4 The forward graph implemented by adjacency table (ListDG.cpp)

3. Java source Code
3.1 Non-direction graph implemented by adjacency matrix (Matrixudg.java)
3.2 Non-direction graph implemented by adjacency table (Listudg.java)
3.3 The direction graph implemented by adjacency matrix (Matrixdg.java)
3.4 The forward graph implemented by adjacency table (Listdg.java)




Http://www.cnblogs.com/skywang12345/p/3711483.html






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.