Interval Heaps Complete binary tree.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Binomial Heaps. Min Binomial Heap Collection of min trees
Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Priority Search Trees Keys are pairs (x,y). Basic (search, insert, delete) and rectangle operations. Two varieties.  Based on a balanced binary search.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Heaps (8.3) CSE 2011 Winter May 2018.
Multiway Search Trees Data may not fit into main memory
Priority Queues and Heaps
Heapsort CSE 373 Data Structures.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Splay Trees Binary search trees.
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Chapter 10.1 Binary Search Trees
Binary Search Trees < > = Binary Search Trees
Source: Muangsin / Weiss
Splay Trees Binary search trees.
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Pairing Heaps Actual Complexity.
Priority Search Trees Keys are pairs (x,y).
Wednesday, April 18, 2018 Announcements… For Today…
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Part-D1 Priority Queues
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
"Teachers open the door, but you must enter by yourself. "
(2,4) Trees (2,4) Trees (2,4) Trees.
Chapter 6: Transform and Conquer
Heaps A heap is a binary tree that satisfies the following properties:
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Binary Trees, Binary Search Trees
Heapsort CSE 373 Data Structures.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
(2,4) Trees (2,4) Trees (2,4) Trees.
HEAPS.
Algorithms: Design and Analysis
Heaps By JJ Shepherd.
การวิเคราะห์และออกแบบขั้นตอนวิธี
Pairing Heaps Actual Complexity
1 Lecture 13 CS2013.
Computer Algorithms CISC4080 CIS, Fordham Univ.
Heaps & Multi-way Search Trees
Binary Trees, Binary Search Trees
A Heap Is Efficiently Represented As An Array
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
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 a b c d [c,d] is contained in [a,b] a <= c d <= b

Example Interval Heap Left end points define a min heap. 28,55 35 25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,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.

Example Interval Heap Min and max elements are in the root. 28,55 35 25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 You essentially have total correspondence! Implicit data structure. Min and max elements are in the root. Store as an array. Height is ~log2 n.

Insert An Element Insert 27. New element becomes a left end point. 28,55 35 25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 27,35 35 Insert 27. New element becomes a left end point. Insert new element into min heap.

Another Insert Insert 18. New element becomes a left end point. 28,55 35 25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 As you bubble up, node intervals expand leftward. So, descendent intervals remain contained in ancestor intervals. 35 Insert 18. New element becomes a left end point. Insert new element into min heap.

Another Insert Insert 18. New element becomes a left end point. 28,55 25,35 25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 ,60 Insert 18. New element becomes a left end point. Insert new element into min heap.

Another Insert Insert 18. New element becomes a left end point. 28,55 25,35 20,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 ,70 18,70 Insert 18. New element becomes a left end point. Insert new element into min heap.

Yet Another Insert Insert 82. New element becomes a right end point. 28,55 35 25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 As you bubble up, node intervals expand rightward. So, descendent intervals remain contained in ancestor intervals. 35 Insert 82. New element becomes a right end point. Insert new element into max heap.

After 82 Inserted 28,55 35,60 25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,80 15,82 30,60 10,90

One More Insert Example 28,55 25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,80 15,82 30,60 10,90 Since 8 < 25, insert into min heap. If we were to insert 100, we would insert into max heap, because 100 > 70. When new key is between 25 and 70, just put into new leaf. Insert 8. New element becomes both a left and a right end point. Insert new element into min heap.

After 8 Is Inserted 25 28,55 20,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 15,80 10,82 30,60 8,90

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,55 35,60 25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,80 15,82 30,60 10,90 ,90 35 Remove left end point from root. ,60 Remove left end point from last node. Delete last node if now empty. Reinsert into min heap, begin at root.

Remove Min Element Example 28,55 60 25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,80 15,82 30,60 15,90 35 ,82 Swap with right end point if necessary.

Remove Min Element Example 28,55 60 25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,80 15,82 30,60 15,90 35 ,20 Swap with right end point if necessary.

Remove Min Element Example 28,55 60 25,70 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 16,35 20,80 15,82 30,60 15,90 20 ,19 Swap with right end point if necessary.

Remove Min Element Example 28,55 60 25,70 30,50 19,20 17,17 50,55 47,58 40,45 40,43 35,50 45,60 16,35 20,80 15,82 30,60 15,90

Initialize Examine nodes bottom to top. 68,55 35,14 25,19 57,50 46,19 17,37 50,25 47,28 20,45 40,13 35,50 49,63 48,20 20,23 99,82 1,12 70,39 Reinsert is done top to bottom. 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 Optimization Heap operations. Uniformly distributed keys. Insert bubbles 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 Cache Aligned Array L1 cache line is 32 bytes. Heap node size is 8 bytes (1 8-byte element). 4 nodes/cache line. Cache Aligned Array 1 2 3 1 2 3 4 5 6 7 4 5 6 7 Not as bad as when node numbering begins at 0 instead of at 1. 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 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 logdn. 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. 1 2 3 4 5 6 7 Siblings are in 2 cache lines. ~log2n cache misses for average remove min (max). Shift 4-heap by 2 slots. 1 2 3 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 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. Interval heap recommendation assumes 8-bytes/node (I.e., 4-bytes/element) or larger cache line.

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. 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,55 35 25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Empty => return. Root interval contained in [a,b] => return. Report root points that are not in the range. There must be at least one such point as root interval not in [a,b]. Search left subtree recursively. Search right subtree recursively. Use accounting method to show that cost is O(k). If a node returns a point, charge work at this node to the node. Otherwise charge to parent. Each nodes gets charged at most 3. So, total charge is 3k+1 (1 for work at root that returns no point). Or, consider subtree of visited nodes. Regard nodes that report no points (these are leaves) as external nodes. Number of remaining nodes is at most k. Number of external nodes is at most k+1. Total nodes visited is at most 2k+1. [5,100] [2,65]

Example 28,55 35 25,60 30,50 16,19 17,17 50,55 47,58 40,45 40,43 35,50 45,60 15,20 20,70 15,80 30,60 10,90 Reached nodes colored yellow and pink. Yellow nodes report at least 1 point. #yellow nodes <= k. #pink nodes <= 1 + #yellow nodes <= k+1. [2,65]