CS3334 Lecture 2
Recursive Function
- A recursive function is a function that calls itself (i.e., a recursive call).
- A recursive function has a base case(s), i.e., no more recursive calls.
- Given an input size n, recursive calls reduce n progressively until n reaches a base case.
Worst-Case Time Complexity Analysis (General Framework)
- Let T(n) be the worst case time complexity of the given recursive function
- Find a recurrence formula for T(n)
- Solve the recurrence formula by unrolling the recurrence formula a few times to see the general pattern
Worst-Case Space Complexity
-
For each recursive call, a separate copy of the variables declared in the function is created and stored in a stack. The storage is released when the recursive call finishes (e.g., a return statement is reached)
-
Thus, the amount of space needed to implement a recursive function depends on the depth of recursion, not on the number of times the function is invoked.
Fast Exponentiation
double power(double x, int n) { if (n==0) return 1; //base case if (n==1) return x; //base case if (n%2==0) //n is even return power(x*x,n/2); //recursive call else //n is odd return power(x*x,n/2)*x; //recursive call /* n/2 is integer division */ }
Tree of recursive call (One path)
Worst-case Time Complexity Analysis
- Let T(n) be the worst case running time of the algorithm (where n is the problem size).
- Let c be the constant time for the local work (the running time of the local work performed in the call is at most some constant c).
- Since at most one recursive call is made, the recurrence formula is T(n) ≤ T(n/2) + c.
- Assume n=2k, where k is an integer.
Worst-case Space Complexity Analysis
- Let S(n) be the worst case space requirement of the algorithm (where n is the problem size).
- Let c be the constant space for the local storage (the local storage required in the call is at most some constant c).
- Since at most one recursive call is made, the recurrence formula is S(n) ≤ S(n/2) + c.
- Assume n=2k, where k is an integer.
Worst-Case Time and Space Complexity*******
- The tree of recursive calls can visualize the time and space complexity
- Time complexity is proportional to the number of nodes in the tree.
- Space complexity is proportional to the length of the longest root-to-leaf path.
Recursive Binary Search
int binarySearch(int A[], int low, int up, int x) { if (low>up) return -1; //cannot find x int mid = (low+up)/2; if (A[mid]==x) return mid; //find x else if (A[mid]<x) return binarySearch(A,mid+1,up,x); else return binarySearch(A,low,mid-1,x); } //Elements in A[] are sorted in increasing order.
Worst-case Time Complexity
- Let T(n) be the worst case running time of the algorithm (where n is the problem size).
- Let c be the constant time for the local work (the local work performed in the call is at most some constant c).
- Since at most one recursive call is made, the recurrence formula is T(n) ≤ T(n/2) + c.
- Assume n=2k, where k is an integer.
Worst-case Space Complexity
- Let S(n) be the worst case space requirement of the algorithm (where n is the problem size).
- Let c be the constant time for the local storage (the local storage required in the call is at most some constant c).
- Since at most one recursive call is made, the recurrence formula is S(n) ≤ S(n/2) + c.
- Assume n=2k, where k is an integer.
Note: Increasing Functions
A function f(n) is called increasing, if for all x and y such that x ≤ y, f(x) ≤ f(y), so f(n) preserves the order.
Fibonacci Sequence
f0 = 0, f1 = 1
fi =fi-1 + fi-2 for i≥2
To compute fn
int fib(int n) { if (n==0 || n==1) return n; return fib(n-1)+fib(n-2); }
left-child, right-sibling representation
先从树的最左边的child-node开始寻找,
寻至最低后, 再从右侧相邻的sibling-node.
回到最高点后,找其右边的child-node,
寻至其左边最低后, 再从右侧相邻的sibling-node.
Worst-case Space Complexity
- Let S(n) be the worst-case space required by fib(n).
- Let c be the constant space for the local storage (the local storage required in the call is at most some constant c).
- The calls fib(n-1) and fib(n-2) execute one after the other; hence, storage can be reused.
- Therefore, we can set up the following recurrence formula for S(n):
- S(n) = max{ S(n-1), S(n-2) } + c
- Assuming S(n) is an increasing function (i.e., S(n-1) ≥ S(n-2)), S(n) = S(n-1) + c
Worst-case Time Complexity