fbpx
Wikipedia

Funnelsort

Funnelsort is a comparison-based sorting algorithm. It is similar to mergesort, but it is a cache-oblivious algorithm, designed for a setting where the number of elements to sort is too large to fit in a cache where operations are done. It was introduced by Matteo Frigo, Charles Leiserson, Harald Prokop, and Sridhar Ramachandran in 1999 in the context of the cache oblivious model.[1][2]

Mathematical properties edit

In the external memory model, the number of memory transfers it needs to perform a sort of   items on a machine with cache of size   and cache lines of length   is  , under the tall cache assumption that  . This number of memory transfers has been shown to be asymptotically optimal for comparison sorts. Funnelsort also achieves the asymptotically optimal runtime complexity of  .

Algorithm edit

Basic overview edit

Funnelsort operates on a contiguous array of   elements. To sort the elements, it performs the following:

  1. Split the input into   arrays of size  , and sort the arrays recursively.
  2. Merge the   sorted sequences using a  -merger. (This process will be described in more detail.)

Funnelsort is similar to merge sort in that some number of subarrays are recursively sorted, after which a merging step combines the subarrays into one sorted array. Merging is performed by a device called a k-merger, which is described in the section below.

k-mergers edit

A k-merger takes   sorted sequences. Upon one invocation of a k-merger, it outputs the first   elements of the sorted sequence obtained by merging the k input sequences.

At the top level, funnelsort uses a  -merger on   sequences of length  , and invokes this merger once.

The k-merger is built recursively out of  -mergers. It consists of   input  -mergers  , and a single output  -merger  . The k inputs are separated into   sets of   inputs each. Each of these sets is an input to one of the input mergers. The output of each input merger is connected to a buffer, a FIFO queue that can hold   elements. The buffers are implemented as circular queues. The outputs of the   buffers are connected to the inputs of the output merger  . Finally, the output of   is the output of the entire k-merger.

In this construction, any input merger only outputs   items at once, but the buffer it outputs to has double the space. This is done so that an input merger can be called only when its buffer does not have enough items, but that when it is called, it outputs a lot of items at once (namely,   of them).

A k-merger works recursively in the following way. To output   elements, it recursively invokes its output merger   times. However, before it makes a call to  , it checks all of its buffers, filling each of them that are less than half full. To fill the i-th buffer, it recursively invokes the corresponding input merger   once. If this cannot be done (due to the merger running out of inputs), this step is skipped. Since this call outputs   elements, the buffer contains at least   elements. At the end of all these operations, the k-merger has output the first   of its input elements, in sorted order.

Analysis edit

Most of the analysis of this algorithm revolves around analyzing the space and cache miss complexity of the k-merger.

The first important bound is that a k-merger can be fit in   space. To see this, we let   denote the space needed for a k-merger. To fit the   buffers of size   takes   space. To fit the   smaller buffers takes   space. Thus, the space satisfies the recurrence  . This recurrence has solution  .

It follows that there is a positive constant   such that a problem of size at most   fits entirely in cache, meaning that it incurs no additional cache misses.

Letting   denote the number of cache misses incurred by a call to a k-merger, one can show that   This is done by an induction argument. It has   as a base case. For larger k, we can bound the number of times a  -merger is called. The output merger is called exactly   times. The total number of calls on input mergers is at most  . This gives a total bound of   recursive calls. In addition, the algorithm checks every buffer to see if needs to be filled. This is done on   buffers every step for   steps, leading to a max of   cache misses for all the checks.

This leads to the recurrence  , which can be shown to have the solution given above.

Finally, the total cache misses   for the entire sort can be analyzed. It satisfies the recurrence   This can be shown to have solution  

Lazy funnelsort edit

Lazy funnelsort is a modification of the funnelsort, introduced by Gerth Stølting Brodal and Rolf Fagerberg in 2002.[3] The modification is that when a merger is invoked, it does not have to fill each of its buffers. Instead, it lazily fills a buffer only when it is empty. This modification has the same asymptotic runtime and memory transfers as the original funnelsort, but has applications in cache-oblivious algorithms for problems in computational geometry in a method known as distribution sweeping.

See also edit

