Recursions By Dr Hanh Pham 1 What is
Recursions By Dr. Hanh Pham 1
What is Recursion ? = when a procedure contains steps which call the procedure itself. = a method to find solution for a problem of size N by delaying it to the same problem of size N-1 … on and on 2
Example 1: I want to be a Millionaire ! Problem/Goal = to have $1, 000 in my bag 3
Example 1: I want to be a Millionaire ! Problem/Goal = to have $1, 000 in my bag Solution = I can have 1, 000 (N) USD if I already have 999, 999 (N-1) USD and earn $1 more … 4
Example 1: I want to be a Millionaire ! Problem/Goal = to have $1, 000 in my bag Solution = I can have 1, 000 (N) USD if I already have 999, 999 (N-1) USD and earn $1 more … Procedure: bag(N) = bag(N-1)+1 bag(N-1) = bag(N-2)+1 … bag(2) = bag(1)+1 bag(1) = bag(0)+1 5
Example 1: I want to be a Millionaire ! Procedure: bag(N) = bag(N-1)+1 bag(N-1) = bag(N-2)+1 … bag(2) = bag(1)+1 bag(1) = bag(0)+1 Code: int bag(int n) { int bag. Of. N; bag. Of. N = bag(n-1)+1; return bag. Of. N; } Call: bag(1, 000); 6
Example 1: I want to be a Millionaire ! Procedure: bag(N) = bag(N-1)+1 bag(N-1) = bag(N-2)+1 … bag(2) = bag(1)+1 bag(1) = bag(0)+1 Code: int bag(int n) { return bag(n-1)+1; } 7 Where is the EXIT ? This code will NEVER end , never stop !
Example 1: I want to be a Millionaire ! Procedure: bag(N) = bag(N-1)+1 bag(N-1) = bag(N-2)+1 … bag(2) = bag(1)+1 bag(1) = bag(0)+1 Code: int bag(int n) { return bag(n-1)+1; } Where is the EXIT ? Code: int bag(int n) { if (n ==1) return 1; else return bag(n-1)+1; } 8
Example 2: Exponentiation • Its simple form: • can be expressed like this by recurrence: 9
Example 2: Exponentiation • GOAL = Expo(N) • Expo(N) = Expo(N-1) * X • Expo(N-1) = Expo(N-2) * X. . . • Expo(3) = Expo(2) * X • Expo(2) = Expo(1) * X • Expo(1) = X => we know this !!! 10
Example 2: Exponentiation • GOAL = Expo(N) Step 1: Expo(N) = Expo(N-1) * X Step 2: Expo(N-1) = Expo(N-2) * X. . . Step N-2: Expo(3) = Expo(2) * X Step N-1: Expo(2) = Expo(1) * X Step N: Expo(1) = X => we know this !!! • ALSO: all steps 1, 2, …, N-1 are the “same” 11
Example 2: Exponentiation Procedure: X=10 Step 1: Expo(N) = Expo(N-1) * X Step 2: Expo(N-1) = Expo(N-2) * X. . . Step N-2: Expo(3) = Expo(2) * X Step N-1: Expo(2) = Expo(1) * X Step N: Expo(1) = X 12 Where is the EXIT ? Code: double X=10; int Expo(int n) { if (n ==1) return X; else return Expo(n-1)*X; }
Example 2: Exponentiation Procedure: X=10 Step 1: Expo(N) = Expo(N-1) * X Step 2: Expo(N-1) = Expo(N-2) * X. . . Step N-2: Expo(3) = Expo(2) * X Step N-1: Expo(2) = Expo(1) * X Step N: Expo(1) = X 13 Where is the EXIT ? Code: int Expo(int n) { double X=10; if (n ==1) return X; else return Expo(n-1)*X; }
Example 2: Exponentiation Procedure: X=10 Step 1: Expo(N) = Expo(N-1) * X Step 2: Expo(N-1) = Expo(N-2) * X. . . Step N-2: Expo(3) = Expo(2) * X Step N-1: Expo(2) = Expo(1) * X Step N: Expo(1) = X 14 Where is the EXIT ? Code: int Expo(int n, double X) { if (n ==1) return X; else return Expo(n-1, X)*X; } Call: double A = Expo(3, 5);
- Slides: 14