CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a
Announcements Homework 6 available. Due 07/10, 8a. Quiz time
Binary Search Example Looking for 42 FLM
Looking for 42 FLM Binary Search Example
found – in 3 comparisons F L M Binary Search Example
Looking for 89 FLM Binary Search Example
Looking for 89 FLM Binary Search Example
Looking for 89 FL M Binary Search Example
not found – 3 comparisons FL Binary Search Example
Algorithm Complexity How long does the “linear search algorithm” take? Suppose the data is (2, 8, 3, 9, 12, 1, 6, 4, 10, 7) and we’re looking for # 8124 The running time of the algorithm depends on the particular input to the problem. In this case we have two different complexity measures: Worst case complexity - running time on worst input Average case - average running time among all inputs Worst case for linear search is time n. Average case (1+2+…+n)/n = n(n+1)/2n = (n+1)/2. Both are O(n)
Algorithm Complexity How long does the “binary search algorithm” take? Binary search Input: a sorted array of numbers a 1, a 2, … a n and a number x Output: position i where a i = x, if x is in the list 1.i = 1, j = n; 2.while i < j 3. m = (i + j)/2 ; /* midpt of range (i,j)*/ 4. if x > a m then i = m else j = m 6.if x = a i then output “x is in position a i ” 7.else output “x is not in list” The main point of showing you this code is to remind you that the range is cut in half at every iteration.
Algorithm Complexity How long does the “binary search algorithm” take? Binary search Input: a sorted array of numbers a 1, a 2, … a n and a number x Output: position i where a i = x, if x is in the list 1.i = 1, j = n; 2.while i < j 3. m = (i + j)/2 ; /* midpt of range (i,j)*/ 4. if x > a m then i = m else j = m 6.if x = a i then output “x is in position a i ” 7.else output “x is not in list” If n is a power of 2, n = 2 k, then k = lg n iterations occur. If n is not a power of 2, let k be the number so that 2k < n < 2 k+1, and imagine that the array has 2 k+1 elements. Then k+1 < lg n + 1 = O(log n)
Recursive Function recursive function The recursive function is a kind of function that calls itself, or a function that is part of a cycle in the sequence of function calls. f1 f2fn …
What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first smaller problem Recursion is a technique that solves a problem by solving a smaller problem of the same type
Recursive Function
How do I write a recursive function? Determine the size factor Determine the base case(s) (the one for which you know the answer) Determine the general case(s) (the one where the problem is expressed as a smaller version of itself) Verify the algorithm
10-17 An Example of Recursive Function We can implement the multiplication by addition. The simple case is “m*1=m.” The recursive step uses the following equation: “m*n = m+m*(n-1).”
Copyright ©2004 Pearson Addison- Wesley. All rights reserved Trace of Function multiply(6,3)
Problems defined recursively There are many problems whose solution can be defined recursively Example: n factorial 1if n = 0 n!= (recursive solution) (n-1)!*nif n > 0 1if n = 0 n!= (closed form solution) 1*2*3*…*(n-1)*nif n > 0
Coding the factorial function Recursive implementation int Factorial(int n) { if (n==0) return 1; else return n * Factorial(n-1); }
Coding the factorial function
Coding the factorial function (cont.) Iterative implementation int Factorial (int n) { int fact = 1; for(int count = 2; count <= n; count++) fact = fact * count; return fact; }
On to recursive algorithms… Factorial (n) Input: an integer n > 0. Output: n! 1.If n = 1 then output 1 2.else 3. output n x Factorial(n-1) Let T(n) denote the running time of the algorithm on input of size n. T(n) = C + T(n-1) T(1) = c T(n) = C + (C + T(n-2)) = C + (C + (C + T(n-3)))… = nC = O(n)
On to recursive algorithms… Now let’s analyze the running time. T(n) = 2 T(n-1) + C = 2 (2 T(n-2) + C) + C = 4 T(n-2) + 3C = 4 (2 T(n-3) + C) + 3C = 8 T(n-3) + 7C … = 2 k T(n-k) + (2 k -1)C T(1) = C = 2 n-1 T(1) + (2 n-1 -1)C = (2 n -1) C = O(2 n )
Recursion vs. iteration Iteration can be used in place of recursion An iterative algorithm uses a looping construct A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code