Docsity

Recursion in Java: Examples and Applications - Prof. Saumendra Sengupta, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
A recursive routine or a method calls itself to accomplish
its objective. The divide and rule principle is evoked.
Large problem
๏ƒž
smaller problems
Solve smaller problems first.
import java.io.*;
class Recursion0
{
public static int SumSquares (int m, int n)
{
if (m == n)
return m*m;
else
return n*n + SumSquares(m,n-1);
}
public static void main(String [] args)
{
int i=10;
int j=20;
for (int k=i; k<=j; k++)
{
System.out.println("Sum_of_squares from " +
i +" to "+ k +" "+
Solve this
A+B
A
B
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Recursion in Java: Examples and Applications - Prof. Saumendra Sengupta and more Study notes Data Structures and Algorithms in PDF only on Docsity!

A recursive routine or a method calls itself to accomplish

its objective. The divide and rule principle is evoked.

Large problem ๏ƒž^ smaller problems

Solve smaller problems first.

import java.io.; class Recursion { public static int SumSquares (int m, int n) { if (m == n) return mm; else return n*n + SumSquares(m,n-1); } public static void main(String [] args) { int i=10; int j=20; for (int k=i; k<=j; k++) { System.out.println("Sum_of_squares from " + i +" to "+ k +" "+

Solve this

A+B

A

B

SumSquares(i,k)); } } } Output: Sum_of_squares from 10 to 10 100 Sum_of_squares from 10 to 11 221 Sum_of_squares from 10 to 12 365 Sum_of_squares from 10 to 13 534 Sum_of_squares from 10 to 14 730 Sum_of_squares from 10 to 15 955 Sum_of_squares from 10 to 16 1211 Sum_of_squares from 10 to 17 1500 Sum_of_squares from 10 to 18 1824 Sum_of_squares from 10 to 19 2185 Sum_of_squares from 10 to 20 2585

This version would change the method by working

symmetrically from both ends.

public static int SumSquares (int m, int n) { if (m == n) return m*m; else { int middle = (m+n)/2; return (SumSquares (m, middle) + SumSquares(middle+1,n)); } }

Thus, we compute the normal iterative routines recursively

in the following way:

Otherwise

Print a line of n stars ******************

Print the rest of the pattern pattern(n-1).

A typical function might be suggested as

public static void Pattern (int n) { if (n == 1) System.out.println(''); else { for (int i=1; i<=n ; i++) System.out.print(''); System.out.println(); Pattern(n-1); }**

What would have happened if the task components within

the else block are switched? What would be the output

from

public static void Pattern (int n) { if (n == 1) System.out.println(''); else { pattern(n-1); for (int i=1; i<=n ; i++) System.out.print(''); System.out.println(); } }**

Timing of the tasks in a recursive module is an important

issue. Seen as a tree, it shows the relative order of the

executed tasks when the module is evoked.

Recursive tree.

When expanded, it would execute the modules in the

following time-sequence

More recursions.

3. GCD of two integers. The Greatest Common Divisor of

two integers m and n is the largest integer that divides both

m and n evenly.

If GCD(m,n) = k then no other number larger than k

divides both m and n evenly.

Program code:

import java.io.; import java.util.; Time

GCD(916,165) = 1

GCD(595,525) = 35

GCD(448,382) = 2

GCD(523,428) = 1

GCD(959,171) = 1

GCD(527,514) = 1

GCD(322,806) = 2

GCD(973,48) = 1

GCD(815,283) = 1

GCD(833,21) = 7

GCD(787,576) = 1

GCD(430,816) = 2

GCD(682,978) = 2

GCD(364,48) = 4

GCD(598,415) = 1

GCD(304,398) = 2

GCD(170,47) = 1

GCD(962,508) = 2

4. Tower of Hanoi. Given 3 towers and a set of circular

disks (of decreasing size, with no larger disk ever allowed

to sit on top of a smaller disk). Find a set of moves that

would take the disks one at a time and put it all on the

destination disk.

A recursive solution to this problem

Source Spare Sink

Move top n-1 disks (using recursion) from source to spare

using the sink. Move the last disk from the source to sink.

Now move all the (n-1) disks from spare to the sink using

the source.

Code:

public class Tower { public static void move(int n, int source, int spare, int sink) { if (n==1) System.out.println("Move "+source+" -> "+ sink); else { move(n-1, source, sink, spare); move(1, source, spare, sink); move(n-1, spare, source, sink); } } public static void main (String [] args) { int n=4; int source=1; int spare=2; int sink=3; move(n, source, spare, sink); } }

Output: