Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 9/11/2015 MATH 224 – Discrete Mathematics Iterative version Recursive version Normally i is initialized to 0 and j to n–1 for an array of size n. Recursive.

Similar presentations


Presentation on theme: "1 9/11/2015 MATH 224 – Discrete Mathematics Iterative version Recursive version Normally i is initialized to 0 and j to n–1 for an array of size n. Recursive."— Presentation transcript:

1 1 9/11/2015 MATH 224 – Discrete Mathematics Iterative version Recursive version Normally i is initialized to 0 and j to n–1 for an array of size n. Recursive C++ Functions int binary_search(in x, int a[ ], int i, int j) while (i < j) { m = (i+j)/2; if (x > a[m]) i = m+1; else j = m } //end while return i; } //end binary_search int binary_search(int x, int a[ ], int i, int j) if (i < j) { m = (i+j)/2; if (x > a[m]) i = bin_search(x, a, m+1, j) ; else i = bin_search(x, a, i, m) ; } //fi return i; } //end binary_search

2 2 9/11/2015 MATH 224 – Discrete Mathematics Recursive C++ Functions With the recursive version of binary search below, how many times will binary_search call it self for an array of size N? What will happen if x is not in array a? int binary_search(int x, int a[ ], int i, int j) if (i < j) { m = (i+j)/2; if (x > a[m]) i = bin_search(x, a, m+1, j) ; else i = bin_search(x, a, i, m) ; } //fi return i; } //end binary_search

3 3 9/11/2015 MATH 224 – Discrete Mathematics Recursive C++ Functions and the Program Stack int binary_search(int x, int a[ ], int i, int j) if (i < j) { m = (i+j)/2; if (x > a[m]) i = bin_search(x, a, m+1, j) ; else i = bin_search(x, a, i, m) ; } //fi return i; } //end binary_search x = 14, i =3, j =3 x = 14, i =3, j =4, m = 3 x = 14, i =3, j =5, m = 4 x = 14, i =0, j =5, m = 2 x = 14, i =0, j =11, m = 5 main 3 8 12 15 20 32 44 52 75 100 105 117 0 1 2 3 4 5 6 7 8 9 10 11 Array a with 12 elements, x = 14 What value will be returned for this example? How will you determine if x is in the array?

4 4 9/11/2015 MATH 224 – Discrete Mathematics Recursive Definitions 1 if x = 1 or x = 2 f(x–2) + f(x–1) if x > 2 { Fib(x) = 1 if N = 1 N. (N–1)! if N > 1 { N! = δ if n = 1 δδif n = 2 δPal(n-2) δ if n > 2 { Pal(n) = Fibonacci Series Factorial Palindromes In this example, δ is a letter in the English alphabet. These are called palindromes. What special property do these strings have?

5 5 9/11/2015 MATH 224 – Discrete Mathematics More Recursive Definitions a if n = 0 if n > 0 The values B(j, k) are called binomial coefficients and are sometimes read as j choose k. They are sometimes referred to as the number of combinations of k objects out of j. For example, if a coin is flipped 5 times B(5, 3) refers to the number of combinations that leads to three heads. [Written in our text as C(5, 3).] Can you calculate B(5, 3) from the definition above? Do you remember what binomials are? How are they related to B(j, k)? Do you know a non- recursive formula for B? { = for r ≠ 1 B(j, k) = 1 if k = 0 or j = k B(j –1, k –1) + B(j – 1, k,) all other cases { j ≥ k ≥ 0

6 6 9/11/2015 MATH 224 – Discrete Mathematics Binomial Coefficients Do you know why binomial coefficients are important in probability? What is the probability of getting 3 head when tossing a fair coin 6 times? (Hint: it is not ½. In order to find the answer first calculate B(6, 3). Then multiply this by the probability of one exact configuration e.g, for example three heads followed by 3 tails. What is that probability? Finally multiply your two answers. What is your answer? B(j, k) = 1 if k = 0 or j = k B(j –1, k –1) + B(j – 1, k,) all other cases { j ≥ k ≥ 0 Binomial coefficients may also be defined as B(j,k) =

7 7 9/11/2015 MATH 224 – Discrete Mathematics Binomial Coefficients B(j, k) = 1 if k = 0 or j = k B(j –1, k –1) + B(j – 1, k,) all other cases { j ≥ k ≥ 0 B(5,3) B(4,2) + B(4,3) B(3,1) + B(3,2) + B(3,2) + B(3,3) B(2,0)+B(2,1) + B(2,1) + B(2,2) + B(2,1) + B(2,2) + 1 1 + B(1,0)+B(1,1) + B(1,0)+B(1,1) + 1 + B(1,0)+B(1,1) + 1 1 + 1 + 1 + 1 + 1 + 1 Add all the ones to get 10. Note B(3,2), B(2,1) are repeated among others.

8 8 9/11/2015 MATH 224 – Discrete Mathematics Binary Search Trees A Binary Search Tree is 1. An empty set with no nodes, 2. A singleton set with one node and stored value tree, 3. A tree with single node with one value and a left and right binary search trees, where all values on the left are smaller and all values on the right are larger. Note that 2. above is not actually necessary. Why is that?

9 9 9/11/2015 MATH 224 – Discrete Mathematics Binary Search Trees A Binary Search Tree is 1. An empty set with no nodes, 2. A singleton set with one node and stored value, 3. A tree with single node with one value and a left and right binary search trees, where all values on the left are smaller and all values on the right are larger. 20Single Node Tree Multi-Node Tree 20 25 4023 15 187 3 Right Binary Search Tree Satisfies Rule 3. Left Binary Search Tree


Download ppt "1 9/11/2015 MATH 224 – Discrete Mathematics Iterative version Recursive version Normally i is initialized to 0 and j to n–1 for an array of size n. Recursive."

Similar presentations


Ads by Google