20

Click here to load reader

Data Structures - 11. Recursion, I

Embed Size (px)

DESCRIPTION

Slide da cadeira de Estrutura de Dados, ministrado pelo Prof. Dr. Christian Pagot, na Universidade Federal da Paraíba.

Citation preview

Page 1: Data Structures - 11. Recursion, I

Universidade Federal da ParaíbaCentro de Informática

Recursion ILecture 14

1107186 – Estrutura de Dados – Turma 02

Prof. Christian Azambuja PagotCI / UFPB

Page 2: Data Structures - 11. Recursion, I

2Universidade Federal da ParaíbaCentro de Informática

Factorial

● The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

● Examples:– 2! = 2 x 1 = 2

– 3! = 3 x 2 x 1 = 6

– 5! = 5 x 4 x 3 x 2 x 1 = 120

– 0! = 1

Page 3: Data Structures - 11. Recursion, I

3Universidade Federal da ParaíbaCentro de Informática

Computing the Factorial Iteratively

● Straightforward iterative approach:

unsigned int Factorial(unsigned int n){

unsigned int i;unsigned int factorial = 1;

for (i=1; i<=n; i++)factorial = factorial * i;

return factorial;}

C code excerpt:

Page 4: Data Structures - 11. Recursion, I

4Universidade Federal da ParaíbaCentro de Informática

Recursion

● It can be the case that the solution of a given problem depends on the solution of smaller, but related, subproblems.

● In these cases, algorithms might break the original problem into smaller subproblems, solving these subproblems recursively and combining those solutions in order to solve the original problem.

Page 5: Data Structures - 11. Recursion, I

5Universidade Federal da ParaíbaCentro de Informática

● Definition:– n! = n x … x 2 x 1

● Example:– 5! = 5 x 4 x 3 x 2 x 1

– 5! = 5 x 4 x 3 x 2 x 1!

– 5! = 5 x 4 x 3 x 2!

– 5! = 5 x 4 x 3!

– 5! = 5 x 4!

Back to the Factorial...

Factorial can be defined recursively:

n! = n x (n - 1)!

Page 6: Data Structures - 11. Recursion, I

6Universidade Federal da ParaíbaCentro de Informática

Computing the Factorial Recursively

● Recursive approach:

unsigned int Factorial(unsigned int n){

if (n == 0)return 1;

elsereturn n * Factorial(n­1);

}

C code excerpt:

Page 7: Data Structures - 11. Recursion, I

7Universidade Federal da ParaíbaCentro de Informática

Computing the Factorial Recursively

● How it works?

unsigned int Factorial(unsigned int n){

if (n == 0)return 1;

elsereturn n * Factorial(n­1);

}

... 

unsigned int X = Factorial(5);

C code excerpt:Factorial(5) : 5 * Factorial(4)

Factorial(4) : 4 * Factorial(3)

Factorial(3) : 3 * Factorial(2)

Factorial(2) : 2 * Factorial(1)

Factorial(1) : 1 * Factorial(0)

Factorial(0) : 1

Page 8: Data Structures - 11. Recursion, I

8Universidade Federal da ParaíbaCentro de Informática

Computing the Factorial Recursively

● How it works?

unsigned int Factorial(unsigned int n){

if (n == 0)return 1;

elsereturn n * Factorial(n­1);

}

... 

unsigned int X = Factorial(5);

C code excerpt:Factorial(5) : 5 * Factorial(4)

Factorial(4) : 4 * Factorial(3)

Factorial(3) : 3 * Factorial(2)

Factorial(2) : 2 * Factorial(1)

Factorial(1) : 1

Page 9: Data Structures - 11. Recursion, I

9Universidade Federal da ParaíbaCentro de Informática

Computing the Factorial Recursively

● How it works?

unsigned int Factorial(unsigned int n){

if (n == 0)return 1;

elsereturn n * Factorial(n­1);

}

... 

unsigned int X = Factorial(5);

C code excerpt:Factorial(5) : 5 * Factorial(4)

Factorial(4) : 4 * Factorial(3)

Factorial(3) : 3 * Factorial(2)

Factorial(2) : 2

Page 10: Data Structures - 11. Recursion, I

10Universidade Federal da ParaíbaCentro de Informática

Computing the Factorial Recursively

● How it works?

unsigned int Factorial(unsigned int n){

if (n == 0)return 1;

elsereturn n * Factorial(n­1);

}

... 

unsigned int X = Factorial(5);

C code excerpt:Factorial(5) : 5 * Factorial(4)

Factorial(4) : 4 * Factorial(3)

Factorial(3) : 6

