Cache-Oblivious Priority Queue with Decrease-Keys )
Total Page:16
File Type:pdf, Size:1020Kb
CSE 638: Advanced Algorithms Lectures 20 & 21 ( Cache-oblivious Priority Queue with Decrease-Keys ) Rezaul A. Chowdhury Department of Computer Science SUNY Stony Brook Spring 2013 Cache-Oblivious Buffer Heap Amortized I/O Bounds Priority Queue Delete / Delete-Min Decrease-Key Cache- Buffer Heap oblivious 1 N O log B2 B Cache- Tournament Tree aware Binary Heap O(((log 2 N ))) ( worst-case ) Internal Memory O 1 Fibonacci Heap O(((log 2 N ))) ((( ))) Cache-Oblivious Buffer Heap: Structure Consists of r = 1 + log 2N levels, where N = total number of elements. For 0 ≤≤≤ i ≤≤≤ r - 1, level i contains two buffers: Element Buffers Update Buffers element buffer Bi B U contains elements of the form 0 0 B U (x, kx), where x is the element 1 1 id, and kx is its key B2 U2 update buffer Ui B U contains updates ( Delete , i i Decrease-Key and Sink ), each B U augmented with a time-stamp. r - 1 r - 1 Fig: The Buffer Heap Cache-Oblivious Buffer Heap: Invariants i Element Buffers Update Buffers Invariant 1 : |Bi|≤≤≤ 2 B0 U0 U Invariant 2 : B1 1 B2 U2 (a) No key in Bi is larger than any key in Bi+1 (b) For each element x in Bi, all updates yet Bi Ui to be applied on x reside in U0, U1, … , Ui Br - 1 Ur - 1 Invariant 3 : Fig: The Buffer Heap (a) Each Bi is kept sorted by element id (b) Each Ui ( except U0 ) is kept ( coarsely ) sorted by element id and time-stamp Cache-Oblivious Buffer Heap: Operations The following operations are supported: − Delete (x): Deletes the element x from the queue. − Delete-Min ( ) : Extracts an element with minimum key from queue. − Decrease-Key (x, kx): ( weak Decrease-Key ) ′′′ ′′′ If x already exists in the queue, replaces key k x of x with min( kx, k x), otherwise inserts x with key kx into the queue. A new element x with key kx can be inserted into queue by Decrease-Key (x, kx ). Cache-Oblivious Buffer Heap: Operations Decrease-Key (x, kx) : Insert the operation into U0 augmented with current time-stamp. Delete (x) : Insert the operation into U0 augmented with current time-stamp. Delete-Min ( ) : Two phases: − Descending Phase ( Apply Updates ) − Ascending Phase ( Redistribute Elements ) Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : 1. sort updates B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : B0 U0 B1 2. apply updates: U1 − Delete (x) B2 − Decrease-Key (x, kx) U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : B0 U0 B1 U1 B2 U2 3. carry updates: − all updates that did not apply − applied Decrease-Keys as Deletes Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : 1. sort updates: − merge segments B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : B0 U0 B1 U1 B2 2. apply updates: U2 − Delete (x) − Decrease-Key (x, kx) − Sink (x, kx) Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : B0 U0 B1 U1 B2 U2 3. carry updates Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Descending Phase ( Apply Updates ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk − find 2k elements with 2k smallest keys Uk+1 − convert each remaining element x with key kx to Sink (x, kx) Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 push the Sinks to Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : B0 U0 B1 U1 B2 U2 move all elements from Bk to shallower levels leaving Bk empty Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: Delete-Min Delete-Min ( ) - Ascending Phase ( Redistribute Elements ) : element with minimum key B0 U0 B1 U1 B2 U2 Bk-1 Uk-1 Bk Uk Uk+1 Cache-Oblivious Buffer Heap: I/O Complexity 1 r−1 r − 1 Potential Function: 4 3 1 Φ ((()(H))) = rU0 +∑((()( riU −++))) i ∑ ((()( iB))) i B i=1 i = 0 Element Buffers Update Buffers direction of direction of B0 U0 elements updates B1 U1 B2 U2 Bi Ui updates generate elements Br - 1 Ur - 1 Fig: Major Data Flow Paths in the Buffer Heap Lemma: A Buffer Heap on N elements supports Delete , Delete-Min and 1 Decrease-Key operations cache-obliviously in amortized Ο log 2 N B I/Os each using Ο ((( N ))) space. Cache-Oblivious Buffer Heap: I/O Complexity 1 r−1 r − 1 Potential Function: 4 3 1 Φ ((()(H))) = rU0 +∑((()( riU −++))) i ∑ ((()( iB))) i B i=1 i = 0 Element Buffers Update Buffers direction of direction of B0 U0 elements updates B1 U1 B2 U2 Bi Ui updates generate elements Br - 1 Ur - 1 Fig: Major Data Flow Paths in the Buffer Heap Lemma: A Buffer Heap on N elements supports Delete , Delete-Min and 1 Decrease-Key operations cache-obliviously in amortized Ο log 2 N B I/Os each using Ο ((( N ))) space. Cache-Oblivious Buffer Heap: I/O Complexity 1 r−1 r − 1 Potential Function: 4 3 1 Φ ((()(H))) = rU0 +∑((()( riU −++))) i ∑ ((()( iB))) i B i=1 i = 0 Element Buffers Update Buffers direction of direction of B0 U0 elements updates B1 U1 B2 U2 Bi Ui updates generate elements Br - 1 Ur - 1 Fig: Major Data Flow Paths in the Buffer Heap Lemma: A Buffer Heap on N elements supports Delete , Delete-Min and 1 Decrease-Key operations cache-obliviously in amortized Ο log 2 N B I/Os each using Ο ((( N ))) space. Cache-Oblivious Buffer Heap: I/O Complexity 1 r−1 r − 1 Potential Function: 4 3 1 Φ ((()(H))) = rU0 +∑((()( riU −++))) i ∑ ((()( iB))) i B i=1 i = 0 Element Buffers Update Buffers direction of direction of B0 U0 elements updates B1 U1 B2 U2 Bi Ui updates generate elements Br - 1 Ur - 1 Fig: Major Data Flow Paths in the Buffer Heap Lemma: A Buffer Heap on N elements supports Delete , Delete-Min and 1 Decrease-Key operations cache-obliviously in amortized Ο log 2 N B I/Os each using Ο ((( N ))) space. Cache-Oblivious Buffer Heap: I/O Complexity 1 r−1 r − 1 Potential Function: 4 3 1 Φ ((()(H))) = rU0 +∑((()( riU −++))) i ∑ ((()( iB))) i B i=1 i = 0 Element Buffers Update Buffers direction of direction of B0 U0 elements updates B1 U1 B2 U2 Bi Ui updates generate elements Br - 1 Ur - 1 Fig: Major Data Flow Paths in the Buffer Heap Lemma: A Buffer Heap on N elements supports Delete , Delete-Min and 1 Decrease-Key operations cache-obliviously in amortized Ο log 2 N B I/Os each using Ο ((( N ))) space. Cache-Oblivious Buffer Heap: Layout • All Bi’s are kept in a stack SB. • All Ui’s are kept in a stack SU. • An array As is maintained in a stack SA. for 0 ≤≤≤ i ≤≤≤ r – 1 As[i] contains: − |Bi| − number of segments in Ui − number of updates in each segment of Ui In both stacks lower level buffers are placed above higher level buffers. The left to right order of the elements in any buffer are maintained top to bottom in the stack. Cache-Oblivious Buffer Heap: Reconstruction After each operation check whether ∑∑∑|Ui| ≥≥≥ ∑∑∑|Bi|, and if so, Step 1: Sort the elements in SB by element id and level number. Step 2: Sort the updates in SU by element id and time-stamp. Step 3: Scan SB and Su simultaneously, and apply the updates in Su on the elements of SB. Step 4: Reconstruct the data structure by filling the shallowest levels with the current elements in SB, and emptying Su.