CN111767522A - Recursive algorithm implementation method and device and electronic equipment - Google Patents

Recursive algorithm implementation method and device and electronic equipment Download PDF

Info

Publication number
CN111767522A
CN111767522A CN202010608367.7A CN202010608367A CN111767522A CN 111767522 A CN111767522 A CN 111767522A CN 202010608367 A CN202010608367 A CN 202010608367A CN 111767522 A CN111767522 A CN 111767522A
Authority
CN
China
Prior art keywords
recursive algorithm
recursive
executing
executed
time
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202010608367.7A
Other languages
Chinese (zh)
Inventor
陈舜雨
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hangzhou Hikvision System Technology Co Ltd
Original Assignee
Hangzhou Hikvision System Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Hangzhou Hikvision System Technology Co Ltd filed Critical Hangzhou Hikvision System Technology Co Ltd
Priority to CN202010608367.7A priority Critical patent/CN111767522A/en
Publication of CN111767522A publication Critical patent/CN111767522A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F17/00Digital computing or data processing equipment or methods, specially adapted for specific functions
    • G06F17/10Complex mathematical operations
    • G06F17/18Complex mathematical operations for evaluating statistical data, e.g. average values, frequency distributions, probability functions, regression analysis

Abstract

The embodiment of the invention provides a method and a device for realizing a recursive algorithm and electronic equipment. The method comprises the following steps: starting to execute a recursive algorithm aiming at a current variable, wherein an initial value of the current variable is a target variable; in the process of executing the recursive algorithm, recording the execution progress of executing the recursive algorithm at this time when the executed steps are recursive steps, wherein the recursive steps are steps needing to be called; in the process of executing the recursive algorithm, when 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. The recursive algorithm can be realized on the premise of not using stacks, so that the recursive algorithm is not limited by the depth of the stacks, and the recursive algorithm with higher complexity can be realized.

Description

