Priority Queues and Binary Heaps, Slides of Computer Science

An in-depth exploration of priority queues and binary heaps. It covers various operations such as make-heap, insert, find-min, delete-min, union, decrease-key, delete, and heap sort. The document also discusses the properties and use of binomial trees and heaps, as well as their representation in a binomial heap.

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharmaveer
dharmaveer 🇮🇳

4.5

(4)

54 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Binary and Binomial Heaps
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

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