Vectors and Array Lists 2004 Goodrich Tamassia Vectors

  • Slides: 11
Download presentation
Vectors and Array Lists © 2004 Goodrich, Tamassia Vectors 1

Vectors and Array Lists © 2004 Goodrich, Tamassia Vectors 1

The Vector ADT (§ 5. 1) The Vector ADT extends the notion of array

The Vector ADT (§ 5. 1) The Vector ADT extends the notion of array by storing 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 (e. g. negative rank, or larger than current size) © 2004 Goodrich, Tamassia Main vector operations: n object elem. At. Rank(integer r): returns the element at rank r without removing it, 0≤r≤s-1 n object replace. At. Rank(integer r, object o): replace the element at rank with o and return the old element, 0≤r≤s-1 n insert. At. Rank(integer r, object o): insert a new element o to have rank r, 0≤r≤s n object remove. At. Rank(integer r): removes and returns the element at rank r, 0≤r≤s-1 Additional operations size() and is. Empty() Vectors 2

Applications of Vectors Direct applications n Sorted collection of objects (elementary database) Indirect applications

Applications of Vectors Direct applications n Sorted collection of objects (elementary database) Indirect applications n n Auxiliary data structure for algorithms Component of other data structures © 2004 Goodrich, Tamassia Vectors 3

Array-based Vector Use an array V of size N A variable n keeps track

Array-based Vector Use an array V of size N A variable n keeps track of the size of the vector (number of elements stored) Operation elem. At. Rank(r) is implemented in O(1) time by returning V[r] V 0 1 2 © 2004 Goodrich, Tamassia n r Vectors 4

Insertion In operation insert. At. Rank(r, o), we need to make room for the

Insertion In operation insert. At. Rank(r, o), we need to make room for the new element by shifting forward the n r elements V[r], …, V[n 1] In the worst case (r = 0), this takes O(n) time V 0 1 2 r n 0 1 2 o r V V © 2004 Goodrich, Tamassia Vectors n 5

Deletion In operation remove. At. Rank(r), we need to fill the hole left by

Deletion In operation remove. At. Rank(r), we need to fill the hole left by the removed element by shifting backward the n r 1 elements V[r + 1], …, V[n 1] In the worst case (r = 0), this takes O(n) time V 0 1 2 o r n 0 1 2 r V V © 2004 Goodrich, Tamassia Vectors n 6

Performance In the array based implementation of a Vector n n n The space

Performance In the array based implementation of a Vector n n n The space used by the data structure is O(n) size, is. Empty, elem. At. Rank and replace. At. Rank run in O(1) time insert. At. Rank and remove. At. Rank run in O(n) time If we use the array in a circular fashion, insert. At. Rank(0) and remove. At. Rank(0) run in O(1) time In an insert. At. Rank operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one © 2004 Goodrich, Tamassia Vectors 7

Growable Array-based Vector In a push operation, when Algorithm push(o) the array is full,

Growable Array-based Vector In a push operation, when Algorithm push(o) the array is full, instead of if t = S. length 1 then throwing an exception, we A new array of can replace the array with size … a larger one for i 0 to t do A[i] S[i] How large should the new S A array be? n n incremental strategy: increase the size by a constant c doubling strategy: double the size © 2004 Goodrich, Tamassia Vectors t t+1 S[t] o 8

Comparison of the Strategies We compare the incremental strategy and the doubling strategy by

Comparison of the Strategies We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operations We assume that we start with an empty stack represented by an array of size 1 We call amortized time of a push operation the average time taken by a push over the series of operations, i. e. , T(n)/n © 2004 Goodrich, Tamassia Vectors 9

Incremental Strategy Analysis We replace the array k = n/c times The total cost

Incremental Strategy Analysis We replace the array k = n/c times The total cost T(n) of a series of n push operations is proportional to n + 2(c + 2 c + 3 c + 4 c + … + kc) = n + 2 c(1 + 2 + 3 + … + k) = n + 2 c × k(k + 1)/2 = O(n+k 2) Since c is a constant, T(n) = O(n+k 2) = O(n+(n/c)2) = O(n 2) The amortized time of 1 push is O(n 2)/n = O(n) © 2004 Goodrich, Tamassia Vectors 10

Doubling Strategy Analysis We replace the array k = log 2 n times geometric

Doubling Strategy Analysis We replace the array k = log 2 n times geometric series The total time T(n) of a series of 2 n push operations is 4 proportional to 1 1 n + 2×(1 + 2 + 4 + 8 + …+ 2 k) = n + 2×(2 k + 1 1) = 3 n 2 = O(n) 8 The amortized time of 1 push is O(n)/n = O(1) © 2004 Goodrich, Tamassia Vectors 11