Binary and Binomial Heaps: Data Structures for Priority Queues, Slides of Computer Science

An in-depth exploration of binary and binomial heaps, two essential data structures used for implementing priority queues. The slides cover the definition, properties, operations, and applications of binary and binomial heaps, including dijkstra's shortest path algorithm, prim's mst algorithm, and heapsort. The document also discusses the differences between binary and binomial heaps and their respective advantages.

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharmaveer
dharmaveer 🇮🇳

4.5

(4)

54 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Binary and Binomial Heaps
These lecture slides are adapted
from CLRS, Chapters 6, 19.
2
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.
. . .
3
Priority Queues in Action
PQinit()
for each v V
key(v) ←∞
PQinsert(v)
key(s) 0
while (!PQisempty())
v = PQdelmin()
for each w Q s.t (v,w) E
if π(w) > π(v) + c(v,w)
PQdecrease(w, π(v) + c(v,w))
Dijkstra’s Shortest Path Algorithm
4
Dijkstra/Prim
1 make-heap
|V| insert
|V| delete-min
|E| decrease-key
Priority Queues
make-heap
Operation
insert
find-min
delete-min
union
decrease-key
delete
1
Binary
log N
1
log N
N
log N
log N
1
Binomial
log N
log N
log N
log N
log N
log N
1
Fibonacci *
1
1
log N
1
1
log N
1
Relaxed
1
1
log N
1
1
log N
1
Linked List
1
N
N
1
1
N
is-empty 1 1 1 11
Heaps
O(|E| + |V| log |V|)O(|E| log |V|)O(|V|2)
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Binary and Binomial Heaps: Data Structures for Priority Queues and more Slides Computer Science in PDF only on Docsity!

Binary and Binomial Heaps

These lecture slides are adaptedfrom CLRS, Chapters 6, 19.

Priority Queues

Supports the following operations.

n^

Insert element x. n^

Return min element. n^

Return and delete minimum element. n^

Decrease key of element x to k.

Applications.

n^

Dijkstra’s shortest path algorithm. n^

Prim’s MST algorithm. n^

Event-driven simulation. n^

Huffman encoding. n^

Heapsort. n^

3

Priority Queues in Action

PQinit()for each v

V

key(v)

PQinsert(v)

key(s)

while (!PQisempty())

v = PQdelmin()for each w

Q s.t (v,w)

E

if

π

(w) >

π

(v) + c(v,w)

PQdecrease(w,

π

(v) + c(v,w))

Dijkstra’s Shortest Path Algorithm

Dijkstra/Prim1 make-heap|V| insert|V| delete-min|E| decrease-key

Priority Queues

Operationmake-heap

insert find-min delete-min

union

decrease-key

delete

Binarylog N

log N

N

log Nlog N

Binomial

log Nlog Nlog Nlog Nlog Nlog N

Fibonacci *

log N

log N

Relaxed

log N

log N

Linked List

1 N N 11 N

is-empty

Heaps

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

O(|E| log |V|)

