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

Data Structure and Algorithms

The document discusses recursive algorithms and procedures. It begins with basic concepts of recursion including recursive definitions and properties. It then provides examples of recursive algorithms for searching, calculating Fibonacci series and finding minimal elements. The structure and operation of recursive procedures are explained, noting they involve recursive calls and base/recursive branches. Recursive calls are processed in two stages - recursive calls stacking intermediate results, then backtracking to process these results.

Uploaded by

ifire
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Data Structure and Algorithms

The document discusses recursive algorithms and procedures. It begins with basic concepts of recursion including recursive definitions and properties. It then provides examples of recursive algorithms for searching, calculating Fibonacci series and finding minimal elements. The structure and operation of recursive procedures are explained, noting they involve recursive calls and base/recursive branches. Recursive calls are processed in two stages - recursive calls stacking intermediate results, then backtracking to process these results.

Uploaded by

ifire
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Data structure and Algorithms

Recursive algorithms
Thanh-Hai Tran

Electronics and Computer Engineering


School of Electronics and Telecommunications
Hanoi University of Science and Technology
1 Dai Co Viet - Hanoi - Vietnam
Outline

 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

 Examples of recursive algorithms


 Searching in linked lists
 Problem of Hanoi tower (Bài toán Tháp Hà Nội)
 Problem of 8 queens (Bài toán 8 con hậu)

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

 Recursive algorithm (Giải thuật đệ quy)


 Exp 1: Searching for a given word w in a dictionary T
 Main idea of algorithm:
 T is partitioned into 2 equal sub-dictionaries T1 and T2.
 Searching for w in T1 and T2
 The process continues until the final sub-dictionary has only
one page, and the search can be done directly.

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)

P P1 P2 … Pn-1 Pn điểm dừng

2020 Backtracking (Quay lui) 8


Recursive algorithms

 Exp 3: Find the minimal element of a given series with N


elements a1, a2, …, aN
 If N=1 then min=a1;
 Else the series is divided into 2 sub-series:
L1 = a1, a2,…, am and L2 = am+1, am+2,…, aN
with m = (1+N) DIV 2.
Find min1 in L1 and min2 in L2.
Compare min1 and min2 to find the minimal.

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

 Examples int Fact (int n){


 Exp 6: Calculation of n! if (n <= 1) return 1;
 0! = 1 else return n * Fact (n-1);
 n! = n (n-1)! }

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

 Structure of recursive procedure/function: In C/C++, a


recursive function has the form:

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.

 Indirect recursive call (Gọi gián tiếp):


Example:

void A(n) { void B(m){


... ...
B(p); A(k);
... ...
} }

2020 14
Operation of recursive procedure

int Fact (int n){


if (n <= 1) return 1;
else return n * Fact (n-1);
}

void main(){
int n = 3;
int f = Fact(n);//operating process starts here

2020 15
Operation of recursive procedure

 Running process of a RP includes 2 stages:


 Recursive call stage (Giai đoạn gọi đệ quy):
 Starting from the first call, the recursive branch will be repeated
until the base branch arrives. In this stage, many intermediate
results generated by intermediate calls need to be saved,
because they will be used in the next stage.
 The next stage (backtracking) will be triggered automatically.
 Backtracking stage (Giai đoạn quay lui):
 In this stage, all intermediate generated calls will be run one by
one, and in reversed order.
 Problem: how intermediate calls and results can be saved
in fisrt stage (recursive call) to be processed in the second
stage (backtracking)?

2020 16
Operation of recursive procedure

 Implementation method: The saving and processing of


recursive calls in 2 operating stages have following
features:
 Last In, First Out (LIFO):
 The processing order of recursive calls in second stage is
reversed to the order in first stage. Therefore LIFO structure
(stack) is a suitable choice.
 Homogeneity (Tính đồng nhất):
 Structure of recursive calls are the same, so homogenous stack
can be used.
 In reality, most programming languages (including
C/C++) have already supported/implemented recursive
calls and procedures.

2020 17
Operation of recursive procedure

(1) Initialize Stack: S = empty;


(2) Recursive calls stage (Giai đoạn gọi đệ quy):
• In this stage, intermediate recursive calls and results will be pushed
into stack.
(3) Backtracking stage (Giai đoạn quay lui)
• Recursive calls and results saved in the stack will be popped to be
processed until the stack is empty.

2020 18
Operation of recursive procedure

 Exp: operation of recursive function Fact(n) with n=3


(calculation of 3!).
Recursive algorithm (recall)
3! = 3 x 2!
Recursive 2! = 2 x 1! Backtracking
induction 1! = 1 x 0!
0! = 1: base case

Operation of recursive function Fact()


Call 3!: Fact(3); Return F3 (6)

(1)
F1=1*Fact(0);
(2) F2=2*Fact(1); (3)
F3=3*Fact(2);

Stack is empty Stack Stack is empty


2020 19
Recursive vs Non-recursive procedures

 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:

int Fact (int n){ int Fact (int n){


if (n <= 1) return 1; if (n <= 1) return 1;
else return n * Fact (n-1); else {
} int x=1;
for (int i=2;i<=n;i++)
x*=i;
return x;
}
}

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.

PNode Search (Item k, PNode H) {


if (H == NULL) return NULL;
else
if (H->info == k) return H;
else return Search (k; H->next) ;
}

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)

 For example with 3 disks:


 Move 2 top disks from A to C: A  B, A  C, B  C

