0% found this document useful (0 votes)
44 views7 pages

Post - 2030 L3 Current-Recursion

The document discusses recursion in programming. Recursion is defined as solving a problem by reducing it to smaller instances of the same problem until a base case is reached. An example of recursively calculating the length of a string is given, showing how it reduces to adding 1 to the length of successive substrings until the empty string base case is reached. Recursion involves base cases that are solved directly and general cases that call the function recursively on smaller inputs until a base case is hit.

Uploaded by

Ya Lan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views7 pages

Post - 2030 L3 Current-Recursion

The document discusses recursion in programming. Recursion is defined as solving a problem by reducing it to smaller instances of the same problem until a base case is reached. An example of recursively calculating the length of a string is given, showing how it reduces to adding 1 to the length of successive substrings until the empty string base case is reached. Recursion involves base cases that are solved directly and general cases that call the function recursively on smaller inputs until a base case is hit.

Uploaded by

Ya Lan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2020-09-18

Advanced Object Oriented


Programming

EECS 2030
Lecture 3 :: Recursion

Think recursively

1
2020-09-18

Think
recursively

“how many people are there,


from you to the end of line?”

“ 1 + the answer of the guy


behind me“

General Definition of Recursion


• Recursion: The definition of an operation in terms of
itself.
▪ Solving a problem using recursion depends on solving
smaller occurrences of the same problem.

• Recursion solution: algorithm that finds the solution to


a given problem by reducing the problem to smaller
versions of itself.

2
2020-09-18

Recursion
• Recursion is the process of defining a problem (or the solution to a
problem) in terms of (a simpler version of) itself.
▪ number of characters in a string is 1 + number characters in the
remaining string
o lengh("ABCD") = 1 + length("BCD")
▪ a × b can be solved by a + a × (b-1)
▪ ab can be solved by a × ab-1
▪ sum of n numbers is sum of first and rest
o sum(4,8,7,5) = 4 + sum(8,7,5)

▪ Max value in a list is the larger of first and max of rest list
▪ Factorial n can be defined/solved as n! = n * (n-1)!
▪ Fibonacci sequence is defined as: F(i) = F(i-1) + F(i-2)
5

Why recursion?
• Solves some problems more naturally than
iteration
▪ In computer science, some problems are more easily
solved by using recursive functions.
o Tower of Hanoi

• Leads to elegant, simplistic, short code (when


used well)
▪ More efficient?

3
2020-09-18

Recursive Definitions
• Every recursive algorithm involves at least 2 cases:
▪ Base case(s).
▪ Recursive calls/cases.

• Anchor, ground, or base case:


▪ Case in recursive definition in which the solution is obtained
directly
▪ Stops the recursion If your instance is sufficiently
small, solve it yourself as a base
case.

• General case:
▪ Case in recursive definition in which a smaller version of itself
is called
▪ Usually by changing the parameter(s).
▪ Must eventually be reduced to a base case
o Progress toward base case

Recursive Algorithm: smaller instance


• Assume you have an algorithm that works.
• Use it to write an algorithm that works.

To get into my house


I must get the key from a smaller house
Assume I made it.
8

4
2020-09-18

Recursive Algorithm: base cases


• Assume you have an algorithm that works.
• Use it to write an algorithm that works.

Use brute force


to get into
the smallest house.

Recursion
“To get into my house
I must get the key from a smaller house

int length (string s) // Java


{ if (s contains no letter)
return 0;
else
int restL = length(substring on the right);
return 1 + restL;
}

length(“ABCD”)
= 1 + length(“BCD”)
= 1 + ( 1 + length(“CD”))
= 1 + ( 1 + ( 1 + length(“D”))) Can save restL
=101 + ( 1 + ( 1 + (1+ (1+length(“”) )))
=1+ (1+ ( 1 + (1+ (1+0) ))) = 4
10

5
2020-09-18

Recursion
“To get into my house
I must get the key from a smaller house

int length (string s) // Java


{ if (s contains no letter)
return 0;
return 1 + length(substring on the right);
}

length(“ABCD”)
= 1 + length(“BCD”)
= 1 + ( 1 + length(“CD”))
= 1 + ( 1 + ( 1 + length(“D”)))
= 1 + ( 1 + ( 1 + (1+ length(“”) )))
=11 1 + ( 1 + ( 1 + (1+ 0 ))) = 4

11

Recursion
“To get into my house
I must get the key from a smaller house

static int length (String s)


{ if (s.equals("") // contains no letter
return 0;
return 1 + length(s.substring(1));
}

length(“ABCD”)
= 1 + length(“BCD”)
= 1 + ( 1 + length(“CD”))
= 1 + ( 1 + ( 1 + length(“D”)))
= 1 + ( 1 + ( 1 + (1+ length(“”) )))
=12 1 + ( 1 + ( 1 + (1+ 0 ))) = 4

12

6
2020-09-18

Recursive Methods
• Caller “pause” on method call

• Every recursive call has its own


▪ Code
▪ Set of parameters
▪ Set of local variables

• After completing a recursive call


▪ Control goes back to the calling environment
▪ Recursive call must execute completely before control
goes back to previous call
▪ Execution in previous call resumes from point
immediately following recursive call
13

13

static int length ("ABCD")


{
if ("ABCD".equals("")
return 0;
return 1 + length("ABCD".substring(1));
} static int length ("BCD")
{
if ("BCD".equals("")
return 0;
3 return 1 + length("BCD".substring(1));
4 }

2
int a = length("ABCD") static int length ("CD")
{
System.out.println(a); if ("CD".equals("")
return 0;
return 1 + length("CD".substring(1));
}

static int length ("D")


static int length ("")
1
{
{ if ("D".equals("")
if ("".equals("") return 0;
return 0; 0 return 1 + length("D".substring(1));
return 1 + length(s.substring(1)); }
}

14

14

You might also like