Docsity

Priority Queues and Binary Heaps, Slides of Computer Science

Partial preview of the text

Download Priority Queues and Binary Heaps and more Slides Computer Science in PDF only on Docsity!

Binary and Binomial Heaps

Priority Queues

• Supports the following operations.

  • Insert element x.
  • Return min element.
  • Return and delete minimum element.
  • Decrease key of element x to k.

• Applications.

  • Dijkstra's shortest path algorithm.
  • Prim's MST algorithm.
  • Event-driven simulation.
  • Huffman encoding.
  • Heapsort. -...

Priority Queues

Dijkstra/Prim

1 make-heap

|V| insert

|V| delete-min

|E| decrease-key

make-heap

Operation

insert

find-min

delete-min

union

decrease-key

delete

Binary

log N

log N

N

log N

log N

Binomial

log N

log N

log N

log N

log N

log N

Fibonacci *

log N

log N

Relaxed

log N

log N

Linked List

1 N N 1 1 N

is-empty 1 1 1 1 1

Heaps

O(|V| O(|E| log |V|) O(|E| + |V| log |V|)

2

Binary Heap: Definition

• Binary heap.

– Almost complete binary tree.

• filled on all levels, except last, where filled from left to

right

– Min-heap ordered.

• every child greater than (or equal to) parent

Binary Heaps: Array

Implementation

• Implementing binary heaps.

– Use an array: no need for explicit parent or child

pointers.

• Parent(i) = i/2

• Left(i) = 2i

• Right(i) = 2i + 1

Binary Heap: Insertion

• Insert element x into heap.

– Insert into next available slot.

– Bubble up until it's heap ordered.

• Peter principle: nodes rise to level of incompetence

83 84 996442 next free slot

Binary Heap: Insertion

• Insert element x into heap.

– Insert into next available slot.

– Bubble up until it's heap ordered.

• Peter principle: nodes rise to level of incompetence

swap with parent

Binary Heap: Insertion

• Insert element x into heap.

– Insert into next available slot.

– Bubble up until it's heap ordered.

• Peter principle: nodes rise to level of incompetence

– O(log N) operations.

stop: heap ordered

Binary Heap: Delete Min

• Delete minimum element from heap.

– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.

• power struggle principle: better subordinate is

promoted

Binary Heap: Delete Min

• Delete minimum element from heap.

– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.

• power struggle principle: better subordinate is

promoted

Binary Heap: Delete Min

• Delete minimum element from heap.

– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.

• power struggle principle: better subordinate is

promoted

exchange with right child

Binary Heap: Delete Min

• Delete minimum element from heap.

– Exchange root with rightmost leaf.

– Bubble root down until it's heap ordered.

• power struggle principle: better subordinate is

promoted

– O(log N) operations.

stop: heap ordered

Binary Heap: Union

• Union.

– Combine two binary heaps H

1

and H

2

into a single

heap.

– No easy solution.

• Ω(N) operations apparently required

– Can support fast union with fancier heaps.

H 1 H^2

Priority Queues

make-heap

Operation

insert

find-min

delete-min

union

decrease-key

delete

Binary

log N

log N

N

log N

log N

Binomial

log N

log N

log N

log N

log N

log N

Fibonacci *

log N

log N

Relaxed

log N

log N

Linked List

1 N N 1 1 N

is-empty 1 1 1 1 1

Heaps