Recursive algorithm implementation method and device and electronic equipment
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a method and an apparatus for implementing a recursive algorithm, and an electronic device.
Background
The recursive algorithm is an algorithm for calling itself in an algorithm execution flow, and is often designed to solve the problem of high complexity. In the related art, the recursive algorithm is usually implemented by a recursive function, and for example, taking the calculation of the value of the xth bit of the fibonacci number sequence as an example, the recursive algorithm can be implemented by the following recursive function:
Figure BDA0002559983720000011
it can be seen that the recursive function itself is called in the recursive function, so when the recursive function is executed, the recursive function itself will be called once or multiple times, for example, assuming that fun (3) needs to be executed, fun (2) and fun (1) need to be called, and the execution of fun (3) is returned after the execution of fun (2) and fun (1) is completed, 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 parameter of the recursive function changes, for example, when fun (3) is executed, the input parameter x is 3, when fun (2) is executed, the input parameter x is 2, and when it returns to execution of fun (3), the input parameter x is 3.
In the related art, during the execution of the recursive function, the input parameters may be stored by using a stack, the stack stores the newly stored data on the top of the stack, and the process obtains the latest input parameters by accessing the top of the stack. Illustratively, assuming that fun (3) needs to be executed, 3 is saved in the stack at the top of 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 in the process of executing fun (3), saving 2 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 now accesses the top of the stack to get x 2. When execution of fun (2) is complete and it is necessary to return to fun (3), then 2 may be removed from the stack, at which point 3 is again at the top of the stack, so that the process executing the recursive function accesses the top of the stack to obtain x-3.
However, in a recursive algorithm designed to solve the problem of excessive complexity, such as a recursive algorithm designed to process a large amount of data, when the recursive algorithm is implemented by a recursive function, the number of times the recursive function itself may be called in the recursive function may be excessive, and the depth of the stack is limited, and a variable (e.g., the input parameter) at each call cannot be stored, so that the recursive function cannot be normally executed. Therefore, it is difficult to implement the recursive algorithm by a recursive function, resulting in that these problems with high complexity cannot be solved.
Disclosure of Invention
The embodiment of the invention aims to provide a method and a device for realizing a recursive algorithm and electronic equipment, so as to solve the problem of high complexity through the recursive algorithm. The specific technical scheme is as follows:
in a first aspect of the present invention, a recursive algorithm implementation method is provided, the method including:
starting to execute a recursive algorithm aiming at a current variable, wherein an initial value of the current variable is a target variable;
in the process of executing the recursive algorithm, recording the execution progress of executing the recursive algorithm at this time when the executed steps are recursive steps, wherein the recursive steps are steps needing to be called;
in the process of executing the recursive algorithm, when a result of executing the recursive algorithm on a 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 on the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed on the last current variable.
In a second aspect of the present invention, there is provided a recursive algorithm implementation apparatus, comprising:
the recursive execution module is used for starting to execute a recursive algorithm aiming at a current variable, and an initial value of the current variable is a target variable;
the execution progress recording module is used for recording the execution progress of the execution of the recursive algorithm at this time when the executed steps are recursive steps in the process of executing the recursive algorithm, and the recursive steps are the steps needing to call the recursive algorithm;
and the return execution module is used for continuously executing the recursive algorithm from the execution progress of the recursive algorithm executed last time to the result of executing the recursive algorithm aiming at the target variable according to the obtained result when the result of executing the recursive algorithm aiming at the current variable is obtained every time in the process of executing the recursive algorithm, and the recursive algorithm executed last time is the recursive algorithm executed aiming at the last current variable.
In a third aspect of the present invention, there is provided an electronic device comprising:
a memory for storing a computer program;
a processor adapted to perform the method steps of any of the above first aspects when executing a program stored in the memory.
In a fourth aspect of the present invention, a computer-readable storage medium is provided, having stored therein a computer program which, when executed by a processor, performs the method steps of any of the above-mentioned first aspects.
The recursive algorithm implementation method, the device and the electronic equipment provided by the embodiment of the invention can continuously execute the previous recursive algorithm after completing the calling of the recursive algorithm once in a mode of maintaining the execution progress, so that the recursive execution process is converted into a cyclic execution process, the recursive algorithm is implemented on the premise of not using a stack, and therefore, the recursive algorithm is not limited by the stack depth, and the recursive algorithm with higher complexity can be implemented. Of course, not all of the advantages described above need to be achieved at the same time in the practice of any one product or method of the invention.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts.
FIG. 1a is a diagram illustrating an embodiment of a stack storing data;
FIG. 1b is another diagram of a stack storing data according to an embodiment of the present invention;
FIG. 1c is another diagram of a stack storing data according to an embodiment of the present invention;
FIG. 2 is a schematic flow chart of a recursive algorithm implementation method according to an embodiment of the present invention;
fig. 3 is another schematic flow chart of a recursive algorithm implementation method according to an embodiment of the present invention;
fig. 4 is another schematic flow chart of a recursive algorithm implementation method according to an embodiment of the present invention;
fig. 5 is another schematic flow chart of a recursive algorithm implementation method according to an embodiment of the present invention;
fig. 6 is a schematic structural diagram of a recursive algorithm implementation apparatus according to an embodiment of the present invention;
fig. 7 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In order to more clearly describe the recursive function execution method provided by the embodiment of the present invention, a recursive algorithm will be described below. For a problem with high complexity, it may be difficult for a developer to design an algorithm that directly solves the problem, and the developer may simplify the problem step by step to obtain a plurality of similar sub-problems, and solve the problem by solving the sub-problems.
For example, assume that n!needs to be calculated! (factorial of n) due to n! N (n-1)! Therefore, the problem can be equivalent to calculating the factorial of n-1, and then multiplying the factorial of n-1 with n, namely the problem can be simplified to calculating the factorial of n-1, and similarly, the factorial of n-1 can be simplified to calculating the factorial of n-2. Based on this idea, a recursive algorithm can be designed that includes the following steps for computing n! :
the first step is as follows: calculate (n-1)! (ii) a
The second step is that: calculate n x (n-1)! .
Wherein (n-1)! If n is greater than 1, it can likewise be calculated by the recursive algorithm, if n is equal to 1, then since 0! 1, so that (n-1)! 1. Other forms of recursive algorithms may be designed for calculating n! It is understood that how to design the recursive algorithm is not an improvement point of the embodiment of the present invention, and therefore, the detailed description thereof is omitted here.
In the related art, the recursive algorithm may be implemented by using a recursive function, which may be as follows:
Figure BDA0002559983720000051
since the recursive function is often called multiple times in the process of implementing the recursive algorithm, and the variable value is different when the recursive function is called each time, the process needs to rely on the stack to cache the variable value when the recursive function is called each time when the recursive function is executed, taking execution fun (2) as an example, the variable value 2 can be stored at the top of the stack, and the data in the stack is shown in fig. 1a, the process accesses the top of the stack, calls the recursive function with the variable value 2 for the first time, and executes fun (2); since 2 is not equal to 0, the recursive function needs to be called again with the variable value of 1, the call can be interrupted and the variable value of 1 is stored at the top of the stack, the data in the stack is shown in fig. 1b, the process accesses the top of the stack, calls the recursive function with the variable value of 1 for the second time, and fun (1) is executed. Similarly, since 1 is not equal to 0, the recursive function needs to be called again with the variable value 0, the 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 shown in fig. 1c, the process accesses the top of the stack, calls the recursive function for the third time with the variable value 0, and executes fun (0); since the variable value is 0 at this time, the third call may be normally ended, and the calculation result 1 of fun (0) is returned, and the data at the top of the stack is deleted, where the data in the stack is as shown in fig. 1b, the variable value 1 is located at the top of the stack, the process accesses the top of the stack, so that the second call interrupted before is restored, x may be obtained as 1 by accessing the top of the stack, and the execution fun (1) is executed, and since fun (1) as 1 fun (0), the return value of the execution fun (0) according to the third call of the recursive function is 1, and the return value of the execution fun (1) of the recursive function for the second call may be calculated as 1. Therefore, the second call is normally ended, and returns 1, and deletes the data at the top of the stack, at this time, the data in the stack is as shown in fig. 1a, the variable value 2 is located at the top of the stack, the process accesses the top of the stack, so that the first call interrupted before is resumed, x ═ 2 can be obtained by accessing the top of the stack, the fun (2) is executed, since the return value of fun (2) × 2, the return value of the recursive function execution fun (2) called for the first time is 1, the execution of fun (2) is completed, and the result 2 is returned.
However, if the recursive function needs to be called too many times during the execution process, for example, the number of times is more than the depth of the stack, the stack cannot store the variable value of each time the recursive function is called, stack overflow will occur, resulting in deadlock or crash, and the function cannot be normally executed, i.e., the recursive algorithm cannot be implemented. Therefore, it is difficult to implement a recursive algorithm with a recursive function, which is too complex, for example, when implementing a binary search (a recursive algorithm) using a recursive function, if elements included in a table to be searched are too many, it is difficult to implement a binary search using a recursive function.
Based on this, an embodiment of the present invention provides a recursive function execution method, which may be referred to fig. 2, where fig. 2 is a schematic flow diagram of the recursive function execution method provided in the embodiment of the present invention, and the method may include:
s201, aiming at the current variable, a recursive algorithm is started to be executed, and the initial value of the current variable is a target variable.
S202, in the process of executing the recursive algorithm, when the executed steps are recursive steps, the execution progress of executing the recursive algorithm at this time is recorded, and the recursive steps are steps needing to call the recursive algorithm.
S203, in the process of executing the recursive algorithm, when a result of executing the recursive algorithm on 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 a result of executing the recursive algorithm on the target variable, and the last execution of the recursive algorithm is a recursive algorithm executed on the last current variable.
By adopting the embodiment, the previous recursive algorithm can be continuously executed after the calling of the recursive algorithm is completed once by maintaining the execution progress, so that the recursive execution process is converted into the cyclic execution process, the recursive algorithm is realized on the premise of not using a stack, the limitation of the stack depth is avoided, and the recursive algorithm with higher complexity can be realized.
In S201, the target variable may refer to a variable value that needs to be targeted when the user designs the recursive algorithm, the target variable may be different according to different application scenarios, and the target variable may be a numeric value or a non-numeric value, for example, assuming that the user needs to calculate 6000 |! Then the target variable is 6000. For another example, if the user needs to search for the specified element in the preset number sequence, the target variable is the preset number sequence.
The recursive algorithm may be executed such that each step in the recursive algorithm is executed in turn according to the steps designed for the recursive algorithm. It will be appreciated that there are some steps in the recursive algorithm that require invocation of the recursive algorithm, illustratively, the foregoing for calculating n! For example, the recursive algorithm of (1)! Cannot be directly calculated, and the recursive algorithm needs to be called to be able to calculate (n-1)! These steps that require to invoke a recursive algorithm are referred to herein as recursive steps, and the steps that do not require to invoke a recursive algorithm are referred to herein as non-recursive steps, and ideally 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, and therefore, the details are not described herein.
In this embodiment, for a non-recursive step, the result of the non-recursive step can be directly calculated, while for a recursive step, the recursive algorithm calculation needs to be invoked for the new current variable to obtain the result of the recursive step, as described above for calculating n! For example, when the recursive algorithm is started, the current variable is n, and after the first step is executed for the first time, the recursive algorithm needs to be called for n-1 to obtain (n-1)! At this time, the current variation changes to n-1.
In S202, the execution of the recursive algorithm this time refers to the recursive algorithm executed for the current variable, and the execution progress is used to indicate the completion of the execution of the recursive algorithm this time, for example, the above-mentioned calculation is used to calculate n! The recursive algorithm of (2) is an example, in which the first step is a recursive step, and 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 is to execute the second step next" or "the recursive algorithm executed for the variable value 100 has executed the first step" may be recorded as the execution progress. The representation form of the execution progress may be different according to different application scenarios, and for example, the step identifier of the second step may be used as the execution progress to represent that "the recursive algorithm executed for the variable value 100 is to execute the second step next", or the step identifier of the first step may be used as the execution progress to represent that "the recursive algorithm executed for the variable value 100 has executed the first step", where the step identifier is a unique identifier of a step in the recursive algorithm.
In S203, the result of performing the recursive algorithm on the current variable is obtained by completing the recursive algorithm on the current variable. If the current variable is not the target variable, the result obtained is not what the user needs to obtain by a recursive algorithm, for example, the calculation of n! For example, the user needs to obtain the result n! If the current variable is n-3, then the result of performing the recursive algorithm on the current variable is (n-3)! And not the result desired by the user. Need to utilize (n-3)! The recursive algorithm for n-2 is continued to get the result the user wants.
The object targeted for the last time the recursive algorithm was executed is continued is the last current variable, which in some application scenarios may be derived from the current variable, as described above for calculating n! For 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 derived from the current variable, for example, in a recursive algorithm for calculating the value of the nth bit of a fibonacci sequence, if the current variable is j, the last current variable may be j +1 or j + 2. In a possible embodiment, the recursive algorithm is executed in such a way that, each time the executed step is a recursive step, the current variable is recorded as an input variable for the current execution of the recursive algorithm. When the recursive algorithm is continuously executed last time, the recursive algorithm can be continuously executed last time from the execution progress of the recursive algorithm executed last time according to the input parameters of the recursive algorithm executed last time, the embodiment can be applied to an application scenario in which the last current variable may not be derived according to the current variable, and the embodiment can also be applied to an application scenario in which the last current variable may be derived according to the current variable.
In order to more clearly illustrate 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 illustrated with reference to a specific application scenario, and it can be understood that, since the recursive algorithm needs to be called in the process of implementing the recursive algorithm, more steps need to be executed in the process of implementing the recursive algorithm than the steps designed by the recursive algorithm. Herein, steps are used to represent steps performed when implementing the recursive algorithm, so as to distinguish steps designed for the recursive algorithm from steps performed when implementing the recursive algorithm.
The application scene one: calculate 3! And assuming that the designed recursive algorithm is:
the first step, a ═ x-1!
The second step, result ═ x a
Where result represents the result of the recursive algorithm and x represents the input. The termination condition of the recursive algorithm is x ═ 1, and a result 1 is output when the condition is satisfied.
The target variable is 3 and thus the current variable is initially 3
Step 1: the recursive algorithm is started for 3.
Step 2: the first step in performing the recursive algorithm for 3 is to compute 2! This step is a recursive step, so the execution progress of the recursive algorithm for 3 is recorded.
The execution progress may be used to indicate that the recursive algorithm for 3 has executed the first step, or that the recursive algorithm for 3 needs to execute the second step next, and the two representations are different, but the progress represented is consistent in practice.
Step 3: the next iteration starts, when the current variable becomes 2, and the recursive algorithm starts for 2.
Step 4: the first step in performing the recursive algorithm for 2 is to calculate 1! This step is still a recursive step, so the progress of the execution of the recursive algorithm for 2 is recorded.
Step 5: the next iteration starts, when the current variable becomes 1, and the recursive algorithm starts for 1. When the termination condition of the recursive algorithm is triggered, the result of executing the recursive algorithm for 1 can be 1.
Step 7: since the result is obtained, the last iteration is returned, i.e. the recursive algorithm is executed for 2, and since the execution progress recorded before 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 next, according to the recorded execution progress. When the second step is executed, the result "1" obtained before is used as a obtained by executing the first step of the recursive algorithm for 2, that is, a is 1, and then result 2 is calculated as 2, that is, the result of executing the recursive algorithm for 2 is 2.
Step 8: since the result is obtained, the last iteration is returned, i.e. the recursive algorithm is executed for 1, and since the execution progress recorded before 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 next according to the recorded execution progress. When the second step is executed, the result "2" obtained before is used as a obtained by executing the first step of the recursive algorithm for 1, that is, a is 2, and then result 3 is 6, that is, the result of executing the recursive algorithm for 1 is obtained as 6. Now the result of performing the recursive algorithm on the target variable has been obtained, so the flow ends, resulting in 3! 6.
Application scenario 2: calculating the 4 th bit of the fibonacci number sequence, recording the ith bit of the fibonacci number sequence as fib (i) for convenience of description, the fibonacci number sequence means that the recurrence formula is satisfied: fib (x) ═ fib (x-1) + fib (x-2), and fib (1) ═ fib (2) ═ 1. Assume that the designed recursive algorithm is as follows:
the first step is as follows: a ═ fib (x-1)
The second step is that: b ═ fib (x-2)
The third step: result ═ a + b
Where 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 a result 1 is output when the condition is satisfied.
The target variable is 4 and thus the current variable is initially 4.
Step 1: a recursive algorithm is performed for 4.
Step 2: the first step in executing the recursive algorithm for 4 is to calculate fib (3), which is a recursive step, thus recording the progress of the execution of the recursive algorithm for 4.
The execution progress may be used to indicate that the recursive algorithm for 4 has executed the first step, or that the recursive algorithm for 4 needs to execute the second step next, and the two representations are different, but the progress represented is consistent in practice.
Step 3: the next iteration starts, when the current variable becomes 3, and the recursive algorithm starts for 3.
Step 4: the first step in executing the recursive algorithm for 3 is to calculate fib (2), which is a recursive step, thus recording the progress of the execution of the recursive algorithm for 3.
Step 5: the next iteration starts, when the current variable becomes 2, and the recursive algorithm starts for 2. The termination condition of the recursive algorithm is triggered at this time, and the result of executing the recursive algorithm for 2 can be obtained as 1.
Step 6: since the result is obtained, the last iteration is returned, i.e. the recursive algorithm is executed for 3, and since the execution progress recorded before 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 next, according to the recorded execution progress. The result "1" obtained at Step5 may be taken as a obtained by performing the first Step of the recursive algorithm for 3, i.e., a equals 1.
Step 7: the second step in executing the recursive algorithm for 3 is to calculate fib (1), which is a recursive step, thus recording the progress of the execution of the recursive algorithm for 3.
Step 8: the next iteration starts, when the current variable becomes 1, and the recursive algorithm starts for 1. When the termination condition of the recursive algorithm is triggered, the result of executing the recursive algorithm for 1 can be 1.
Step 9: since the result is obtained, the last iteration is returned, i.e. the recursive algorithm is executed for 3, and since the execution progress recorded before 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 next according to the recorded execution progress. The result "1" obtained at Step8 may be taken as b obtained by performing the second Step of the recursive algorithm for 3, i.e., b equals 1. At this time, it can be calculated that result is 2, i.e., 2 is obtained as a result of executing the recursive algorithm for 3.
Step 10: since the result is obtained, the last iteration is returned, i.e. the recursive algorithm is executed for 4, and since the execution progress recorded before 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 next, according to the recorded execution progress. The result "2" obtained at Step9 may be taken as a obtained by performing the first Step of the recursive algorithm for 4, i.e., a is 2.
Step 11: the second step in executing the recursive algorithm for 4 is to calculate fib (2), which is a recursive step, thus recording the progress of the execution of the recursive algorithm for 4.
Step 12: the next iteration starts, when the current variable becomes 2, and the recursive algorithm starts for 2. The termination condition of the recursive algorithm is triggered at this time, and the result of executing the recursive algorithm for 2 can be obtained as 1.
Step 13: since the result is obtained, the last iteration is returned, i.e. the recursive algorithm is executed for 4, and since the execution progress recorded before 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 next, according to the recorded execution progress. The result "1" obtained at Step12 may be taken as b obtained by performing the second Step of the recursive algorithm for 4, i.e., b equals 1. At this time, result of calculation of result a + b 3, that is, result of execution of the recursive algorithm for 4 is 3. At this time, the result of executing the recursive algorithm on the target variable is obtained, and thus the flow ends, and fib (4) ═ 3 is obtained.
Referring to fig. 3, fig. 3 is another schematic flow chart of a method for implementing a recursive algorithm according to an embodiment of the present invention, which may include:
s301, aiming at the current variable, a recursive algorithm is started to be executed, and the initial value of the current variable is the target variable.
The step is the same as the foregoing step S201, and reference may be made to the related description of the foregoing step S201, which is not described herein again.
S302, every time a step is executed, recording the execution progress of the execution of the recursive algorithm at this time.
It will be appreciated that if the progress of the execution of the recursive algorithm this time is recorded each time a step is executed, the progress of the execution of the recursive algorithm this time will also be recorded each time a recursive step is executed. In this embodiment, if the execution progress of the recursive algorithm executed this time is already recorded before the execution progress of the recursive algorithm executed this time is recorded, the existing execution progress may be updated, or a new execution progress may be recorded. Illustratively, assuming the current variable is 7, when the first step of the recursive algorithm is performed for 7, the progress of the execution indicating that the recursive algorithm executed against the variable value 7 is to execute the second step next may be recorded, when the second step of the recursive algorithm is executed with respect to the variable value 7, the execution schedule, which is originally recorded to indicate that the "recursive algorithm executed with respect to the variable value 7 is to execute the second step next", may be updated to the execution schedule, which is originally recorded to indicate that the "recursive algorithm executed with respect to the variable value 7 is to execute the third step next", or the execution schedule, which is originally recorded to indicate that the "recursive algorithm executed with respect to the variable value 7 is to execute the second step next", may be maintained, and a new execution schedule is recorded indicating that the recursive algorithm executed against the variable value 7 is to execute the third step next.
And S303, when the execution progress of the recursive algorithm executed this time indicates that the recursive algorithm executed this time is completed, taking the output of the latest executed step as the result of executing the recursive algorithm aiming at the current variable.
Illustratively, the foregoing is used to calculate n! For example, if the execution progress indicates that the recursive algorithm for the current variable has already executed the second step, it may be considered that the execution of the recursive algorithm this time is completed.
And S304, based on the obtained result, continuing to execute the recursive algorithm from the latest recorded execution progress of the recursive algorithm executed last time.
For the last time the recursive algorithm is executed, reference may be made to the related description in the foregoing S203, which is not described herein again. The execution progress of the latest record refers to the execution progress that is last recorded among all the execution progresses that have been recorded. 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 execution progress of the latest recording is execution progress 2.
By adopting the embodiment, the logic of the execution progress can be recorded when the non-recursion step and the recursion step are executed uniformly, the non-recursion step and the recursion step do not need to be distinguished when the execution progress is recorded, and the implementation logic of the recursion algorithm can be simplified.
The manner in which execution progress is recorded will be described below, and in one possible embodiment, execution progress may be recorded in the form of a step identifier by recording the steps executed. The step identifier is used for uniquely identifying the step in the recursive algorithm, the step identifiers of different steps in the recursive algorithm are different, and the form of the step identifier may be different according to different application scenarios, for example, the step identifier may be a numeric value, or an alphabetic value, or may be another character other than the numeric value and the alphabetic value, or may be a single character, or may be a combination of a plurality of characters.
In this embodiment, the recursive algorithm implementation method may be as shown in fig. 4, and includes:
s401, aiming at the current variable, a recursive algorithm is started to be executed, and the initial value of the current variable is the target variable.
The step is the same as the foregoing step S201, and reference may be made to the related description of the foregoing step S201, which is not described herein again.
S402, recording the step identification of the executed step each time a step is executed.
The recorded step identifier is the execution progress of the recursive algorithm executed this time, and may indicate that the recursive algorithm executed this time is the step indicated by the recorded step identifier. In other possible embodiments, the step identifier of the next step of the executed steps may be recorded to indicate that the recursive algorithm is to execute the step indicated by the recorded step identifier next. The two ways of recording execution progress although the step identifications of the recording are different, the recorded execution progress is equivalent in meaning.
And S403, when the step identifier recorded in the execution of the recursive algorithm at this time represents the last step of the recursive algorithm, taking the output of the step executed latest as the result of the recursive algorithm executed on the current variable.
In a possible embodiment, the step identifier may be used to indicate the number of steps remaining from the last step in the recursive algorithm, the last step in the recursive algorithm is executed according to the designed execution order, the result of the last step in the recursive algorithm is the result of the recursive algorithm, and the last step should be a non-recursive step since the result of the last step is the result of the recursive algorithm. Illustratively, the foregoing is used to calculate n! For 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, the output of the latest executed step may be taken as the result of executing the recursive algorithm for the current variable whenever the step identifier indicates that the number of steps remaining from the last step in the recursive algorithm is 0.
S404, based on the obtained result, starting from the step represented by the step mark of the latest record of the last executed recursive algorithm, the last executed recursive algorithm is continuously executed.
How to judge whether the obtained result is the result of performing the recursive algorithm on the target variable will be described below:
in a possible embodiment, it may be determined whether the performing of the recursive algorithm this time is to continue performing the recursive algorithm for the first time, and if the performing of the recursive algorithm this time is not to continue performing the recursive algorithm for the first time, since only the variable targeted for performing the recursive algorithm for the first time is the target variable, it may be considered that the result obtained by performing the recursive algorithm this time is not the result obtained by performing the recursive algorithm on the target variable, and the performing of the recursive algorithm last time may be continued from the execution progress of the recursive algorithm last time. If the execution of the recursive algorithm for the current time is to continue to execute the recursive algorithm for the first time, and the variable targeted for the execution of the recursive algorithm for the first time is the target variable, the result obtained by the execution of the recursive algorithm for the current time can be regarded as the result obtained by the execution of the recursive algorithm for the target variable, and the obtained result can be regarded as the result of the execution of the recursive algorithm for the target variable.
How to determine whether the recursive algorithm executed this time is to continue executing the recursive algorithm for the first time may be to determine whether a variable targeted by the recursive algorithm executed this time is a target variable, if the variable targeted by the recursive algorithm executed this time is the target variable, the recursive algorithm executed this time is to continue executing the recursive algorithm for the first time, and if the variable targeted by the recursive algorithm executed this time is not the target variable, the recursive algorithm executed this time is not to continue executing the recursive algorithm for the first time.
In a possible embodiment, in the process of executing the recursive algorithm, every time the result of executing the recursive algorithm for the current variable is obtained, the execution progress recorded in executing the recursive algorithm this time is deleted. It may be determined whether an execution progress is recorded, if the execution progress is recorded, it may be determined that the recursive algorithm executed this time is not to be executed for the first time, and if the execution progress is not recorded, it may be determined that the recursive algorithm executed this time is to be executed for the first time.
In one possible embodiment, the execution progress may be recorded in an array. An exemplary start of maintaining a preset array is that, each element of the preset array is empty initially (in other possible embodiments, each element may also be a preset initial value initially), and each time the execution progress is recorded, the execution progress may be recorded at the end of the array, where the end of the array refers to a first empty element in the array in a front-to-back order, 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 bit of the array, and therefore the execution progress is recorded in the first bit of the array. When the execution progress is recorded for the second time, the first bit of the array is a non-empty element because the execution progress is already recorded in the first bit of the array, and the second bit is an empty element, so the end of the array is the second bit, and the execution progress is recorded in the second bit of the array.
The aforementioned input parameters may be recorded corresponding to the execution progress, and for example, in one possible embodiment, the input parameters and the execution progress may be used as member variables in a structure, and the structure is written into an array, so as to record the input parameters and the execution progress in the array correspondingly. For example, a Scope (Scope) may be defined, where the Scope may include a member variable n and a member variable mark, and the input parameter is assigned to n in the Scope, and the execution progress is assigned to the mark in the Scope, and then the Scope is written into the array, so that the corresponding recording of the input parameter and the execution progress in the array may be implemented.
By adopting the embodiment, the working principle of stack pushing and popping can be simulated through arrays, so that developers can better design and understand the implementation mode of the recursive algorithm.
In order to more clearly describe the implementation method of the recursive algorithm provided by the embodiment of the present invention, the implementation method of the recursive algorithm provided by the embodiment of the present invention will be described in detail below with reference to the computer language of the implementation method of the recursive algorithm provided by the embodiment of the present invention, and taking the application scenario 2 as an example, the computer language may be as follows:
Figure BDA0002559983720000151
Figure BDA0002559983720000161
Figure BDA0002559983720000172
the following description will be made by taking the above computer language as an example, and referring to fig. 5, fig. 5 is a schematic flow chart of a recursive algorithm implementation method provided by an embodiment of the present invention, which may include:
s501, determining the number n of times of calling the recursive algorithm in the recursive algorithm to be executed.
Taking the application scenario one as an example, the number of times the recursive algorithm is called in the recursive algorithm is 1, so n is 1, and taking the application scenario two as an example, the number of times the recursive algorithm is called in the recursive algorithm is 2, so n is 2.
S502, according to the recursive algorithm, n recursive subfunctions and 1 non-recursive subfunctions are generated.
Wherein each recursive subfunction is used for implementing a recursive step in the recursive algorithm, and a non-recursive subfunction is used for implementing a non-recursive step in the recursive algorithm. It is understood that the recursive sub-function is only named, and the recursive sub-function itself does not call the recursive sub-function, i.e., the recursive sub-function is not a recursive function. For exemplary purposes, reference may be made to the aforementioned computer language
Figure BDA0002559983720000171
I.e. a recursive subfunction. The Current is a Current variable, and the makeScope is a custom function for generating the structure Scope. The stack push is a custom function for recording Scope to the end of the stack, and the stack is the preset array.
For convenience of description, it is assumed hereinafter that the generated subfunctions are { P (1), P (2), …, P (n +1) }, respectively. It should be understood that the embodiment shown in fig. 5 is only one possible flow diagram of the recursive algorithm implementation method provided by the embodiment of the present invention, and in other possible embodiments, a plurality of non-recursive sub-functions may also be generated, and the plurality of non-recursive sub-functions are used to implement non-recursive steps in the recursive algorithm together. And a non-recursive step in the recursive algorithm is realized through a non-recursive subfunction, so that the complexity in realizing the recursive algorithm can be effectively reduced.
S503, creating a Scope for the object of the first iteration operation, and recording the created Scope at the end of stack.
Scope includes at least two member variables: input parameters and execution schedule. For the input parameters and the execution progress, reference may be made to the related description above, and details are not repeated here. For convenience of description hereinafter, it is assumed that the execution progress is represented by a 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.
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.
S504, reading the last element in the stack.
Reading the last element means reading the last non-empty element in the stack from front to back, and in some possible embodiments, if no empty element is included in the preset array, reading the last element in the preset array is equivalent to reading the last element in the preset array from front to back.
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.
The rest is the rest in the Scope read.
S506, entering P (n + 2-rest).
It will be appreciated that if rest is 0, this indicates that the result of performing the recursive algorithm on the current variable has been obtained, and therefore there is no need to continue to perform the recursive algorithm on the current variable. If rest is not 0, it means that the result of executing the recursive algorithm on the current variable is not obtained, and as described above with respect to the execution progress, rest means that the last-to-last rest-1 steps in the recursive algorithm need to be executed next on the current variable, and since the recursive algorithm includes n +1 steps in total, the last-to-last rest-1 steps are positive n +2-rest steps, and therefore P (n +2-rest) is entered.
S507, rest is decreased by 1.
The rest in the read Scope is reduced from 1.
S508, determine whether the entered sub-function is a recursive sub-function, if so, execute S509, if not, return to S504.
S509, creating Scope for the variable for next execution of the recursive algorithm, and recording the created Scope at the end of stack.
If the entered subfunction is a recursive subfunction, the recursive subfunction is used for realizing the recursive step in the recursive algorithm, and the recursive algorithm is called in the recursive step, and the variable for next execution of the recursive algorithm is the variable when the recursive algorithm is called in the recursive step for realizing the entered recursive subfunction.
The input parameters in the created Scope are the variables for next execution of the recursive algorithm, and the execution progress is n + 1.
And S510, summarizing the results of the previous recursive algorithms, and calculating the result of the recursive algorithm executed at this time.
It is understood that if rest is equal to 0, it indicates that the calculation of the current iteration is to be completed, and according to the principle of the recursive algorithm, the calculation of performing the recursive algorithm once needs to use the results of performing the recursive algorithm each time before.
S511, deleting the last element in prestack
S512, judging whether the stack is empty, if not, returning to execute S504, and if so, executing S513.
S513, the last result is taken as the result of executing the recursive algorithm on the target variable.
In the following, the embodiment will be exemplarily described with reference to the foregoing application scenario 2, and for convenience of description, Scope is represented in the form of (i, rest), where i is an input parameter in Scope, and rest is an execution progress in Scope.
In the process of calculating the 4 th bit of the fibonacci sequence, since the first step and the second step are both recursive steps, n is 2, and recursive subfunctions P (1), P (2) and non-recursive subfunctions P (3) are constructed, wherein P (1) is used for realizing the first step, P (2) is used for realizing the second step, and P (3) is used for realizing the third step.
Recording (4,3) in the stack, where the stack is [ (4,3) ], reading the last element in the stack, i.e. reading (4,3), may determine that rest ═ 3 is not equal to 0, and thus go to P (2+2-3), i.e. to P (1), to perform the first step of the recursive algorithm for 4, i.e. to calculate a ═ fib (3). And self-decrements rest of the read element by 1, at which time the stack is [ (4,2) ].
Since P (1) is a recursive subfunction for 4, Scope is created for the variable for which the recursive algorithm is to be executed next, i.e., 3, and the created Scope is recorded at the end of stack, where (3,3) is the created Scope and [ (4,2), (3,3) ] is the created Scope after the end of stack.
Reading the last element of the stack again, i.e. reading (3,3), may determine that rest ═ 3 is not equal to 0, and so go to P (2+2-3), i.e. go to P (1), to perform the first step of the recursive algorithm for 3, i.e. calculate a ═ fib (2). And self-decrements rest of the read element by 1, at which time the stack is [ (4,2), (3,2) ].
Since P (1) is a recursive subfunction for 3, Scope is created for the variable for which the recursive algorithm is to be executed next, i.e., 2, and the created Scope is recorded at the end of stack, where (2,3) is the created Scope, and after the Scope is recorded at the end of stack, stack is [ (4,2), (3,2), (2,3) ].
Reading the last element of the stack again, i.e. reading (2,3), may determine that rest ═ 3 is not equal to 0, and so go to P (2+2-3), i.e. go to P (1), to perform the first step of the recursive algorithm for 2, i.e. calculate fib (1). And self-decrements rest of the read element by 1, at which time the stack is [ (4,2), (3,2), (2,2) ]. Since fib (1) can be directly calculated to be 1, performing P (1) for 2 is not a recursive subfunction.
Reading the last element of the stack again, i.e. reading (2,2), may determine that rest ═ 2 is not equal to 0, and so go to P (2+2-2), i.e. go to P (2), to perform the second step of the recursive algorithm for 2, i.e. calculate b ═ fib (0). And self-decrements rest of the read element by 1, at which time the stack is [ (4,2), (3,2), (2,1) ]. Since fib (0) ═ 0 can be directly calculated, performing P (2) for 2 is not a recursive subfunction.
Reading the last element of the stack again, i.e. reading (2,1), may determine that rest ═ 1 is not equal to 0, and so go to P (2+2-1), i.e. go to P (3), to perform the third step of the recursive algorithm for 2, i.e. calculate rest ═ a + b. And self-decrements rest of the read element by 1, at which time the stack is [ (4,2), (3,2), (2,0) ].
Since P (3) is a non-recursive subfunction, reading the last element of the stack again, i.e. reading (2,0), it can be determined that rest is 0, at this time, a obtained before the summary is 1, b is 0, and it can be calculated that the result of executing the recursive algorithm this time is fib (2) ═ a + b is 1. Deleting the element at the end of the stack, wherein the stack is [ (4,2), (3,2) ].
At this time, the stack is not empty, so the last element of the stack is read again, i.e. read (3,2), it can be determined that rest ═ 2 is not equal to 0, so P (2+2-2) is entered, i.e. P (2), to perform the second step of the recursive algorithm for 3, i.e. to calculate b ═ fib (1). And self-decrements rest of the read element by 1, at which time the stack is [ (4,2), (3, 1). Since fib (1) ═ 1 can be directly calculated, performing P (2) for 3 is not a recursive subfunction, and a ═ fib (2) ═ 1 is obtained.
Reading the last element of the stack again, i.e. reading (3,1), may determine that rest ═ 1 is not equal to 0, and therefore go to P (2+2-1), i.e. go to P (3), to perform the third step of the recursive algorithm for 3, i.e. calculate rest ═ a + b, and may obtain b ═ fib (1) ═ 1. And self-decrements rest of the read element by 1, at which time the stack is [ (4,2), (3,0) ].
Since P (3) is a non-recursive subfunction, reading the last element of the stack again, i.e. reading (3,0), it can be determined that rest is 0, at this time, a-fib (2) obtained before the summary is 1, b-fib (1) is 1, and it can be calculated that the result of executing the recursive algorithm this time is fib (3) -a + b-2. The element at the end of the stack is deleted, at which point the stack is [ (4,2) ].
At this time, the stack is not empty, so the last element of the stack is read again, i.e. read (4,2), it may be determined that rest ═ 2 is not equal to 0, so P (2+2-2), i.e. P (2), is entered, to perform the second step of the recursive algorithm for 4, i.e. to calculate b ═ fib (2), and a ═ fib (3) ═ 2 may be obtained. And self-decrements the rest of the read element by 1, at which time the stack is [ (4,1) ].
Since P (2) is a recursive subfunction for 4, Scope is created for the variable for which the recursive algorithm is to be executed next, i.e., 2, and the created Scope is recorded at the end of stack, where (2,3) is the created Scope and [ (4,1), (2,3) ] is the created Scope after the end of stack.
Reading the last element of the stack again, i.e. reading (2,3), may determine that rest ═ 3 is not equal to 0, and so go to P (2+2-3), i.e. go to P (1), to perform the first step of the recursive algorithm for 2, i.e. calculate fib (1). And self-decrements rest of the read element by 1, at which time the stack is [ (4,1), (2,2) ]. Since fib (1) can be directly calculated to be 1, performing P (1) for 2 is not a recursive subfunction.
Reading the last element of the stack again, i.e. reading (2,2), may determine that rest ═ 2 is not equal to 0, and so go to P (2+2-2), i.e. go to P (2), to perform the second step of the recursive algorithm for 2, i.e. calculate b ═ fib (0). And self-decrements rest of the read element by 1, at which time the stack is [ (4,1), (2,1) ]. Since fib (0) ═ 0 can be directly calculated, performing P (2) for 2 is not a recursive subfunction.
Reading the last element of the stack again, i.e. reading (2,1), may determine that rest ═ 1 is not equal to 0, and so go to P (2+2-1), i.e. go to P (3), to perform the third step of the recursive algorithm for 2, i.e. calculate rest ═ a + b. And self-decrements rest of the read element by 1, at which time the stack is [ (4,2), (2,0) ].
Since P (3) is a non-recursive subfunction, reading the last element of the stack again, i.e. reading (2,0), it can be determined that rest is 0, at this time, a obtained before the summary is 1, b is 0, and it can be calculated that the result of executing the recursive algorithm this time is fib (2) ═ a + b is 1. The element at the end of the stack is deleted, at which point the stack is [ (4,1) ].
At this time, the stack is not empty, so the last element of the stack is read again, i.e. read (4,1), it may be determined that rest ═ 1 is not equal to 0, so P (2+2-1), i.e. P (3), is entered, to perform the third step of the recursive algorithm for 4, i.e. calculate rest ═ a + b, and b ═ fib (2) ═ 1 may be obtained. And self-decrements rest of the read element by 1, at which time the stack is [ (4,0) ].
Since P (3) is a non-recursive subfunction, reading the last element of the stack again, i.e. reading (4,0), it can be determined that rest is 0, at this time, a-fib (3) obtained before the summary is 2, b-fib (2) is 1, and it can be calculated that the result of executing the recursive algorithm this time is fib (4) -a + b-3. Deleting the element at the end of the stack, wherein the stack is [ ], namely the stack is empty, and the last result 3 is the result obtained by executing the recursive algorithm for 4.
Referring to fig. 6, fig. 6 is a schematic structural diagram of a recursive algorithm implementation apparatus provided in an embodiment of the present invention, which may include:
a recursive execution module 601, configured to start executing a recursive algorithm for a current variable, where an initial value of the current variable is a target variable;
an execution progress recording module 602, configured to record, in a process of executing a recursive algorithm, an execution progress of executing the recursive algorithm this time whenever an executed step is a recursive step, where the recursive step is a step that needs to invoke the recursive algorithm;
and a return execution module 603, configured to, in the process of executing the recursive algorithm, when a result of executing the recursive algorithm on the current variable is obtained, based on the obtained result, continue to execute the recursive algorithm from the execution progress of the recursive algorithm executed last time until the obtained result is the result of executing the recursive algorithm on the target variable, where the recursive algorithm executed last time is the recursive algorithm executed on the current variable last time.
In a possible embodiment, the execution progress recording module 602 records the execution progress of the recursive algorithm executed this time whenever the executed step is a recursive step, including:
recording the execution progress of executing the recursive algorithm at this time when executing one step;
the returning execution module 603, each time a result of executing the recursive algorithm on the current variable is obtained, based on the obtained result, continuing to execute the recursive algorithm from the execution progress of the recursive algorithm last time until the obtained result is the result of executing the recursive algorithm on the target variable, including:
when the execution progress of the recursive algorithm executed this time indicates that the recursive algorithm executed this time is completed, taking the output of the latest executed step as the result of executing the recursive algorithm aiming at the current variable;
based on the obtained result, the execution of the recursive algorithm is continued from the most recently recorded execution progress of the recursive algorithm executed last time.
In a possible embodiment, the execution progress recording module 602 records the execution progress of the recursive algorithm, including:
recording a step identification of the executed step;
the return execution module 603, when the execution progress of the recursive algorithm executed this time indicates that the recursive algorithm executed this time is completed, takes the output of the latest executed step as the result of executing the recursive algorithm for the current variable, and includes:
when the step identification recorded by executing the recursive algorithm at this time represents the last step of the recursive algorithm, taking the output of the step executed latest as the result of executing the recursive algorithm aiming at the current variable;
the return execution module 603, based on the obtained result, continues to execute the recursive algorithm from the last recorded execution progress of the recursive algorithm, and includes:
based on the obtained result, the recursive algorithm is executed continuously from the step represented by the step identifier recorded last time the recursive algorithm was executed.
The step identification is used for representing the number of steps left from the last step in the recursive algorithm;
the execution progress recording module 602, whenever the step identification recorded by locally executing the recursive algorithm represents the last step of the recursive algorithm, takes the output of the newly executed step as the result of executing the recursive algorithm for the current variable, including:
whenever the step identification indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the newly executed step is taken as the result of executing the recursive algorithm for the current variable.
The return execution module 603 is further configured to, in the process of executing the recursive algorithm, determine whether executing the recursive algorithm this time is to continue executing the recursive algorithm for the first time when a result of executing the recursive algorithm for the current variable is obtained each time;
the return execution module 603, based on the obtained result, continues to execute the recursive algorithm from the execution progress of the recursive algorithm last time until the obtained result is the result of executing the recursive algorithm for the target variable, and includes:
if the execution of the recursive algorithm is not to continue the execution of the recursive algorithm for the first time, continuing to execute the recursive algorithm for the last time from the execution progress of the recursive algorithm for the last time;
and if the execution of the recursive algorithm is the first time, taking the obtained result as the result of executing the recursive algorithm aiming at the target variable.
In a possible embodiment, the returning execution module 603, in executing the recursive algorithm, determining whether executing the recursive algorithm this time is to continue executing the recursive algorithm for the first time when a result of executing the recursive algorithm for the current variable is obtained, including:
in the process of executing the recursive algorithm, deleting the execution progress recorded by executing the recursive algorithm at this time when a result of executing the recursive algorithm aiming at the current variable is obtained;
determining whether an execution progress is recorded;
if the execution progress is recorded, determining that the execution of the recursive algorithm is not the continuous execution of the recursive algorithm for the first time;
and if the execution progress is not recorded, determining that the recursive algorithm is executed for the first time continuously.
In a possible embodiment, the execution progress recording module 602 is further configured to record, during the execution of the recursive algorithm, a current variable as an input parameter for the current execution of the recursive algorithm whenever the executed step is a recursive step;
the return execution module continues to execute the recursive algorithm from the execution progress of the recursive algorithm executed last time, and comprises:
and aiming at the input parameters of the last executed recursive algorithm, continuing to execute the last executed recursive algorithm from the execution progress of the last executed recursive algorithm.
An embodiment of the present invention further provides an electronic device, as shown in fig. 7, including:
a memory 701 for storing a computer program;
the processor 702 is configured to implement the following steps when executing the program stored in the memory 701:
starting to execute a recursive algorithm aiming at a current variable, wherein an initial value of the current variable is a target variable;
in the process of executing the recursive algorithm, recording the execution progress of executing the recursive algorithm at this time when the executed steps are recursive steps, wherein the recursive steps are steps needing to be called;
in the process of executing the recursive algorithm, when a result of executing the recursive algorithm on a 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 on the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed on the last current variable.
In a possible embodiment, the recording the execution progress of the recursive algorithm executed this time each time the executed step is a recursive step includes:
recording the execution progress of executing the recursive algorithm at this time when executing one step;
the, each time a result of executing the recursive algorithm on the current variable is obtained, continuing to execute the recursive algorithm from the execution progress of the recursive algorithm last time based on the obtained result until the obtained result is the result of executing the recursive algorithm on the target variable, including:
when the execution progress of the recursive algorithm executed this time indicates that the recursive algorithm executed this time is completed, taking the output of the latest executed step as the result of executing the recursive algorithm aiming at the current variable;
based on the obtained result, the execution of the recursive algorithm is continued from the most recently recorded execution progress of the recursive algorithm executed last time.
In one possible embodiment, the recording of the progress of the execution of the recursive algorithm includes:
recording a step identification of the executed step;
when the execution progress of the recursive algorithm executed this time indicates that the recursive algorithm executed this time is completed, the method takes the output of the latest executed step as the result of executing the recursive algorithm aiming at the current variable, and comprises the following steps:
when the step identification recorded by executing the recursive algorithm at this time represents the last step of the recursive algorithm, taking the output of the step executed latest as the result of executing the recursive algorithm aiming at the current variable;
the executing the recursive algorithm for the last time is continued from the execution progress of the latest record of the recursive algorithm for the last time based on the obtained result, and the executing method comprises the following steps:
based on the obtained result, the recursive algorithm is executed continuously from the step represented by the step identifier recorded last time the recursive algorithm was executed.
In one possible embodiment, the step identification is used to indicate the number of steps remaining from the last step in the recursive algorithm;
the step of executing the recursive algorithm locally, each time the recorded step identification represents the last step of the recursive algorithm, taking the output of the last executed step as the result of executing the recursive algorithm for the current variable, comprises:
whenever the step identification indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the newly executed step is taken as the result of executing the recursive algorithm for the current variable.
In a possible embodiment, the method further comprises:
in the process of executing the recursive algorithm, when a result of executing the recursive algorithm aiming at the current variable is obtained, whether the execution of the recursive algorithm for the current time is the continuous execution of the recursive algorithm for the first time is determined;
the executing the recursive algorithm for the last time from the execution progress of the recursive algorithm for the last time based on the obtained result until the obtained result is the result of executing the recursive algorithm for the target variable includes:
if the execution of the recursive algorithm is not to continue the execution of the recursive algorithm for the first time, continuing to execute the recursive algorithm for the last time from the execution progress of the recursive algorithm for the last time;
and if the execution of the recursive algorithm is the first time, taking the obtained result as the result of executing the recursive algorithm aiming at the target variable.
In a possible embodiment, in the executing the recursive algorithm, each time a result of executing the recursive algorithm on the current variable is obtained, determining whether executing the recursive algorithm this time is to continue executing the recursive algorithm for the first time includes:
in the process of executing the recursive algorithm, deleting the execution progress recorded by executing the recursive algorithm at this time when a result of executing the recursive algorithm aiming at the current variable is obtained;
determining whether an execution progress is recorded;
if the execution progress is recorded, determining that the execution of the recursive algorithm is not the continuous execution of the recursive algorithm for the first time;
and if the execution progress is not recorded, determining that the recursive algorithm is executed for the first time continuously.
In a possible embodiment, the method further comprises:
in the process of executing the recursive algorithm, recording a current variable as an input parameter for executing the recursive algorithm at this time when the executed step is the recursive step;
the executing the recursive algorithm for the last time is continued from the executing progress of the recursive algorithm for the last time, and the executing method comprises the following steps:
and aiming at the input parameters of the last executed recursive algorithm, continuing to execute the last executed recursive algorithm from the execution progress of the last executed recursive algorithm.
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 memory device located remotely from the processor.
The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), and the like; but may also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field Programmable Gate Array (FPGA) or other Programmable logic device, discrete Gate or transistor logic device, discrete hardware component.
In yet another embodiment of the present invention, a computer-readable storage medium is further provided, which has instructions stored therein, which when run on a computer, cause the computer to perform any of the recursive function execution methods of the above embodiments.
In a further embodiment provided by the present invention, there is also provided a computer program product containing instructions which, when run on a computer, cause the computer to perform any of the recursive function execution methods of the embodiments described above.
In the above embodiments, the implementation may be wholly or partially realized by software, hardware, firmware, or any combination thereof. When implemented in software, may 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 loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the invention to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website site, computer, server, or data center to another website site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, Digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that incorporates one or more of the available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., Solid State Disk (SSD)), among others.
It is noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the embodiments of the apparatus, the electronic device, the computer-readable storage medium, and the computer program product, since they are substantially similar to the method embodiments, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiments.
The above description is only for the preferred embodiment of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention shall fall within the protection scope of the present invention.