References edit

  1. ^ M. Frigo, C.E. Leiserson, H. Prokop, and S. Ramachandran. Cache-oblivious algorithms. In Proceedings of the 40th IEEE Symposium on Foundations of Computer Science (FOCS 99), pp. 285-297. 1999. Extended abstract at IEEE, at Citeseer.
  2. ^ Harald Prokop. Cache-Oblivious Algorithms. Masters thesis, MIT. 1999.
  3. ^ Brodal, Gerth Stølting; Fagerberg, Rolf (25 June 2002). "Cache Oblivious Distribution Sweeping". Automata, Languages and Programming. Lecture Notes in Computer Science. Vol. 2380. Springer. pp. 426–438. CiteSeerX 10.1.1.117.6837. doi:10.1007/3-540-45465-9_37. ISBN 978-3-540-43864-9.. See also the longer technical report.

funnelsort, this, article, multiple, issues, please, help, improve, discuss, these, issues, talk, page, learn, when, remove, these, template, messages, this, article, includes, list, general, references, lacks, sufficient, corresponding, inline, citations, ple. This article has multiple issues Please help improve it or discuss these issues on the talk page Learn how and when to remove these template messages This article includes a list of general references but it lacks sufficient corresponding inline citations Please help to improve this article by introducing more precise citations May 2014 Learn how and when to remove this template message This article relies excessively on references to primary sources Please improve this article by adding secondary or tertiary sources Find sources Funnelsort news newspapers books scholar JSTOR May 2014 Learn how and when to remove this template message This article may be too technical for most readers to understand Please help improve it to make it understandable to non experts without removing the technical details May 2014 Learn how and when to remove this template message Learn how and when to remove this template message Funnelsort is a comparison based sorting algorithm It is similar to mergesort but it is a cache oblivious algorithm designed for a setting where the number of elements to sort is too large to fit in a cache where operations are done It was introduced by Matteo Frigo Charles Leiserson Harald Prokop and Sridhar Ramachandran in 1999 in the context of the cache oblivious model 1 2 Contents 1 Mathematical properties 2 Algorithm 2 1 Basic overview 2 2 k mergers 3 Analysis 4 Lazy funnelsort 5 See also 6 ReferencesMathematical properties editIn the external memory model the number of memory transfers it needs to perform a sort of N displaystyle N nbsp items on a machine with cache of size Z displaystyle Z nbsp and cache lines of length L displaystyle L nbsp is O NLlogZ N displaystyle O left tfrac N L log Z N right nbsp under the tall cache assumption that Z W L2 displaystyle Z Omega L 2 nbsp This number of memory transfers has been shown to be asymptotically optimal for comparison sorts Funnelsort also achieves the asymptotically optimal runtime complexity of 8 Nlog N displaystyle Theta N log N nbsp Algorithm editBasic overview edit Funnelsort operates on a contiguous array of N displaystyle N nbsp elements To sort the elements it performs the following Split the input into N1 3 displaystyle N 1 3 nbsp arrays of size N2 3 displaystyle N 2 3 nbsp and sort the arrays recursively Merge the N1 3 displaystyle N 1 3 nbsp sorted sequences using a N1 3 displaystyle N 1 3 nbsp merger This process will be described in more detail Funnelsort is similar to merge sort in that some number of subarrays are recursively sorted after which a merging step combines the subarrays into one sorted array Merging is performed by a device called a k merger which is described in the section below k mergers edit A k merger takes k displaystyle k nbsp sorted sequences Upon one invocation of a k merger it outputs the first k3 displaystyle k 3 nbsp elements of the sorted sequence obtained by merging the k input sequences At the top level funnelsort uses a N1 3 displaystyle N 1 3 nbsp merger on N1 3 displaystyle N 1 3 nbsp sequences of length N2 3 displaystyle N 2 3 nbsp and invokes this merger once The k merger is built recursively out of k displaystyle sqrt k nbsp mergers It consists of k displaystyle sqrt k nbsp input k displaystyle sqrt k nbsp mergers I1 I2 Ik displaystyle I 1 I 2 ldots I sqrt k nbsp and a single output k displaystyle sqrt k nbsp merger O displaystyle O nbsp The k inputs are separated into k displaystyle sqrt k nbsp sets of k displaystyle sqrt k nbsp inputs each Each of these sets is an input to one of the input mergers The output of each input merger is connected to a buffer a FIFO queue that can hold 2k3 2 displaystyle 2k 3 2 nbsp elements The buffers are implemented as circular queues The outputs of the k displaystyle sqrt k nbsp buffers are connected to the inputs of the output merger O displaystyle O nbsp Finally the output of O displaystyle O nbsp is the output of the entire k merger In this construction any input merger only outputs k3 2 displaystyle k 3 2 nbsp items at once but the buffer it outputs to has double the space This is done so that an input merger can be called only when its buffer does not have enough items but that when it is called it outputs a lot of items at once namely k3 2 displaystyle k 3 2 nbsp of them A k merger works recursively in the following way To output k3 displaystyle k 3 nbsp elements it recursively invokes its output merger k3 2 displaystyle k 3 2 nbsp times However before it makes a call to O displaystyle O nbsp it checks all of its buffers filling each of them that are less than half full To fill the i th buffer it recursively invokes the corresponding input merger Ii displaystyle I i nbsp once If this cannot be done due to the merger running out of inputs this step is skipped Since this call outputs k3 2 displaystyle k 3 2 nbsp elements the buffer contains at least k3 2 displaystyle k 3 2 nbsp elements At the end of all these operations the k merger has output the first k3 displaystyle k 3 nbsp of its input elements in sorted order Analysis editMost of the analysis of this algorithm revolves around analyzing the space and cache miss complexity of the k merger The first important bound is that a k merger can be fit in O k2 displaystyle O k 2 nbsp space To see this we let S k displaystyle S k nbsp denote the space needed for a k merger To fit the k1 2 displaystyle k 1 2 nbsp buffers of size 2k3 2 displaystyle 2k 3 2 nbsp takes O k2 displaystyle O k 2 nbsp space To fit the k 1 displaystyle sqrt k 1 nbsp smaller buffers takes k 1 S k displaystyle sqrt k 1 S sqrt k nbsp space Thus the space satisfies the recurrence S k k 1 S k O k2 displaystyle S k sqrt k 1 S sqrt k O k 2 nbsp This recurrence has solution S k O k2 displaystyle S k O k 2 nbsp It follows that there is a positive constant a displaystyle alpha nbsp such that a problem of size at most aZ displaystyle alpha sqrt Z nbsp fits entirely in cache meaning that it incurs no additional cache misses Letting QM k displaystyle Q M k nbsp denote the number of cache misses incurred by a call to a k merger one can show that QM k O k3logZ k L displaystyle Q M k O k 3 log Z k L nbsp This is done by an induction argument It has k aZ displaystyle k leq alpha sqrt Z nbsp as a base case For larger k we can bound the number of times a k displaystyle sqrt k nbsp merger is called The output merger is called exactly k3 2 displaystyle k 3 2 nbsp times The total number of calls on input mergers is at most k3 2 2k displaystyle k 3 2 2 sqrt k nbsp This gives a total bound of 2k3 2 2k displaystyle 2k 3 2 2 sqrt k nbsp recursive calls In addition the algorithm checks every buffer to see if needs to be filled This is done on k displaystyle sqrt k nbsp buffers every step for k3 2 displaystyle k 3 2 nbsp steps leading to a max of k2 displaystyle k 2 nbsp cache misses for all the checks This leads to the recurrence QM k 2k3 2 2k QM k k2 displaystyle Q M k leq 2k 3 2 2 sqrt k Q M sqrt k k 2 nbsp which can be shown to have the solution given above Finally the total cache misses Q N displaystyle Q N nbsp for the entire sort can be analyzed It satisfies the recurrence Q N N1 3Q N2 3 QM N1 3 displaystyle Q N N 1 3 Q N 2 3 Q M N 1 3 nbsp This can be shown to have solution Q N O N L logZ N displaystyle Q N O N L log Z N nbsp Lazy funnelsort editLazy funnelsort is a modification of the funnelsort introduced by Gerth Stolting Brodal and Rolf Fagerberg in 2002 3 The modification is that when a merger is invoked it does not have to fill each of its buffers Instead it lazily fills a buffer only when it is empty This modification has the same asymptotic runtime and memory transfers as the original funnelsort but has applications in cache oblivious algorithms for problems in computational geometry in a method known as distribution sweeping See also editCache oblivious algorithm Cache oblivious distribution sort External sortingReferences edit M Frigo C E Leiserson H Prokop and S Ramachandran Cache oblivious algorithms In Proceedings of the 40th IEEE Symposium on Foundations of Computer Science FOCS 99 pp 285 297 1999 Extended abstract at IEEE at Citeseer Harald Prokop Cache Oblivious Algorithms Masters thesis MIT 1999 Brodal Gerth Stolting Fagerberg Rolf 25 June 2002 Cache Oblivious Distribution Sweeping Automata Languages and Programming Lecture Notes in Computer Science Vol 2380 Springer pp 426 438 CiteSeerX 10 1 1 117 6837 doi 10 1007 3 540 45465 9 37 ISBN 978 3 540 43864 9 See also the longer technical report Retrieved from https en wikipedia org w index php title Funnelsort amp oldid 1185334811, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.