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
funcbe a program, for each , define set to be the set of all inputs sofuncof size . -
-
Average-case running time of
funcis 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
lstis always the list[0, 1, 2, ..., n-1],xis an integer between0andn-1inclusive
-
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 timeAnd so the average-case running time for
searchon 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)wherelstis a list of length that contains only ’s and ’s ,x = 0- This case,
xis fixed, andlsthas multiple possibilities.
-
Compute the number of inputs,
- Since
xis 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.
- Since
-
Partition the inputs by their running time
-
For the function
search, iflsthas 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
lstwherelst[0] == 0- (every element in takes 1 step for
search)
- (every element in takes 1 step for
- Is the set of binary lists
lstwherelst[0] == 0andlst[1] == 0- Every element in takes 2 steps for
search
- Every element in takes 2 steps for
- Is the set of binary lists
lstwherelst[j] == 1for all natural numbers andlst[i] == 0- Every element in takes steps for
search
- Every element in takes steps for
- Is the set of binary lists
-
Define a special set that contains just the list
[1, 1, ..., 1]. That input causessearchto take steps: steps for the loop, and then 1 step for thereturn Falseat the end
-
-
Compute the size of each partition
- For
-
Using Step 2 and 3, calculate the sum of the running time for all the inputs in
-
Using Step 1 and 4, calculate the average-case running time for