Vectors, Stacks & Queues Flashcards
What is a Vector in ADE?
An Abstract Data Type corresponding to generalising the notion of the ‘array’ CDT
Based off of the array CDT, and stores a sequence of arbitrary objects
What is a common usage of Vector? How does this work briefly?
Using it as a stack
Add and remove elements at the end i.e. maximum rank values
push(o) = insert after all the existing ones
pop() = remove the element that is located after all the existing ones
How does an array-based Vector work?
Use an array V of size N as the CDT
A variable n keeps track of the size of the vector (number of elements currently stored)
Operation elementAtRank(r) is O(1) time by returning V[r]
What is the time complexity of an insertion into an array-based Vector and how does it work?
Worst time complexity - O(n)
Shifts all the elements after the specified point forward another space, so as to make room for the new element
What is the time complexity of deletion in an array-based Vector and how does it work?
Worst case - O(n)
Shifts all the elements back a space that are after the specified point so as to ‘fill the hole’, thereby removing the redundant element
What are the time complexities for the space used by the data structure, the methods size(), isEmpty(), insertAtRank and removeAtRank, as well as push and pop?
Data structure - O(n)
size, isEmpty - O(1)
insertAtRank, removeAtRank - O(n)
push and pop - O(1) (unless needing to resize the array with push, in which case, this will change to O(n))
How would you go about increasing the size of an array-based Vector?
Incremental strategy - increase size by a constant c
Doubling strategy - double the size
What is the difference between amortised analysis and average case analysis?
Amortised - real sequence of dependent operations
Average - set of possibly independent operations
What is the amortised time of a push operation within an incremental strategy?
Amortised time of a push operation - O(n)
How does the incremental strategy work?
Replace the array k = n/c times (c being the length added to the array, and n being the number of elements
T(n) is O(n + k^2) i.e. O(n^2)
How does the doubling strategy work?
When the array needs to be extended, takes the current length of the array and then doubles it. Afterwards, transfers over all the old elements.
What is the amortised cost of the doubling strategy?
Gives an average of O(1) per push operation, as the overall amount of pushes performed at O(1) vastly outweigh those of O(n)
What is the stack ADT and how does it work?
Stores arbitrary references to objects
Insertions and deletions follow last-in-first-out (LIFO)
Main stack operations:
push(object) - inserts an element
pop(object) - removes and returns the last inserted element
Other elements are not accessible
What is a common implementation of a stack CDT?
Using it within an array
Add elements from left to right, and a variable (t) keeps track of the index of the top element
If trying to push to an already full array, will throw an exception
What is the performance of an array-based Stack and the limitations?
The space used is O(n)
Each operation runs in O(1)
Limitations:
Maximum size of the array-based Stack must be defined in advance and cannot be changed dynamically
Trying to push a new element into a full stack causes an implementation-specific exception