RECURSION
1
Contents
1- Introduction to recursion.
2- Describing a recursive operation.
3- Implementing a recursive method.
4- Classifying recursive functions
5- How to manage running functions?
6- Anatomy of a Recursive Call
7- Evaluating performance of recursive
methods.
8- Eliminating recursion
9- Backtracking
2
Learning Outcomes
LO3.1 Describe the basic ideas of recursion and how to set up
recursive systems that represent certain real-world phenomena.
LO3.2 Know how to develop recursive algorithms and programs
LO3.3 Write programs in Java using recursion to solve some
problems, like creating the Fibonacci sequence.
LO3.4 Analyze a recursive function to find out its’ output without
running.
LO3.5 Explain type of recursive functions, give examples and
comparing them.
LO3.6 Compare recursion with iteration, analyze their pros and
cons
3
1- Introduction to recursion
Recursion is a technique in which a concept/an operation is defined by itself ( đệ:
đưa ra, quy: quay về).
Examples
Person = a child of other two persons
n! = n* (n-1)! ( factorial)
F(n) = F(n-1) + F(n-2) // Fibonacci sequence
a(n) = a(n-1)+ d: Arithmetic progression – CS cộng
b(n) = q*b(n-1) // geometric progression- CS nhân
public static int fibo (int n){
if (n<3) return 1;
return fibo(n-1) + fibo(n-2);
}
4
1- Introduction to recursion…
How to understand Recursion?
We use a reverse deduction (suy diễn ngược) with an initial data.
Examples:
Person = a child of other two persons
Initial persons: Adam, Eve
1 3 5 7 9 …..
A(n) = 1, n=1
A recursive method
A(n) = A(n-1) + 2, n> 1 public static int fibo ( int n){
1, 1, 2, 3, 5, 8, 13, … if (n<3) return 1;
F(n) = 1, n<3 return fibo(n-1) + fibo(n-2);
}
F(n) = F(n-1) + F(n-2)
5
1- Introduction to recursion…
Where recursion are used? Examine the Fibonacci
sequence:
1- Recursion is used to generate new n 1 2 3 4
F(n) 1 1 2 3
elements of a group.
F(10) =?
2- Recursion is used to test whether a value
belonging a group or not. Test whether 34 is an element of the
Fibo sequence or not?
A way to express a loop when number of
executions is not known in advance.
F(n) = 1, n<=2 public int fiboRec( int n) { public int fiboLoop ( int n) {
= F(n-2)+F(n-1), n>2 if (n<3) return 1; int result =1, t1 =1, t2=1;
return fiboRec(n-2) + fiboRec(n-1); for (int i=3; i<=n; i++) {
result = t1 + t2;
} t1 = t2;
t2= result;
}
return result;
}
6
2- Describing a recursive operation.
A recursive definition consists of two parts:
- The anchor or ground case or base case, the basic
elements that are the building blocks of all other elements
of the set
- Rules that allow for the construction of new objects out
of basic elements or objects that have already been
constructed
7
2- Describing a recursive operation…
Practice the skill of recursive expression of numeric sequences
-Use the Divide and conquer principle– chia để trị
- Write numbers in a line: d1, d2, d3, d4, d5, ………, dn
- Some beginning values can be anchors.
- Find out relationship next values with previous values
Deduction
Example: Examine the Fibonacci sequence:
1 1 2 3 5 8 13 21 34
Anchors: 1, n<=2
Deductive rule: F(n) = F(n-2) + F(n-1), n>2
8
2- Describing a recursive operation…
Practice the skill of recursive expression of operations
- With recursive methods, there may be more difficulties.
- Use the Divide and conquer principle
- Write the operation in details
- Right side may be anchor
- Left side is the same operation with smaller input
Example: Calculate n!
n! = 1.2.3.4.5.6.…(n-1).n
= (n-1)!n = n.(n-1)!
Anchors: 1, n<2
Deductive rule: n! = n.(n-1)!
9
2- Describing a recursive operation…
Practice the skill of recursive operation on an array
Example: Sum of an array, named a, having n elements
Sum(a,n) = a[0] + a[1] + … + a[n-2] + a[n-1]
Sum(a, n-1)
Return 0,n=0
Return a[n-1] + Sum (a, n-1);
10
3- Implementing a recursive method Demo 1
Pseudo-code
n! = 1, n<2
= n* (n-1)!, n>=2
2 lines
// Other recursive method
// Other test
Để dễ tiếp thu bài học, xin đừng thắc mắc “Hàm đệ quy chạy như
thế nào?”. Câu hỏi này sẽ được trả lời ở phần sau của chương. Ở
đây, chúng ta chú tâm vào cách suy nghĩ đệ quy, diễn đạt đệ quy
và viết hàm đệ quy cho quen đã nhé.
11
3- Implementing a recursive method… Demo 2
Recursive
definitions serve
two purposes:
(1) Generating
new elements
(2) Testing
whether an
element
belongs to a
set
Demo: The
Fibonacci
sequence
12
3- Implementing a recursive method… Demo 3
1.5 3.5 5.5 7.5 9.5 11.5
-Compute the nth item of an arithmetic progression having the
first item a and common difference d:
ap(n, a, d) = a, n=1
= ap(n-1, a, d) + d, n>1
public static double ap( int n, double a, double d) {
<Code yourself>
}
// Test 1.5 3.5 5.5 7.5 9.5 11.5
System.out.println( ap(6, 1.5, 2 )); // 11.5
13
3- Implementing a recursive method… Demo 4
Compute the nth item of a geometric progression having the first
item a and common multiplier q:
Gp (n, a, q) = a , n=1
= Gp(n-1, a, q) * q, n>1
public static double gp( int n, double a, double q) {
<Code yourself>
}
// Test 1.5 3 6 12 24 48
System.out.println( gp(6, 1.5, 2)); // 48
14
3- Implementing a recursive method… Demo 5
Calculate sum of integral array having n elements
Sum(a,n) = 0 + a[0] + a[1] + …….. + a[n-2] + a[n-1]
Sum(a, n) = 0, n=0;
= Sum(a, n-1) + a[n-1], n>1
public static double sum( double[] a, int n) {
<Code yourself>
}
// Test :
double a[] = { 1.5, 2, 4, 5, 2, 6.5 };
System.out.println( sum(a, 6)); // 21
15
3- Implementing a recursive method… Demo 6
Calculate the maximum value in an integral array having n elements
1 5 9 7 2 10 19
max(a, n) := if (n==1) return a[0]
int m= max(a, n-1) // 10
return m>a[n-1]? m : a[n-1]
public static double max( int[] a, int n) {
<Code yourself>
}
// Test :
int b[] = { 1, 5, 9, 7, 2, 19,10 };
System.out.println( max(b, 7)); // 19
16
3- Implementing a recursive method… Demo 7
Calculate the minimum value in an integral array having n
elements
min (a,n) = (Do yourself)
public static double min( int[] a, int n) {
<Code yourself>
}
// Test : int b[] = { 1, 5, 9, 7, 2, 19,10 };
System.out.println( min(b, 7)); // 1
17
3- Implementing a recursive method… Demo 8
Convert a decimal integer to a b-based numeric string:
n= 35, base=2 100011
“100011” = “10001” + “1”
convert(35,2) = convert(17,2) + digit of 35%2
convert(n,base) = convert(n/base,base) + digit of n%base
Anchor: If n==0 return “0”
18
4- Classifying recursive function
Aspect Types
Position where a recursive call is Tail/ non-tail | Head/ non-head
put recursion
Number of recursive calls Linear/ non-linear recursion
Recursive call is easily detected Direct/ indirect recursion
19
4- Classifying recursive functions
Tail recursion is characterized by the use of only one recursive call at
the very end of a method implementation.
The last operation is a multiplication.
It is not a tail recursion
20
4- Classifying recursive functions…
Based on number of recursive calls: Linear, non-linear recursion.
int factorial ( int n) { int fibo ( int n) {
if (n==1) return 1; if (n<3) return 1;
return n*factorial(n-1); return fibo(n-2) + fibo(n-1);
} }
Two recursice calls
Binary recursion
21
4- Classifying recursive functions…
receive(buffer)
Based on
while buffer is not filled up
number of if information is still incoming
functions get a character and store it in buffer;
included in else exit();
recursive call: decode(buffer);
Indirect
recursion. decode(buffer)
decode information in buffer;
Direct store(buffer);
recursion?
Recursice call is store(buffer)
put in the body transfer information from buffer to file;
receive(buffer);
of itself
22
5- How to manage running functions?
How to manage a thing? We need its data.
How to manage a running method? … ?
Data of a method:
Its parameters (data type, value)
Its return data type This data group is
called as method’s
Its extra local variables Activation
Return address in its caller record:
Dynamic link: Address of its caller methods
activation record
23
5- How to manage running functions?...
Activation record in details:
Activation records contain the following:
- Values for all parameters to the method, location of the first cell
if an array is passed or a variable is passed by reference, and
copies of all other data items
- Local (automatic) variables that can be stored elsewhere.
- The return address to resume control by the caller, the address of
the caller’s instruction immediately following the call.
- A dynamic link, which is a pointer to the caller’s activation
record
- The returned value for a method not declared as void
24
A demo.
5- How to manage running functions?...
Parameters and Local
variables ( n ) Activation
Dynamic link (4000) record of
Return Address ( = )
= 5000 Return value ( int )
f3(…)
Parameters and Local
variables ( n, x ) Activation
Dynamic link (3000) record of
+ Return Address ( + )
Return value ( int )
f2(…)
4000
Parameters and Local
variables (n, k) Activation
Dynamic link (2000)
= Return Address ( = )
record of
f1(…)
3000 Return value ( int )
Parameters and Local
Figure 5-1 Contents of variables (args, result) Activation
Dynamic link (1000) record of
the run-time stack when Return Address ( null )
main() calls method main(…)
Return value (void)
2000
f1(), f1() calls f2(),
and f2() calls f3()
1000
Constants
All methods use Dynamic links to access constants.
25
6- Anatomy of a Recursive Call
AR of
factorial(1)
AR of AR of AR of
factorial(2) factorial(2) factorial(2)
AR of AR of AR of AR of AR of
factorial(3) factorial(3) factorial(3) factorial(3) factorial(3)
AR of main AR of main AR of main AR of main AR of main AR of main AR of main
26
7- Evaluating performance of recursive methods.
Disadvantages of Recursive Call:
-So many ARs need to be allocated.
High memory cost
-So many method calls
High time cost
AR of
factorial(1)
AR of AR of AR of
factorial(2) factorial(2) factorial(2)
AR of AR of AR of AR of AR of
factorial(3) factorial(3) factorial(3) factorial(3) factorial(3)
AR of main AR of main AR of main AR of main AR of main AR of main AR of main
27
7- Evaluating performance of recursive methods…
Disadvantages of Recursive Call:
-So many ARs need to be allocated High memory cost
-So many method calls High time cost
double fact1(int n) {
if (n<2) return 1; fact1(100); // 100 activation records
return n*fact1(n-1);
}
double fact2 (int n) {
double result =1; fact2(100); // 1 activation record
for (int i=2; i<=n; i++)
result *= i;
return result;
}
28
7- Evaluating performance of recursive methods…
Disadvantages of Recursive Call
So many ARs need to be allocated High memory cost
-So many method calls. High time cost
One operation
must be
performed
many times.
Figure 5-8 The tree of calls for Fib(6)
29
7- Evaluating performance of recursive methods…
Figure 5-9: Number of addition operations and number of recursive
calls to calculate Fibonacci numbers
30
Recursive Call: High
memory cost + High
8- Eliminating tail recursion time cost
If you can, you should eliminate recursive methods?
How? Use loop statement + Stack for storing data.
An example:
Recursive version
Loop version, no additive memory is needed.
31
Demo 9:
8- Eliminating tail recursion…
Convert an integer to
string numbers.
32
8- Eliminating tail recursion…
33
9- Backtracking – Hồi quy
Backtracking is a technique for returning to a given position (e.g.,
entry point) after trying other avenues that are unsuccessful in solving
a particular problem.
Backtracking is a technique for finding solutions of a multiple-
variable problem.
One-Step Backtracking: The simplest and common-use
backtracking. If there is no suitable value which can be assigned to
the ith variable, backtrack to the (i-1)th variable.
K-step Backtracking: If there is no suitable value which can be
assigned to the ith variable, backtrack to the (i-k)th variable.
Recursion: Tính toán lùi về tình huống chặn rồi tiến tới sau
Backtracking: Đệ quy tính toán tới nếu bí thì lùi.
Backtracking is an approach for finding all solutions of a problem
(greedy algorithm ). 34
9- Backtracking…
Example: One-step backtracking in C.
2 loại 2 vòng lặp lồng nhau
Với n loại bất kỳ thì làm sao để giải?
Đệ quy (vét cạn/hồi quy)
35
9- Backtracking…
Definitions:
Problem: a set of variables (V), each variable has a distinct domain (D, set of
domains).
Problem’s Constraints: Conditions (C) on values of variables.
A solution: set of specific values of variables (configuration) which satisfy the
problem’s constraints.
Example 1: Vừa gà vừa chó bó đủ trăm chân. Hỏi có bao nhiêu gà bao nhiêu chó:
V: 2 variables, 2 domains:
sốGà: [1, (100-4)/2] , ít nhất có 1 con chó
sốChó: [ 1, (100-2)/4], ít nhất có 1 con gà
C: sốGà * 2 + sốChó *4 =100.
36
9- Backtracking…
Example 2: Bài toán sinh chuỗi 4 bít
V: 4 biến: [0,1]
C: không có điều kiện cần kiểm tra
Example 3: Bài toán trâu
Trăm trâu trăm cỏ, con nhỏ ăn một, con lớn ăn hai, con già hai con một bó. Hỏi
số trâu trong mỗi loại.
V: 3 variables, 3 domains.
sốNhỏ: [0, 100] Tối đa có 100 bó cỏ và 100 con trâu
sốLớn: [0, 50] Tối đa có 100 bó cỏ và trâu lớn ăn 2 bó
số Già: [0, 100] Tối đa có 100 con trâu
C: sốGià là số chẵn AND
sốNhỏ + sốLớn + số Già = 100 AND
sốNhỏ * 1+ sốLớn * 2+ sốGià/2 = 100
37
9- Backtracking…
Example 4:
A customer likes to buy 3 TVs, 2 refrigerators, 1 fan with budget less than or
equals 30,000,000$ (budget)
V: 6 variables, 3 domains.
v0: { TVs }, v1: { TVs }, v2: { TVs }
v3: { Refs }, v4: { Refs }, v5: { Fans }
C: v0.price + v1.price + v2.price +
v3.price + v4.price + v5.price <= budget
Backtracking implementations:
(1) Using recursive approach
(2) Using loop approach. A generator which will generate values
(CONFIGURATION) for checking constraints.
38
9- Backtracking…
Procedure backtrack (index, V, D, C, solutions)
Input: index, index of current variable At a time, a variable
V: Set of variables
D: Set of domains of variables respectively
is examined to assign
C: set of constraints a value to it
Output: solutions
V0 V1 … Vindex Vindex+1 … Vn-1
For each value in Dindex {
vindex = value;
if (index = n-1) { //the last variable was assigned
if (new configuration satisfied C) // new solution is detected
Add new configuration to solutions
}
else backtrack (index+1, V, D, C, solutions) // One-step backtracking
}
Procedure backtrack (V, D, C) { Recursive implementation:
solutions = empty set; Memory cost? Time cost?
backtrack (0, V, D, C, solutions);
return solutions:
}
39
Demo 10: The Eight-
9- Backtracking… Queen Problem
With size=8, 64 cells
No. of queens = 8
Complexity: 648
(64.63.62.61.60,59.58.57)
We need a very very long
time to find all solutions of the
problem using greedy algorithm
(our generator)
We should use recursive
backtracking approach and stop
immediately when a solution is
detected.
40
Demo 10: The Eight-
9- Backtracking… Queen Problem
On the same row dx=0
On the same column dy=0
On the diagonal dx=dy
41
Demo 10: The Eight-
9- Backtracking… Queen Problem
Q0 Q1 Q2 Q… Qindex
42
Demo 10: The Eight-
9- Backtracking… Queen Problem
43
Demo 10: The Eight-
9- Backtracking… Queen Problem
44
Backtracking Impl.:
Generator Approach
9- Backtracking… Generator in the Product
consulting Problem
tvDom tvDom tvDom rfDom rfDom fanDom
0 TV1 TV1 TV1 RF1 RF1 F1
1 TV2 TV2 TV2 RF2 RF2 F2
2 TV3 TV3 TV3 RF3 RF3 F3
3 TV4 TV4 TV4 Get a
RF4 RF4 suggestion
4 TV5 TV5 TV5 RF5 RF5
5 TV6 TV6 TV6
iDom0 iDom1 iDom2 iDom3 iDom4 iDom5
0, 5 0, 5 0, 5 0, 4 0, 4 0, 2 IntMinMaxSet
Backtrack
0 0 1 3 2 0
Generator
000000 0 0 1 3 2 0 Current Configuration
000001
000002 Configuration is a set of specific generated values.
000010
….. 0 0 0 0 0 0 First configuration
555442
5 5 5 4 4 2 Last configuration
45
9- Backtracking… Demo 10: The Eight-
Queen Problem
Procedure Solve-Problem (D0, D1, …, Dn, C)
Input: Set of domains, C: problem condition
Output: Solutions
Begin
Solutions = empty set;
Setup generator from domains;
suggestion = The first configuration of the generator;
while still having a suggestion {
if the suggestion meets C the add the suggestion to solutions;
suggestion = next configuration of the generator;
}
return solutions;
End
46
9- Backtracking…
Backtracking: Complexity
• N: average quantity of each domain. There are n options which can
be used to assign a value to each variable
• |V|: number of variables ( a configuration has V values)
Product principal Complexity of backtracking algorithm: N|V|
Hard problem
N-1 N-1 N-1 N-1 … N-1
… … … … …
0 0 0 0 0
var0 var1 var2 … var|V|-1
47
9- Backtracking… Demo 11,12,13,14
Backtracking Implementation: Generator Approach
Backtracking
generator
Demo 14
Demo 11
Demo 12
Demo 13
48
9- Backtracking…
49
9- Backtracking…
50
9- Backtracking…
51
9- Backtracking…
52
9- Backtracking…
53
9- Backtracking…
54
9- Backtracking…
One-step backtracking
55
9- Backtracking…
Using Backtrack
generator in some
specific problems
56
9- Backtracking…
Using Backtracking
Generator
57
9- Backtracking…
Using Backtracking Generator
58
9- Backtracking…
Using Backtracking Generator
59
9- Backtracking…
Using Backtracking Generator
60
9- Backtracking…
Using Backtracking Generator
61
9- Backtracking…
Using Backtracking Generator
62
9- Backtracking…
Using Backtracking Generator
63
9- Backtracking…
Using Backtracking Generator
64
9- Backtracking…
Using Backtracking Generator
65
9- Backtracking…
Using Backtracking Generator
66
9- Backtracking…
Using Backtracking Generator
Situation:
A customer likes to
buy 3 TVs, 2
refrigerators, 1 fan
with maximum budget
of 30,000,000$
67
9- Backtracking…
Using Backtracking Generator
68
9- Backtracking…
Using Backtracking Generator
69
9- Backtracking…
Using Backtracking Generator
70
9- Backtracking…
Using Backtracking Generator
71
9- Backtracking…
Using Backtracking Generator
72
9- Backtracking…
Using Backtracking Generator
73
9- Backtracking…
Using Backtracking Generator
74
9- Backtracking…
Using Backtracking Generator
75
9- Backtracking…
Using Backtracking Generator
76
9- Backtracking…
Using
Backtracking
Generator
77
9- Backtracking…
Steps to use backtracking Generator:
From the problem:
1- Determine related real domains.
2- Create a generator
3- Add domains to the generator in suitable order.
4- Initiate the generator (make the generator ready)
5- Get a suggestion from the first configuration of the generator
6- do {
if (suggestion is accepted) Add suggestion to solution set
suggestion = get the next configuration of the generator
} while(still having a suggestion)
78
Recursion Review
LO3.1thatDescribe the basic ideas of recursion and how to set up recursive systems
represent certain real-world phenomena.
LO3.2 Know how to develop recursive algorithms and programs
LO3.3creating
Write programs in Java using recursion to solve some problems, like
the Fibonacci sequence.
LO3.4 Analyse a recursive function to find out its’ ouput without running.
LO3.5 Explain type of recursive functions, give examples and comparing them.
LO3.6 Compare recursion with iteration, analyzes their pros and cons
79
Recursion Review
Ôn tập – Viết vào vở
1- Đệ quy là gì?
2- Con người có dùng kỹ thuật đệ quy hay không? Cho hai thí dụ.
3- Làm sao con người hiểu được đệ quy?
4- Tác vụ đệ quy được mô tả như thế nào? Cho thí dụ.
5- Làm sao có được kỹ năng diễn đạt đệ quy?
6- Đệ quy được dùng để làm gì?
7- Đệ quy đuôi là gì?
8- Đệ quy đầu là gì?
9- Đệ quy tuyến tính là gì?
10- Đệ quy phi tuyến là gì?
11- Đệ quy gián tiếp là gì?
12- Đệ quy hỗ tương là gì?
13- Làm sao hệ thống có thể quản lý các hàm đang thực thi?
14- Hãy cho biết activation record của một hàm gồm có những gì cùng với ý nghĩa của từng thành phần.
15- Tại sao hàm đệ quy khi thực thi lại tốn bộ nhớ?
16- Tại sao hàm đệ quy thực thi lại chậm?
17- Khử đệ quy là gì? Tại sao lại nên khử đệ quy?
18- Khử đệ quy bằng cách nào?
19- Hồi quy là gì?
20- Kỹ thuật hồi quy có thể được dùng trong những bài toán nào? Cho ví dụ.
80
Q&A
81
82