Lecture 9
Learning objectives
After this class, you should be able to:
- Define the terms: best case analysis, worst case analysis, average case analysis, and amortized analysis.
- Explain how amortized time complexity analysis differs from average time complexity analysis.
- Given an algorithm, give an input in which the best case behavior is observed, and derive its time complexity.
- Given an algorithm, give an input in which the worst case behavior is observed, and derive its time complexity.
- Given an algorithm and probabilities for possible inputs, derive its average case time complexity.
- Given a sequence of operations, derive the amortized time complexity for the sequence of operations.
Reading assignment
- Section 2.8 - 2.9 ( pages 65-66 only, for sec 2.9), class notes.
- Review material on integration.
Exercises and review questions
- Exercises and review questions on current lecture's material
- Instead of doubling the capacity of a vector each time that the current capacity is exceeded, assume that we increase the size of the array by
10
. What is the amortized time complexity forn
inserts?- Vectors are typically implemented by doubling the current capacity, when the capacity is exceeded. In addition, let us suppose that we reduce the capacity by half, when the size is less than half the capacity, by allocating a smaller array and copying the current data to the new array (similar to the procedure with expanding the array). Show that this can cause a sequence of
n
push_back
andpop_back
operations to takeO(n2)
time.- Given a sorted vector, what is the average number of array elements accessed during an insertion (after the location to insert has been found), assuming that there is an equal probability of insertion at any permissible position in the array? (Note: (i) The vector should be in sorted order after the insertion too. (ii) Assume that an insert does not cause the capacity to be exceeded.)
- Given an unsorted vector, what is the worst case number of array elements accessed during a
push_back
operation? (Note: Assume that this operation does not cause the capacity to be exceeded.)- Given a vector, what is the average number of array elements accessed during a deletion (after the location to delete has been found), assuming that each element is equally likely to be deleted?
- Given a vector, what is the average number of array elements accessed in checking for membership of a given key in the vector? Assume that the probability of the key not being in the vector is
1/n
, and all members of the vector are equally likely to be the key.- Questions on next lecture's material
- What is the integral of
x2 dx
?- What is the integral of
xk dx
?- What is the integral of
1/x dx
?- What is the integral of
ex dx
?
Last modified: 23 Sep 2009