A C B

 Move 1 bottom disk from A to B: A  B

A B C

 Move 2 disks from C to B: C  A, C  B, A  B

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

void TowerHN (int n, Tower A, B, C){


if (n == 1) Transfer (A, B);
else {
TowerHN (n-1, A, C, B);
TowerHN (1, A, B, C);
TowerHN (n-1, C, B, A);
}
}

2020 27
Backtracking Algorithms

 Ideas of backtracking algorithm (Ý tưởng giải thuật)


 Implementation methods (Cách cài đặt)

2020 28
Backtracking Algorithms

 Ideas of backtracking algorithm:


 Problem: given a set of N variables x1, x2,…, xN with N
corresponding value domains D1, D2,…, DN. Find a set of N
variables with suitable values that satisfies given condition K.
 Ideas of Backtracking Algorithm:
 Set vector Vi = <x1,x2,…,xi>, with i=1..N.
 We need to find vector VN satisfying condition K, with xiDi and
i=1..N.
 The algorithm includes N steps, each one aims to find one
suitable variable in vector V.

2020 29
Ideas of backtracking algorithm

Recursive algorithm for backtracking as follows:


 Suppose that first i steps have been done, it means that
we found Vi=<x1, x2,…, xi> satisfies K. If i=N the
algorithm finishes, it returns VN. Otherwise, next step
continues with two exclusive cases:
 If found suitable variable, means that  p  Di+1 so that with
xi+1=p then Vi+1 satisfies K, set xi+1=p. It repeats for next
step (i+1) (recursive induction).
 Otherwise, means that p  Di+1 we all have Vi+1 not
satisfied K. We have to backtrack to step i to try next suitable
value of xi (current xi is suitable). If not found and i=1, the
algorithm terminates without any suitable vector.

2020 30
Example of backtracking

 Problem of 8 queens (Bài toán 8 con hậu)


Find an arrangement of 8 queens on chessboard so that not any
two queens check each other. This problem can be generalized
for N queens.
 One of 92 solutions:

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

 Implementation methods: there are 2 methods:


 Method 1: Using recursive procedures (thủ tục đệ quy)
 Method 2: Using non-recursive procedures (thủ tục không đệ
quy)

2020 34
Backtracking Algorithms
Implementation methods
 Method 1: Using recursive procedures

void RBacktrack(vector V, int step)


{
if (step == N) {
V is a solution;
}
else
while (exists xi in Di and V+{xi} satisfies K)
{
Di = Di \ {xi} ;
V = V+ {xi};
RBacktrack(V, step+1);
V = V \ {xi}; //prepare before backtrack
}
}

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

1. Find recursive algorithm and function that sum up a


series of N numbers.
2. Find recursive algorithm and function that look for
minimal element in a series of N numbers.
3. Write a recursive function for insertion sort algorithm.
4. (Subset sum problem): Given a series A of N numbers
and a number K, is there a group of numbers of A that
sums exactly to K?

2020 37
References

 Recursive algorithm – slide – Nguyen Thanh Binh –


Data structure and Algorithm

2020 38

You might also like