O(|V|

Docsity.com

5

Binary Heap: Definition

Binary heap.

n^

Almost complete binary tree.

-^

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

n^

Min-heap ordered.

-^

every child greater than (or equal to) parent

06

14

78

18

81

77

91

45

53

47

64

84

99

83

Binary Heap: Properties

Properties.

n^

Min element is in root. n^

Heap with N elements has height =

log

2

N

06

14

78

18

81

77

91

45

53

47

64

84

99

83

N = 14Height = 3

7

Binary Heaps: Array Implementation

Implementing binary heaps.

n^

Use an array: no need for explicit parent or child pointers.

-^

Parent(i) =

i/

-^

Left(i)

= 2i

-^

Right(i)

= 2i + 1

06

14

78

18

81

77

91

45

53

47

64

84

99

83

Binary Heap: Insertion

Insert element x into heap.

n^

Insert into next available slot. n^

Bubble up until it’s heap ordered.

-^

Peter principle: nodes rise to level of incompetence

06

14

78

18

81

77

91

45

53

47

64

84

99

83

42

next free slot

Docsity.com

13

Binary Heap: Delete Min

Delete minimum element from heap.

n^

Exchange root with rightmost leaf. n^

Bubble root down until it’s heap ordered.

-^

power struggle principle: better subordinate is promoted

06

14

78

18

81

77

91

42

45

47

64

84

99

83

53

Binary Heap: Delete Min

Delete minimum element from heap.

n^

Exchange root with rightmost leaf. n^

Bubble root down until it’s heap ordered.

-^

power struggle principle: better subordinate is promoted

53

14

78

18

81

77

91

42

45

47

64

84

99

83

06

15

Binary Heap: Delete Min

Delete minimum element from heap.

n^

Exchange root with rightmost leaf. n^

Bubble root down until it’s heap ordered.

-^

power struggle principle: better subordinate is promoted

53

14

78

18

81

77

91

42

45

47

64

84

99

83

exchange with left child

Binary Heap: Delete Min

Delete minimum element from heap.

n^

Exchange root with rightmost leaf. n^

Bubble root down until it’s heap ordered.

-^

power struggle principle: better subordinate is promoted

14

53

78

18

81

77

91

42

45

47

64

84

99

83

exchange with right child

Docsity.com

17

Binary Heap: Delete Min

Delete minimum element from heap.

n^

Exchange root with rightmost leaf. n^

Bubble root down until it’s heap ordered.

-^

power struggle principle: better subordinate is promoted

n^

O(log N) operations.

14

18

78

53

81

77

91

42

45

47

64

84

99

83

stop: heap ordered

Binary Heap: Heapsort

Heapsort.

n^

Insert N items into binary heap. n^

Perform N delete-min operations. n^

O(N log N) sort. n^

No extra storage.

19

H

1

H

2

14

78

18

81

77

91

11

62

53

64

84

99

41

Binary Heap: Union

Union.

n^

Combine two binary heaps H

1

and H

2

into a single heap.

n^

No easy solution.

-^

(N) operations apparently required

n^

Can support fast union with fancier heaps.

Priority Queues

Operationmake-heap

insert find-min delete-min

union

decrease-key

delete

Binarylog N

log N

N

log Nlog N

Binomial

log Nlog Nlog Nlog Nlog Nlog N

Fibonacci *

log N

log N

Relaxed

log N

log N

Linked List

1 N N 11 N

is-empty

Heaps

Docsity.com

25

Binomial Heap: Implementation

Implementation.

n^

Represent trees using left-child, right sibling pointers.

-^

three links per node (parent, left, right)

n^

Roots of trees connected with singly linked list.

-^

degrees of trees strictly decreasing from left to right

48 50

31

17

44

10

6

(^337)

18

29

6

37

3

18

48

31

50

10

44

17

heap

29

Leftist Power-of-2 Heap

Binomial Heap

Parent

Left

Right

Binomial Heap: Properties

Properties of N-node binomial heap.

n^

Min key contained in root of B

, B 0
,... , B 1

.k

n^

Contains binomial tree B

i^

iff b

= 1 where bi^

⋅n

b

b 2

b 1

0

is binary

representation of N. n^

At most

log

2

N
^
  • 1 binomial trees.

n^

Height

log

2

N
B

4

B

0

B

1

45 55

30 32

23 24

22

48 50

31

17

44

8

29

10

6

(^337)

18

N = 19# trees = 3height = 4binary = 10011

27

Binomial Heap: Union

Create heap H that is union of heaps H’ and H’’.

n^

"Mergeable heaps." n^

Easy if H’ and H’’ are each order k binomial trees.

-^

connect roots of H’ and H’’

-^

choose smaller key to be root of H

H’’

45 55

30 32

23 24

22

48 50

31

17

44

8

29

10

6

H’

Binomial Heap: Union

45 55

30 32

23 24

22

48 50

31

17

44

8

29

10

6

(^3 )

18

41

33

28

15

(^7 )

12

Docsity.com

29

Binomial Heap: Union

45 55

30 32

23 24

22

48 50

31

17

44

8

29

10

6

(^3 )

18

41

33

28

15

(^7 )

12

Binomial Heap: Union

45 55

30 32

23 24

22

48 50

31

17

44

8

29

10

6

(^337)

41

33

28

15

(^725)

12 18

18 12

31

45 55

30 32

23 24

22

48 50

31

17

44

8

29

10

6

(^337)

41

33

28

15

(^725)

12 18

25

37

7

3

12 18

18 12

45 55

30 32

23 24

22

48 50

31

17

44

8

29

10

6

(^337)

41

33

28

15

12 7 25

18

25

37

7

3

28 41

33

25

37

15

7

3

12 18

18 12

Docsity.com

37

Binomial Heap: Delete Min

Delete node with minimum key in binomial heap H.

n^

Find root x with min key in root list of H, and delete n^

H’

broken binomial trees

n^

H

Union(H’, H)

Running time. O(log N)

45 55

30 32

23 24

22

48 50

31

17

(^637)

18

44

8

29

10

H
H’

3

(^637)

18

x 55

30 32

23 24

22

48 50

31

17

44

8

29

10

H

Binomial Heap: Decrease Key

Decrease key of node x in binomial heap H.

n^

Suppose x is in binomial tree B

.k

n^

Bubble node x up the tree if x is too small.

Running time. O(log N)

n^

Proportional to depth of node x

log

2

N
^

depth = 3

39

Binomial Heap: Delete

Delete node x in binomial heap H.

n^

Decrease key of x to -

n^

Delete min.

Running time. O(log N)

Binomial Heap: Insert

Insert a new node x into binomial heap H.

n^

H’

MakeHeap(x)

n^

H

Union(H’, H)

Running time. O(log N)

3

(^637)

18

45 55

30 32

23 24

22

48 50

31

17

44

8

29

10

H

x H’

Docsity.com

41

Binomial Heap: Sequence of Inserts

Insert a new node x into binomial heap H.

n^

If N =

, then only 1 steps.

n^

If N =

, then only 2 steps.

n^

If N =

, then only 3 steps.

n^

If N =

, then only 4 steps.

Inserting 1 item can take

(log N) time.

n^

If N =

, then log

2

N steps.

But, inserting sequence of N items takes O(N) time!

n^

(N/2)(1) + (N/4)(2) + (N/8)(3) +...
2N

n^

Amortized analysis. n^

Basis for getting most operationsdown to constant time.

48 50

31

17

44

29

10

3

(^637)

x

1

1

=^

N

N

N n^

n

N

n

Priority Queues

Operationmake-heap

insert find-min delete-min

union

decrease-key

delete

Binarylog N

log N

N

log Nlog N

Binomial

log Nlog Nlog Nlog Nlog Nlog N

Fibonacci *

log N

log N

Relaxed

log N

log N

Linked List

1 N N 11 N

is-empty

Heaps

just did this

Docsity.com