11. The Vector Data Type, and Amortised Analysis
10/03/23
Vector ADT
The Vector is an ADT, corresponding to generalising the notion of the Array Key Ideas:
The "index" of an entry in an array can be though of as the "number of elements preceding it"
E.g. in an array A, then A[2] has two elements, A[0], A[1] that precede it
It is then called a "rank"
The notion of "rank" can be more general than the idea of index
The Vector ADT is based on the array CDT, and stores a sequence of arbitrary objects
An element can be accessed, inserted or removed by specifying its rank (number of elements preceding it)
An exception is thrown if an incorrect rank is specified
Vector as a Stack
- Common usage of a Vector is as a Stack
- Add and remove elements at the end
- This is very useful if reading elements from a file, and not knowing how many there will be
Applications
There is not an automatic limit on the storage size
Direction applications - sorted collection of objects
Indirect applications - Auxiliary data structure for many algorithms. Components of other data structures
Array-based Vector - Operation
elemAtRank(r)
is implemented in time by returningInsertion - Need to make room for the new element by shifting forward the elements. In the worst case , this takes time
Deletion - Need to fill the hole left by removed element by shifting backward the elements. In the worst case this takes time
Performance
- Space used by the data structure is
size
,isEmpty
,elemAtRank
andreplaceAtRank
run ininsertAtRank
andremoveAtRank
run in timepush
runs in time, as do not need to move elementspop
runs in time
Growable Array-based Vector
- When the array is full, instead of throwing an exception, can replace the array with a larger one. Resizing has a high cost of , as needs to copy at all elements
- Compare the incremental strategy and the doubling strategy by analysing the total time needed to perform a series of push operations
- Call amortized time of a push operation the average time taken by a push over the series of operations
Amortize
Meaning - Refers to writing off, or paying off, debts over a period of time. Similar to the way a mortgage for a house is paid back over many years, as opposed to need to pay all in one go.
Remarks on Amortised Analysis
- Suppose individual operation takes time in the worst-case
- Suppose do a sequence of operations:
- Suppose s such operations take total time
- Then is an upper-bound to the total time
- But, such an upper-bound might not ever occur
- The time might well be even in the worst-case
- The average time per operation can be the most relevant quantity in practice
Amortised - (long) real sequence of dependent operations Average - Set of (possibly independent) operations
Describing amortised costs
- Usual big-Oh family is still used to described amortised analysis
- Have different measures of the runtime cost:
- Worse case - Cost per operation of a sequence not just worst case of a single operation
Incremental Strategy Analysis
- Replace the array times
- Each "replace" costs the current size
- The total time of a series of push operations is proportional to
- Since is a constant, is , i.e.
- The amortized time of a push operation is
- This is bad as the normal cost of a push is
Doubling Strategy Analysis
- Assume n is a power of 2. Replace the array times
- The total time of a series of push operations is proportional to
Sometimes have to use geometric sequences. Have to set is and end up doing induction.