Algorithm of Graph Search algorithm (i) _ algorithm

Source: Internet
Author: User
1. Introduce

In this paper, we introduce a relatively primary graph search algorithm, which includes depth first traversal, breadth first traversal and bidirectional breadth first traversal.

2. Depth First traversal DFS

2.1 Algorithm thought

Begins with a vertex v in the diagram, accesses the node, and then starts the depth-first traversal of the graph from the unreachable adjacent points in the V. Until all vertices in the diagram are connected with the V path, and if a vertex is not already accessed in the diagram, then a vertex in the other graph is not accessed, and the above process is repeated. Until all vertices in the diagram are accessed.

A depth-first search traverses a tree-like first-order traversal. Assuming that the initial state of a given graph G is that all vertices are not accessed and that a vertex i is chosen as the starting point of the traversal in G, the depth-first search traversal can be defined as follows:

(1) The vertex i is first accessed and the access mark is set to the visited, i.e. visited[i]=1;

(2) Then search for the next vertex J that is connected with the vertex i if J has not been visited, then access it, and put J's access Mark as visited, Visited[j]=1, and then repeat the process from J, if J has been visited, and then look at other vertices connected with I;

(3) If the vertex connected with I has been accessed, return to the previous access vertex and repeat the process until all vertices in the diagram are accessed.

2.2 Algorithm implementation

The algorithm of the adjacency matrix is described in the following form: 1 2 3 4 5 6 7 8 9 DFS1 (graph & G, int i, int n)//from vertex I go through {                cout<<g.v[i];                    Output access vertex visited[i]=1;   Access token 1 indicates that the for (J=1 j<=n; j + +) if (g.arcs[i][j]= =1) && (!visited[j)) DFS (G,J,N) has been accessed; }

adjacency Table algorithm description for the following form: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

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.