Binary and Binomial Heaps - PowerPoint PPT Presentation

About This Presentation
Title:

Binary and Binomial Heaps

Description:

Combine two binary heaps H1 and H2 into a single heap. No easy solution. ... Can support fast union with fancier heaps. 20. Priority Queues. make-heap. Operation ... – PowerPoint PPT presentation

Number of Views:450
Avg rating:3.0/5.0
Slides: 43
Provided by: kevin59
Category:
Tags: binary | binomial | heap | heaps

less

Transcript and Presenter's Notes

Title: Binary and Binomial Heaps


1
Binary and Binomial Heaps
These lecture slides are adaptedfrom 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
4
Priority Queues
Heaps
Operation
Binary
Binomial
Fibonacci
Relaxed
Linked List
make-heap
1
1
1
1
1
insert
log N
log N
1
1
1
find-min
1
log N
1
1
N
delete-min
log N
log N
log N
log N
N
union
N
log N
1
1
1
decrease-key
log N
log N
1
1
1
delete
log N
log N
log N
log N
N
is-empty
1
1
1
1
1
O(E V log V)
O(E log V)
O(V2)
Dijkstra/Prim 1 make-heap V insert V
delete-min E decrease-key
5
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

06
14
45
78
18
53
47
77
91
81
84
83
64
99
6
Binary Heap Properties
  • Properties.
  • Min element is in root.
  • Heap with N elements has height ?log2 N?.

06
N 14Height 3
14
45
78
18
53
47
77
91
81
84
83
64
99
7
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

06
1
14
45
2
3
78
18
53
47
4
5
6
7
77
91
81
84
83
64
99
8
9
10
12
14
11
13
8
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

06
14
45
78
18
53
47
77
91
81
84
83
64
99
9
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
06
14
45
78
18
53
47
77
91
81
84
83
64
99
10
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
06
14
45
78
18
42
47
77
91
81
84
83
64
99
53
11
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
06
14
42
78
18
45
47
77
91
81
84
83
64
99
53
12
Binary Heap Decrease Key
  • Decrease key of element x to k.
  • Bubble up until it's heap ordered.
  • O(log N) operations.

06
14
42
78
18
45
47
77
91
81
84
83
64
99
53
13
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

06
14
42
78
18
45
47
77
91
81
84
83
64
99
53
14
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

53
14
42
78
18
45
47
77
91
81
84
83
64
99
06
15
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 left child
53
14
42
78
18
45
47
77
91
81
84
83
64
99
16
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
14
53
42
78
18
45
47
77
91
81
84
83
64
99
17
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
14
18
42
78
53
45
47
77
91
81
84
83
64
99
18
Binary Heap Heapsort
  • Heapsort.
  • Insert N items into binary heap.
  • Perform N delete-min operations.
  • O(N log N) sort.
  • No extra storage.

19
Binary Heap Union
  • Union.
  • Combine two binary heaps H1 and H2 into a single
    heap.
  • No easy solution.
  • ?(N) operations apparently required
  • Can support fast union with fancier heaps.

H2
H1
14
11
78
18
62
53
77
91
81
84
41
64
99
20
Priority Queues
Heaps
Operation
Binary
Binomial
Fibonacci
Relaxed
Linked List
make-heap
1
1
1
1
1
insert
log N
log N
1
1
1
find-min
1
log N
1
1
N
delete-min
log N
log N
log N
log N
N
union
N
log N
1
1
1
decrease-key
log N
log N
1
1
1
delete
log N
log N
log N
log N
N
is-empty
1
1
1
1
1
21
Binomial Tree
  • Binomial tree.
  • Recursive definition

B0
B1
B2
B3
B4
22
Binomial Tree
  • Useful properties of order k binomial tree Bk.
  • Number of nodes 2k.
  • Height k.
  • Degree of root k.
  • Deleting root yields binomialtrees Bk-1, , B0.
  • Proof.
  • By induction on k.

B0
B1
B2
B3
B4
23
Binomial Tree
  • A property useful for naming the data structure.
  • Bk has nodes at depth i.

depth 0
depth 1
depth 2
depth 3
depth 4
B4
24
Binomial Heap
  • Binomial heap. Vuillemin, 1978.
  • Sequence of binomial trees that satisfy binomial
    heap property.
  • each tree is min-heap ordered
  • 0 or 1 binomial tree of order k

3
18
37
B4
B0
B1
25
Binomial Heap Implementation
  • Implementation.
  • Represent trees using left-child, right sibling
    pointers.
  • three links per node (parent, left, right)
  • Roots of trees connected with singly linked list.
  • degrees of trees strictly decreasing from left to
    right

heap
6
3
18
6
3
18
Parent
37
37
44
10
29
29
Right
Left
48
31
17
48
10
50
31
50
44
17
Leftist Power-of-2 Heap
Binomial Heap
26
Binomial Heap Properties
  • Properties of N-node binomial heap.
  • Min key contained in root of B0, B1, . . . , Bk.
  • Contains binomial tree Bi iff bi 1 where bn?
    b2b1b0 is binary representation of N.
  • At most ?log2 N? 1 binomial trees.
  • Height ? ?log2 N?.

