PQ and Binary Heap Flashcards

1
Q

What are the 3 implementations of Priority Queue?

A
  1. Circular Array (Quick Find)
  2. Circular Array (Quick Insert)
  3. Binary Heap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does Circular Array (Quick Find) work?

A

Maintain a PQ by always inserting in the correct place. Front and Back index are maintained.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Time complexity of enqueue and dequeue of Circular Array (Quick Find)

A

Enqueue is O(N) as you search through the array to find a suitable insertion point and advance the back index. Dequeue is O(1) as you just dequeue from the front of the queue and advance the front index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does Circular Array (Quick Insert) work?

A

Same as Quick Find, but instead, you scan through the queue to find the item of the highest priority to dequeue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Time complexity of enqueue and dequeue of Circular Array (Quick Find)

A

Enqueue is O(1) since you just insert to the back of the queue. Dequeue is O(N) since you have the search for the item of highest priority. You then need to close the gap, which is O(N).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

2 Properties of a Binary Heap

A
  1. It’s a complete tree -> All levels are completely filled except for the last level, where all leaves are flushed to the left.
  2. For max heap, parent is always greater than children and for min heap, parent is always less than children.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the main implementation of a binary heap?

A

1-based array -> we start at index 1 because we can easily calculate the index of the right, left child and parent of any vertex

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Time Complexity of Insert

A

O(logn) -> Insert to the last position and shift up

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Time Complexity of ExtractMax

A

O(logn) -> Replace the first index with the last index, then shift down the last replaced value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Time Complexity of ShiftUp

A

O(logn) -> Traverse the height

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Time Complexity of ShiftDown

A

O(logn) -> Traverse the height

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Time Complexity of CreateHeap

A

O(N) -> Copy the content in O(N). For every parent -> O(N/2), shift it down O(logn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Time Complexity of HeapSort

A

O(NlogN) -> Convert the array to a binary heap -> O(N) and pull out the items in order of priority -> O(N) x O(logn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly