Iteration Vs Recursion Java
2016-01-06 23:04
501 查看
Iteration vs. Recursion in Java
1. RecursionConsider the factorial function:
n!=n*(n-1)*(n-2)*...*1
There are many ways to compute factorials. One way is that n! is equal to n*(n-1)!. Therefore the program can be directly written as:
Program 1:
int factorial (int n) { if (n == 1) { return 1; } else { return n*factorial(n-1); } } |
of program, characterized by a chain of operations, is called recursion. Recursion can be further categorized into linear and treerecursion. When the amount of information
needed to keep track of the chain of operations grows linearly with the input, the recursion is called linear recursion. The computation of n! is such a case, because the time required grows linearly with n. Another type of recursion, tree recursion, happens
when the amount of information grows exponentially with the input. But we will leave it undiscussed here and go back shortly afterwards.
2. Iteration
A different perspective on computing factorials is by first multiplying 1 by 2, then multiplying the result by 3, then by 4, and so on until n. More formally, the program can use a counter that counts from 1 up to n and compute the product simultaneously until
the counter exceeds n. Therefore the program can be written as:
Program 2:
int factorial (int n) { int product = 1; for(int i=2; i<n; i++) { product *= i; } return product; } |
number of variables, a fixed rule that describes how the variables should be updated, and an end test that specifies conditions under which the process should terminate. Same as recursion, when the time required grows linearly with the input, we call the iteration
linear recursion.
3. Recursion vs Iteration
Compared the two processes, we can find that they seem almost same, especially in term of mathematical function. They both require a number of steps proportional to n to compute n!. On the other hand, when we consider the running processes of the two programs,
they evolve quite differently.
In the iterative case, the program variables provide a complete description of the state. If we stopped the computation in the middle, to resume it only need to supply the computer with all variables. However, in the recursive process, information is maintained
by the computer, therefore "hidden" to the program. This makes it almost impossible to resume the program after stopping it.
4. Tree recursion
As described above, tree recursion happens when the amount of information grows exponentially with the input. For instance, consider the sequence of Fibonacci numbers defined as follows:
By the definition, Fibonacci numbers have the following sequence, where each number is the sum of the previous two:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
A recursive program can be immediately written as:
Program 3:
int fib (int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); } } |
The ith Fibonacci number Fib(i) is equal to phi(i)/rootsquare(5) rounded to the nearest integer, which indicates that Fibonacci numbers grow exponentially.
This is a bad way to compute Fibonacci numbers because it does redundant computation. Computing the running time of this procedure is beyond the scope of this article, but one can easily find that in books of algorithms,
which is O(phi(n)). Thus, the program takes an amount of time that grows exponentially with the input.
On the other hand, we can also write the program in an iterative way for computing the Fibonacci numbers. Program 4 is a linear iteration. The difference in time required by Program 3 and 4 is enormous, even for small inputs.
Program 4:
int fib (int n) { int fib = 0; int a = 1; for(int i=0; i<n; i++) { fib = fib + a; a = fib; } return fib; } |
with Program 3 and 4, we can easily tell Program 3 is more straightforward, even if less efficient. After that, we can most likely reformulate the program into an iterative way.
Reference:
1. Cornell cs211 lecture
You May Also Like ...
LeetCode– Two Sum (Java)
LeetCode
– Maximum Product Subarray (Java)
Two
Sum II – Input array is sorted (Java)
LeetCode
– Symmetric Tree (Java)
Category >> Algorithms
If you want to post syntax highlighted code and let me or someone else review it, please put the code inside <pre><code> and </code></pre> tags.
For example:
<pre><code> String foo = "bar"; </code></pre>
相关文章推荐
- JAVA之Switch语句
- 【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3
- Win下,通过Jstack截取Java进程中的堆栈信息
- 【第六章】 AOP 之 6.1 AOP基础 ——跟我学spring3
- JAVA操作Excel表
- 从头认识java-16.5 nio的数据转换
- 【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3
- 在eclipse中设计BPMN 2.0工作流定义的根本步骤
- Spring事务学习
- Windows配置Android环境(eclipse+adt+sdk+genymotion)
- java调用7zip demo
- JAVA堆外内存
- java线程池学习(六)——线程池生命周期管理
- Struts2防止表单重复提交
- Struts2防止表单重复提交
- 《java并发编程实战》读书笔记——并发应用
- JDK配置步骤
- 4. 【创建和销毁对象】通过私有化构造方法强化不可实例化的能力
- JDBC之模型层的完成
- Java下载文件的几种方式