23
1 Section 3.5 Recursive Algorithms

1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

  • View
    217

  • Download
    3

Embed Size (px)

Citation preview

Page 1: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

1

Section 3.5

Recursive Algorithms

Page 2: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

2

Recursive Algorithms

• Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values

• When such reduction is possible, solution to original problem can be found with series of reductions, until problem is reduced to case for which solution is known

• Algorithms which take this approach are called recursive algorithms

Page 3: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

3

Example 1: computing an

• Can base algorithm on recursive definition of an:– for n=0, an = 1

– for n>0, an+1 = a(an)

– where a is a non-zero real number and n is a non-negative integer

procedure power(inputs: a, n)if (n=0) then power(a,n) = 1

else power (a,n) = power(a, n-1)

Page 4: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

4

Extending example 1

• The algorithm in example 1 works only for non-negative powers of non-zero a - we can extend the algorithm to work for all powers of any value a, as follows:procedure power (inputs: a, n)

if (a = 0) power(a,n) = 0else if (n = 0) power(a,n) = 1else if (n > 0) power(a,n) = (a * power(a, n-1))else power(a,n) = (1 / rpower(a, -n))

Page 5: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

5

Example 1

int power ( int num, int p){

if (num == 0 )return 0;

if (p ==0)return 1;

if (p < 0)return 1 / power(num, -p);

return num * power(num, p-1);}

Page 6: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

6

Example 2: computing gcd

• The algorithm on the following slide is based on the reduction gcb(a,b) = gcd(b mod a, a) and the condition gcd(0,b) = b where:– a < b– b > 0

Page 7: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

7

Example 2: computing gcd

Procedure gcd (inputs: a, b with a < b)

if (a=0) then gcd(a,b) = b

else gcd(a,b) = gcd(b mod a, a)

Page 8: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

8

Example 2

int gcd (unsigned int smaller, unsigned int larger){

if (larger < smaller){

int tmp = larger;larger = smaller;smaller = tmp;

}if (larger == smaller || smaller == 0)

return larger;return gcd (larger % smaller, smaller);

}

Page 9: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

9

Linear search revisited

• The linear, or sequential search algorithm was introduced in section 2.1, as follows:– Examine each item in succession to see if it

matches target value– If target found or no elements left to examine,

stop search– If target was found, location = found index;

otherwise, location = 0

Page 10: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

10

Linear search revisited

• In the search for x in the sequence a1 … an, x and ai are compared at the ith step

• If x = ai, the search is finished; otherwise the search problem is reduced by one element, since now the sequence to be searched consists of ai+1 … an

• Looking at the problem this way, a recursive procedure can be developed

Page 11: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

11

Linear search revisited

• Let search(x,y,z) be the procedure that searches for z in the sequence ax … ay

• The procedure begins with the triple (x,y,z), terminating when the first term of the sequence is z or when no terms are left to be searched

• If z is not the first term and additional terms exist, same procedure is carried out but with input of (x+1, y, z), then with (x+2, y, z), etc.

Page 12: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

12

Linear search revisited

Procedure lsearch (x,y,z)

if ax = z then location = x

else if x = y then location = 0 (not found)

else lsearch(x+1, y, z)

Page 13: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

13

Linear search

int lsearch(int index, int len, int target, int array[]){

if (index == len)return len; // not found

if (array[index]==target)return index; // found

return lsearch(index+1, len, target, array);}

Page 14: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

14

Binary search revisited

• The binary search algorithm was also introduced in section 2.1:– Works by splitting list in half, then examining

the half that might contain the target value• if not found, split and examine again

• eventually, set is split down to one element

– If the one element is the target, set location to index of item; otherwise, location = 0

Page 15: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

15

Binary search - recursive version

procedure bsearch (inputs: x,y,z)mid = (x+y)/2

if z = amid then location = mid (found)

else if z < amid and x < mid then

bsearch(x, mid-1, z)

else if z > amid and y > mid then

bsearch (mid+1, y, z)else location = 0 (not found)

Page 16: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

16

Implementation of binary search void BinarySearch(int array[], int first, int size,

int target, bool& found, int& location){

size_t middle; if (size == 0)

found = false; // base caseelse{

middle = first + size / 2;if (target == array[middle]){

location = middle;found = true;

}

Page 17: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

17

Binary search code continued // target not found at current midpoint -- search appropriate half else if (target < array[middle]) BinarySearch (array, first, size/2, target, found, location); // searches from start of array to index before midpoint

else BinarySearch (array, middle+1, (size-1)/2, target, found, location); // searches from index after midpoint to end of array

} // ends outer else} // ends function

Page 18: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

18

Recursion Vs. Iteration

• A recursive definition expresses the value of a function at a positive integer in terms of its value at smaller integers

• Another approach is to start with the value of the function at 1 and successively apply the recursive definition to find the value at successively larger integers - this method is called iteration

Page 19: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

19

Example 3: finding n!

• Recursive algorithm:int factorial (int n) {

if (n==1) return 1;return n * factorial(n-1);}

• Iterative algorithm:int factorial (int n) {

int x = 1;for (int c = 1; c<= n; c++)

x = c * x ;return x; }

Page 20: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

20

Recursion Vs. Iteration

• Iterative approach often requires much less computation than recursive procedure

• Recursion is best suited to tasks for which there is no obvious iterative solution

Page 21: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

21

Example 4: finding nth term of a sequence

• Devise a recursive algorithm to find the nth term of the sequence defined by:– a0 = 1, a1 = 2

– an = an-1 * an-2 for n=2, 3, 4 …

Page 22: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

22

Example 4

int sequence (int n)

{

if (n < 2)

return n+1;

return sequence(n-1)*sequence(n-2);

}

Page 23: 1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such

23

Section 3.5

Recursive Algorithms

- ends -