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.

Slides:



Advertisements
Similar presentations
The Derivative in Graphing and Application
Advertisements

Splay Trees Binary search trees.
Lower Bounds for Sorting, Searching and Selection
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.
CS2420: Lecture 19 Vladimir Kulyukin Computer Science Department Utah State University.
Binary Heap viewed as an array viewed as a binary tree Left(i) = 2*i Right(i) = 2*i + 1 Parent(i) = i.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 14 Ming Li Department of.
A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
Insert A tree starts with the dummy node D D 200 D 7 Insert D
DEAPS By: Michael Gresenz Austin Forrest. Deaps A deap is a double-ended heap that supports the double-ended priority operations of insert, delete-min,
Optimal binary search trees
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
RemoveMin We must take the value from the root node and return it to the user. Then we must remove the node. Easy array implementation: –Take the last.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Heapsort By Pedro Oñate CS-146 Dr. Sin-Min Lee. Overview: Uses a heap as its data structure In-place sorting algorithm – memory efficient Time complexity.
Balanced Search Trees Problem: Efficiency of BST is related to tree’s height.  search, insert and remove follow a path from root to desired location 
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Data Structure II. Outline Heap Binary Search Tree Hash Table Binary Indexed Tree Segment Tree.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Build a heap with 27, 35, 23, 22, 4, 45, 21, 5, 42 and 19. With a series of insertions, here’s the result
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Priority Search Trees Keys are pairs (x,y). Basic (search, insert, delete) and rectangle operations. Two varieties.  Based on a balanced binary search.
Lecture: Priority Queue. Questions Is array a data structure? What is a data structure? What data structures are implemented by array? Priority queue.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
بسم الله الرحمن الرحيم شرح جميع طرق الترتيب باللغة العربية
Partially Ordered Data ,Heap,Binary Heap
Lecture: Priority Queue
David Kauchak cs062 Spring 2010
Heap Chapter 9 Objectives Define and implement heap structures
Recursive Objects (Part 4)
Binary search tree. Removing a node
Heapsort CSE 373 Data Structures.
Splay Trees Binary search trees.
SNS COLLEGE OF TECHNOLOGY (Autonomous ) COIMBATORE-35
Part-D1 Priority Queues
Heapsort.
Splay Trees Binary search trees.
Interval Heaps Complete binary tree.
Node Voltage and dependent sources Quiz 2
Binary Heaps Text Binary Heap Building a Binary Heap
Fundamentals of Python: From First Programs Through Data Structures
Design and Analysis of Algorithms Heapsort
Priority Queue and Binary Heap Neil Tang 02/12/2008
Heap Sort Ameya Damle.
Heapsort and d-Heap Neil Tang 02/11/2010
10:00.
Sorting.
Just define the prefixes somewhere.
Lecture No.20 Data Structures Dr. Sohail Aslam
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Representing binary trees with lists
Heapsort CSE 373 Data Structures.
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Data Structures Lecture 29 Sohail Aslam.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Heaps CIS265/506: Chapter 12 Heaps.
Heapsort.
Heaps By JJ Shepherd.
Heapsort and d-Heap Neil Tang 02/14/2008
Fundamental Structures of Computer Science II
Priority Queues Binary Heaps
A Heap Is Efficiently Represented As An Array
Presentation transcript:

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 node’s (except for root) interval is contained in that of its parent.

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

Example Interval Heap 28, ,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Left end points define a min heap. Right end points define a max heap.

28, ,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Min and max elements are in the root. Store as an array. Height is ~log 2 n. Example Interval Heap

Insert An Element 28, ,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Insert ,35 New element becomes a left end point. Insert new element into min heap.

Another Insert 28, ,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Insert New element becomes a left end point. Insert new element into min heap.

28,5525,35 25,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Insert 18.,60 New element becomes a left end point. Insert new element into min heap. Another Insert

28,5525,35 20,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Insert 18.,70 New element becomes a left end point. Insert new element into min heap. 18,70 Another Insert

Yet Another Insert 28, ,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Insert New element becomes a right end point. Insert new element into max heap.

After 82 Inserted 28,5535,60 25,7030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,80 15,82 30,60 10,90

28,55 25,7030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,80 15,82 30,60 10,90 One More Insert Example Insert 8. New element becomes both a left and a right end point. Insert new element into min heap.

2528,55 20,7030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 15,80 10,82 30,60 8,90 After 8 Is Inserted

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 28,5535,60 25,7030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,80 15,82 30,60 10,90 Remove left end point from root. Remove left end point from last node. Reinsert into min heap, begin at root.,90,60 Delete last node if now empty. 35

28, ,7030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,80 15,82 30,60 15,90 Swap with right end point if necessary.,82 35 Remove Min Element Example

28, ,7030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,80 15,82 30,60 15,90 Swap with right end point if necessary.,20 35 Remove Min Element Example

28, ,7030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 16,35 20,80 15,82 30,60 15,90 Swap with right end point if necessary.,19 20 Remove Min Element Example

28, ,7030,5019,2017,1750,5547,5840,4540,43 35,50 45,60 16,35 20,80 15,82 30,60 15,90 Remove Min Element Example

Initialize 68,5535,14 25,1957,5046,1917,3750,2547,2820,4540,13 35,50 49,63 48,20 20,23 99,82 1,12 70,39 Examine nodes bottom to top. Swap end points in current root if needed. Reinsert right end point into max heap. Reinsert left end point into min heap.

Cache Optimization Heap operations.  Uniformly distributed keys.  Insert percolates 1.6 levels up the heap on average.  Remove min (max) height – 1 levels down the heap. Optimize cache utilization for remove min (max).

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 A remove min (max) has ~h L1 cache misses on average.  Root and its children are in the same cache line.  ~log 2 n cache misses.  Only half of each cache line is used (except root’s).

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 log d n. Height of 4-ary heap is half that of 2-ary heap.

d = 4, 4-Heap Worst-case insert moves up half as many levels as when d = 2.  Average remains at about 1.6 levels. 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.)

4-Heap Cache Utilization Standard mapping into cache-aligned array Siblings are in 2 cache lines.  ~log 2 n cache misses for average remove min (max). Shift 4-heap by 2 slots. Siblings are in same cache line.  ~log 4 n 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 cache- aligned 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 28, ,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 [5,100] [2,65]

Example 28, ,6030,5016,1917,1750,5547,5840,4540,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 [2,65]