In programming, if a program calls yourself or passs yourself through a series of procedures, then this program is called recursive program, recursive program (direct or indirect) calls your own process called recursive call, where you call yourself Become a direct recursive call, indirect calls, called indirect delivery. Direct recursive calls such as:
Funa ()
{. . .
Funa ();
. . .
}
Indirect homage modes, such as:
Funa ()
{. . .
Funb ();
. . .
}
Funb ()
{. . .
Funa ();
. . .
}
The recursive program often meets in programming. The main reasons are: There are many mathematical functions to be defined by recursive, such as FACT (N) =
, 2nd-order Fibonacci number Fib (n) =
;
Also some data structures such as binary tree, the structure itself is inherently recursive; in addition, there is a class of problems, it does not have obvious recursive structure, but the recursive process is solved more easily than other methods, such as the eight queen problems, Hanno problem, etc. .
Because of the universality of recursive procedures, we should learn to use recursive to solve problems. Direct recursive procedures and indirect recursive must implement the parameter delivery of the current layer to call the next layer, and obtain the result returned by the next layer, and call the result of the current layer to the upward layer. As for the preservation and recovery of the on-site call in each layer call, it is automatically implemented by the program and does not require manual intervention. Therefore, the key in the design of the recursive program is to identify the parameters required to call, the returned result and the condition of the recursive call. For example, in the step function FACT (N), each layer requires a natural number N, returns N * FACT (N-1), and the condition of recursive call is N = 0; according to this, it is convenient to write its corresponding program (Java language):
Class ClassFact
{Public Static Long Fact (INT N)
{
IF (n == 0) Return 1;
RETURN N * FACT (N - 1);
}
}
In some slightly complicated issues, the recursive program can be easily written according to the above methods, and the implementation of the traveler's question is described below.
Traveler Question: Travelers want to travel N cities and ask each city to experience and only experience it once and require the shortest distance. The problem is also known as the goods, the postman problem is a famous N-P puzzle. When N is large, the recursive traversal method used herein is not used, but the method of the neural network, genetic algorithm, and other methods are used.
To get the shortest path experienced by N cities, each should be compared to the route through the N cities, and select the minimum value as the return result. When n is relatively small, if n is fixed, it can be used to realize the traversal of N (n = 3) cities with a loop, as shown below:
Class Cities
{
Private int [] [] cities = {{1,23}, {45, 68}, {34, 26}}; // The city is expressed as (x, y) x, y is between 0 and 99 Private long shortestLength = 1000000; // Possible maximum distance
Private Long Getlength (INT I, INT J, INT K)
{. . . } // Calculate the approach to experience in I j k
Public int [] getshortestpath ()
{
Int [] ShortestPath = New Int ";
For (int i = 0; i <3; i )
{
For (int J = 0; j <3; j )
{
IF (j == i) Continue;
For (int K = 0; k <3; k )
{
IF (k == j) | | (k == i) Continue;
// calculate the approach to experience in I, J, and K
Long Templength = GetLength (i, J, K);
/ / Update the shortest distance, experience the order
IF (Templength { ShortestLength = Templength; Shortestpath [0] = i; ShortestPath [1] = j; ShortestPath [2] = K; } // end if } // end fork } // end for j } // end for i Return ShortestPath; } Obviously, when n changes, you must rewrite the program. Therefore, considering the use of recursive procedures to achieve solve the N-time. When using recursive procedures to solve the travelist problem, the idea is the same as the loop method: find out various possible sequences, compare the approach taken under various sequences, and find out the shortest approach to experience. In the recursive call of this problem, the current layer is judged to the previous layer that has undergone the city that has undergone, to determine if it has traversed, and if it has traversed, the call is ended and returned to the upward; if not end, select An unnecessary city experience, then pass the experience information to the next layer. Here, the N-layer invoking the incoming parameters can be seen as the city that has traversed, the shortest distance, the resulting result can be seen as an updated experience order and shortest distance (if n Define a class in Java Class Cities { Private int [] [] CITIES; // Each city is expressed as (x, y) x, y is between 0 and 99 Private int [] shortestpath; // Save the shortest distance corresponding to the experience Private int Num; // Save N (Urban number) Private long shortestLength = 100000000; // N cities have passed through the maximum distance private long getlength (int [] tpath) {. . . } // Calculate the approach to tPath Public cities (int N) // Constructs the coordinates of n cities, assumes random numbers between 0 and 99 { . . . } Public int [] getshortestpath () // Get the shortest path { int [] Temppath = new int [Num]; ShortestPath = new int [Num]; int [] citive = new int [Num]; // Whether to save the iChate I have experienced INT CITIESNUM = 0; // has experienced the number of cities For (int i = 0; i CITIESTOURED [I] = 0; Gothrough (Temppath, CitiesNum, Citiestoured); // Traverse For (int i = 0; i Temppath [i] = shortestpath [i]; // get traversal order Return Temppath; // Returns the result } Private vid gothrough (int [] tpath, int cnum, int [] ctoured) // traversal N cities { IF (cnum == 0) // When there is no experience in the city, choose 1st City { CNUM ; Tpath [0] = 0; CTOURED [0] = 1; Gothrough (TPath, CNUM, CTOURED); } Else if (cnum == Num) // Each city has experienced, end { Long templength = getLength (TPATH); // Calculate the approach to this experience IF (Templength { ShortestLength = Templength; // Update the shortest distance and its experience For (int i = 0; i ShortestPath [i] = tpath [i]; } } Else { For (int i = 0; i IF (ctoured [i]! = 1) // Choose an unnecessary city { CTOURED [I] = 1; // Join the city Tpath [cnum] = i; CNUM ; // Expected city number 1 Gothrough (tpath, cnum, ctoured); // Call the next layer CTOURED [I] = 0; // Restore the status of this layer: CNUM -; // Visual city and number } // End if in for (i) } // END ELSE } Private long getLength (int [] tpath) // calculate traversal access in the specified order { Long length = 0; // journey INT nowPoint = 0; // Current city, take 0 For (int i = 1; i { INT j = tpath [i]; Length = (long) Math.SQRT ((CITIES [J] [0]) * (CITIES [J] [0] -cities [nowpoint] [0]) (cities [j] [1] -cities [nowPoint] [1]) * (CITIES [J] [1]); // Plus the distance between the next city NowPoint = J; // Update the current city } Length = (long) Math.SQRT ((CITIES [0] [0]) * (CITIES [0] [0] -cities [nowpoint] [0]) (CITIES [0] [1] -cities [nowPoint] [1]) * (CITIES [0] [1] -cities [nowpoint] [1])); / / Plus the distance between the first tail Return Length; } } // CITIES class definition end This achieves a solution to the N variable time by using recursive. Because the recursive process is clear, the program is easy to read, and the correctness is easy to get verification. Therefore, it is very convenient to make the user compiler and debugging by using the procedure for programming, such as PASCAL, C ( ), and the like. At the same time, due to the preservation and recovery of large quantities, local variables, and program controls in the recursive call. Therefore, the efficiency of the recursive program is relatively low, and the memory is much more, the execution time is long, and if the recursive call of the process can be eliminated in the program, it will definitely improve the time and space efficiency of the program. On the other hand, it is not an incoming recursive, because in some cases, the program structure is simple, and the readability is more meaningful than running efficiency.