0% found this document useful (0 votes)
42 views5 pages

Interval Heaps Interval: - (C, D) Is Contained in (A, B) - A C - D B

An interval heap is a complete binary tree where each node contains an interval represented by its minimum and maximum values. The interval of a node must contain the intervals of its children. It can be used to efficiently insert and remove intervals and find all intervals not contained within a given range in O(k) time where k is the number of reported intervals. Interval heaps support insertion and removal of intervals in O(log n) time and finding intervals outside a range in O(k) time. They can be optimized using a 4-ary tree structure and cache-aligned array to improve performance.

Uploaded by

Upinder Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views5 pages

Interval Heaps Interval: - (C, D) Is Contained in (A, B) - A C - D B

An interval heap is a complete binary tree where each node contains an interval represented by its minimum and maximum values. The interval of a node must contain the intervals of its children. It can be used to efficiently insert and remove intervals and find all intervals not contained within a given range in O(k) time where k is the number of reported intervals. Interval heaps support insertion and removal of intervals in O(log n) time and finding intervals outside a range in O(k) time. They can be optimized using a 4-ary tree structure and cache-aligned array to improve performance.

Uploaded by

Upinder Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Interval Heaps

Complete binary tree. Each node (except possibly last one) has 2 elements. Last node has 1 or 2 elements. Let a and b be the elements in a node P, a <= b. [a, b] is the interval represented by P. The interval represented by a node that has just one element a is [a, a]. The interval [c, d] is contained in interval [a, b] iff a <= c <= d <= b. In an interval heap each nodes (except for root) interval is contained in that of its parent.
a c

Interval
b d

[c,d] is contained in [a,b] a <= c d <= b

Example Interval Heap


10,90

Example Interval Heap


10,90

15,80 20,70 15,20 45,60

30,60 35,50 20,70

15,80 15,20 45,60

30,60 35,50

25,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

25,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

28,55

35

Left end points define a min heap. Right end points define a max heap.

28,55

35

Min and max elements are in the root. Store as an array. Height is ~log2 n.

Insert An Element
10,90

Another Insert
10,90

15,80 20,70 15,20 45,60

30,60 35,50 20,70

15,80 15,20 45,60

30,60 35,50

25,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

25,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

28,55

27,35 35

Insert 27. New element becomes a left end point. Insert new element into min heap.

28,55

35

Insert 18. New element becomes a left end point. Insert new element into min heap.

Another Insert
10,90

Another Insert
10,90

15,80 20,70 15,20 45,60

30,60 35,50 18,70 20,70 ,70

15,80 15,20 45,60

30,60 35,50

25,60 ,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

20,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

28,55

25,35

Insert 18. New element becomes a left end point. Insert new element into min heap.

28,55

25,35

Insert 18. New element becomes a left end point. Insert new element into min heap.

Yet Another Insert


10,90

After 82 Inserted
10,90

15,80 20,70 15,20 45,60

30,60 35,50 20,80

15,82 15,20 45,60

30,60 35,50

25,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

25,70

30,50

16,19

17,17

50,55

47,58

40,45

40,43

28,55

35

Insert 82. New element becomes a right end point. Insert new element into max heap.

28,55

35,60

One More Insert Example


10,90

After 8 Is Inserted
8,90

15,82 20,80 15,20 45,60

30,60 35,50 15,80

10,82 15,20 45,60

30,60 35,50

25,70

30,50

16,19

17,17

50,55

47,58

40,45

40,43

20,70

30,50

16,19

17,17

50,55

47,58

40,45

40,43

28,55

Insert 8. New element becomes both a left and a right end point. Insert new element into min heap.

28,55

25

Remove Min Element


n = 0 => fail. n = 1 => heap becomes empty. n = 2 => only one node, take out left end point. n > 2 => not as simple.

Remove Min Element Example


10,90 ,90 35

15,82 20,80 15,20 45,60

30,60 35,50

25,70

30,50

16,19

17,17

50,55

