Download presentation
Presentation is loading. Please wait.
Published byWinifred Terry Modified over 5 years ago
1
Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011
Suprakash Datta Office: CSEB 3043 Phone: ext 77875 Course page: 5/10/2019
2
Next: Recursion Important in mathematics, programming, thinking about problems 5/10/2019
3
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
4
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
5
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
6
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
7
Simple recursive programs
Factorial Fibonacci Binary search Merge sort 5/10/2019
8
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
9
Drawbacks of recursion
Repeated computation of the same terms Wasted storage Overhead incurred by OS due to stack maintenance 5/10/2019
10
Analyzing recursive programs
Mergesort f(n) = 2f(n/2) + n What constitutes a “solution”? How do you solve this recurrence? 5/10/2019
11
Next More uses of recursion 5/10/2019
12
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
13
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
14
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
15
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.