Navigation Piles with Applications to Sorting, Priority Queues, and Priority Deques Jyrki Katajainen and Fabio Vitale Department of Computing, University.

Slides:



Advertisements
Similar presentations
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Week 10: Heap and Priority queue. Any feature here?
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Important Problem Types and Fundamental Data Structures
Binary Trees Chapter 6.
Heapsort Based off slides by: David Matuszek
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Chapter 21 Binary Heap.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
B + -Trees Same structure as B-trees. Dictionary pairs are in leaves only. Leaves form a doubly-linked list. Remaining nodes have following structure:
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.
An experimental study of priority queues By Claus Jensen University of Copenhagen.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
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.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
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.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Space Efficient and Output Sensitive Greedy Algorithms on Intervals Toshiki Saitoh (Kobe University) Joint work with ・ Takashi Horiyama (Saitama University)
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Final Exam Review COP4530.
"Teachers open the door, but you must enter by yourself. "
Data Structure By Amee Trivedi.
Multiway Search Trees Data may not fit into main memory
Data Structures Binary Trees 1.
Experimental evaluation of Navigation piles
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
ADT Heap data structure
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Priority Queue & Heap CSCI 3110 Nan Chen.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Find in a linked list? first last 7  4  3  8 NULL
Final Exam Review COP4530.
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Binary Search Trees.
Binary Trees, Binary Search Trees
Important Problem Types and Fundamental Data Structures
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Navigation Piles with Applications to Sorting, Priority Queues, and Priority Deques Jyrki Katajainen and Fabio Vitale Department of Computing, University of Copenhagen Universitetsparken 1, 2100 Copenhagen East, Denmark

Navigation Piles Using of the new data structure Using of the new data structure Sorting algorithms Priority queues Priority deques Implementation of other data structures

Structure Properties Shape It is a complete binary tree of size Leaves The first n leaves store one element each The remaining are empty Branch nodes Each branch node stores the index – inside the leaf sequence dominated by the node – containing the relative top element Representation The sequence A[0..n) stores the elements The sequence B[0.. ) stores the navigation information

Structure Elements Element indices: Indices of the elements stored in A[0..n) Bit indices Indices of the bits stored in B[0.. ) Branch node indices Leaf indices Levels (no larger than  ) Offsets Indices stored in the branch nodes

Navigation Pile Example A navigation pile of size 14 and capacity 16. The normal parent/child relationships are shown with dotted arrows and the references indicated by the offsets with solid arrows. The gray nodes are not in use.

Number of extra bits Number of branch nodes with depth Maximum number of leaves dominated by a branch node whose height is  Number of bits used for the offsets

Operations Construction Visiting the branch nodes in a bottom-up manner (depth- first order visit to improve the cache performances) n-1 element comparisons n-1 branch nodes with more than 1 child n element moves n elements copied to the container sequence O(n) instructions O(1) instructions for every comparison and move

Operations top() O(1) instructions Return the index stored at the root push() element comparisons Binary search on the path from the new last leaf to the root 1 element move The element is appended at the end of the sequence A[0..n) O(log n) instructions

Operations pop() l : index of the last leaf (n - 1) m : index of the top element Case 1: m = l The top element is erased and the offsets on the path from the new last leaf to the root are updated

pop() Case 1 Illustration of Case 1. The offset of the root is refering the last leaf. The leaves whose contents may change are indicated in magenta. In dark blue are indicated the branch nodes whose updating offset require one element comparison each. When updating the contents of the light blue branch nodes, no element comparisons are necessary.

Operations i 1 : first ancestor of the last leaf having two children (in use) (n > 1) i 2 : second ancestor of the last leaf having two children (in use) ( l ) k, j 1 : index of the leaves referred respectively by the offset stored at the branch node with index i 2 and at the first child of that one with index i 1 (or j 1 = l -1 if l is odd) Case 2: A[m]  A[n] and the element copied is erased The offsets stored at the branch node on the path from [i 1.. i 2 ) are updated to refer the leaf at the position j 1 The offsets stored on the path from the leaf at the position m to the root are updated

pop() Case 2 Illustration of Case 2. The offset of i 2 is not refering the last leaf. The offsets of the branch nodes in light blue will refer the leaf whose content is the number 4, and the the top element (44) will be overwritten by the current last leaf (22).