Page 11: Data Structures - 11. Recursion, I

11Universidade Federal da ParaíbaCentro de Informática

Computing the Factorial Recursively

● How it works?

unsigned int Factorial(unsigned int n){

if (n == 0)return 1;

elsereturn n * Factorial(n­1);

}

... 

unsigned int X = Factorial(5);

C code excerpt:Factorial(5) : 5 * Factorial(4)

Factorial(4) : 24

Page 12: Data Structures - 11. Recursion, I

12Universidade Federal da ParaíbaCentro de Informática

Computing the Factorial Recursively

● How it works?

unsigned int Factorial(unsigned int n){

if (n == 0)return 1;

elsereturn n * Factorial(n­1);

}

... 

unsigned int X = Factorial(5);

C code excerpt:Factorial(5) : 120

Page 13: Data Structures - 11. Recursion, I

13Universidade Federal da ParaíbaCentro de Informática

Tail Recursion

● Tail call is when the last action of a subroutine is the call of another subroutine.

● If the tail call refers to the current active subroutine, then it is called tail recursion.

● Tail recursions can be turned into iterative code, which avoids the costs related to subroutine calls (pushes) and returns (pops).

Page 14: Data Structures - 11. Recursion, I

14Universidade Federal da ParaíbaCentro de Informática

Process Memory Layout

● Approximated process memory layout (in the virtual memory space):

Code segment

Data

.bss

Heap

Stack

Instructions.

Initialized global, const and static vars.

Uninitialized vars.

Dynamic memoryallocation

Local vars, return Addresses, etc.

Process

Addr: 0x00000000

Addr: 0x0000FFFF

Page 15: Data Structures - 11. Recursion, I

15Universidade Federal da ParaíbaCentro de Informática

Factorial Revisited

unsigned int Factorial(unsigned int n) {return Factorial1(n, 1);

}unsigned int Factorial1(unsigned int n, unsigned int acc) {

if (n == 0)return acc;

return Factorial1(n ­ 1, n * acc);}

C code excerpt (tail recursion):

http://c2.com/cgi/wiki?TailRecursion

unsigned int Factorial(unsigned int n){

if (n == 0)return 1;

elsereturn n * Factorial(n­1);

}

C code excerpt (recursion):

Page 16: Data Structures - 11. Recursion, I

16Universidade Federal da ParaíbaCentro de Informática

Factorial Revisited

unsigned int Factorial(unsigned int n) {return Factorial1(n, 1);

}unsigned int Factorial1(unsigned int n, unsigned int acc) {

if (n == 0)return acc;

return Factorial1(n ­ 1, n * acc);}

C code excerpt (tail recursion):

http://c2.com/cgi/wiki?TailRecursion

unsigned int Factorial1(unsigned int n, unsigned int acc) {begin:

if (n == 0) return acc;else {

acc = n * acc;n = n – 1;goto begin;

}}

C code excerpt (iterative):

Page 17: Data Structures - 11. Recursion, I

17Universidade Federal da ParaíbaCentro de Informática

Binary Search

● Suppose we want to search for an integer key k in a sorted vector of keys.

● One approach is to look for the key k using linear search. However, this search is O(n).

● Since the keys are ordered, a better approach is the binary search, which is O(log n).

Page 18: Data Structures - 11. Recursion, I

18Universidade Federal da ParaíbaCentro de Informática

Binary Search

● Algorithm:0 1 2 3 4 5 6 7 8 9

1 3 4 5 7 8 10 11 14 25v = k = 5?

low high

1

mid = (int) (low + high) / 2

mid

Page 19: Data Structures - 11. Recursion, I

19Universidade Federal da ParaíbaCentro de Informática

Binary Search

● Algorithm:0 1 2 3 4 5 6 7 8 9

1 3 4 5 7 8 10 11 14 25

0 1 2 3

1 3 4 5

2 3

4 5

3

5

low high

low high

low = mid high

low = mid = high

mid

mid

v = 1

2

3

4

k = 5?

mid = (int) (low + high) / 2high = mid (prev) - 1

low = mid (prev) + 1

low = mid (prev) + 1

Page 20: Data Structures - 11. Recursion, I

20Universidade Federal da ParaíbaCentro de Informática

Recursive Binary Search

int BinarySearch(int* v, int k, int low, int high){

int mid;

if (low > high)return ­1;

mid = (low + high) / 2;

if (k == v[mid])return mid;

if (k < v[mid])BinarySearch(v, k, low, mid ­ 1);

elseBinarySearch(v, k, mid + 1, high);

}

C code excerpt (tail recursion):