A recursive algorithm implementation method, device and electronic device
By maintaining the execution progress, the recursive algorithm is converted into a loop execution process, solving the problem of stack overflow caused by excessive recursive function calls, and realizing the normal execution of recursive algorithms with high complexity.
Patent Information
- Application Number
- CN202010608367.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-29
- Publication Date
- 2024-11-01
- Estimated Expiration
- 2040-06-29
AI Technical Summary
When existing recursive algorithms deal with high complexity problems, too many recursive function calls lead to stack overflow, unable to execute normally, and it is difficult to achieve high complexity problems.
By maintaining the execution progress, the previous recursive algorithm can be continued after completing a call to the recursive algorithm, thus converting the recursive execution process into a process of loop execution, avoiding the use of the stack, and implementing a highly complex recursive algorithm.
Without being limited by stack depth, the execution of recursive algorithms with high complexity is implemented, which avoids stack overflow problems and ensures the normal operation of recursive algorithms.
Smart Images

Figure CN111767522B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a recursive algorithm implementation method, device and electronic equipment. Background Art
[0002] A recursive algorithm is an algorithm that calls itself during the algorithm execution process, and is often designed to solve complex problems. In related technologies, a recursive algorithm is usually implemented by a recursive function. For example, taking the calculation of the value of the xth digit of the Fibonacci sequence as an example, it can be implemented by a recursive function as shown below:
[0003]
[0004] It can be seen that the recursive function calls itself in the recursive function, so when executing the recursive function, the recursive function itself will be called once or multiple times. For example, if fun(3) needs to be executed, fun(2) and fun(1) need to be called, and after fun(2) and fun(1) are executed, fun(3) is returned to be executed, so as to determine the return value of fun(3) according to the return values of fun(2) and fun(1). In this process, the input parameters of the recursive function will change. For example, when executing fun(3), the input parameter x=3, when executing fun(2), the input parameter x=2, and when returning to execute fun(3), the input parameter x=3.
[0005] In the related art, during the execution of a recursive function, a stack can be used to store these input parameters. The stack will store the newly saved data at the top of the stack, and the process obtains the latest input parameters by accessing the top of the stack. For example, assuming that fun(3) needs to be executed, 3 is saved at the top of the stack in the stack, and the process executing the recursive function can obtain x=3 by accessing the top of the stack. When fun(2) is called during the execution of fun(3), 2 is saved at the top of the stack. Since the stack will store the newly saved data at the top of the stack, the process executing the recursive function accesses the top of the stack to obtain x=2. After fun(2) is executed, when it is necessary to return to execute fun(3), 2 can be deleted from the stack. At this time, 3 is back at the top of the stack, so the process executing the recursive function can obtain x=3 by accessing the top of the stack.
[0006] However, for a recursive algorithm designed to solve a problem with excessive complexity, such as a recursive algorithm designed to process a large amount of data, when the recursive algorithm is implemented through a recursive function, the recursive function itself may be called too many times in the recursive function, and the stack depth is limited, and the variables (such as the above-mentioned input parameters) at each call cannot be stored, resulting in the inability to execute the recursive function normally. Therefore, it is difficult to implement the recursive algorithm through a recursive function, resulting in these highly complex problems being unable to be solved. Summary of the invention
[0007] The purpose of the embodiments of the present invention is to provide a method, device and electronic device for implementing a recursive algorithm, so as to solve a problem with high complexity through a recursive algorithm. The specific technical solution is as follows:
[0008] In a first aspect of the present invention, a recursive algorithm implementation method is provided, the method comprising:
[0009] For the current variable, a recursive algorithm is started, wherein the initial value of the current variable is the target variable;
[0010] During the execution of the recursive algorithm, whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded, and the recursive step is the step that needs to call the recursive algorithm;
[0011] During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the execution of the last execution of the recursive algorithm continues from the execution progress of the last execution of the recursive algorithm until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable.
[0012] In a second aspect of the present invention, a recursive algorithm implementation device is provided, the device comprising:
[0013] A recursive execution module, used to start executing a recursive algorithm for a current variable, wherein the initial value of the current variable is a target variable;
[0014] An execution progress recording module is used to record the execution progress of the recursive algorithm in the process of executing the recursive algorithm, whenever the executed step is a recursive step, and the recursive step is a step that needs to call the recursive algorithm;
[0015] The return execution module is used to, during the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, continue to execute the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm based on the obtained result, until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable.
[0016] In a third aspect of the present invention, there is provided an electronic device, comprising:
[0017] Memory, used to store computer programs;
[0018] The processor is used to implement any method step described in the first aspect when executing the program stored in the memory.
[0019] In a fourth aspect of the present invention, a computer-readable storage medium is provided, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, any method step described in the first aspect is implemented.
[0020] The recursive algorithm implementation method, device and electronic device provided by the embodiment of the present invention can maintain the execution progress so that the previous recursive algorithm can be continued to be executed after the call of a recursive algorithm is completed, thereby converting the recursive execution process into a loop execution process, thereby implementing the recursive algorithm without using a stack, and thus will not be limited by the stack depth, so that a recursive algorithm with higher complexity can be implemented. Of course, any product or method implementing the present invention does not necessarily need to achieve all the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0022] Figure 1a A schematic diagram of stack storage data provided by an embodiment of the present invention;
[0023] Figure 1b Another schematic diagram of stack storage data provided by an embodiment of the present invention;
[0024] Figure 1c Another schematic diagram of stack storage data provided by an embodiment of the present invention;
[0025] Figure 2A schematic diagram of a flow chart of a recursive algorithm implementation method provided in an embodiment of the present invention;
[0026] Figure 3 Another schematic diagram of a flow chart of a recursive algorithm implementation method provided in an embodiment of the present invention;
[0027] Figure 4 Another schematic diagram of a flow chart of a recursive algorithm implementation method provided in an embodiment of the present invention;
[0028] Figure 5 Another schematic diagram of a flow chart of a recursive algorithm implementation method provided in an embodiment of the present invention;
[0029] Figure 6 A schematic diagram of a structure of a recursive algorithm implementation device provided in an embodiment of the present invention;
[0030] Figure 7 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0031] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0032] To more clearly describe the recursive function execution method provided by the embodiment of the present invention, the recursive algorithm will be described below. For a problem with high complexity, it may be difficult for a developer to design an algorithm to directly solve the problem. The developer can simplify the problem step by step to obtain multiple similar sub-problems, and solve the problem by solving these sub-problems.
[0033] For example, suppose we need to calculate n! (n factorial). Since n! = n*(n-1)!, we can equate this problem to calculating n-1 factorial, and then multiplying n-1 factorial by n. That is, this problem can be simplified to calculating n-1 factorial. Similarly, calculating n-1 factorial can be simplified to calculating n-2 factorial. Based on this idea, we can design a recursive algorithm that includes the following steps to calculate n!:
[0034] First step: calculate (n-1)! ;
[0035] The second step: calculate n*(n-1)! .
[0036] Among them, (n-1)! in the first step, if n is greater than 1, can also be calculated by the recursive algorithm. If n is equal to 1, since 0! = 1, (n-1)! = 1 can be directly obtained. For practical needs, other forms of recursive algorithms can also be designed to calculate n!. It can be understood that how to design a recursive algorithm is not an improvement point of the embodiment of the present invention, so it will not be repeated here.
[0037] In the related art, the above recursive algorithm can be implemented in the form of a recursive function, and the recursive function can be as follows:
[0038]
[0039] In the process of implementing a recursive algorithm, a recursive function often needs to be called multiple times, and the variable value is different each time the recursive function is called. Therefore, when executing a recursive function, the process needs to rely on the stack to cache the variable value each time the recursive function is called. For example, when executing fun(2), the variable value 2 can be stored at the top of the stack. At this time, the data in the stack is as follows: Figure 1a As shown, the process accesses the top of the stack, calls the recursive function for the first time with the variable value 2, and executes fun(2); since 2 is not equal to 0, the recursive function needs to be called again with the variable value 1. This call can be interrupted and the variable value 1 is stored at the top of the stack. At this time, the data in the stack is as follows Figure 1b As shown in the figure, the process accesses the top of the stack, calls the recursive function for the second time with the variable value 1, and executes fun(1). Similarly, since 1 is not equal to 0, the recursive function needs to be called again with the variable value 0. This call can be interrupted and the variable value 0 is stored at the top of the stack. At this time, the data in the stack is as follows: Figure 1c As shown in the figure, the process accesses the top of the stack and calls the recursive function for the third time with the variable value 0, executing fun(0). Since the variable value is 0 at this time, the third call can end normally, and the calculation result 1 of fun(0) is returned, and the data at the top of the stack is deleted. At this time, the data in the stack is as follows: Figure 1b As shown, the variable value 1 is at the top of the stack. The process accesses the top of the stack, so that the second call that was interrupted before is resumed. By accessing the top of the stack, x=1 can be obtained and fun(1) is executed. Since fun(1)=1*fun(0), according to the return value of the third call to the recursive function to execute fun(0) is 1, it can be calculated that the return value of the second call to the recursive function to execute fun(1) is 1*1=1. Therefore, the second call ends normally and returns 1, deleting the data at the top of the stack. At this time, the data in the stack is as follows: Figure 1aAs shown, the variable value 2 is at the top of the stack. The process accesses the top of the stack, so that the first call that was interrupted before is restored. By accessing the top of the stack, x=2 can be obtained, and fun(2) is executed. Since fun(2)=2*fun(1), according to the return value of the second call to the recursive function to execute fun(1) is 1, it can be calculated that the return value of the first call to the recursive function to execute fun(2) is 2*1=2. Fun(2) is executed and the result 2 is returned.
[0040] However, if the recursive function needs to be called too many times during execution, for example, the number of times is greater than the depth of the stack, the stack cannot save the variable values of each call to the recursive function, and a stack overflow will occur, causing a freeze or crash, resulting in the function being unable to be executed normally, that is, the recursive algorithm cannot be implemented. Therefore, it is difficult to implement a recursive algorithm with high complexity through a recursive function. For example, when a binary search method (a recursive algorithm) is implemented using a recursive function, if there are too many elements included in the table to be looked up, it is difficult to implement the binary search method through a recursive function.
[0041] Based on this, the embodiment of the present invention provides a recursive function execution method, which can be seen in Figure 2 , Figure 2 The figure is a flow chart of a recursive function execution method provided by an embodiment of the present invention, which may include:
[0042] S201, start executing the recursive algorithm for the current variable, and the initial value of the current variable is the target variable.
[0043] S202, during the execution of the recursive algorithm, whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded. The recursive step is a step that needs to call the recursive algorithm.
[0044] S203, during the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the execution of the last execution of the recursive algorithm continues from the execution progress of the last execution of the recursive algorithm, until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the previous current variable.
[0045] By selecting this embodiment, the execution progress can be maintained so that the previous recursive algorithm can be continued to be executed after completing the call of a recursive algorithm, thereby converting the recursive execution process into a loop execution process, thereby implementing the recursive algorithm without using a stack, and therefore will not be limited by the stack depth, so a recursive algorithm with higher complexity can be implemented.
[0046] In S201, the target variable may refer to the variable value that the user needs to target when designing the recursive algorithm. The target variable may be different according to different application scenarios, and the target variable may be a numerical value or a non-numerical value. For example, if the user needs to calculate 6000!, the target variable is 6000. For another example, if the user needs to find a specified element in a preset sequence, the target variable is the preset sequence.
[0047] The execution of a recursive algorithm may be to execute each step of the recursive algorithm in sequence according to the steps designed by the recursive algorithm. It is understandable that there are some steps in the recursive algorithm that need to call the recursive algorithm. For example, taking the aforementioned recursive algorithm for calculating n! as an example, in the first step, when n is greater than 0, (n-1)! cannot be directly calculated, and the recursive algorithm needs to be called to calculate (n-1)!. These steps that need to call the recursive algorithm are referred to as recursive steps in this article, and the steps that do not need to call the recursive algorithm are referred to as non-recursive steps. In theory, the recursive algorithm should include at least one recursive step and at least one non-recursive step. The number of recursive steps and non-recursive steps in the recursive algorithm depends on the design of the recursive algorithm, and the design of the recursive algorithm is not an improvement point of the embodiment of the present invention, so it will not be repeated here.
[0048] In this embodiment, for a non-recursive step, the result of the non-recursive step can be directly calculated, while for a recursive step, it is necessary to call the recursive algorithm for the new current variable to obtain the result of the recursive step. Taking the aforementioned recursive algorithm for calculating n! as an example, when the recursive algorithm is started, the current variable is n. After the first step is executed for the first time, the recursive algorithm needs to be called for n-1 to obtain (n-1)!, and the current variable changes to n-1.
[0049] In S202, the recursive algorithm executed this time refers to the recursive algorithm executed for the current variable, and the execution progress is used to indicate the execution completion status of the recursive algorithm executed this time. For example, taking the aforementioned recursive algorithm for calculating n! as an example, the first step is a recursive step. Assuming that the current variable is 100, when the first step is executed for 100, since the first step is a recursive step, information indicating that "the recursive algorithm executed for the variable value 100 will next execute the second step" or "the recursive algorithm executed for the variable value 100 has already executed the first step" can be recorded as the execution progress. The representation form of the execution progress may be different according to different application scenarios. For example, the step identifier of the second step may be used as the execution progress to indicate that "the recursive algorithm executed for the variable value 100 will next execute the second step", or the step identifier of the first step may be used as the execution progress to indicate that "the recursive algorithm executed for the variable value 100 has already executed the first step", wherein the step identifier is the unique identifier of the step in the recursive algorithm.
[0050] In S203, the result of executing the recursive algorithm for the current variable is obtained by completing the recursive algorithm for the current variable. If the current variable is not the target variable, the result obtained is not the result that the user needs to obtain through the recursive algorithm. For example, taking the aforementioned recursive algorithm for calculating n! as an example, the result that the user needs to obtain through the recursive algorithm is n!. If the current variable is n-3, the result of executing the recursive algorithm for the current variable is (n-3)!, which is not the result that the user needs. It is necessary to use (n-3)! to continue the recursive algorithm for n-2 to obtain the result that the user needs.
[0051] The object targeted when continuing to execute the last recursive algorithm is the last current variable. In some application scenarios, the last current variable can be calculated based on the current variable. Taking the aforementioned recursive algorithm for calculating n! as an example, assuming that the current variable is j, the last current variable is j+1. In some application scenarios, the last current variable may not be calculated based on the current variable. For example, in the recursive algorithm for calculating the value of the nth position of the Fibonacci sequence, if the current variable is j, the last current variable may be j+1 or j+2. Based on this, in a possible embodiment, in the execution of the recursive algorithm, whenever the executed step is a recursive step, the current variable is recorded as the input parameter of the recursive algorithm this time. When continuing to execute the last execution of the recursive algorithm, the last execution of the recursive algorithm can be continued from the execution progress of the last execution of the recursive algorithm for the input parameters of the last execution of the recursive algorithm. This embodiment can be applied to the application scenario where the last current variable may not be calculated based on the current variable, and this embodiment can also be applied to the application scenario where the last current variable can be calculated based on the current variable.
[0052] In order to more clearly explain the recursive algorithm provided by the embodiment of the present invention, the recursive algorithm provided by the embodiment of the present invention will be exemplarily explained in combination with a specific application scenario. It can be understood that since the recursive algorithm needs to be called in the process of implementing the recursive algorithm, the steps that need to be executed in the process of implementing the recursive algorithm are more than the steps designed by the recursive algorithm. In this article, in order to distinguish the steps designed by the recursive algorithm from the steps executed when implementing the recursive algorithm, the steps executed when implementing the recursive algorithm are represented by Step.
[0053] Application scenario 1: Calculate 3! , and assume that the designed recursive algorithm is:
[0054] The first step, a=(x-1)!
[0055] The second step, result = x*a
[0056] Among them, result represents the result of the recursive algorithm, and x represents the input. The termination condition of the recursive algorithm is x=1, and the output result 1 is met when the condition is met.
[0057] The target variable is 3, so the current variable is initially 3
[0058] Step 1: Start executing the recursive algorithm for 3.
[0059] Step 2: The first step of executing the recursive algorithm for 3 is to calculate 2! This step is a recursive step, so the execution progress of the recursive algorithm for 3 is recorded.
[0060] The execution progress can be used to indicate that the recursive algorithm for 3 has executed the first step, or it can be used to indicate that the recursive algorithm for 3 needs to execute the second step next. The two representation methods are different, but the progress they represent is actually the same.
[0061] Step 3: Start the next round of iteration. At this time, the current variable becomes 2, and the recursive algorithm starts to execute for 2.
[0062] Step 4: The first step of executing the recursive algorithm for 2 is to calculate 1! This step is still a recursive step, so the execution progress of the recursive algorithm for 2 is recorded.
[0063] Step 5: Start the next iteration. At this time, the current variable becomes 1, and the recursive algorithm starts to execute for 1. At this time, the termination condition of the recursive algorithm is triggered, and the result of executing the recursive algorithm for 1 is 1.
[0064] Step 7: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 2. Since the previously recorded execution progress indicates that the first step of the recursive algorithm has been executed for 2, the second step of the recursive algorithm is executed for 2 according to the recorded execution progress. When executing the second step, the previously obtained result "1" is used as a obtained by executing the first step of the recursive algorithm for 2, that is, a = 1. At this time, result = 2*1 = 2 can be calculated, that is, the result of executing the recursive algorithm for 2 is 2.
[0065] Step 8: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 1. Since the previously recorded execution progress indicates that the first step of the recursive algorithm has been executed for 1, the second step of the recursive algorithm is executed for 1 according to the recorded execution progress. When executing the second step, the previously obtained result "2" is used as a obtained by executing the first step of the recursive algorithm for 1, that is, a=2. At this time, result=3*2=6 can be calculated, that is, the result of executing the recursive algorithm for 1 is 6. At this time, the result of executing the recursive algorithm for the target variable has been obtained, so the process ends and 3! =6 is obtained.
[0066] Application scenario 2: Calculate the 4th digit of the Fibonacci sequence. For the convenience of description, the Fibonacci sequence digit i is recorded as fib(i) below. The Fibonacci sequence refers to the sequence that satisfies the recursive formula: fib(x) = fib(x-1) + fib(x-2), and fib(1) = fib(2) = 1. Assume that the designed recursive algorithm is as follows:
[0067] The first step: a = fib(x-1)
[0068] The second step: b = fib(x-2)
[0069] The third step: result = a + b
[0070] Among them, result represents the result of the recursive algorithm, and x represents the input. The termination condition of the recursive algorithm is that x is less than or equal to 2, and the output result 1 is met when this condition is met.
[0071] The target variable is 4, so the current variable is initially 4.
[0072] Step 1: Execute the recursive algorithm for 4.
[0073] Step 2: The first step of executing the recursive algorithm for 4 is to calculate fib(3). This step is a recursive step, so the execution progress of the recursive algorithm for 4 is recorded.
[0074] The execution progress can be used to indicate that the recursive algorithm for 4 has executed the first step, or it can be used to indicate that the recursive algorithm for 4 needs to execute the second step next. The two representation methods are different, but the progress they represent is actually the same.
[0075] Step 3: Start the next round of iteration. At this time, the current variable becomes 3, and the recursive algorithm starts to execute for 3.
[0076] Step 4: The first step of executing the recursive algorithm for 3 is to calculate fib(2). This step is a recursive step, so the execution progress of the recursive algorithm for 3 is recorded.
[0077] Step 5: Start the next iteration. At this time, the current variable becomes 2, and the recursive algorithm starts to execute for 2. At this time, the termination condition of the recursive algorithm is triggered, and the result of executing the recursive algorithm for 2 is 1.
[0078] Step 6: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 3. Since the previously recorded execution progress indicates that the first step of the recursive algorithm has been executed for 3, the second step of the recursive algorithm is executed for 3 according to the recorded execution progress. The result "1" obtained in Step 5 can be used as a obtained by executing the first step of the recursive algorithm for 3, that is, a = 1.
[0079] Step 7: The second step of the recursive algorithm for 3 is to calculate fib(1). This step is a recursive step, so the execution progress of the recursive algorithm for 3 is recorded.
[0080] Step 8: Start the next iteration. At this time, the current variable becomes 1, and the recursive algorithm starts to execute for 1. At this time, the termination condition of the recursive algorithm is triggered, and the result of executing the recursive algorithm for 1 is 1.
[0081] Step 9: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 3. Since the previously recorded execution progress indicates that the second step of the recursive algorithm has been executed for 3, the third step of the recursive algorithm is executed for 3 according to the recorded execution progress. The result "1" obtained in Step 8 can be used as b obtained by executing the second step of the recursive algorithm for 3, that is, b = 1. At this time, it can be calculated that result = a + b = 2, that is, the result of executing the recursive algorithm for 3 is 2.
[0082] Step 10: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 4. Since the previously recorded execution progress indicates that the first step of the recursive algorithm has been executed for 4, the second step of the recursive algorithm is executed for 4 according to the recorded execution progress. The result "2" obtained in Step 9 can be used as a obtained by executing the first step of the recursive algorithm for 4, that is, a = 2.
[0083] Step 11: The second step of the recursive algorithm for 4 is to calculate fib(2). This step is a recursive step, so the execution progress of the recursive algorithm for 4 is recorded.
[0084] Step 12: Start the next iteration. At this time, the current variable becomes 2, and the recursive algorithm starts to execute for 2. At this time, the termination condition of the recursive algorithm is triggered, and the result of executing the recursive algorithm for 2 is 1.
[0085] Step 13: Since the result is obtained, the previous iteration is returned, that is, the recursive algorithm is executed for 4. Since the previously recorded execution progress indicates that the second step of the recursive algorithm has been executed for 4, the third step of the recursive algorithm is executed for 4 according to the recorded execution progress. The result "1" obtained in Step 12 can be used as b obtained by the second step of the recursive algorithm for 4, that is, b = 1. At this time, it can be calculated that result = a + b = 3, that is, the result of the recursive algorithm for 4 is 3. At this time, the result of the recursive algorithm for the target variable has been obtained, so the process ends and fib(4) = 3 is obtained.
[0086] See also Figure 3 , Figure 3 Another flow chart of a recursive algorithm implementation method provided by an embodiment of the present invention is shown, which may include:
[0087] S301, start executing the recursive algorithm for the current variable, and the initial value of the current variable is the target variable.
[0088] This step is the same as the aforementioned S201. Please refer to the relevant description of the aforementioned S201 and will not be repeated here.
[0089] S302, each time a step is executed, the execution progress of the recursive algorithm is recorded.
[0090] It is understandable that if the execution progress of the recursive algorithm is recorded each time a step is executed, the execution progress of the recursive algorithm will also be recorded each time a recursive step is executed. In this embodiment, if the execution progress of the recursive algorithm has been recorded before the execution progress of the recursive algorithm is recorded, the existing execution progress can be updated or a new execution progress can be recorded. Exemplarily, assuming that the current variable is 7, when the first step of the recursive algorithm is executed for 7, the execution progress indicating that "the recursive algorithm executed for the variable value 7 will next execute the second step" can be recorded; when the second step of the recursive algorithm is executed for 7, the execution progress originally recorded to indicate that "the recursive algorithm executed for the variable value 7 will next execute the second step" can be updated to indicate that "the recursive algorithm executed for the variable value 7 will next execute the third step". Alternatively, the execution progress originally recorded to indicate that "the recursive algorithm executed for the variable value 7 will next execute the second step" can be retained, and a new execution progress indicating that "the recursive algorithm executed for the variable value 7 will next execute the third step" can be recorded.
[0091] S303, whenever the execution progress of the current recursive algorithm indicates that the current recursive algorithm is completed, the output of the latest executed step is used as the result of executing the recursive algorithm for the current variable.
[0092] For example, taking the aforementioned recursive algorithm for calculating n! as an example, if the execution progress indicates that the recursive algorithm for the current variable has completed the second step, it can be considered that the execution of the recursive algorithm is completed.
[0093] S304: Based on the obtained result, continue to execute the last execution of the recursive algorithm starting from the execution progress of the latest record of the last execution of the recursive algorithm.
[0094] For the last execution of the recursive algorithm, please refer to the relevant description in the aforementioned S203 and no further details will be given here. The latest recorded execution progress refers to the last recorded execution progress among all the recorded execution progress. For example, assuming that execution progress 1 and execution progress 2 have been recorded for the last execution of the recursive algorithm, where execution progress 1 is recorded before execution progress 2, the latest recorded execution progress is execution progress 2.
[0095] By selecting this embodiment, the logic for recording the execution progress when executing non-recursive steps and recursive steps can be unified. There is no need to distinguish between non-recursive steps and recursive steps when recording the execution progress, which can simplify the implementation logic of the recursive algorithm.
[0096] The following will describe the method of recording the execution progress. In a possible embodiment, the execution progress may be recorded in the form of a step identifier of the executed step. The step identifier is used to uniquely identify the step in the recursive algorithm. Different steps in the recursive algorithm have different step identifiers. The form of the step identifier may be different depending on the application scenario. For example, it may be a numerical value, a letter, or other characters other than numerical values and letters. It may be a single character or a combination of multiple characters.
[0097] In this embodiment, the recursive algorithm implementation method can be as follows Figure 4 As shown, including:
[0098] S401, start executing the recursive algorithm for the current variable, and the initial value of the current variable is the target variable.
[0099] This step is the same as the aforementioned S201. Please refer to the relevant description of the aforementioned S201 and will not be repeated here.
[0100] S402, whenever a step is executed, the step identifier of the executed step is recorded.
[0101] The recorded step identifier is the execution progress of the recursive algorithm this time, which can indicate that the recursive algorithm has executed the step indicated by the recorded step identifier. In other possible embodiments, it can also be the step identifier of the next step of the executed step to indicate that the recursive algorithm will execute the step indicated by the recorded step identifier next. Although the two methods of recording the execution progress record different step identifiers, the meaning of the recorded execution progress is equivalent.
[0102] S403: Whenever the step identifier recorded in the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable.
[0103] In a possible embodiment, the step identifier can be used to indicate the number of steps remaining from the last step in the recursive algorithm. The step that is executed last in the recursive algorithm according to the designed execution sequence, the result of the last step in the recursive algorithm is the result of the recursive algorithm, and since the result of the last step is the result of the recursive algorithm, the last step should be a non-recursive step. Exemplarily, taking the aforementioned recursive algorithm for calculating n! as an example, the last step is the second step, the number of steps remaining from the first step to the second step is 1, so the step identifier of the first step can be 1, and the number of steps remaining from the second step to the second step is 0, so the step identifier of the second step can be 0. In this embodiment, whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable.
[0104] S404: Based on the obtained result, continue to execute the last execution of the recursive algorithm starting from the step indicated by the step identifier of the latest record of the last execution of the recursive algorithm.
[0105] The following describes how to determine whether the result is the result of executing the recursive algorithm on the target variable:
[0106] In a possible embodiment, it can be determined whether the current execution of the recursive algorithm is a continuation of the first execution of the recursive algorithm. If the current execution of the recursive algorithm is not a continuation of the first execution of the recursive algorithm, since only the variable targeted by the first execution of the recursive algorithm is the target variable, it can be considered that the result obtained by the current execution of the recursive algorithm is not the result obtained by executing the recursive algorithm on the target variable, and the execution of the last execution of the recursive algorithm can be continued from the execution progress of the last execution of the recursive algorithm. If the current execution of the recursive algorithm is a continuation of the first execution of the recursive algorithm, since the variable targeted by the first execution of the recursive algorithm is the target variable, it can be considered that the result obtained by the current execution of the recursive algorithm is the result obtained by executing the recursive algorithm on the target variable, and the obtained result can be used as the result of executing the recursive algorithm on the target variable.
[0107] Regarding how to determine whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm, it can be determined whether the variable targeted by the current execution of the recursive algorithm is the target variable. If the variable targeted by the current execution of the recursive algorithm is the target variable, then the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm. If the variable targeted by the current execution of the recursive algorithm is not the target variable, then the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm.
[0108] In a possible embodiment, during the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in the current execution of the recursive algorithm is deleted. Then, it can be determined whether the execution progress is still recorded. If the execution progress is still recorded, it can be determined that the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm. If the execution progress is not recorded, it is determined that the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm.
[0109] In a possible embodiment, the execution progress may be recorded in an array. Exemplarily, a preset array is maintained, and each element of the preset array is initially empty (in other possible embodiments, each element may also be a preset initial value at the beginning). Whenever the execution progress is recorded, the execution progress may be recorded at the end of the array, wherein the end of the array refers to the first empty element in the array in order from front to back. For example, when the execution progress is recorded for the first time, since each element in the array is empty, the end of the array is the first position of the array, so the execution progress is recorded in the first position of the array. When the execution progress is recorded for the second time, since the first position of the array has already recorded the execution progress, the first position of the array is a non-empty element, and the second position is an empty element, so the end of the array is the second position, so the execution progress is recorded in the second position of the array.
[0110] The aforementioned input parameters can be recorded corresponding to the execution progress. For example, in a possible embodiment, the input parameters and the execution progress can be used as member variables in a structure, and the structure can be written into an array to record the input parameters and the execution progress in the array. For example, a structure Scope can be defined, and the Scope can include a member variable n and a member variable mark. The input parameter is assigned to n in the Scope, and the execution progress is assigned to mark in the Scope. Then the Scope is written into the array, so that the input parameter and the execution progress can be recorded in the array.
[0111] By selecting this embodiment, the working principle of stack push and pop can be simulated through an array, which is convenient for developers to better design and understand the implementation method of the recursive algorithm.
[0112] In order to more clearly illustrate the recursive algorithm implementation method provided by the embodiment of the present invention, the recursive algorithm implementation method provided by the embodiment of the present invention will be described in detail below in combination with the computer language of the recursive algorithm implementation method provided by the embodiment of the present invention. Taking the aforementioned application scenario 2 as an example, the computer language can be as follows:
[0113]
[0114]
[0115]
[0116] The following will take the above computer language as an example to explain, please refer to Figure 5 , Figure 5 Another flow chart of a recursive algorithm implementation method provided by an embodiment of the present invention is shown, which may include:
[0117] S501, determining the number n of times a recursive algorithm is called in a recursive algorithm to be executed.
[0118] Taking the aforementioned application scenario 1 as an example, the number of times the recursive algorithm is called in the recursive algorithm is 1, so n=1. Taking the aforementioned application scenario 2 as an example, the number of times the recursive algorithm is called in the recursive algorithm is 2, so n=2.
[0119] S502: Generate n recursive sub-functions and 1 non-recursive sub-function according to the recursive algorithm.
[0120] Each recursive sub-function is used to implement a recursive step in a recursive algorithm, and a non-recursive sub-function is used to implement a non-recursive step in a recursive algorithm. It can be understood that the recursive sub-function here is just a name, and the recursive sub-function itself does not call the recursive sub-function, that is, the recursive sub-function is not a recursive function. For example, see the aforementioned computer language, in which
[0121]
[0122] It is a recursive sub-function. Among them, Current is the current variable, makeScope is a custom function used to generate the structure Scope. Stack push is a custom function used to record Scope at the end of the stack, and stack is the aforementioned preset array.
[0123] For the convenience of description, it is assumed that the generated sub-functions are {P(1), P(2), ..., P(n+1)}. It can be understood that Figure 5 The embodiment shown is only a possible flow chart of the recursive algorithm implementation method provided by the embodiment of the present invention. In other possible embodiments, multiple non-recursive sub-functions may be generated, and these multiple non-recursive sub-functions are used to jointly implement the non-recursive steps in the recursive algorithm. Implementing the non-recursive steps in the recursive algorithm through a non-recursive sub-function can effectively reduce the complexity of implementing the recursive algorithm.
[0124] S503, creating a Scope for the object of the first iteration operation, and recording the created Scope at the end of the stack.
[0125] Scope includes at least two member variables: input parameters and execution progress. For input parameters and execution progress, please refer to the above related descriptions, which will not be repeated here. For the convenience of description below, it is assumed that the execution progress is represented by the step identifier, and the step identifier is the number of steps remaining from the last step in the recursive algorithm, and the execution progress is represented as rest.
[0126] In this embodiment, since no step in the recursive algorithm is executed initially, the number of steps remaining from the last step in the recursive algorithm is n+1, that is, the initial value of the execution progress is n+1.
[0127] S504, read the last element in the stack.
[0128] Reading the last element means reading the last non-empty element in the stack in order from front to back. In some possible embodiments, if the preset array does not include empty elements, reading the last element in the preset array is equivalent to reading the last element in the preset array in order from front to back.
[0129] S505, determine whether rest is equal to 0, if rest is not equal to 0, execute S506, if rest is equal to 0, execute S510.
[0130] The rest is the rest in the read Scope.
[0131] S506, enter P(n+2-rest).
[0132] It is understandable that if rest is 0, it means that the result of executing the recursive algorithm for the current variable has been obtained, so there is no need to continue to execute the recursive algorithm for the current variable. If rest is not 0, it means that the result of executing the recursive algorithm for the current variable has not been obtained. As described above about the execution progress, rest means that the rest-1th to last step of the recursive algorithm needs to be executed for the current variable. Since the recursive algorithm includes a total of n+1 steps, the rest-1th to last step is the n+2-rest step, so enter P(n+2-rest).
[0133] S507, rest is decremented by 1.
[0134] Decrement the rest in the read Scope by 1.
[0135] S508, determine whether the entered sub-function is a recursive sub-function, if it is a recursive sub-function, execute S509, if it is not a recursive sub-function, return to S504.
[0136] S509, creating a Scope for the variable targeted by the next execution of the recursive algorithm, and recording the created Scope at the end of the stack.
[0137] If the sub-function entered is a recursive sub-function, since the recursive sub-function is used to implement the recursive step in the recursive algorithm, and the recursive algorithm is called in the recursive step, the variable targeted at the next execution of the recursive algorithm is the variable when the recursive algorithm is called in the recursive step implemented by the recursive sub-function entered.
[0138] The input parameter in the created Scope is the variable for the next execution of the recursive algorithm, and the execution progress is n+1.
[0139] S510, summarizing the results of the previous executions of the recursive algorithm to calculate the result of the current execution of the recursive algorithm.
[0140] It is understandable that if rest is equal to 0, it means that the calculation of this iteration is about to be completed. According to the principle of the recursive algorithm, the results of the previous executions of the recursive algorithm need to be used to complete the calculation of the recursive algorithm.
[0141] S511, delete the last element in the pre-stack
[0142] S512, determine whether the stack is empty. If the stack is not empty, return to execute S504. If the stack is empty, execute S513.
[0143] S513: Use the last result obtained as the result of executing the recursive algorithm for the target variable.
[0144] The embodiment will be exemplarily described below in conjunction with the aforementioned application scenario 2. For the convenience of description, Scope is represented in the form of (i, rest), where i is the input parameter in Scope and rest is the execution progress in Scope.
[0145] In the process of calculating the 4th digit of the Fibonacci sequence, since the first step and the second step are both recursive steps, n=2, and recursive subfunctions P(1), P(2) and non-recursive subfunction P(3) are constructed, where P(1) is used to implement the first step, P(2) is used to implement the second step, and P(3) is used to implement the third step.
[0146] Record (4,3) in the stack. At this time, the stack is [(4,3)]. Read the last element in the stack, that is, read (4,3). It can be determined that rest = 3 is not equal to 0. Therefore, enter P(2+2-3) or enter P(1) to perform the first step of the recursive algorithm for 4, that is, calculate a = fib(3). And reduce rest in the read element by 1. At this time, the stack is [(4,2)].
[0147] Since the execution of P(1) for 4 is a recursive sub-function, a Scope is created for the variable targeted by the next execution of the recursive algorithm, that is, 3, and the created Scope is recorded at the end of the stack. The created Scope is (3,3). After the Scope is recorded at the end of the stack, the stack is [(4,2), (3,3)].
[0148] Read the element at the end of the stack again, that is, read (3,3), and you can determine that rest = 3 is not equal to 0, so enter P(2+2-3), that is, enter P(1), and execute the first step of the recursive algorithm for 3, that is, calculate a = fib(2). And decrement the rest in the read element by 1, and the stack is now [(4,2), (3,2)].
[0149] Since the execution of P(1) for 3 is a recursive sub-function, a Scope is created for the variable targeted by the next execution of the recursive algorithm, that is, 2, and the created Scope is recorded at the end of the stack. The created Scope is (2,3). After the Scope is recorded at the end of the stack, the stack is [(4,2), (3,2), (2,3)].
[0150] Read the element at the end of the stack again, that is, read (2,3), and we can determine that rest = 3 is not equal to 0, so we enter P(2+2-3), that is, enter P(1), and execute the first step of the recursive algorithm for 2, that is, calculate fib(1). And decrement the rest in the read element by 1. At this time, the stack is [(4,2), (3,2), (2,2)]. Since fib(1) = 1 can be directly calculated, executing P(1) for 2 is not a recursive subfunction.
[0151] Read the element at the end of the stack again, that is, read (2,2), and we can determine that rest = 2 is not equal to 0, so we enter P(2+2-2), that is, enter P(2), and execute the second step of the recursive algorithm for 2, that is, calculate b = fib(0). And decrement the rest in the read element by 1. At this time, the stack is [(4,2), (3,2), (2,1)]. Since fib(0) = 0 can be directly calculated, executing P(2) for 2 is not a recursive subfunction.
[0152] Read the element at the end of the stack again, that is, read (2,1), and you can determine that rest = 1 is not equal to 0, so enter P(2+2-1) or P(3) to perform the third step of the recursive algorithm for 2, that is, calculate result = a + b. And decrement the rest in the read element by 1. At this time, the stack is [(4,2), (3,2), (2,0)].
[0153] Since P(3) is a non-recursive subfunction, we read the element at the end of the stack again, that is, read (2,0), and we can determine that rest = 0. At this time, we can summarize the previously obtained a = 1 and b = 0, and calculate that the result of this recursive algorithm is fib(2) = a + b = 1. Delete the element at the end of the stack, and the stack is now [(4,2), (3,2)].
[0154] At this time, stack is not empty, so the element at the end of stack is read again, that is, (3,2). It can be determined that rest = 2 is not equal to 0, so enter P(2+2-2), that is, enter P(2) to execute the second step of the recursive algorithm for 3, that is, calculate b = fib(1). And reduce rest in the read element by 1. At this time, stack is [(4,2), (3,1). Since fib(1) = 1 can be directly calculated, executing P(2) for 3 is not a recursive subfunction, and a = fib(2) = 1 is obtained.
[0155] Read the element at the end of the stack again, that is, read (3,1), and you can determine that rest = 1 is not equal to 0, so enter P(2+2-1), that is, enter P(3), and perform the third step of the recursive algorithm for 3, that is, calculate result = a + b, and you can get b = fib(1) = 1. And decrement the rest in the read element by 1, and the stack is now [(4,2), (3,0)].
[0156] Since P(3) is a non-recursive subfunction, we read the element at the end of the stack again, that is, read (3,0), and we can determine that rest = 0. At this time, we can summarize the previously obtained a = fib(2) = 1 and b = fib(1) = 1, and we can calculate that the result of this recursive algorithm is fib(3) = a + b = 2. Delete the element at the end of the stack, and the stack is now [(4,2)].
[0157] At this time, stack is not empty, so we read the element at the end of stack again, that is, read (4,2), and we can determine that rest = 2 is not equal to 0, so we enter P(2+2-2), that is, enter P(2), to perform the second step of the recursive algorithm for 4, that is, calculate b = fib(2), and we can get a = fib(3) = 2. And we decrement rest in the read element by 1, and now stack is [(4,1)].
[0158] Since P(2) executed for 4 is a recursive sub-function, a Scope is created for the variable targeted by the next execution of the recursive algorithm, that is, 2, and the created Scope is recorded at the end of the stack. The created Scope is (2,3). After the Scope is recorded at the end of the stack, the stack is [(4,1), (2,3)].
[0159] Read the element at the end of the stack again, that is, read (2,3), and we can determine that rest = 3 is not equal to 0, so we enter P(2+2-3), that is, enter P(1), to execute the first step of the recursive algorithm for 2, that is, calculate fib(1). And decrement the rest in the read element by 1. At this time, the stack is [(4,1), (2,2)]. Since fib(1) = 1 can be directly calculated, executing P(1) for 2 is not a recursive subfunction.
[0160] Read the element at the end of the stack again, that is, read (2,2), and we can determine that rest = 2 is not equal to 0, so we enter P(2+2-2), that is, enter P(2), to execute the second step of the recursive algorithm for 2, that is, calculate b = fib(0). And decrement the rest in the read element by 1. At this time, the stack is [(4,1), (2,1)]. Since fib(0) = 0 can be directly calculated, executing P(2) for 2 is not a recursive subfunction.
[0161] Read the element at the end of the stack again, that is, read (2,1), and you can determine that rest = 1 is not equal to 0, so enter P(2+2-1) or P(3) to perform the third step of the recursive algorithm for 2, that is, calculate result = a + b. And decrement the rest in the read element by 1. At this time, the stack is [(4,2), (2,0)].
[0162] Since P(3) is a non-recursive subfunction, we read the element at the end of the stack again, that is, (2,0), and we can determine that rest = 0. At this time, we can summarize the previously obtained a = 1 and b = 0, and calculate that the result of this recursive algorithm is fib(2) = a + b = 1. Delete the element at the end of the stack, and the stack is now [(4,1)].
[0163] At this time, stack is not empty, so we read the element at the end of stack again, that is, read (4,1), and we can determine that rest = 1 is not equal to 0, so we enter P(2+2-1), that is, enter P(3), to perform the third step of the recursive algorithm for 4, that is, calculate result = a + b, and we can get b = fib(2) = 1. And we decrement rest in the read element by 1, and now stack is [(4,0)].
[0164] Since P(3) is a non-recursive subfunction, we read the element at the end of the stack again, that is, read (4,0), and we can determine that rest = 0. At this time, we can summarize the previously obtained a = fib(3) = 2 and b = fib(2) = 1, and we can calculate that the result of this recursive algorithm is fib(4) = a + b = 3. Delete the element at the end of the stack. At this time, the stack is [], that is, the stack is empty. The last result 3 is the result of the recursive algorithm for 4.
[0165] See also Figure 6 , Figure 6 The figure is a schematic diagram of a structure of a recursive algorithm implementation device provided by an embodiment of the present invention, which may include:
[0166] A recursive execution module 601 is used to start executing a recursive algorithm for a current variable, where the initial value of the current variable is a target variable;
[0167] An execution progress recording module 602 is used to record the execution progress of the recursive algorithm each time the executed step is a recursive step during the execution of the recursive algorithm, wherein the recursive step is a step that needs to call the recursive algorithm;
[0168] Return execution module 603 is used to continue executing the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm, based on the obtained result, whenever the result of executing the recursive algorithm for the current variable is obtained during the execution of the recursive algorithm, until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable.
[0169] In a possible embodiment, the execution progress recording module 602 records the execution progress of the recursive algorithm whenever the executed step is a recursive step, including:
[0170] Every time a step is executed, the progress of the recursive algorithm is recorded;
[0171] Whenever the return execution module 603 obtains the result of executing the recursive algorithm for the current variable, based on the obtained result, the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, including:
[0172] Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is complete, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable;
[0173] Based on the obtained result, the last recursive algorithm is continued to be executed starting from the latest recorded execution progress of the last recursive algorithm.
[0174] In a possible embodiment, the execution progress recording module 602 records the execution progress of the recursive algorithm, including:
[0175] The step ID that records the steps performed;
[0176] The return execution module 603 uses the output of the latest executed step as the result of executing the recursive algorithm for the current variable whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is completed, including:
[0177] Whenever the step identifier recorded in the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable;
[0178] The return execution module 603 continues to execute the last execution of the recursive algorithm based on the obtained result, starting from the execution progress of the latest record of the last execution of the recursive algorithm, including:
[0179] Based on the obtained result, the last recursive algorithm is continued to be executed starting from the step indicated by the step identifier most recently recorded when the recursive algorithm was executed last time.
[0180] The step identifier is used to indicate the number of steps remaining from the last step in the recursive algorithm;
[0181] Whenever the step identifier recorded by the locally executed recursive algorithm indicates the last step of the recursive algorithm, the execution progress recording module 602 uses the output of the latest executed step as the result of executing the recursive algorithm for the current variable, including:
[0182] Whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the most recently executed step is taken as the result of executing the recursive algorithm for the current variable.
[0183] The return execution module 603 is also used to determine whether the current execution of the recursive algorithm is the first execution of the recursive algorithm whenever a result of executing the recursive algorithm for the current variable is obtained during the execution of the recursive algorithm;
[0184] The return execution module 603 continues to execute the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm based on the obtained result, until the obtained result is the result of executing the recursive algorithm for the target variable, including:
[0185] If the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm, the execution of the last execution of the recursive algorithm will continue from the execution progress of the last execution of the recursive algorithm;
[0186] If the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm, the obtained result is used as the result of executing the recursive algorithm for the target variable.
[0187] In a possible embodiment, during the process of executing the recursive algorithm, the return execution module 603 determines whether the current execution of the recursive algorithm is the first execution of the recursive algorithm whenever a result of executing the recursive algorithm for the current variable is obtained, including:
[0188] During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in the current execution of the recursive algorithm is deleted;
[0189] Determine whether there is any execution progress recorded;
[0190] If the execution progress is still recorded, it is determined that the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm;
[0191] If the execution progress is not recorded, it is determined that the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm.
[0192] In a possible embodiment, the execution progress recording module 602 is further used to record the current variable as an input parameter of the current execution of the recursive algorithm whenever the executed step is a recursive step during the execution of the recursive algorithm;
[0193] The return execution module continues to execute the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm, including:
[0194] For the input parameters of the last recursive algorithm, continue to execute the last recursive algorithm from the execution progress of the last recursive algorithm.
[0195] The embodiment of the present invention further provides an electronic device, such as Figure 7 As shown, including:
[0196] Memory 701, used for storing computer programs;
[0197] The processor 702 is used to execute the program stored in the memory 701, and implements the following steps:
[0198] For the current variable, a recursive algorithm is started, wherein the initial value of the current variable is the target variable;
[0199] During the execution of the recursive algorithm, whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded, and the recursive step is the step that needs to call the recursive algorithm;
[0200] During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the execution of the last execution of the recursive algorithm continues from the execution progress of the last execution of the recursive algorithm until the result obtained is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable.
[0201] In a possible embodiment, whenever the executed step is a recursive step, recording the execution progress of the recursive algorithm this time includes:
[0202] Every time a step is executed, the progress of the recursive algorithm is recorded;
[0203] Whenever a result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, including:
[0204] Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is complete, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable;
[0205] Based on the obtained result, the last recursive algorithm is continued to be executed starting from the latest recorded execution progress of the last recursive algorithm.
[0206] In a possible embodiment, recording the execution progress of the recursive algorithm includes:
[0207] The step ID that records the steps performed;
[0208] Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is completed, the output of the latest executed step is used as the result of executing the recursive algorithm for the current variable, including:
[0209] Whenever the step identifier recorded in the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable;
[0210] The method of continuing to execute the last recursive algorithm based on the obtained result starting from the execution progress of the latest record of the last recursive algorithm execution includes:
[0211] Based on the obtained result, the last recursive algorithm is continued to be executed starting from the step indicated by the step identifier most recently recorded when the recursive algorithm was executed last time.
[0212] In a possible embodiment, the step identifier is used to indicate the number of steps remaining from the last step in the recursive algorithm;
[0213] Whenever the step identifier recorded by the local execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable, including:
[0214] Whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the most recently executed step is taken as the result of executing the recursive algorithm for the current variable.
[0215] In a possible embodiment, the method further includes:
[0216] During the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, determining whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm;
[0217] The step of continuing to execute the last execution of the recursive algorithm based on the obtained result from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, includes:
[0218] If the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm, the execution of the last execution of the recursive algorithm will continue from the execution progress of the last execution of the recursive algorithm;
[0219] If the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm, the obtained result is used as the result of executing the recursive algorithm for the target variable.
[0220] In a possible embodiment, during the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, determining whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm includes:
[0221] During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in the current execution of the recursive algorithm is deleted;
[0222] Determine whether there is any execution progress recorded;
[0223] If the execution progress is still recorded, it is determined that the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm;
[0224] If the execution progress is not recorded, it is determined that the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm.
[0225] In a possible embodiment, the method further includes:
[0226] During the execution of the recursive algorithm, whenever the executed step is a recursive step, the current variable is recorded as the input parameter of this execution of the recursive algorithm;
[0227] The step of continuing to execute the last recursive algorithm from the execution progress of the last recursive algorithm includes:
[0228] For the input parameters of the last recursive algorithm, continue to execute the last recursive algorithm from the execution progress of the last recursive algorithm.
[0229] The memory mentioned in the above electronic device may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the above processor.
[0230] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0231] In another embodiment of the present invention, a computer-readable storage medium is provided. The computer-readable storage medium stores instructions. When the instructions are executed on a computer, the computer executes any recursive function execution method in the above embodiments.
[0232] In another embodiment of the present invention, a computer program product including instructions is provided. When the computer program product is run on a computer, the computer executes any recursive function execution method in the above embodiments.
[0233] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.
[0234] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0235] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the embodiments of the device, electronic device, computer-readable storage medium, and computer program product, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments.
[0236] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. A recursive algorithm implementation method, characterized in that: The method comprises: For the current variable, start executing the recursive algorithm, where the initial value of the current variable is the target variable; During the execution of the recursive algorithm, whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded, and the recursive step is the step that needs to call the recursive algorithm; During the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the execution of the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm, until the obtained result is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable; The method further comprises: Determine the number of times the recursive algorithm is called in the recursive algorithm to be executed; According to the recursive algorithm, generate the number of recursive sub-functions and a non-recursive sub-function; Create a structure for the object of the first iteration operation and record the created structure at the end of the preset array; Read the last element in the preset array; Determine whether the execution progress is equal to 0. If the execution progress is not equal to 0, execute to enter step (the number of times + 2-the execution progress). If the execution progress is equal to 0, execute to summarize the results of the previous executions of the recursive algorithm to calculate the result of the current execution of the recursive algorithm. The execution progress is decremented by one; Determine whether the entered sub-function is a recursive sub-function. If it is a recursive sub-function, create a structure for the variable targeted by the next execution of the recursive algorithm, and record the created structure at the end of the preset array. If it is not a recursive sub-function, return the element at the end of the preset array; Creating a structure for the variable targeted by the next execution of the recursive algorithm, and recording the created structure at the end of the preset array; Calculate the result of the current execution of the recursive algorithm by summarizing the results of the previous executions of the recursive algorithm; Deleting the last element in the preset array; Determine whether the preset array is empty. If the preset array is not empty, return to execute the reading of the last element in the preset array. If the preset array is empty, execute the last obtained result as the result of executing the recursive algorithm for the target variable; Whenever the executed step is a recursive step, the execution progress of the recursive algorithm is recorded, including: Every time a step is executed, the progress of the recursive algorithm is recorded; Whenever a result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, including: Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is complete, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable; Based on the obtained result, the last recursive algorithm is continued to be executed starting from the latest recorded execution progress of the last recursive algorithm.
2. The method according to claim 1, characterized in that The recording of the execution progress of the recursive algorithm includes: The step ID that records the steps performed; Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is completed, the output of the latest executed step is used as the result of executing the recursive algorithm for the current variable, including: Whenever the step identifier recorded in the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable; The method of continuing to execute the last execution of the recursive algorithm based on the obtained result starting from the execution progress of the latest record of the last execution of the recursive algorithm includes: Based on the obtained result, the last recursive algorithm is continued to be executed starting from the step indicated by the step identifier most recently recorded when the recursive algorithm was executed last time.
3. The method according to claim 2, characterized in that The step identifier is used to indicate the number of steps remaining from the last step in the recursive algorithm; Whenever the step identifier recorded by the current execution of the recursive algorithm indicates the last step of the recursive algorithm, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable, including: Whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the most recently executed step is taken as the result of executing the recursive algorithm for the current variable.
4. The method according to claim 1, characterized in that: The method further comprises: During the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, determining whether the current execution of the recursive algorithm is a continuation of the first execution of the recursive algorithm; The step of continuing to execute the last execution of the recursive algorithm based on the obtained result from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, includes: If the current execution of the recursive algorithm is not to continue the first execution of the recursive algorithm, the execution of the last execution of the recursive algorithm will continue from the execution progress of the last execution of the recursive algorithm; If the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm, the obtained result is used as the result of executing the recursive algorithm for the target variable.
5. The method according to claim 4, characterized in that During the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, determining whether the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm includes: During the execution of the recursive algorithm, whenever the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in the current execution of the recursive algorithm is deleted; Determine whether there is any execution progress recorded; If the execution progress is still recorded, it is determined that the current execution of the recursive algorithm is not a continuation of the first execution of the recursive algorithm; If the execution progress is not recorded, it is determined that the current execution of the recursive algorithm is to continue the first execution of the recursive algorithm.
6. The method according to claim 1, characterized in that The method further comprises: During the execution of the recursive algorithm, whenever the executed step is a recursive step, the current variable is recorded as the input parameter of this execution of the recursive algorithm; The step of continuing to execute the last recursive algorithm from the execution progress of the last recursive algorithm includes: For the input parameters of the last recursive algorithm, continue to execute the last recursive algorithm from the execution progress of the last recursive algorithm.
7. A recursive algorithm implementation device, characterized in that: The device comprises: A recursive execution module, used to start executing a recursive algorithm for a current variable, wherein the initial value of the current variable is a target variable; An execution progress recording module is used to record the execution progress of the recursive algorithm in the process of executing the recursive algorithm, whenever the executed step is a recursive step, and the recursive step is a step that needs to call the recursive algorithm; The return execution module is used for, during the execution of the recursive algorithm, whenever a result of executing the recursive algorithm for the current variable is obtained, based on the obtained result, continuing to execute the last execution of the recursive algorithm from the execution progress of the last execution of the recursive algorithm, until the obtained result is the result of executing the recursive algorithm for the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed for the last current variable; The device is also used for: Determine the number of times the recursive algorithm is called in the recursive algorithm to be executed; According to the recursive algorithm, generate the number of recursive sub-functions and a non-recursive sub-function; Create a structure for the object of the first iteration operation and record the created structure at the end of the preset array; Read the last element in the preset array; Determine whether the execution progress is equal to 0. If the execution progress is not equal to 0, execute to enter step (the number of times + 2-the execution progress). If the execution progress is equal to 0, execute to summarize the results of the previous executions of the recursive algorithm to calculate the result of the current execution of the recursive algorithm. The execution progress is decremented by one; Determine whether the entered sub-function is a recursive sub-function. If it is a recursive sub-function, create a structure for the variable targeted by the next execution of the recursive algorithm, and record the created structure at the end of the preset array. If it is not a recursive sub-function, return the element at the end of the preset array; Creating a structure for the variable targeted by the next execution of the recursive algorithm, and recording the created structure at the end of the preset array; Calculate the result of the current execution of the recursive algorithm by summarizing the results of the previous executions of the recursive algorithm; Deleting the last element in the preset array; Determine whether the preset array is empty. If the preset array is not empty, return to execute the reading of the last element in the preset array. If the preset array is empty, execute the last obtained result as the result of executing the recursive algorithm for the target variable; The execution progress recording module records the execution progress of the recursive algorithm whenever the executed step is a recursive step, including: Every time a step is executed, the progress of the recursive algorithm is recorded; Whenever the return execution module obtains a result of executing the recursive algorithm for the current variable, based on the obtained result, the last execution of the recursive algorithm is continued from the execution progress of the last execution of the recursive algorithm until the obtained result is the result of executing the recursive algorithm for the target variable, including: Whenever the execution progress of the recursive algorithm indicates that the execution of the recursive algorithm is complete, the output of the most recently executed step is used as the result of executing the recursive algorithm for the current variable; Based on the obtained result, the last recursive algorithm is continued to be executed starting from the latest recorded execution progress of the last recursive algorithm.
8. An electronic device, characterized in that: include: Memory, used to store computer programs; A processor, for implementing the method steps described in any one of claims 1 to 6 when executing a program stored in a memory.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in any one of claims 1 to 6 are implemented.
