Lecture 12 Oct 15 - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 12 Oct 15

Description:

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

Number of Views:81
Avg rating:3.0/5.0
Slides: 26
Provided by: Engine2
Category:
Tags: cell | lecture | oct | sorting

less

Transcript and Presenter's Notes

Title: Lecture 12 Oct 15


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

2
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)
3
  • Rules for recursive functions
  • 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.

4
Example 1 Write a recursive function to compute
n! function out fact(n) if n lt 1 out
1 else out n . fact(n-1) end
5
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) will return the largest t such that A(t) lt
x. Pre-condition A(low) lt x lt A(high) Thus
low lt t lt high. Initially, low 1, high
size(A)
6
Recursive binary search program
function out search(A, low, high, x) returns
the largest t s.t. A(t) lt x where low lt t lt
high A is assumed to be sorted if high -
low 1 if A(high) x out high
else out low end else mid
floor((low high)/2) if A(mid) x out
mid elseif A(mid) lt x out
search(A, mid 1, high, x) else out
search(A, low, mid - 1, x) end end
7
Binary search program function out
binary_search(A, x) if A(1) lt x out 0
else out search(A, 1, length(A), x)
end
8
Merge Sorting recursive algorithm Write a
program in Matlab to merge two arrays. Merging
involves combining two sorted arrays into a
single sorted array. gtgt A merge(2 8 11 20,
4 5 7 13 19 24) A 2 4 5 7 8 11 13 19 20
24
9
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.
B
A
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) should return 5 7
since the second array is empty. (high index lt
low index means the array is empty.)
10
What are the base cases?
11
  • What are the base cases?
  • one array segment is empty.
  • In this case, what is the output?

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

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

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

15
Merge function function out merge(A, B) if
length(A)0 out B elseif length(B)
0 out A else if A(1) lt B(1)
temp A(1) out temp,
merge(A(2end),B) else temp
B(1) out temp, merge(A,B(2end))
end end
16
Merge-sorting To sort an array, recursively sort
the two halves, then apply merge.
function outmergesort(A) if length(A)1
out A else s length(A) mid
floor(s/2) out1 mergesort(A(1mid))
out2 mergesort(A(mid1end)) out
merge(out1, out2) end
17
Exercise 8.1. gtgt repeat(2 3 3 1 4 2 ans
2 2 2 3 4 4
18
Exercise 8.1. gtgt 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
19
(No Transcript)
20
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 1s and 0s.
21
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 1s and 0s. function out myfind(A, x) out
for j 1length(A) if A(j) x
out out, j end end
22
Exercise 8.8 Write a function intoBits to take
an integer number as input and output a string of
0s and 1s representing the number in base 2.
23
Exercise 8.11 Write functions minAll, minCol and
minRow that find the overall minimum, column
minima and row minima of a matrix A.
24
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
25
Lec 13
Oct 17
Write a Comment
User Comments (0)
About PowerShow.com