0% found this document useful (0 votes)
73 views

Find Out How Many Types of Recursive Algorithm That You Can Find

1. The document defines and provides examples of 15 different types of recursive algorithms: linear, tail, binary, exponential, nested, mutual, head, tree, indirect, single, multiple, anonymous, structural, generative, and non-tail recursion. 2. It provides a recursive program for calculating factorials using a for loop that multiplies integers from 1 to the given number n. 3. The document does not provide a recursive Fibonacci program but mentions it will in the next section.

Uploaded by

Yuyun Joe
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
73 views

Find Out How Many Types of Recursive Algorithm That You Can Find

1. The document defines and provides examples of 15 different types of recursive algorithms: linear, tail, binary, exponential, nested, mutual, head, tree, indirect, single, multiple, anonymous, structural, generative, and non-tail recursion. 2. It provides a recursive program for calculating factorials using a for loop that multiplies integers from 1 to the given number n. 3. The document does not provide a recursive Fibonacci program but mentions it will in the next section.

Uploaded by

Yuyun Joe
Copyright
© © All Rights Reserved
You are on page 1/ 10

Dwi Putri Wahyuningsih

2019390004

Assignment: Recursive

Basic Recursive Algorithm Exercises

1. Find out what is the definition of recursive.


Recursive functions in programming are functions that call themselves. I often think of
recursive functions as loops. Because of the behavior that repeats every call itself.
Recursive functions can solve several problems such as calculating Fibonacci numbers
and factorials.

2. Find out how many types of recursive algorithm that you can find.
1) Linear Recursive
A linear recursive function is one that only makes one call to itself each time the
function is run (as opposed to a function that calls itself multiple times during its
execution). Factorial functions are good examples of linear recursion.
2) Tail recursive
Tail recursion is a form of linear recursion. In tail recursion, the recursive call is
the last thing the function does. Often, recursive call values are returned. A great
example of a tail-recursive function is a function for calculating the GCD, or
Largest Common Denominator, from two numbers:
int gcd (int m, int n) {int r; if (m <n) return gcd (n, m); r = m% n; if (r == 0)
returns (n); else return (gcd (n, r)); }
3) Binary Recursive
Some recursive functions don't just have one call, they have two (or more). A
function with two recursive calls is called a binary recursive function.
Mathematical combination operations are a great example of a function that can
be quickly implemented as a recursive binary function. The number of
combinations, often represented as nCk where we select n elements from a set of
k elements, can be implemented as follows:
int select (int n, int k) {if (k == 0 || n == k) return (1); else return (select (n-1, k)
+ select (n-1, k-1));}
4) Exponential recursion
An exponential recursive function is a function that, if you draw a representation
of all the function calls, will have an exponential number of calls about the size of
the data set (meaning exponential if there are n elements, there will be O (a)
function calls where a is a positive number). A great example of an exponentially
recursive function is a function for calculating all permutations of a data set. Let
us write a function to take an array of n integers and print each one.
clear print_array (int arr [], int n) {int i; for (i = 0; i <n; i) printf ("% d", arr
[i]); printf ("\ n"); } void print_permutations (int arr [], int n, int i) {int j, swap;
print_array (arr, n); for (j = i + 1; j <n; j) {swap = arr [i]; arr [i] = arr [j]; arr
[j] = exchange; print_permutations (arr, n, i + 1); exchange = arr [i]; arr [i] =
arr [j]; arr [j] = exchange; }}
To run this function on an array of arrays of length n, we would do
print_permutations (arr, n, 0) where 0 tells to start over from the beginning of the
array.
5) Nested Recursion
In nested recursion, one of the arguments for a recursive function is the recursive
function itself! This function tends to develop very quickly. A good example is
the classical math function, "Ackerman's function. It grows very fast (even for
small values of x and y, Ackermann (x, y) is very large) and cannot be computed
by just iteration definite (for () loops for example); it requires infinite iteration
(recursion, for example).
Ackerman function int ackerman (int m, int n) {if (m == 0) return (n + 1); else if
(n == 0) return (ackerman (m-1,1)); else return (ackerman (m-1, ackerman (m,
n-1))); }
Try counting Ackerman (4,2) by hand ... have fun!
6) Mutual Recursion
Recursive functions do not need to call themselves. Some recursive functions
work in pairs or even in larger groups. For example, function A calls function B
which calls function C which in turn calls function A. A simple example of
reciprocal recursion is a series of functions for determining whether an integer is
even or odd. How do we know if a number is even? Well, we know 0 is even.
And we also know that if a number n is even, then n - 1 must be odd. How do we
know if a number is odd? It is not even!
int is even (unsigned int n) {if (n == 0) return 1; else return (is odd (n-1)); } int is
odd (unsigned int n) {return (! is even (n)); }
The above situation is not the best example when we want to use recursion instead
of iteration or closed form solutions. A more efficient set of functions for
determining whether an integer is even or odd is as follows:
int is even (unsigned int n) {if (n% 2 == 0) return 1; else returns 0;} int is odd
(unsigned int n) {if (n% 2! = 0) return 1; else returns 0;}
7) Head Recursion
If the recursive function calls itself and the recursive call is the first statement in
the function, then this is known as Head Recursion. No statement, no pre-call
operation. The function does not have to process or perform any operation at the
time of calling and all operations are performed at the time of return.
8) Tree Recursion
Tree Recursion: To understand Tree Recursion, let's first understand Linear
Recursion. If a function is recursive calling itself for a time, then it is known as
Linear Recursion. Conversely, if a recursive function calls itself more than once
then it is called Tree Recursion.
Example:
Pseudocode for linear recursion.
9) Indirect Recursion
In this recursion, there may be more than one function and they call each other in
a circle.
10) Single Recursion
A recursion that contains only one self-reference is called a single recursion,
while a recursion that contains multiple self-references is known as multiple
recursions. Standard examples of single recursion include browsing of lists, as in
linear search, or computation of factorial functions, while standard examples of
double recursion include browsing of trees, as in first deep search. Single
recursion is often much more efficient than multiple recursions, and can generally
be replaced by iterative computing, runs in linear time, and requires constant
space. Some recursions, in contrast, may take exponential time and space and is
more inherently recursive, cannot be replaced by iterations without an explicit
stack.
11) Multiple Recursions
Can sometimes be converted to a single recursion (and, if desired, then to
iteration). For example, when calculating the Fibonacci sequence naively it is
multiple iterations since each value requires two preceding values, this can be
calculated by single recursion passing two consecutive values as parameters. This
is more naturally framed as corecursion, constructing from an initial value,
tracking at each step the two consecutive values - see corecursion: example. A
more sophisticated example is using a threaded binary tree, which allows for
looping hierarchy traversal instead of double recursion.
12) Anonymous Recursion
Recursion is usually accomplished by explicitly calling a function by name.
However, recursion can also be performed via implicit function calls based on the
current context, which is very useful for anonymous functions and is known as
anonymous recursion.
13) Structural Recursion
The term "structural recursion" comes from the fact that this structure (list, BST,
etc.) can be recursively defined:
 A list does not mean anything, or the cells followed by a list.
 A binary tree means nothing or a node with two binary trees as children.
