0% found this document useful (0 votes)
40 views

6.851 Advanced Data Structures (Spring'12) Prof. Erik Demaine Problem 4 Sample Solution

This document contains sample solutions for two cache-oblivious data structures: median finding and queues. For median finding, it summarizes that the standard median of medians algorithm already meets the desired time and memory bounds to be cache-oblivious. For cache-oblivious queues, it proposes storing items sequentially in external memory from a head to tail index. Enqueue increases the tail and dequeue reports the item at head and increases head. Shifting elements is needed to keep indices within memory bounds, and this shift can be charged to past dequeues to maintain amortized constant time per operation.

Uploaded by

djoseph_1
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)
40 views

6.851 Advanced Data Structures (Spring'12) Prof. Erik Demaine Problem 4 Sample Solution

This document contains sample solutions for two cache-oblivious data structures: median finding and queues. For median finding, it summarizes that the standard median of medians algorithm already meets the desired time and memory bounds to be cache-oblivious. For cache-oblivious queues, it proposes storing items sequentially in external memory from a head to tail index. Enqueue increases the tail and dequeue reports the item at head and increases head. Shifting elements is needed to keep indices within memory bounds, and this shift can be charged to past dequeues to maintain amortized constant time per operation.

Uploaded by

djoseph_1
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/ 2

6.851 Advanced Data Structures (Spring12) Prof.

Erik Demaine Problem 4 Sample solution

Cache-oblivious median nding. The standard median of medians algorithm is already a cache-oblivious algorithm meeting the desired bounds, so all the remains is to prove this. Each step of the recursion (nding the medians of the groups of ve and selecting the elements for recursion) can be done in O(1) passes over the the array for a total of O(IN/B l) memory transfers. The number of memory transfers is then T (N ) = T (N/5) + T (7N/10) + O(IN/B l). With a base case of T (B ) = O(1) since once the list ts in a single line of cache, the median can be found with a single memory transfer. T (N ) decreases geometrically from the root, down to O((N/B )c ) (c < 1) work at the leaves. Thus, T (N ) = O(IN/B l). Cache-oblivious queue. We will store the items in the queue sequentially in external memory from an index head to an index tail. Initially, head = tail = 0. To enqueue an item, we store it at index tail, and we add 1 to tail. To dequeue an item, we report the item stored at index head and add 1 head. Assuming optimal cache behavior, and M/B 2, enqueue and dequeue clearly take an amortized O(1/B ) memory transfers per operation. This is because we can just keep the lines containing head and tail in cache at all times so a memory transfer only occurs one out of every B enqueues or dequeues. Note that this algorithm does not stay in memory indices {0, 1, , O(N )}. To x this, whenever 2 head > tail, we shift all the elements to the beginning of external memory. This shift takes O(k/B ) memory transfers, where k is the current size of the queue, and we can charge it to the (k ) dequeues that must have occurred between it and the previous shift, so we still have an amortized O(1/B ) memory transfers per operation.

MIT OpenCourseWare http://ocw.mit.edu

6.851 Advanced Data Structures


Spring 2012

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like