47,58

40,45

40,43

28,55

35,60 ,60

Remove left end point from root. Remove left end point from last node. Delete last node if now empty. Reinsert into min heap, begin at root.

Remove Min Element Example


15,90 35 15,82 ,82 20,80 15,20 45,60 30,60 35,50

Remove Min Element Example


15,90

15,82 35 20,80 15,20 ,20 45,60

30,60 35,50

25,70

30,50

16,19

17,17

50,55

47,58

40,45

40,43

25,70

30,50

16,19

17,17

50,55

47,58

40,45

40,43

28,55

60

Swap with right end point if necessary.

28,55

60

Swap with right end point if necessary.

Remove Min Element Example


15,90

Remove Min Element Example


15,90

15,82 20,80 20 25,70 30,50 16,19 ,19 17,17 50,55 47,58 16,35 45,60

30,60 35,50 20,80

15,82 16,35 45,60

30,60 35,50

40,45

40,43

25,70

30,50

19,20

17,17

50,55

47,58

40,45

40,43

28,55

60

Swap with right end point if necessary.

28,55

60

Initialize
70,39

Cache Optimization
Heap operations.
35,50

99,82 20,23 48,20 49,63

1,12

Uniformly distributed keys. Insert percolates 1.6 levels up the heap on average. Remove min (max) height 1 levels down the heap.

25,19

57,50

46,19

17,37

50,25

47,28

20,45

40,13

Optimize cache utilization for remove min (max).

68,55

35,14

Examine nodes bottom to top. Swap end points in current root if needed. Reinsert left end point into min heap. Reinsert right end point into max heap.

Cache Aligned Array


L1 cache line is 32 bytes. L1 cache is 16KB. Heap node size is 8 bytes (1 8-byte element). 4 nodes/cache line.
0 1 2 3 4 5 6 7

d-ary Heap
Complete n node tree whose degree is d. Min (max) tree. Number nodes in breadth-first manner with root being numbered 1. Parent(i) = ceil((i 1)/d). Children are d*(i 1) + 2, , min{d*i + 1, n}. Height is logdn. Height of 4-ary heap is half that of 2-ary heap.

A remove min (max) has ~h L1 cache misses on average.


Root and its children are in the same cache line. ~log2n cache misses. Only half of each cache line is used (except roots).

d = 4, 4-Heap
Worst-case insert moves up half as many levels as when d = 2.
Average remains at about 1.6 levels.

4-Heap Cache Utilization


Standard mapping into cache-aligned array.
0 1 2 3 4 5 6 7

Remove-min operations now do 4 compares per level rather than 2 (determine smallest child and see if this child is smaller than element being relocated).
But, number of levels is half. Other operations associated with remove min are halved (move small element up, loop iterations, etc.)

Siblings are in 2 cache lines.


~log2n cache misses for average remove min (max).

Shift 4-heap by 2 slots.


- 1 - 3 2 3 4 5 0 - 2 1 4 5 6 7

Siblings are in same cache line.


~log4n cache misses for average remove min (max).

d-ary Heap Performance


Speedup of about 1.5 to 1.8 when sorting 1 million elements using heapsort and cachealigned 4-heap vs. 2-heap that begins at array position 0. Cache-aligned 4-heap generally performs as well as, or better, than other d-heaps. Use degree 4 complete tree for interval heaps instead of degree 2.

Application Of Interval Heaps


Complementary range search problem.
Collection of 1D points (numbers). Insert a point.
O(log n)

Remove a point given its location in the structure.


O(log n)

Report all points not in the range [a,b], a <= b.


O(k), where k is the number of points not in the range.

Example
10,90

Example
10,90

15,80 20,70 15,20 45,60

30,60 35,50 20,70

15,80 15,20 45,60

30,60 35,50

25,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

25,60

30,50

16,19

17,17

50,55

47,58

40,45

40,43

28,55

35

[5,100] [2,65]

28,55

35

[2,65]

You might also like