Discrete Math Example 1 of AverageCase Computational Complexity

  • Slides: 5
Download presentation
Discrete Math: Example 1 of Average-Case Computational Complexity

Discrete Math: Example 1 of Average-Case Computational Complexity

Example 1 We are given a real number x and a list of n

Example 1 We are given a real number x and a list of n distinct real numbers. The linear search algorithm, locates x by successively comparing it to each element in the list, terminating when x is located or when all the elements have been examined and it has been determined that x is not in the list. What is the average-case computational complexity of the linear search algorithm if the probability that x is in the list is p and it is equally likely that x is any of the n elements in the list? (There are n + 1 possible types of input: one type for each of the n numbers in the list and a last type for numbers not in the list, which we treat as a single input. )

Solution The probability that x equals ai, the ith element in the list, is

Solution The probability that x equals ai, the ith element in the list, is p/n, and the probability that x is not in the list is q = 1 − p. It follows that the average-case computational complexity of the linear search algorithm is For instance, when x is guaranteed to be in the list, we have p=1(so the probability that x =ai is 1/n for each i) and q=0. When p, the probability that x is in the list, is 1/2, it follows that q = 1 − p = 1/2, so E=(n+2)/2+n+1=(3 n+4)/2.

Solution Similarly, if the probability that x is in the list is 3/4, we

Solution Similarly, if the probability that x is in the list is 3/4, we have p = 3/4 and q = 1/4, so E = 3(n + 2)/4 + (n + 1)/2 = (5 n + 8)/4. Finally, when x is guaranteed not to be in the list, we have p = 0 and q = 1. It follows that E = 2 n + 2, which is not surprising because we have to search the entire list.

References Discrete Mathematics and Its Applications, Mc. Graw-Hill; 7 th edition (June 26, 2006).

References Discrete Mathematics and Its Applications, Mc. Graw-Hill; 7 th edition (June 26, 2006). Kenneth Rosen Discrete Mathematics An Open Introduction, 2 nd edition. Oscar Levin A Short Course in Discrete Mathematics, 01 Dec 2004, Edward Bender & S. Gill Williamson