3
18
37
N 19 trees 3height 4binary 10011
B4
B0
B1
27
Binomial Heap Union
  • Create heap H that is union of heaps H' and H''.
  • "Mergeable heaps."
  • 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

6
44
8
29
10
30
23
22
48
31
17
45
32
24
50
55
H''
H'
28
Binomial Heap Union
6
3
18
37
44
8
29
10
30
23
22
48
31
17
15
7
12
45
32
24
50
28
25
33

55
41
1
1
1
0
0
1
1
1
1
0
0
1

1
19 7 26
0
1
1
1
0
29
Binomial Heap Union
6
3
18
37
44
8
29
10
30
23
22
48
31
17
15
7
12
45
32
24
50
28
25
33

55
41
30
Binomial Heap Union
12
18
6
3
18
37
44
8
29
10
30
23
22
48
31
17
15
7
12
45
32
24
50
28
25
33

55
41
31
12
3
18
7
37
25
6
3
18
37
44
8
29
10
30
23
22
48
31
17
15
7
12
45
32
24
50
28
25
33

55
41
12
18
32
12
3
3
18
7
37
15
7
37
25
28
33
25
41
6
3
18
37
44
8
29
10
30
23
22
48
31
17
15
7
12
45
32
24
50
28
25
33

55
41
12
18
33
12
3
3
18
7
37
15
7
37
25
28
33
25
41
6
3
18
37
44
8
29
10
30
23
22
48
31
17
15
7
12
45
32
24
50
28
25
33

55
41
12
3
37
15
7
18
28
33
25
41
34
12
3
3
18
7
37
15
7
37
25
28
33
25
41
6
3
18
37
44
8
29
10
30
23
22
48
31
17
15
7
12
45
32
24
50
28
25
33

55
41
12
3
6
37
15
7
44
8
29
10
18
28
33
25
30
23
22
48
31
17
45
32
41
24
50
55
35
Binomial Heap Union
  • Create heap H that is union of heaps H' and H''.
  • Analogous to binary addition.
  • Running time. O(log N)
  • Proportional to number of trees in root lists ?
    2( ?log2 N? 1).

1
1
1
0
0
1
1
1
1
0
0
1

1
19 7 26
0
1
1
1
0
36
Binomial Heap Delete Min
  • Delete node with minimum key in binomial heap H.
  • Find root x with min key in root list of H, and
    delete
  • H' ? broken binomial trees
  • H ? Union(H', H)
  • Running time. O(log N)

37
Binomial Heap Delete Min
  • Delete node with minimum key in binomial heap H.
  • Find root x with min key in root list of H, and
    delete
  • H' ? broken binomial trees
  • H ? Union(H', H)
  • Running time. O(log N)

6
18
44
8
29
10
37
30
23
22
48
31
17
H
45
32
24
50
55
38
Binomial Heap Decrease Key
  • Decrease key of node x in binomial heap H.
  • Suppose x is in binomial tree Bk.
  • Bubble node x up the tree if x is too small.
  • Running time. O(log N)
  • Proportional to depth of node x ? ?log2 N? .

3
6
18
depth 3
44
8
29
10
37
30
23
22
48
31
17
H
x
32
24
50
55
39
Binomial Heap Delete
  • Delete node x in binomial heap H.
  • Decrease key of x to -?.
  • Delete min.
  • Running time. O(log N)

40
Binomial Heap Insert
  • Insert a new node x into binomial heap H.
  • H' ? MakeHeap(x)
  • H ? Union(H', H)
  • Running time. O(log N)

3
6
18
x
44
8
29
10
37
30
23
22
48
31
17
H
H'
45
32
24
50
55
41
Binomial Heap Sequence of Inserts
  • Insert a new node x into binomial heap H.
  • If N .......0, then only 1 steps.
  • If N ......01, then only 2 steps.
  • If N .....011, then only 3 steps.
  • If N ....0111, then only 4 steps.
  • Inserting 1 item can take ?(log N) time.
  • If N 11...111, then log2 N steps.
  • But, inserting sequence of N items takes O(N)
    time!
  • (N/2)(1) (N/4)(2) (N/8)(3) . . . ? 2N
  • Amortized analysis.
  • Basis for getting most operationsdown to
    constant time.

3
6
x
44
29
10
37
48
31
17
50
42
Priority Queues
Heaps
Operation
Binary
Binomial
Fibonacci
Relaxed
Linked List
make-heap
1
1
1
1
1
insert
log N
log N
1
1
1
find-min
1
log N
1
1
N
delete-min
log N
log N
log N
log N
N
union
N
log N
1
1
1
decrease-key
log N
log N
1
1
1
delete
log N
log N
log N
log N
N
is-empty
1
1
1
1
1
just did this
Write a Comment
User Comments (0)
About PowerShow.com