41 Toad Data Structure Note the depth of the traversal of the 41 graph is preferred

Source: Internet
Author: User

41 Toad Data Structure Note the depth of the traversal of the 41 graph is preferred

This famous article:"For me , the meaning of life is to put yourself in the shoes of people , worry about others , Joy of others." -- Einstein "

In the previous chapter we implemented the graph of adjacency multiple table representations, as well as the depth traversal and breadth traversal of the code, this time we first look at depth traversal.

Welcome reprint, Reproduced please indicate the source:

1. Principle

Graph traversal, also known as graph traversal, belongs to the content of the data structure. Refers to any vertex that is set to be accessed once and only once for all vertices in the graph. The traversal operation of the graph is similar to the traversal operation of the tree. The traversal of graphs is a basic operation of graphs, and many other operations of graphs are based on the traversal operation.

Because of the complexity of the graph structure itself, the traverse operation of the graph is more complex, which is mainly manifested in the following four aspects:

① in the graph structure, there is no "natural" first node, and any vertex in the graph can be the first visited node.

② in a non-connected diagram, you can only access all the vertices on the connected component where it is located, so consider how to select the next starting point to access the remaining connected components in the diagram.

③ in the graph structure, if there is a loop, then a vertex is accessed, it is possible to return to the vertex along the loop.

④ in the graph structure, one vertex can be connected to several other vertices, and when such vertices are accessed, there is an issue of how to pick the next vertex to be accessed.

2. Depth-First traversal

Basic idea: First a vertex v0, access this vertex, and then sequentially from the v0 adjacent vertices of the depth of the first traversal, until all the vertices connected to the V0 path are accessed, and if the vertex is not accessed at this point, select a vertex as the starting point, repeat the process until all the vertices are accessed. It can be seen that the depth-first traversal is a recursive process.

As shown in Figure 1 , one of the non-direction graphs

The resulting sequence of its depth-first traversal is:

0->1->3->7->4->2->5->6

3. Code implementation

The code is part of section 40 code.

A total of 5 points 1,2,3,4,5

where 1 and 2,1 and 3, link

2 and 3,2 and 4 links

3 and 1,3 and 2,3 and 4 links

4 and 2,4 and 3 links.

3.1 Dfstraverse

Sets the flag array original initialized to 0.

Then the DFS function.

3.2 Dfs

From the first v vertex, recursively depth first traverses the graph.

The traversed point set flag bit 1.

Then call the Visitvex function to output the vertex value.

Then each time you call Nextadjvex to get the next adjacency point, and then see if it is already output, and if there is no output, DFS is recursive.

void dfs (amlgraphG,intv)

{

intw,p=1;

visitedvexes[v]=1;

Visitvex (G,v); // the value of the output vertex

for (W=firstadjvex (g,v); W>0;w=nextadjvex (G,v, W))

if (!visitedvexes[w]) // to a fixed-point call that has not been visited DFS

DFS (G, W);

}

3.3 Firstadjvex

Find the first adjacency point of v W

Returns 1 if no vertices are present.

If the village is now, then determine if there are adjacency points.

If there is an adjacency point, determine if Ivex is V, and if so, return another point of the edge, or return the point if it is not.

3.4 Nextadjvex

Returns the next adjacency point of V (relative to W)

The main methods of judging are:

Each time you start with a linked list of V nodes, find the next one if the next node Ivex and V are equal, use the Ilink connection. If the next node is equal to Jvex and V, the Jlink link is used. This is where the points connected to the V vertices are found. Until the list is empty.

In the Code, v represents the current point for range Traversal, W represents the point adjacent to the current and V points, and W is a small-scale traversal.

The code starts with v=2, which is the input Traversal node 2, and then w=4,

Then recursive v=4,w=3

Recursive v=3,w=4

v=3,w=2,

V=3,w=1

V=1,w=3

v=1,w=2

v=4,w=2

V=2,w=3

V=2,w=1

The rule is written as:

v=2, w=4

V=4,w=3

V=3,w=4

v=3,w=2

V=3,w=1

V=1,w=3

V=1, w=2

v=4,w=2

v=2, w=3

V=2,w=1

End.

3.5 Visitvex

The value of the output point.

4. Source Code

The source code is also refer to the Notes in section 40.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

41 Toad Data Structure Note the depth of the traversal of the 41 graph is preferred

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.