Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 113: Data Structures and Algorithms

Similar presentations


Presentation on theme: "CS 113: Data Structures and Algorithms"— Presentation transcript:

1 CS 113: Data Structures and Algorithms
Abhiram G. Ranade Some remarks on vectors

2 How much time do different vector operations take?
Indexing: The vector class stores elements in an array on the heap. So indexing takes time O(1) Appending an element (push_back) When allocating the array, some room is kept for pushbacks. If size of vector < allocated space, then new element is placed beyond the current size. So time taken = O(1) If size of vector = size of allocated array, new space must be allocated, and the vector must be copied to new space. So time taken = O(vector size) Typical strategy: If allocated size n is exceeded because of pushback, allocate size 2n (and use only n+1 elements in it).

3 Example of doubling protocol
Statement executed vector v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); v.push_back(6); v.push_back(7); Content of array _ allocation 1 allocation + copy 1 1 2 3 _ a + copy 2 _ _ _ a + copy 4 _ _ _

4 So what is the cost of a pushback?
Theorem: If the size of a vector grows to n as a result of pushback operations, the total time spent on pushback operations = O(n) Proof: Total time = time on allocating memory + time spent copying Since the allocated size always doubles, after k allocations the allocated size will be 2k-1 Because the last of these allocations was necessary, n > 2k-2 Thus k-2 < log n So k < 2+log n Time spent on copying: k-2 = 2*2k-2 – 1 < 2n -1 So total is at most 2n + log n = O(n) Average per pushback is O(1), so pushbacks can be considered to be as fast as indexing!

5 Remarks If n push_back operations happen, some push_back will take time O(n), but total time is also O(n), i.e. most push_backs take time O(1), and average per operation is O(1). This called “amortization”. Amortization is distributing the cost over many items for the purpose of accounting. Inserting in the middle of a vector: requires copying after every operation so time for each operation is O(n) thus this is an expensive operation always.

6 Homework Suppose like push_back, we have pop_back which just drops off the last element. We would also like that the size of the allocated array be at most k times the size of the used portion at all times, so that memory will be used at least somewhat efficiently. So when the number of used elements fall below 1/k you should deallocate memory. What k will you use so that push_back and pop_back work in O(1) time in an amortized sense? (if there have been n push_backs and m pop_backs, then we want total time to be O(m+n), and memory to always be k times the number of elements in the vector at that time)


Download ppt "CS 113: Data Structures and Algorithms"

Similar presentations


Ads by Google