560 likes | 729 Views
Lecture 7 Recursive function calls. Recursive programming Factorial Fibonacci number Matrix determinant Hanoi tower. Exercise I. Calculate the average of a series of numbers Use a while-loop to get numbers Allow the user to key in a number at each iteration
E N D
Lecture 7 Recursive function calls • Recursive programming • Factorial • Fibonacci number • Matrix determinant • Hanoi tower 軟體實作與計算實驗
Exercise I • Calculate the average of a series of numbers • Use a while-loop to get numbers • Allow the user to key in a number at each iteration • Output the average of given numbers at each iteration • Halt when the given number equals zero • Draw a flow chart for problem solving • Write a MATLAB function to implement the flow chart 軟體實作與計算實驗
Exercise #a#b • Generate four distinct digit characters within {’0’, ’1’, ’2’, ’3’, ’4’, ’5’,’6’,’7’,’8’,’9’} randomly • Use a while-loop to realize the game of #a#b • Allow a player to key-in a four-digit string • Response na’a’nb’b’ to the given string • Halt if the guess is scored as 4’a’ 軟體實作與計算實驗
na denotes the number of characters in the guess that appear in the target with correct position • nb denotes the number of characters in the guess that appear in the target but stand at wrong position 軟體實作與計算實驗
Example • Target : 6481, Guess : 1628 • Response : 0 ’a’ 3 ’b’ • Target : 6481, Guess : 1946 • Response : 0 ’a’ 3 ‘b’ • Target : 6481, Guess : 6283 • Response : 1 ‘a’ 1 ‘b’ • Target : 6481, Guess : 6481 • Response : 4 ’a’ 0 ‘b’ 軟體實作與計算實驗
Exercise II • Draw a flow chart to realize the game of #a#b using a while-loop • Draw a flow chart to illustrate how to determine na for given target and guess • Draw a flow chart to illustrate how to determine nb for given target and guess 軟體實作與計算實驗
Exercise III • Write MATLAB functions to implement flow charts in exercise II 軟體實作與計算實驗
Recursive programming • Directly solve a task if problem size is small enough • Decompose a task of size n to subtasks of smaller sizes otherwise 軟體實作與計算實驗
Problem size • Factorial • N! • problem size: N • Fibonacci number • Fn =Fn-1 + Fn-2 • problem size: n • Determinant of an n-by-n matrix A • det(A) • problem size : n 軟體實作與計算實驗
Factorial • Definition • The factorial function is formally defined by • or recursively defined by 軟體實作與計算實驗
Recursive definition factorial(n) n==0 T ans=1 ans= factoria(n-1) ans=n*ans 軟體實作與計算實驗
MATLAB codes function ans=factorial(n) if n==0 ans=1; return else ans=n*factorial(n-1) end 軟體實作與計算實驗
Fibonacci number • Definition • Fibonacci numbers is defined by • the recurrence relation • with seed values 軟體實作與計算實驗
Recurrence relation fib(n) n==1 | n==0 ans=n ans=fib(n-1) ans=ans+fib(n-2) 軟體實作與計算實驗
Termination condition • If n==1 or n==0 軟體實作與計算實驗
Recurrence relation • if n > 1 • By the rule, the problem size is reduced from n to n-1 and n-2 • f(n) is decomposed to two sub-tasks, f(n-1), f(n-2) 軟體實作與計算實驗
fib source codes function ans=fib(n) if n==1 | n==0 ans=1; return else ans = fib(n-1); ans = ans+fib(n-2); end 軟體實作與計算實驗
return • MATLAB function halts whenever the ‘return’ statement is executed • The function exits after calling fib(1) or fib(0) 軟體實作與計算實驗
Recursive call • The function fib calls itself when n > 1 • Recursive call • The function fib calls itself during execution • Problem sizes of two recursive calls are respectively n-1 and n-2 軟體實作與計算實驗
A tree of recursive calls >> fib(3) execute fib 3 execute fib 2 execute fib 1 execute fib 0 execute fib 1 ans = 3 • Fib(3) • Fib(2) • Fib(1) • Fib(0) • Fib(1) 軟體實作與計算實驗
>> fib(4) execute fib 4 execute fib 3 execute fib 2 execute fib 1 execute fib 0 execute fib 1 execute fib 2 execute fib 1 execute fib 0 ans = 5 Calling tree • Fib(4) • Fib(3) • Fib(2) • Fib(1) • Fib(0) • Fib(1) • Fib(2) • Fib(1) • Fib(0) 軟體實作與計算實驗
Termination condition • If n==1 or n==0 軟體實作與計算實驗
Binary to decimal representations Let b be a vector of binary bits Translate it to a decimal number such that • Problem statement 軟體實作與計算實驗
Decomposition 軟體實作與計算實驗
Halting condition 軟體實作與計算實驗
Recurrence relation bin2dec(b,n) n==1 b1=b(n) b2=b(1:n-1) ans=bin2dec(b2,n-1) ans=2*ans+b1 ans=b 軟體實作與計算實驗
function ans=bin2dec(b,n) if n==1 ans=b; return else b1=b(n); b2=b(1:n-1); ans=bin2dec(b2,n-1); ans=2*ans+b1; end 軟體實作與計算實驗
Decimal to binary representations • Problem statement Let a denote a decimal number b = dec2bin(a) b denotes the binary representation of a 軟體實作與計算實驗
Recurrence relation dec2bin(a) a<=1 a1=mod(a,2) a2=floor(a/2) ans=dec2bin(a2) ans=[ans a1] ans=a 軟體實作與計算實驗
function ans=dec2bin(a) if a<=1 ans=a; return else a1=mod(a,2); a2=floor(a/2); ans=dec2bin(a2); ans=[ans a1]; end 軟體實作與計算實驗
>> a=53; >> b=dec2bin(a) b = 1 1 0 1 0 1 >> bin2dec(b,6) ans = 53 軟體實作與計算實驗
Determinant >> A=[1 2;3 4] A = 1 2 3 4 >> det(A) ans = -2 軟體實作與計算實驗
Rule I • A is 2x2 軟體實作與計算實驗
Flow chart • Let A be a 2-by-2 matrix [m,n]=size(A) m==2 & n==2 T ans=A(1,1)*A(2,2)-A(1,2)*A(2,1) 軟體實作與計算實驗
n=3 • A is 3x3 • Ex n=3 • Calculating the determinant of a 3-by-3 matrix is translated to subtasks of calculating determinants of 2-by-2 matrices 軟體實作與計算實驗
Rule II • A is nxn and n > 2 denotes a (n-1)x(n-1) sub-matrix of A. It is obtained by removing elements in the first row and the jth column of matrix A 軟體實作與計算實驗
Recurrent relation for reduction of problem size • det(A) is decomposed to n sub-tasks • Each calculates determinant of an (n-1)-by-(n-1) matrix • The problem size is reduced from n to n-1 軟體實作與計算實驗
Tree of recursive calls rule II . . . . . . rule I 軟體實作與計算實驗
Append a=[1 2 3]’ A=[] A=[A a a]; 軟體實作與計算實驗
B1=A(2:n,1:i-1) B2=A(2:n,i+1:n) B=[B1 B2] 軟體實作與計算實驗
Flow chart ans=0 m==2 & n==2 function ans= mydet(A) for i=1:n T ans=A(1,1)*A(2,2)-A(1,2)*A(2,1) B=[A(2:n,1:i-1) A(2:n,i+1:n)] ai= (-1)^(i+1)*A(1,i)*mydet(B) ans = ans +ai 軟體實作與計算實驗
MATLAB codes function ans=mydet(A) ans=0;[m,n]=size(A); if m==1 ans=A; return; end if m==2 ans=A(1,1)*A(2,2)-A(1,2)*A(2,1); return; else for i=1:n … end end 軟體實作與計算實驗
function v=mydet(A) % Calculate determinant of A n=size(A,1);v=0; if n==2 v=A(1,1)*A(2,2)-A(1,2)*A(2,1); return end v=0; for i=1:n B=get_submatrix(A,i); det_B=mydet(B); s=(-1)^(i+1); v=v+s*A(1,i)*det_B; end return 軟體實作與計算實驗
Hanoi Tower Problem 軟體實作與計算實驗
Target 軟體實作與計算實驗
Hanoi Tower PlayHanoiTower.rar 軟體實作與計算實驗
Valid movement • A larger disk is inhibited to be placed on the top of a smaller disk. • Initial state: all disks on the first tower • Final state: all disks on the third tower 軟體實作與計算實驗
Three steps for auto-play • Move n-1 objects from stack 1 to stack 2 • Move 1 object from stack 1 to stack 3 • Move n-1 objects from stack 2 to stack 3 軟體實作與計算實驗