MIT 6.006- Data structures Flashcards
(69 cards)
Define a computational problem
a binary relation between two sets, the set of inputs and the set of outputs, usually expressed as a predicate.
For example, given all birthday of students, which two students were born in the same day etc
What are the course goals?
- Solve computational problems
- Prove correctness
- Argue efficiency
- Communication (be able to convince people it’s correct and efficient)
What’s the difference between an interface and a data structure?
Interface are the operations supported = What I want to do
Data structure - how I do those operations (algorithms). eg i can have different data structures supporting the same interface…
Interface is the “problem” and a specific data structure is the “solution” or implementation
What two main interfaces types are we going to discuss during this class?
Sets (we care about the items values but not order) and sequences (we care about the sequence)
What are the two main DS approaches?
- Array based (static)
- pointers based (dynamic)
What’s the interface for a static sequence dataset?
- Init
- get_at(index)
3.set_at(index,value)
4.len
5.iterate_seq
What’s the natural solution for the interface for a static sequence dataset?
a static array…
What’s the interface for a dynamic sequence dataset? What special operations exist, in why do we care about them?
All of the operations of a static sequence dataset plus:
-Insert_at(i,x) -> push all the items from i to i+1…n+1
remove_at(i)->push all items from i+1…n to i…n-1
special operations-
remove/add first/last:
While those are special cases of the full interface, we care about those sub-case of the full interface because we might be able to implement those efficiently.
What’s a natural solution to the dynamic sequence interface?
a linked list
What’s the time complexity of the following data structures for a dynamic sequences interface:
static array vs linked list
static array/linked list:
init- O(n)/O(n)
set/ get_at(index) - O(1) vs O(n)
iterate - O(n)
remove_at - O(n)
insert_at - O(n)
insert/delete front/last :
O(n) vs O(1) (last only for doubly linked list and if i save a pointed to the last element)
Explain the idea behind dynamic arrays
-relax the contraint of a static array that it has to be the size of exactly n. Instead make it O(n) (of course bigger than n haha, we’re going to discuss 2n but many options are possible).
-basically maintain an array of size n with the n elements that i store plus extra space at the end.
-Now set_last is O(1) as long as actual length of array allocated is at least n+1. if size==n we have to allocate a new array of size O(n) (say,2*n)
What dynamic arrays exist in python?
a python’s list is a dynamic array
an intuitive way to remember that the sum of 1+2+…+2^K is 2^(K+1)-1
it’s just 111111…11 K times, this plus 1 is just 10000000000 of total size of K+1 which is 2^(K+1).
How much time does the resizing operation of a dynamic array cost?
It’s just O(1) AMORTIZED since we pay, if we grow size to be always 2*size:
1+2+4+….+n=sum(1,log n)(2^i) = O(n)
and we do this after n operations, so the amortized total time (average time per operation)is O(1).
Define amortized time
We say that an operation takes T(n) amortized time if for k operations it takes <=K*T(n) time,
averaging over operations sequence
Compare the following operation(s) for, and to which interface does the operation belong
- Dynamic array
- static array
- linked list
get_at,set_at
The operation is both for static sequence and dynamic sequence
O(1) for dynamic and static array,O(n) for linked list
Compare the following operation(s) for, and to which interface does the operation belong
- Dynamic array
- static array
- linked list
insert_first, delete first
Dynamic array: O(n)
static array: O(n)
linked list: O(1)
operation is from the dynamic sequence interface
Compare the following operation(s) for, and to which interface does the operation belong
- Dynamic array
- static array
- linked list
insert_last, delete last
dynamic array: O(1) amortized
static array: O(n)
linked list: O(1) (or O(n) if it’s not a doubly linked list….)
operation is from the dynamic sequence interface
Compare the following operation(s) for, and to which interface does the operation belong
- Dynamic array
- static array
- linked list
insert_at, delete at
O(n) for all of them
operation is from the dynamic sequence interface
a simple dynamic array will have O(n) for the operaion “insert first”. How can I make it O(1)?
O(1)- amortized-
simply add another dynamic array(free space) at the ‘beginning’ of the data as well. This could be just another flipped dynamic array.
Descrive the interface of a set
-build(I) - given iterable I, builds a set from the items
-len
-find(key)
-insert(x)
-delete(key) - remove an object with the key
iter_ord - iterate by order of keys
find_min/find_max
find_next(k) - find the object with the smallest key greater than key
find_prev
Dumping all of the objects into an unsorted array is a simple way of implementing the set interface. How long does each of the operations take? For example, for a dynamic array for insert(x), how long, amortized, will it take?
O(n) for every single operation, including insert(x), and that is because i have to iterate the entire array to make sure that an element of the same key doesn’t exist (even if, amortized, the allocation of the new memory is O(1) as in the dynamic sequence interface)
Sorted dynamic array when implementing the set interface - how long does each of the operations take?
Build - O(nlogn)
find - O(logn)
delete/insert(x) - O(n) (I may need to move all of the objects)
find_min/find_max O(1)
find_next O(logn)
In the context of sorting, what does the following words mean?
- Destructive
2.In place
- Destructive - overwrites the input array
- In place- O(1) extra space