Transcript lec12-oct15

Lecture 12 Oct 15 •Recursion – more examples • Chapter 8 problems and solutions • Cell arrays, Chapter 10

Recursive functions Before we conclude this chapter, we will discuss recursive functions, those that can call themselves.

We have examples of functions that call other functions, e.g.

insertionsort

calling

insert

etc.

If f(n) is defined in terms of f(n – 1), as for example, in the case of factorial, why not let f call itself?

n! = n x (n – 1)!

Or in matlab:

fact(n) = n .* fact(n – 1)

Rules for recursive functions 1. there should be exit from recursion. (i.e., there should be some conditional branch path in which there is no recursive call). Such cases are called the base cases.

2. recursive calls should make towards base case (usually by calling itself with smaller input values).

3. Recursion may be more time or memory consuming so should be careful with their use.

Example 1: Write a recursive function to compute n!

function out = fact(n) if n <= 1 out = 1; else out = n .* fact(n-1); end;

Example 2: Write a recursive function in Matlab to perform binary search.

Assume array A is sorted in ascending order.

Search(A, low, high, x)

such that A(t) <= x.

will return the largest t Pre-condition: A(low) <= x <= A(high) Thus low <= t <= high.

Initially, low = 1, high = size(A)

Recursive binary search program

function out = search(A, low, high, x) % returns the largest t s.t. A(t) <= x where low <= t <= high % A is assumed to be sorted if high - low == 1 if A(high) == x out = high; else else out = low; end; mid = floor((low + high)/2); if A(mid) == x out = mid; elseif A(mid) < x end; end; out = search(A, mid + 1, high, x); else out = search(A, low, mid - 1, x);

Binary search program

function out = binary_search(A, x) if A(1) < x out = 0; else out = search(A, 1, length(A), x) end

Merge Sorting – recursive algorithm

Write a program in Matlab to merge two arrays. Merging involves combining two sorted arrays into a single sorted array.

>> A = merge([2 8 11 20], [4 5 7 13 19 24]) A = 2 4 5 7 8 11 13 19 20 24

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 0 1 2 3 4 5 6 1 2 2 3 4 4 5 merge(A, 2, 2, B, 9,10) should return the array 1 2 4 merge(A, 3, 4, B, 9, 8) empty.) should return 5 7 since the second array is empty. (high index < low index means the array is

What are the base cases?

What are the base cases?

• one array segment is empty.

In this case, what is the output?

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 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.

• 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.

Merge function

function out = merge(A, B) if length(A)==0 out = B; elseif length(B) == 0 out = A; else if A(1) <= B(1) temp =A(1); out = [temp, merge(A(2:end),B)]; else temp = B(1); out = [temp, merge(A,B(2:end))]; end; end;

Merge-sorting

To sort an array, recursively sort the two halves, then apply merge.

function out=mergesort(A) if length(A)==1 out = A; else s = length(A); mid = floor(s/2); out1 = mergesort(A(1:mid)); out2 = mergesort(A(mid+1:end)); out = merge(out1, out2); end

Exercise 8.1.

>> repeat([2 3; 3 1; 4 2] ans = [ 2 2 2 3 4 4 ]

Exercise 8.1.

>> repeat([2 3; 3 1; 4 2] ans = [ 2 2 2 3 4 4 ]

function out = repeat1(m) out = []; [r, c] = size(m); for i = 1 : r %go through each row for j = 1 : m(i, 2) %Repeat the number of times specified in the second column out(end + 1) = m(i, 1); end end

Exercise 8.4. Write a function

myfind

, that mimics the behavior of the built-in function

find

. That is, it takes as input a boolean vector of 1’s and 0’s.

Exercise 8.4. Write a function

myfind

, that mimics the behavior of the built-in function

find

. That is, it takes as input a boolean vector of 1’s and 0’s.

function out = myfind(A, x) out = []; for j = 1:length(A) if A(j) == x out = [out, j]; end; end;

Exercise 8.8

Write a function

intoBits

as input and output a string of 0’s and 1’s representing the number in base 2.

to take an integer number

Exercise 8.11

Write functions minAll, minCol and minRow that find the overall minimum, column minima and row minima of a matrix A.

Exercise 8.11

Write functions minAll, minCol and minRow that find the overall minimum, column minima and row minima of a matrix A.

min(min(matrix)) %Minimum element of the whole matrix min(matrix); %Minimum element of each column min(transpose(matrix)); %Minimum element of each row

Lec 13 Oct 17