Heapsort, Priority Queues and Selection Flashcards

1
Q

What is a heap?

A

A (maximum) heap is a complete binary tree having a key associated with each node, the key of each parent node being greater than or equal to the keys of its child nodes

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

What two operations does the heap ADT support?

A

insert new element and delete/retrieve maximum element

These both can be performed in O(log n) time.

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

What is the most common way to represent a heap?

A

In an array

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

Inserting a node into heap example

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

Deleting maximum node in a heap example

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

What is a priority queue?

A

A priority queue is a container for a totally-ordered set of elements (ADT)

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

What operations does a priority queue support?

A

insert, peek front, delete front, decrease key (i.e. priority of an element)

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

How is priority queue often implemented

A

Often is implemented using a binary heap data structure

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

What can priority queues be used to represent?

A

Particular kinds of stacks and queues.

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

The Selection Problem

A
  • We want to find the k-th smallest item (rank k) of an unsorted collection of n elements.
  • Can solve by sorting the list then pick the k-th element.

(not very efficient O(n lg n) )

  • Using a binary heap we have a Θ(n + k lg n) algorithm.
  • There is a Θ(n) algorithm for all input (worst case).
  • There is a (simple and practical) known average-case Θ(n) algorithm, derived from quicksort, called quickselect.
  • The idea is to do quicksort but only recurse into one of the two partitions that contains the k-th smallest element.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you create a selection algorithm that runs in Θ(n) for all input?

A

ensure that “good” pivots are found

median-of-medians linear-time algorithm

• The divide-and-conquer recursive M.-M. algorithm:

  1. 1 Partition input of n items into groups of five items.
  2. Find exact median of each of the 5-element groups.
  3. Find the median of these dn/5e medians (by recursion) as selected pivot.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly