Average-Case Running Time

  • In practice worst-case running time analysis can be misleading, with a variety of algorithms having a poor worst-case performance still yet performing well on the vast majority of inputs.

  • Let func be a program, for each , define set to be the set of all inputs so func of size .

  • Average-case running time of func is defined as the function where

    • Need to precisely define “all possible inputs of length ” for the given function

    • For infinite set of inputs is a common problem. To resolve it, choose a particular set of allowable inputs, and compute the average running time for that set

Perform an average-case running time analysis

  • Recall the linear search algorithm

    def search(lst: list, x: Any) -> bool:
        """Return whether x is in lst."""
        for item in lst:
            if item == x:
                return True
        return False
     
    • Previously seen the worst-case running time of this function is , where is the length of the list
    • Analysis the average running time using two different input sets

A first example

  • Let , choose the input set to be set of inputs where

    • lst is always the list [0, 1, 2, ..., n-1], x is an integer between 0 and n-1 inclusive
  • Use to denote this set, for this definition, know that , combine with the definition, get

  • Let , this includes all possible inputs, calculate running time in terms of

    • Know that there will be exactly loop iterations until is found in the list
    • Each iterations takes constant time, the loop in total takes steps
  • So for the running time of search(lst, x) equals , calculate the average running time

    And so the average-case running time for search on this set of input is which is

    • Only true for this specific choice of inputs
    • The avenge-case running is asymptotically equal to that of the worst-case for this case

A second example: search in binary lists

  • Use the same function (search) but choose a different input set.
  • Let , and let be the set fo inputs (lst, x) where
    • lst is a list of length that contains only ’s and ’s , x = 0
    • This case, x is fixed, and lst has multiple possibilities.
  1. Compute the number of inputs,

    • Since x is always , the the size of is equal to the number of lists of ’s and ’s of length .
    • Therefore the size of is , since there are independent choice for each of the elements of the lists, and each choice has two possible values.
  2. Partition the inputs by their running time

    • For the function search, if lst has a at index , then the loop will take iterations.

    • So, divide up the lists based on the index of the first in the list, and treat the list [1, 1, ..., ] as a special case, since it has no ‘s.

    • Formally, for each , define to be the set of binary lists of length where their first occurs at index

      • Is the set of binary lists lst where lst[0] == 0
        • (every element in takes 1 step for search)
      • Is the set of binary lists lst where lst[0] == 0 and lst[1] == 0
        • Every element in takes 2 steps for search
      • Is the set of binary lists lst where lst[j] == 1 for all natural numbers and lst[i] == 0
        • ​ Every element in takes steps for search
    • Define a special set that contains just the list [1, 1, ..., 1]. That input causes search to take steps: steps for the loop, and then 1 step for the return False at the end

  3. Compute the size of each partition

    • For
  4. Using Step 2 and 3, calculate the sum of the running time for all the inputs in

  5. Using Step 1 and 4, calculate the average-case running time for