Week 3 Flashcards
What is the sorting problem?
- given a collection of n elements and a total ordering arrange the elements into a non-decreasing order
What is a priority queue?
- a priority queue is a container of elements, each having an associated key
- Keys determine the priority used in picking elements to be removed
What are the 4 fundamental methods performed on a priority queue
- insertItem(k, e) - element e with key k
- removeMin() - remove element with the minimum key
- minElement() - return element with the minimum key currently in the priority queue
- minKey() - return key of minimum element
what order are elements stored in in priority queues?
- we work on maintaining the element with the minimum key currently in the priority queue
How is a deletion completed in a priority queue and what is the time complexity of this?
- The elements with the current minimum key is deleted and the priority queues minimum is updated.
- this is completed in logarithmic time
How can we use a priority queue to perform sorting on a set of elements in two phases?
- Phase 1: put elements of set one by one into an initially empty priority queue by a series of insertions, after each insertion the priority queue pulls up the minimum so it’s ordered and we have the minimum in linear time
- Phase 2: extract the elements from the priority queue with a series of remove operations. When all items have been extracted and added to a list, this list will be sorted in non-decreasing order
Describe the primary queue sorting algorithm
- the priority queue is initially empty
- while the input set is not empty, remove the first element and insert it into the priority queue, do n times for every item in the list
- then when we have a full sorted priority queue, remove all n items, by searching through the priority queue and adding the minimum element to the new sorted list.
What is the running time of the priority queue sorting algorithm?
O(nlog) because there’s technically 2nlogn since putting n elements in PQ then searching logn to find where to insert them takes nlogn time, and we do this again n times when we remove every item from the pq to add to a sorted list, hence nlogn, but 2nlogn = O(nlogn)
What is a heap data structure?
- This is an implementation of a priority queue that is efficient for both insertions and deletions
- elements and keys are stored in a complete binary tree (so every level apart from perhaps the bottom level will have a the maximum number of children possible)
What are the properties of a heap data strucutre?
- In a heap T, for every node v (excluding the root) it’s key is >= it’s parents key
- if we take any path from the root to an external leaf, it only had elements that are non-decreasing, so the root must be the global minimum
How can we implement a heap data structure?
- As a nearly complete binary tree containing elements with keys satisfying the heap-order property stored in an array
- need to store a reference to the last used node T in the bottom level of the binary tree
- a comparator function that defines the total order relation on keys and which is used to maintain the minimum/maximum element at the root of T
What is the last used node/element at the bottom level of the binary tree/heap?
The rightmost element/node on the lowest level of the tree
How are items inserted into heaps?
- begin by adding the element to the bottom of tree in the position of the first unused/empty child
- then if need be, the new element bubbles it’s way up the heap until the heap order property is restored
What is the time complexity of insertion into a heap?
At max we’ll need to do 2^n number of swaps where n is the height of the heap, the insertion is only O(1) so the time complexity is log2(n)
How is deletion in a heap carried out?
- deletion consists of removing the minimum/maximum element which is at the root, so we remove this
- the element in the ‘last’ position is immediately moved to the root and then sinks/bubbles down to restore heap-order property
When bubbling the item at the root node down, how do we decide which subtree’s element to swap with?
- We swap with the element that’s smallest.
- For example if the item at the root = 8 and the left subtree holds 6 and the right subtree holds 5, we would swap 8 with 5 at the right subtree
- this is because if we move the larger value up this will break the heap-order property
- if we moved 6 up, then 6 is bigger than 5 so the heap-order property would be broken.
What are the time complexities of finding the size/is empty, minElement, minKey, insertItem and removeItem methods for a heap?
- size, is Empty - O(1)
- minElement, minKey - O(1)
- insertItem - O(logn)
- removeMin - O(logn)
What is the divide and conquer method and how is it achieved?
- it’s a method for solving certain types of algorithmic problems
- divide the input data into two or more disjoint subsets
- recursively solve the subproblems associated with the subsets
- conquer: take the solutions to sub problems and merge them into a solution to solve the original problem