DFS search algorithm - (1) basic graph traversal definitely do! of! understand!

Content summary from "Aha! algorithm! "

As both a sophomore when a chicken dish, do question of how DFS can not do! ! !

As a sophomore in both (!) Chicken dish .... "Aha algorithm" chapter of the fourth book search, I did not understand where to start, look at the fifth chapter ran up. The results of this summary to understand convenient .... excerpt look to distant unknown friends and for myself.

The following language is not precise place like him to bear, to understand important.

Depth search algorithm is a graph for (FIG their blind painting ugly) as shown below, to which reference circle.

 

 

 

Then we traverse when the "depth priority", starting with a road go black strip road walk back to continue a road go black. That is, start with 1 start, walked 2, 5. Then came a walk to get back, get back to 2, found that there are six to go, then went 6 a. And then he walks. Then back to the 2, but all have branch 2 has been visited, it is a Stepping back, went 1. Back after 1, 2 have run this road, we went to 3, and then went to 7, walk back to 3,3 no other branch can go on back to 1, then go 4

 

Then the access order is a diagram (not duplicate nodes): 1,256,374

If a road has completed, for example, went from 2 5, the end, we must return to a junction vertex 2. From the process is called backtracking 5-2.

So how to store a picture of it? We use a two-dimensional array to store arr:

 

As shown in these drawings, a two-dimensional array in the i-th row j-th column is represented by vertex i to vertex j if there edges. 1 expressed an edge, no edge is empty (at the time of programming can be assigned to 9999, I was drawing too much trouble eliminating the need), two-dimensional array of rows and columns equal to zero my assignment.

Can be seen, above this figure is symmetrical about the main diagonal, because this figure is 'undirected graph'. This figure is no edge direction, from 1 to 2 and from 2 to 1 is the same, so arr [2] [1] = arr [1] [2] = 1.

 

Here is the code to achieve:

Which, book [] array start all this initial value is 0, that has not been visited. 1 are assigned to visit.

There is also a need to know the little knowledge, is within my code and two return, a return indicates the end of the first function, the second return means return on a dfs () function, which is the special use of recursive functions. For example: It is dfs (5), encountered a second return code executing code, and then will return dfs () function on an execution, which became dfs (2), this process is backtracking.

. 1  void DFS ( int CUR) // CUR is the current number of vertices in 
2  {
 . 3      the printf ( " % D " , CUR); // in order to print out the points traversed 
. 4      SUM ++; // SUM is a global variable, the initial value of 0. Each access point, sum ++. 
. 5      IF (SUM == n-)
 . 6          return ; // n-a global variable, the number of total points. If the sum == n, is the access point are finished, exit function. 
. 7      
. 8      for ( int I = . 1 ; I <= n; I ++) // sequentially attempt from point 1 to point number n, see who is connected to the cur point 
. 9      {
 10          IF (ARR [cur] [I] = = 1Book && [I] == 0 ) // if this point is not accessed and is connected with the cur 
. 11          {
 12 is              Book [I] = . 1 ; // this point marked as visited 
13 is              DFS (I); // recursive, from this point we continue to traverse was continued further 
14          }
 15      }
 16      return ; // when all points have been connected cur accessed, and returns to a DFS () 
. 17 }

 

 

The complete code is as follows:

. 1 #include <the iostream>
 2 #include <cstdio>
 . 3  the using  namespace STD;
 . 4  
. 5  int Book [ 101 ], SUM, n-, ARR [ 101 ] [ 101 ];
 . 6  
. 7  void DFS ( int CUR) // CUR current the number of vertices 
. 8  {
 . 9      the printf ( " % D " , CUR); // sequentially traversed to print out dots 
10      SUM ++; // SUM is a global variable, the initial value is 0. Each access point, sum ++. 
. 11      IF (SUM == n-)
 12 is          return ;// the n-as a global variable, the number of total points. If the sum == n, is the access point are finished, exit function. 
13 is  
14      for ( int I = . 1 ; I <= n; I ++) // from point 1 to point number n sequentially try, to see who is connected to the cur point 
15      {
 16          IF (ARR [cur] [I] = = . 1 && Book [I] == 0 ) // if this point is not accessed and is connected with the cur 
. 17          {
 18 is              Book [I] = . 1 ; // this point marked as visited 
. 19              DFS (I); // recursion continues from this point was continued for further traverse 
20 is          }
 21 is      }
 22 is      return ; //When all points have been connected cur accessed, and returns to a DFS () 
23 is  }
 24  
25  
26 is  
27  int main ()
 28  {
 29      SUM = 0 ;
 30      int m, A, B;
 31 is      CIN> > n >> m; // m and n represents many sides expressed a few points 
32      for ( int I = . 1 ; I <= n; I ++ ) {
 33 is          for ( int J = . 1 ; J <= n; J ++ ) {
 34 is              IF (I == J)
 35                  ARR [I] [J] = 0 ;
 36              the else 
37 [                 ARR [I] [J] = 9999999 ; // the unmarked side connected 999999 
38 is          }
 39      }
 40  
41 is      // read those edges between the vertices 
42 is      for ( int I = 0 ; I <m; I ++ )
 43 is      {
 44 is          CIN >> A >> B;
 45          ARR [A] [B] = . 1 ;
 46 is          ARR [B] [A] = . 1 ; // due undirected graphs, the two marks must. I said in front of a 
47      }
 48      Book [ 1 ] = 1 ; // vertex from No. 1. 1 label number has been accessed vertices
49     dfs(1);
50 
51     getchar();getchar();
52 
53 
54     return 0;
55 }

 

ok fine so simple graph traversal say finished next to continue to write

 

Guess you like

Origin www.cnblogs.com/juzijuziju/p/11461471.html