lecture 2 aug 28 goals: introduction to recursion examples of recursive programs

24
Lecture 2 Aug 28 goals: •Introduction to recursion •examples of recursive programs

Post on 19-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Lecture 2 Aug 28

goals:

•Introduction to recursion•examples of recursive programs

Page 2: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Recursion

Recursive program•A function that calls itself. e.g. f(x)= 2 * f(x – 1)

Recursive definition• defining a concept in terms of itself. e.g. an arithmetic expression is either a number, or is of the form X op Y where X and Y are arithmetic expressions and op is an operator.

Recursive data structure• a structure whose substructure is of the same type as the structure

Page 3: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Recursion

Recursive data structure• a structure whose substructure is of the same type as the structure

e.g. a binary tree is either a single node or a node with two children both of which are also binary trees.

Page 4: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Recursion - rules

Rule 1: Provide exit from recursion. (focus on base cases – some times, more than one base case is needed.)

Rule 2: Make sure that the successive recursive calls progress towards the base case.

Rule 3: Assume that recursive calls work.

Rule 4: Compound interest rule: Avoid redundant calls.

Page 5: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Recursion – simple examples

1) Compute n! n! = 1 x 2 … x n = (n – 1)! x n

2) Compute 1 + 2 + … + n f(n) = n + f(n – 1)

3) Compute the n-th Fibonacci number f(n) = f(n – 1) + f(n – 2)

Page 6: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Example: (from Chapter 2 of text)

Given x and n, we want to compute xn.

Obvious iterative solution: int exp(int x, int n) { int temp = x; for (int j= 1; j < n; ++j) temp*= x; return temp; }

The number of multiplications performed is n – 1.

We will now see that a recursive algorithm will provide a much faster solution.

Faster algorithm is crucial for RSA encryption algorithm.

Page 7: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Idea behind the algorithm

Rule of exponents:

xn = (x2)n/2 if n is even x * (x2)(n-1)/2 if n is odd

• base case n = 1 return x

• even n call exp(x*x , n/2) and return the result.

• odd n call exp(x*x, (n – 1)/2) and multiply the result of call by x and return the output.

Page 8: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Code for recursive exponent algorithm

int exp (int x, int n) { if (n == 1) return x; else if (n%2 == 0) return exp(x*x, n/2); else return x* exp(x*x, (n – 1)/2);}

Fact: When called to compute exp(x,100000), the iterative algorithm performs 99999 multiplications while the recursive algorithm will performs 21 multiplications.

Page 9: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Recursive algorithm for modular exponentiation

• In cryptography (RSA algorithm), the following problem arises.

Given x, n and m, compute xn (mod m).

• In addition to the recursive algorithm we described above, we need one more idea to make this computation feasible.

• In practice, x, n and m are extremely large numbers, with hundreds of digits in it.

Page 10: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Modular exponentiation algorithm int exp (int x, int n, int m) { if (n == 1) return x % m; else if (n%2 == 0) return exp((x*x) % m, n/2); else return (x* exp((x*x) % m, (n – 1)/2)) % m;}

Page 11: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

An example involving 1-d array

Given two segments of sorted arrays A and B, output the result of merging them as a single sorted array.

A B

merge(A, 2, 3, B, 9, 13) should return

1 2 2 3 4 4 5

0 1 2 3 4 5 6

merge(A, 2, 2, B, 9,10) should return the array 1 2 4

merge(A, 3, 4, B, 9, 8) should return 5 7 since the second array is empty. (high index < low index means the array is empty.)

Page 12: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

What are the base cases?

Page 13: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

What are the base cases?

• one array segment is empty.

In this case, what is the output?

Page 14: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

What are the base cases?

• one array segment is empty.

In this case, what is the output?

• The other array segment, so we just have to copy the segment to the output.

Page 15: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

What are the base cases?

• one array segment is empty.

In this case, what is the output?

• The other array segment, so we just have to copy the segment to the output.

• what if both segments are not empty? We need to make recursive call.

Page 16: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

• Before we proceed, we need to make a change to the prototype of the function merge. Why?

• We need to add two more parameters - the name of the array and the starting index in the output array at which we want to write.

Page 17: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

A B

merge(A, 2, 3, B, 9, 13, C, 3) should return

1 2 2 3 4 4 5

3 4 5 6 7 8 9

C

Page 18: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

merge(A, low1,high1, B, low2, high2, C, low)

A B

merge(A, 9, 13, B, 2, 3, C, 0)

0 1 2 3 4 5 6

Case 1: A[low1] < B[low2]

Example:

1 2 2 3 4 4 5C

Page 19: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

merge(A, low1,high1, B, low2, high2, C,low)

A B

merge(A, 9, 13, B, 2, 3, C, 0)

0 1 2 3 4 5 6

Case 1: A[low1] < B[low2]

Example:

1 2 2 3 4 4 5

Step1: move A[low1] to C[low].

Now a recursive call to merge will get the rest of the job done.

What call?

Page 20: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

merge(A, low1,high1, B, low2, high2)

A B

merge(A, 9, 13, B, 2, 3, C, 0)

0 1 2 3 4 5 6

Case 1: A[low1] < B[low2]

Example:

1 2 2 3 4 4 5

Step1: move A[low1] to Out[0].

Now a recursive call to merge will get the rest of the job done.

What call? merge(A, low1+1, high1, B, low2,high2, C, low + 1)

Page 21: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Case 2: A[low1] > B[low2].

Move B[low2] to C[low] and make a recursive call to

merge(A,low1, high1, B, low2+1, high2, C, low+1)

The complete code is shown below:

Page 22: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

void merge(int A[], int low1, int high1, int B[], int low2,int high2, int C[], int low) {

if (high1 < low1) copy(B, low2, high2, C,low); else if (high2 < low2) copy(A, low1, high1, C,low); else if (A[low1] < B[low2]) {C[low] = A[low1]; merge(A,low1+1, high1, B, low2, high2, C,low + 1);} else {C[low] = B[low2]; merge(A,low1, high1, B, low2+1, high2, C,low + 1); } }

void copy(int A[], int s, int t, int B[], int s1) { for (int j= s; j<= t; ++j)

B[s1 +j - s] = A[j];

}

Page 23: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Last example: recursion and a linked list.

Suppose we want to insert a key x as a last item of a list.

a k g c

x = ‘b’

a k g c

b

Page 24: Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

void insertLast(int k) { // insert k as the last item of the list Node* tmp = new Node(k); if (head == 0)

head = tmp; else if (head->next == 0) {

head->next = tmp;} else {List rest; rest.head = head->next; rest.insertLast(k); }}