Data Structure and Algorithms
Data Structure and Algorithms
Recursive algorithms
Thanh-Hai Tran
Basic concepts
Recursion (sự đệ quy)
Recursive algorithms (giải thuật đệ quy)
Structure of recursive algorithms
Operation of recursive algorithms
Recursive procedures (thủ tục đệ quy)
Concepts
Structure of recursive procedure
Operation
Implementation principles
Cancellation of recursion (Khử đệ quy)
Backtracking algorithms
2020 2
Outline
2020 3
Basic concept
Recursion by examples:
String: is defined as:
Rule 1: 1 char = String
Rule 2: String = 1 char + (sub) String
Natural number: a natural number N is defined as:
Rule 1: 1 is a natural number
Rule 2: x is a natural number if (x-1) is a natural number
N! = 1.2. … .N can be defined as:
Rule 1: 0! = 1
Rule 2: N! = N (N-1)!
Definition of a linear list:
Rule 1: L = empty is a linear list
Rule 2: If Ln-1 is a linear list of n-1 length, then the construt Ln =
<a, Ln-1> which means element a is before Ln-1, is also a linear
list.
2020 4
Basic concepts
Recursive definition (định nghĩa đệ quy): the way
defining an object that based on other similar (usually
smaller) objects. A recursive definition includes 2
parts:
Basic rule (Quy tắc cơ sở): also called basic case where a
special case of target object is defined directly.
Inductive rule (Quy tắc đệ quy): where the object is defined
indirectly based on other similar objects
Recursive property (tính chất đệ quy): an object has
recursive property if it can be defined recursively. It
means that the object contains other similar objects.
2020 5
Basic concepts
2020 6
Basic concepts
Recursive algorithm
Exp 2: Calculation of Fibonacci series:
F(n) = 1 for n <= 2 (F(1) = F(2) =1);
F(n) = F(n-1) + F(n-2) for n > 2;
2020 7
Basic concepts
Recursive algorithm (Giải thuật đệ quy)
Concept: Given a problem P, an algorithm for P is called
recursive if it has following form:
For solving P, we need to solve problem P1 that is similar and
smaller than P. It means that if P1 is solved, than P is also
solved.
Similarly, for solving P1, we find another similar and smaller
problem P2 needs to be solved. The process continues until we
find problem Pn that is similar and smaller Pn-1.
Pn is small enough that it can be directly solved.
After resolving Pn, we will solve all generated problems Pn-1, Pn-2,
…, P1, and finally P.
Recursive induction (suy diễn đệ quy)
2020 9
Recursive procedure
Concepts:
Recursive procedure: is a simple and effective tool (sub-
program) supported in most programming languages to
implement recursive algorithms.
In C/C++ it is called recursive function.
Recursive procedure/function: is one that contains at least
one recursive call (call to itself) in its body.
2020 10
Recursive procedures
recursive calls
Recall exp 2: Calculation of series Fibonacci
F (1) = 1 if n <= 2;
F (n) = F (n-1) + F (n-2) if n > 2;
int Fibo1 (int n) {
if (n <= 2) return 1;
else return (Fibo1 (n-1) + Fibo1 (n-2));
}
2020 11
Recursive procedures
void P(A) {
if (A==A0)
Base case
else { //recursive case
Q1();
P(A1); //recursive call
Q2();
P(A2); //recursive call
…
}
}
2020 12
Structure of recursive procedure/function:
Header of function: In header a recursive function must have
at least one parameter. Besides of normal role of input/output,
a parameter in recursive function can express the size of the
function.
Body of function: including 2 branches that corresponds to 2
cases of the algorithm:
Base branch (Nhánh cơ sở): containing necessary statements
for implementing the base case.
Recursive branch (Nhánh đệ quy): containing at least one
recursive call.
2020 13
There are 2 types of recursive calls:
Direct recursive call (calling to itself): the called function
contains the call.
2020 14
Operation of recursive procedure
void main(){
int n = 3;
int f = Fact(n);//operating process starts here
2020 15
Operation of recursive procedure
2020 16
Operation of recursive procedure
2020 17
Operation of recursive procedure
2020 18
Operation of recursive procedure
(1)
F1=1*Fact(0);
(2) F2=2*Fact(1); (3)
F3=3*Fact(2);
Recursive procedures
Advantages:
Shorter
Easier to understand
Disadvantages:
More complex
More requirement of memory
Stack overflow error may happen
2020 20
Cancellation of recursion
Concept: cancellation of recursion (sự khử đệ quy)
means that recursive algorithm is replaced by non-
recursive algorithm, or recursive calls is replaced by
other implementation.
Recursive calls are killed (cancelled)
Exp 1:
2020 21
Cancellation of recursion
Ex 2
int Fibo1(int n){ int Fibo2(int n) {
if (n <= 2) return 1; int i, f, f1, f2;
else f1 = 1 ;
return (Fibo1 (n-1) + f2 = 1 ;
Fibo1 (n-2)); if (n <= 2) f = 1;
} else
for (i = 3;i<=n;i++){
f = f1 + f2;
f1 = f2;
f2 = f;
}
return f;
}
2020 22
Examples of recursive algorithms &
procedures
Exp 1: Searching in linked lists:
Write a recursive function that search for an element k in linked list H
which points to head of the list. If found, the function returns the
pointer pointing to found element. Otherwise, it returns NULL.
2020 23
Examples of recursive algorithms &
procedures
Exp 2: Problem of Hanoi tower (Bài toán Tháp Hà Nội)
Problem: there are N disks with different sizes and 3 towers A, B and
C. At the beginning, N disks lie on each others (smaller one on top
of bigger one) at tower A. You are required to move N disks from
tower A to tower B with following conditions:
Each time only one disk can be moved
Bigger disk is never allowed to lie on top of smaller one.
Only one intermediate tower C can be used
A B C
2020 24
Hanoi tower (Bài toán Tháp Hà Nội)
Some simple cases
1 disk: A B
2 disks: A C, A B, C B
General case: movement process is follows:
1. Move N-1 disks from A to C
2. Move 1 disk from A to B
3. Move N-1 disks from C to B
2020 25
Hanoi tower (Bài toán Tháp Hà Nội)
A C B
A B C
2020 26
Hanoi tower (Bài toán Tháp Hà Nội)
Function structure:
Base case: N=1 disk: A B
Recursive case: N>1:
1. Move N-1 disks from A to C
2. Move 1 disk from A to B
3. Move N-1 disks from C to B
2020 27
Backtracking Algorithms
2020 28
Backtracking Algorithms
2020 29
Ideas of backtracking algorithm
2020 30
Example of backtracking
2020 31
Ideas of Algorithm:
Chessboard can be considered as an 2D array aNxN, (N=8 in
reality);
Suppose that N queens are: h1, h2,…, hN, and queen hi will be
placed in row i, with i=1..N.
We need to choose suitable column of each queen so that
they do not check each other.
2020 32
The algorithm consists of N steps, each one will find a
suitable position (column) for one queen as follows:
Suppose that at step i (first step i=1), i queens h1, h2,…, hi
have been placed in first i rows and this arrangement satisfies the
condition (with 1 i N). If i=N the algorithm finishes and
returns the result. Otherwise it continues with next step.
At step i+1, it finds a suitable column at row i+1 in order to put the
queen hi+1. Two cases may happen:
If a suitable column j is found, then put queen hi+1 at that position j.
Go to the next step i+1.
Otherwise if all N columns are not suitable, it must go back to step i and
try to find next suitable column (backtracking) of hi (recall that hi is
already in a suitable position). If no next suitable position exists for hi
and i=1 then it terminates without any solution.
2020 33
Backtracking Algorithms
2020 34
Backtracking Algorithms
Implementation methods
Method 1: Using recursive procedures
2020 35
Backtracking Algorithms
Implementation methods
Method 2: Using non-recursive procedures (loops)
int NBacktrack ()
{
int i = 1;
int tong = 0; //total number of solutions
vector V = ; //V is empty
do {
while (exists xi in Di and V+{xi} satisfies K)
{
Di = Di \ {xi} ;
V = V+ {xi};
if (i == N) {
V is a solution;
tong++;
}
else i++;
}
V = V \ {xi};
i-- ; //Backtrack
}
while (i >= 1);
return tong;
}
2020 36
Exercises
2020 37
References
2020 38