2 - Abstract Data Types, Dynamic Arrays, Amortized Analysis Flashcards
What is abstraction? Give three examples of abstraction from real life.
Abstraction is the process of trying to identify the most important qualities of an object or model. (e.g. table of contents, menu, class schedule)
What is information hiding? How is it related to abstraction?
Information hiding is purposely not including some information as a way of controlling complexity.
How is encapsulation related to abstraction? Explain how a function can be viewed as a type of encapsulation. What information is being hidden or abstracted away?
Encapsulation is placing items into a unit, or capsule. The outside is the task being performed, and the inside is how that task is performed.
What makes an ADT description abstract? How is it different from a function signature or an interface?
The description just provides a metaphor for how it behaves.
An interface, on the other hand, lists out exactly the functions that a file may have.
Come up with another example from everyday life that illustrates the behavior of each of the six classic abstractions (bag, stack, queue, deque, priority queue, map).
Bag: Adding a value, asking if a value is there, and removing a value. (like box of candy)
Set: In addition to bag operations, no element may appear more than once. (like a VIP party)
Stack: LIFO, remembers the order (like an ordered bus).
Queue: FIFO, remembers the order (like a line)
Deque: Insert/remove from either end, but not the middle (like a bus with two doors).
Priority queue: maintains value in order of importance. (like triage)
Map: maintains pairs of elements (each unique key is matched to a corresponding value).
What collection is important, and why? Is order important? Is time of insertion important?
- names of students enrollled
- files being held until can be printed on printer
- URLs for recently visited web pages in browser
- names of patients waiting for operating room in ER
- names and associated employee records in company database
- Set - don’t care about order, but can only enroll once
- Queue - first in, first out
- Stack - want the most recent to be on top
- Priority queue - make sure the most urgent is taken care of first
- Map - must have unique key corresponding to that employee
In what ways is a set similar to a bag? In what ways are they different?
Both are simply unordered lists that allow you to see whether something is there.
They are different in that a bag can contain the SAME thing, while a set must contain unique things.
In what ways is a priority queue similar to a queue? In what ways are they different?
Both place some priority on time as a negative factor.
However, for a queue, relative time is the only factor. For a priority queue, there may be other factors.
What is a dynamic array?
A dynamic array is one that automatically resizes as elements are added on to ensure that the array does not overflow.
What does the term capacity refer to in a dynamic array? What does the term size refer to?
The capacity is how much memory is currently allocated, but the size is how much of that memory is actually storing information.
Can you describe the set of legitimate subscript positions in a dynamic array? Is this different from the set of legal positions in an ordinary array?
In a dynamic array, the legitimate subscript positions are only ones that are within the size or one after the array.
In a normal array, you can’t just add onto the end of the array.
How does the add function in a dynamic array respond if the user enters more values than the current capacity of the array?
The add function doubles the size of the array.
Suppose the user enters 17 numbers into a dynamic array that was initialized with a capacity of 5? What will be the final length of the data array? How many times will it have been expanded?
Once you add onto a 5-sized array, it’ll double into a 10. Once you add onto a 10-sized array, it’ll double into a 20-sized array.
What is the algorithmic execution time for the _dyArrayDoubleCapcity, if n represents the size of the final data array?
First, a new array is created (1). Next, the old values are copied into the new array (n/2). Next, the free statement releases old memory (1). Finally, the pointer is changed to reference the array. (1).
Overall, it’s just n.
Based on your answer to the previous question, what is the worst case algorithmic complexity of the function dyArrayAdd?
Since there’s only one for loop, the copying of the array, it would be O(n).