Operations j 2 : index of the element referred by the offset stored at the first child of the branch node with index i 2 Case 3: A[m]  A[j 2 ] A[j 2 ]  A[ l ] The element at the last leaf is erased The offsets stored at the branch node on the path from [i 1.. i 2 ) are updated to refer the leaf at the position j 1 The offsets stored on the path from i 2 (including it) upwards are updated to refer the leaf in the position j 2 if they referred earlier the last leaf The offsets stored on the path from the leaf at the position m to the root are updated

pop() Case 3 Illustration of Case 3. The offset of i 2 is refering the last leaf. The offsets of the branch nodes in light blue from the bottom til i 2 (excluding it) will refer the leaf j 1 (4); the offset relative to the branch node i 2 will refer the leaf j 2 (15); the the top element (44) will be overwritten by the element in the position j 2 (15); finally the element of the current last leaf (22) will overwrite that one of the leaf j 2.

Operations At most element comparisons In each of the three cases only one path updating involves element comparisons The depth of the root becomes and at most one element comparisons is done at each level 0, 1 or 2 moves O(log n) instructions

Sorting In connection with pop() the top element is not overwritten, but it is saved at the earlier last leaf (pop_and_save() function) The basic version of the sorting algorithm (Pilesort) is a sequence of n-1 pop_and_save() operations The amount of extra space needed is at most 4n bits because

Sorting Element comparisons n-1 comparisons for the construction If we find the bottom element during the construction with at most comparisons (and keeping a separate copy of it) in order to reduce the number of moves, the bound becomes Element moves Not more than 4n+O(1) in the basic version Finding at the beginning the bottom element the bound becomes 2.5n + O(1) (for every odd value of l if we need 3 moves for the execution of pop_and_save(), we will need at most 2 moves in the following iteration)

Priority Queues Dynamization of the container storing the elements Dynamization techniques developed by Katajainen and Mortensen Space-efficient resizable arrays Dynamization of the bit sequence Bit sequence divided into blocks B * of size 2 5 and B h of size 2 h for Bit sequence of the block B * is kept in memory at all time The largest block can be empty (deamortization of the cost of allocations and deallocations in proximity of block boundaries) If there is an empty block and the largest nonempty block lacks more than 2 5 elements, the bit sequences for the largest block is freed

Priority Queues (Illustration) Illustration of the Priority Queue structure. A collection of navigation piles storing 77 elements. A space-efficient resizable array is used as the container for the elements. The gray areas denote empty memory segments allocated for dynamization purposes.

Priority Queues Space 2 h+1 bits for every B h ( ) 4n + O(1) extra bits for all the priority queue Construction n+  element comparisons n element moves O(n) instructions top() We assume that push() and pop() maintain the top and iterator headers O(1) instructions (following the cursor of the iterator headers)

Priority Queues push() The element is inserted into the largest nonempty block (or, if it is full, into a new larger block and a new navigation pile) element comparisons (the iterator and the top headers can need to be updated) 1 element move O(log n) instructions pop() The top element is erased by overwriting with an element taken from the largest nonempty block We need to update also the top and iterator header element comparisons 2 element moves O(log n) instructions

Priority Deques We pair the elements and we sort the resulting pairs If n is odd, we keep one element in a special block B 0 The elements get partitioned in two distinct collections: top-element candidates and bottom-element candidates We build the extra information for the two priority queues Construction Not more than 1.5n+  comparisons and n+1 moves top() and bottom() B 0 can cause 1 comparison

Priority Deques push() If B 0 is empty the element being inserted is copied there If B 0 contains an element this and the new one become twins, and this pairs is added to the data structure In the latter case the resource bounds are 2 times those compared to the push() function

Priority Deques pop_top() and pop_bottom() These two functions are symmetric We consider pop_top() If B 0 is empty we move the twin of the top to B 0, take a pair of elements from the last nonempty block We carry out a partial path updating in the last nonempty block We move the pair of elements from the last nonempty block to the block containing the top element and its twin updating a full path of extra information for both the elements

Priority Deques If B 0 contains an element this is used now as the new twin of the twin of the top element After a single comparison the two elements are moved in their correct locations and we update at most two paths of extra bits The resource bounds are about twice as high as those for the priority queues

Conclusions It have been showed how it is possible to build and use a new data structure with which, in spite of low memory requirements, the worst-case bounds for the number of element comparisons, element moves and other instructions are close to the absolute minimum Is it possible to get similar performance bounds with a sublinear number of extra bits (for example, a sorting algorithm using. comparisons, O(n) moves, and o(n) extra bits)? Is it possible to improve the push() function requiring a constant time without making worse the worst-case bounds for the other operations?