When performing structural recursion, you "undo" the operation from which these
structures are built on each other. For example, the NumberOfNodes function
"undo" construction takes a node and adds it to an existing list. The Find operator
"undoes" the binding operation of the node to the other two trees. Therefore, it's
easy to see why this function should be terminated - in the end, you "undo" all
operations that were performed to build the object in the first place, and the
recursion stops.
14) Generative Recursion
Recursive cases are generated based on the problem to be solved.
 The non-recursive case also does not follow the data definition.
 It is much more difficult to find a solution to such a problem.
 Often requires deeper analysis and domain-specific knowledge
15) Non-tail recursion
Can transform to iteration using explicit stack
Example
int nontail(int n ) {

x = nontail(n-1);
y = nontail(n-2);
z = x + y;
return z;
3. Develop a recursive program for factorial.
/**
* The basic recursive factorial.
*
* @param n The number to compute factorial of.
* @return n factorial.
*/

 Code
import java.util.Scanner;

public class Factorial {


public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number whose
factorial is to be found: ");
int n = scanner.nextInt();
int result = factorial(n);
System.out.println("The factorial of " + n +
" is " + result);
}

public static int factorial(int n)


{
int result = 1;
for (int i = 1; i <= n; i++)
{
result = result * i;
}

return result;
}
}

 Result
4. Develop a recursive program for Fibonacci.
/**
* basic - The simple version of fibonacci.
*
* @param n A positive integer.
* @return The nth fibonacci number.
*/
 Code
import java.util.Scanner;

public class fibonacci {


static int fibo(int n){
if(n == 0 || n == 1){
return n;
} else {
return (fibo(n-1) + fibo(n-2));
}
}
public static void main(String[]args){
int i, j = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of the Fibonacci
sequence: ");
int n = scan.nextInt();
System.out.print("The result of the Fibonacci
number: ");
for (i = 0; i < n; i++){
System.out.print(fibo(j)+", ");
j++;
}
System.out.println();
}
}
 Result

5. Develop a recursive program String Replace.


/**
* replace - Replace all instances of one character by another.
*
* @param s The string to do the replacement on.
* @param from The character to be replaced.
* @param to the character to change to.
* @return A new string with the appropriate characters replaced.
*/
 Code
public class StringReplace {

public static void main(String[] args) {


String words = "hello world, take care of your
health";
char from = 'a';
char to = '/';

System.out.println(replace(words, from, to));


}

public static String replace(String s, char from,


char to){
if (s.length() < 1) {
return s;
}
else {
char first = from == s.charAt(0) ? to :
s.charAt(0);
return first + replace(s.substring(1), from, to);
}
}
}
 Result

You might also like