CS 113: Data Structures and Algorithms Abhiram G.

Published  . 0 views
↓ Download
CS 113: Data Structures and Algorithms Abhiram G.
1 / 1
CS 113: Data Structures and Algorithms Abhiram G. - slide 1 of 6 CS 113: Data Structures and Algorithms Abhiram G. - slide 2 of 6 CS 113: Data Structures and Algorithms Abhiram G. - slide 3 of 6 CS 113: Data Structures and Algorithms Abhiram G. - slide 4 of 6 CS 113: Data Structures and Algorithms Abhiram G. - slide 5 of 6 CS 113: Data Structures and Algorithms Abhiram G. - slide 6 of 6
Description: CS 113: Data Structures and Algorithms Abhiram G. Ranade Some remarks on vectors 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

Related Topics

Download Presentation

"CS 113: Data Structures and Algorithms Abhiram G." is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

slide1. CS 113: Data Structures and Algorithms Abhiram G. Ranade

Some remarks on vectors<br>
slide2. 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).<br>
slide3. 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
1 2 allocation + copy 1
1 2 3 _ a + copy 2
1 2 3 4
1 2 3 4 5 _ _ _ a + copy 4
1 2 3 4 5 6 _ _
1 2 3 4 5 6 7 _<br>
slide4. 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: 1+2+4+...2k-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!<br>
slide5. 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.<br>
slide6. 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)<br>