Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011 Suprakash Datta datta@cse.yorku.ca Office: CSEB 3043 Phone: 416-736-2100 ext 77875 Course page: http://www.cse.yorku.ca/course/1019 5/10/2019
Next: Recursion Important in mathematics, programming, thinking about problems 5/10/2019
Recursive definitions In many cases – a natural way of thinking Ex. 1: f(0) = b, and i>0 f(i)= f(i-1)+a Ex. 2: f(0) = b, and i>0 f(i)= f(i-1)*a Ex. 3: f(0) = 1, f(1) = 1 and i>1 f(i)= f(i-1)+f(i-2) Arithmetic progression Geometric progression Fibonacci series 5/10/2019
Full binary trees Each node has 0 successors 2 successors, each of which is a full binary tree Special case: complete binary tree The two successors of a node are complete binary trees with the same number of nodes 5/10/2019
Height of full binary trees Recursive definition of height h(T) If no of nodes n(T) =1, h(T) =0 Else the height is 1 + max(h(T1),h(T2)) where T1, T2 are the two subtrees of T Theorem: If T is a full binary tree, then n(T) 2h(T)+1 -1 Proof: By structural induction 5/10/2019
Structural induction To prove n(T) 2h(T)+1 -1 Base case: If n(T) = 1, h(T) = 0, so the proposition holds. Inductive step: 5/10/2019
Simple recursive programs Factorial Fibonacci Binary search Merge sort 5/10/2019
Correctness of recursive programs Merge sort Inductively assume the two recursive calls correct Prove the correctness of the merge step By strong induction the algorithm is correct 5/10/2019
Drawbacks of recursion Repeated computation of the same terms Wasted storage Overhead incurred by OS due to stack maintenance 5/10/2019
Analyzing recursive programs Mergesort f(n) = 2f(n/2) + n What constitutes a “solution”? How do you solve this recurrence? 5/10/2019
Next More uses of recursion 5/10/2019
Recursively defined sets Basis step: 2 S Recursive step: If x S and y S then x+ y S Defines the set {2k|k=1,2,3,…} What happens if we change the recursive step to: If x S and y S then x+y S, x-y S ? 5/10/2019
Recursively defined sets - 2 All possible strings (*) over alphabet Basis step: * Recursive step: If x and w * then wx * Length of a string l() = 0 l(wx) = l(w) + 1 if x and w * 5/10/2019
Recursively defined sets - 3 What is this set A defined over = {0,1}? Basis step: A Recursive step: If x A then 0x1 A What is this set B defined over = {0,1}? Basis step: B Recursive step: If x B then 0x1 B and 1x0 B 5/10/2019
Recursively defined sets - 4 Data structures – full binary trees Basis step: single vertex Recursive step: If S, T are full binary trees then the following is a full binary tree r T S 5/10/2019