Ladies and gentlemen, good crossing, the last time we were talking about the example of the figure, this time we say the example is: depth-first traversal. Gossip Hugh,
Words return to the positive turn. Let's talk C chestnuts together!
Crossing, we said in the last time how to store graphs in code, and this one we mainly say how to traverse graphs in code. First select diagram
, and then traverse the point that is connected to the point and not being accessed, repeating the operation until all the points in the diagram are accessed
So far, this traversal method is called deep traversal . From the description of this method, it can be found that using recursion to implement the method is a good
Select, the following is the specific implementation steps:
- 1. Use the adjacency matrix to hold the graph, the order of the points above the rows and columns in the adjacency matrix can be defined by itself;
- Span style= "FONT-FAMILY:SIMSUN; Font-size:18px ">2. The name of the midpoint of the graph is placed in an array in the order of the points in the row or column of the adjacency matrix;
- 3. Use an array to mark whether each point has been accessed, and the size of the array is the number of midpoints in the graph;
- 4. Select a point to traverse the graph sequentially, based on the midpoint of the adjacency matrix or the order of the points on the column;
- 5. View the array in step 3, if the point selected in step 4 has been accessed, then go back to step 4 and select the next point, otherwise go to step 6;
- 6. Mark the point in the array in step 3 has been accessed;
- 7. Find the location of the point in the adjacency matrix, and then look from the adjacency matrix for a path between the point and other points;
- 8. If the point in step 7 has no path to its point, or if another point has been accessed, select the next point, or go to step 9;
- 9. Repeat step 6 through step 8 until all points have been traversed.
Crossing, the text does not write code, the detailed code put in my resources, you can click here to download the use.
The diagram we use in the program and the adjacency matrix of the graph are as follows:
The running results of the program are as follows, comparing the running results and graphs, it can be found that the program is completely traversed by the node depth in the graph.
---Show the Graph---0 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |---DFS---a->b->e->f->c->d->
You crossing, for example of depth-first traversal Let's just talk about this. I want to know what the following example, and listen to tell.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Talk C Chestnut Bar (44th time: C-language instance-depth first traversal)