Claims (10)

1. A method for implementing a recursive algorithm, the method comprising:
starting to execute a recursive algorithm aiming at a current variable, wherein an initial value of the current variable is a target variable;
in the process of executing the recursive algorithm, recording the execution progress of executing the recursive algorithm at this time when the executed steps are recursive steps, wherein the recursive steps are steps needing to be called;
in the process of executing the recursive algorithm, when a result of executing the recursive algorithm on a 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 on the target variable, and the last execution of the recursive algorithm is the recursive algorithm executed on the last current variable.
2. The method of claim 1, wherein recording the progress of executing the recursive algorithm at this time each time the executed step is a recursive step comprises:
recording the execution progress of executing the recursive algorithm at this time when executing one step;
the, each time a result of executing the recursive algorithm on the current variable is obtained, continuing to execute the recursive algorithm from the execution progress of the recursive algorithm last time based on the obtained result until the obtained result is the result of executing the recursive algorithm on the target variable, including:
when the execution progress of the recursive algorithm executed this time indicates that the recursive algorithm executed this time is completed, taking the output of the latest executed step as the result of executing the recursive algorithm aiming at the current variable;
based on the obtained result, the execution of the recursive algorithm is continued from the most recently recorded execution progress of the recursive algorithm executed last time.
3. The method of claim 2, wherein recording progress of execution of the recursive algorithm comprises:
recording a step identification of the executed step;
when the execution progress of the recursive algorithm executed this time indicates that the recursive algorithm executed this time is completed, the method takes the output of the latest executed step as the result of executing the recursive algorithm aiming at the current variable, and comprises the following steps:
when the step identification recorded by executing the recursive algorithm at this time represents the last step of the recursive algorithm, taking the output of the step executed latest as the result of executing the recursive algorithm aiming at the current variable;
the executing the recursive algorithm for the last time is continued from the execution progress of the latest record of the recursive algorithm for the last time based on the obtained result, and the executing method comprises the following steps:
based on the obtained result, the recursive algorithm is executed continuously from the step represented by the step identifier recorded last time the recursive algorithm was executed.
4. The method of claim 3, wherein the step identification is used to indicate the number of steps remaining from the last step in the recursive algorithm;
the step of executing the recursive algorithm locally, each time the recorded step identification represents the last step of the recursive algorithm, taking the output of the last executed step as the result of executing the recursive algorithm for the current variable, comprises:
whenever the step identification indicates that the number of steps remaining from the last step in the recursive algorithm is 0, the output of the newly executed step is taken as the result of executing the recursive algorithm for the current variable.
5. The method of claim 1, further comprising:
in the process of executing the recursive algorithm, when a result of executing the recursive algorithm aiming at the current variable is obtained, whether the execution of the recursive algorithm for the current time is the continuous execution of the recursive algorithm for the first time is determined;
the executing the recursive algorithm for the last time from the execution progress of the recursive algorithm for the last time based on the obtained result until the obtained result is the result of executing the recursive algorithm for the target variable includes:
if the execution of the recursive algorithm is not to continue the execution of the recursive algorithm for the first time, continuing to execute the recursive algorithm for the last time from the execution progress of the recursive algorithm for the last time;
and if the execution of the recursive algorithm is the first time, taking the obtained result as the result of executing the recursive algorithm aiming at the target variable.
6. The method as claimed in claim 5, wherein the determining whether the recursive algorithm is executed this time for continuing to execute the recursive algorithm for the first time when the result of executing the recursive algorithm for the current variable is obtained each time during the execution of the recursive algorithm comprises:
in the process of executing the recursive algorithm, deleting the execution progress recorded by executing the recursive algorithm at this time when a result of executing the recursive algorithm aiming at the current variable is obtained;
determining whether an execution progress is recorded;
if the execution progress is recorded, determining that the execution of the recursive algorithm is not the continuous execution of the recursive algorithm for the first time;
and if the execution progress is not recorded, determining that the recursive algorithm is executed for the first time continuously.
7. The method of claim 1, further comprising:
in the process of executing the recursive algorithm, recording a current variable as an input parameter for executing the recursive algorithm at this time when the executed step is the recursive step;
the executing the recursive algorithm for the last time is continued from the executing progress of the recursive algorithm for the last time, and the executing method comprises the following steps:
and aiming at the input parameters of the last executed recursive algorithm, continuing to execute the last executed recursive algorithm from the execution progress of the last executed recursive algorithm.
8. A recursive algorithm implementation apparatus, characterized in that the apparatus comprises:
the recursive execution module is used for starting to execute a recursive algorithm aiming at a current variable, and an initial value of the current variable is a target variable;
the execution progress recording module is used for recording the execution progress of the execution of the recursive algorithm at this time when the executed steps are recursive steps in the process of executing the recursive algorithm, and the recursive steps are the steps needing to call the recursive algorithm;
and the return execution module is used for continuously executing the recursive algorithm from the execution progress of the recursive algorithm executed last time to the result of executing the recursive algorithm aiming at the target variable according to the obtained result when the result of executing the recursive algorithm aiming at the current variable is obtained every time in the process of executing the recursive algorithm, and the recursive algorithm executed last time is the recursive algorithm executed aiming at the last current variable.
9. An electronic device, comprising:
a memory for storing a computer program;
a processor for implementing the method steps of any of claims 1 to 7 when executing a program stored in the memory.
10. A computer-readable storage medium, characterized in that a computer program is stored in the computer-readable storage medium, which computer program, when being executed by a processor, carries out the method steps of any one of claims 1 to 7.
CN202010608367.7A 2020-06-29 2020-06-29 Recursive algorithm implementation method and device and electronic equipment Pending CN111767522A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010608367.7A CN111767522A (en) 2020-06-29 2020-06-29 Recursive algorithm implementation method and device and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010608367.7A CN111767522A (en) 2020-06-29 2020-06-29 Recursive algorithm implementation method and device and electronic equipment

