Xem mẫu

  1. Lecture 17 
  2. Recap  Remaining Part of Maximum Contiguous Subsequence  Sum Problem General Big­Oh Rule Big­Oh Big­Omega Big­Theta Little­Oh Running Time of Algorithm
  3. Running Time of Cubic  Algorithm
  4. Running Time of Quadratic  Algorithm 
  5. Running Time of Linear  Algorithm  A similar calculation shows that a 10­fold increase in  input size results in a 10­fold increase in running time This relationship has been confirmed experimentally For a linear program the term sufficiently large means a  somewhat higher input size than for the other programs  The reason is that of the overhead of 0.000003 sec is  used in all cases For a linear program, this term is still significant for  moderate input sizes
  6. Running Time of Logarithmic  Terms
  7. The Logarithm  The list of typical growth rate functions includes several  entries containing the logarithm A logarithm is the exponent that indicates the power to  which a number (the base) is raised to produce a given  number
  8. Definition of Logarithm 
  9. Theorem 6.4
  10. Logarithm Continued….
  11. Bits in a Binary Number
  12. Repeated Doubling
  13. Repeated Halving 
  14. Continued…. Many of the algorithms contain logarithms, because of the  repeated halving principle, which holds that, starting at N,  we can halve only logarithmically many times In other words, an algorithm is O(log N) if it takes  constant (O( 1)) time to cut the problem size by a constant  fraction (usually 112) This condition follows directly from the fact that there  will be O(log N) iterations of the loop Any constant fraction will do because the fraction is  reflected in the base of the logarithm, and Theorem 6.4  tells us that the base does not matter
  15. Theorem 6.5
  16. Static Searching Problem An important use of computers is to look up data If the data are not allowed to change (e.g., it is stored on a  CD­ROM), we say that the data are static A static search accesses data that are never altered
  17. Problem GIVEN AN INTEGER X AND AN ARRAY A,  RETURN THE POSITION OF X IN A OR AN  INDICATION THAT IT IS NOT PRESENT. IF X  OCCURS MORE THAN ONCE, RETURN ANY  OCCURRENCE. THE ARRAY A IS NEVER   ALTERED.
  18. Solution  An example of static searching is looking up a person in  the telephone book The efficiency of a static searching algorithm depends on  whether the array being searched is sorted In the case of the telephone book, searching by name is  fast, but searching by phone number is hopeless Some solution Sequential Search Binary Search 
  19. Sequential Search When the input array has not been sorted, we have little  choice but to do a linear sequential search that steps  through the array sequentially until a match is found The complexity of the algorithm is analyzed in three ways First, we provide the cost of an unsuccessful search Second, we give the worst­case cost of a successful search  Finally, we find the average cost of a successful search Analyzing successful and unsuccessful searches  separately is typical 
  20. Continued…. An unsuccessful search requires the examination of every  item in the array, so the time will be O(N) In the worst case, a successful search, too, requires the  examination of every item in the array because we might  not find a match until the last item. Thus the worst­case  running time for a successful search is also linear On average, however, we search only half an array. That  is, for every successful search in position i, there is a  corresponding successful search in position N­1­i  However, N/2 is still O(N)
nguon tai.lieu . vn