Publications (1)

Publication Number Publication Date
CN111767522A true CN111767522A (en) 2020-10-13

Family

ID=72724253

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010608367.7A Pending CN111767522A (en) 2020-06-29 2020-06-29 Recursive algorithm implementation method and device and electronic equipment

Country Status (1)

Country Link
CN (1) CN111767522A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112508228A (en) * 2020-11-03 2021-03-16 北京理工大学前沿技术研究院 Driving behavior risk prediction method and system

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112508228A (en) * 2020-11-03 2021-03-16 北京理工大学前沿技术研究院 Driving behavior risk prediction method and system

Similar Documents

Publication Publication Date Title
CN109766497B (en) Ranking list generation method and device, storage medium and electronic equipment
CN114416667B (en) Method and device for rapidly sharing network disk file, network disk and storage medium
JP2019145925A (en) Method for verifying transaction in blockchain network, and node for constituting the network
CN109033365B (en) Data processing method and related equipment
CN111506580B (en) Transaction storage method based on centralized block chain type account book
CN113591068A (en) Online login equipment management method and device and electronic equipment
CN111767522A (en) Recursive algorithm implementation method and device and electronic equipment
JP2019146137A (en) Method for verifying transaction in blockchain network, and node for constituting the network
CN112182199A (en) Dependency package recommendation method and device, electronic equipment and readable storage medium
JP2020079991A (en) Optimization apparatus, control method of optimization apparatus, and control program of optimization apparatus
CN112149011A (en) Method and device for updating popularity ranking list, server and computer storage medium
CN110097361B (en) Block chain dynamic calculation force consensus method based on X11 algorithm and computer system
CN112306471A (en) Task scheduling method and device
CN111666302A (en) User ranking query method, device, equipment and storage medium
CN111399849A (en) Method and system for determining application program installation package
CN111523048A (en) Recommendation method and device for friends in social network, storage medium and terminal
WO2019228009A1 (en) Lsm tree optimization method and device and computer equipment
CN111309693A (en) Data synchronization method, device and system, electronic equipment and storage medium
JP2022104892A (en) Identification method of long tail keyword, keyword retrieval method, and computer equipment
CN113626483B (en) Front-end caching method, system, equipment and storage medium for filling forms
CN112036133B (en) File storage method and device, electronic equipment and storage medium
CN110020264B (en) Method and device for determining invalid hyperlinks
JP5776403B2 (en) Region search method, region search program, and information processing apparatus
CN107436918A (en) Database implementation method, device and equipment
CN116127792B (en) Interpolation method and device for scattered data

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination