Jump to content

Skew binomial heap

From Wikipedia, the free encyclopedia
Skew binomial heap
Typeheap
Invented1996
Invented byGerth Stølting BrodalandChris Okasaki
Time complexityinbig O notation
Operation Average Worst case
Insert Θ(1) Θ(1)
Find-min Θ(1)
Delete-min O(logn)
Decrease-key O(logn)
Merge O(logn)
Space complexity

Incomputer science,askew binomial heap(orskew binomial queue) is adata structureforpriority queueoperations. It is a variant of thebinomial heapthat supports constant-time insertion operations in the worst case, rather thanamortized time.

Motivation

[edit]

Just asbinomial heapsare based on thebinary number system,skew binary heaps are based on theskew binary number system.[1]Ordinary binomial heaps suffer from worst case logarithmic complexity for insertion, because acarryoperation may cascade, analagous to binary addition. Skew binomial heaps are based on the skew binary number system, where theth digit (zero-indexed) represents,instead of.Digits are either 0 or 1, except the lowest non-zero digit, which may be 2. An advantage of this system is that at most one carry operation is needed. For example, 60 is represented as 11200 in skew binary (31 + 15 + 7 + 7), and adding 1 produces 12000 (31 + 15 + 15). Since the next higher digit is guaranteed not to be 2, a carry is performed at most once. This analogy is applied to the insertion operation by introducing ternary (skew) links, which link 3 trees together. This allows the insertion operation to execute in constant time.

Structure

[edit]
Skew binomial heap containing numbers 1 to 19, showing trees of ranks 0, 1, 2, and 3 constructed from various types of links
Simple, typeaskew, and typebskew links

A skew binomial heap is aforestof skew binomialtrees,which are defined inductively:

  • A skew binomial tree of rank 0 is a singleton node.
  • A skew binomial tree of rankcan be constructed in three ways:
    • asimple linklinks two ranktrees, making one the leftmost child of the other;
    • atype A skew linklinks three trees. Two ranktrees become the children of a rank 0 tree;
    • atype B skew linklinks three trees. A rank 0 tree and ranktree become the leftmost children of another ranktree.

When performing any link, the tree with the smallest key always becomes the root. Additionally, we impose the invariant that there may be only one tree of each rank, except the lowest rank which may have up to two.

The followingOCamlcode demonstrates the linking operations:

type'aheap='atreelist
and'atree=Treeof'a*int*'atreelist

letrank(Tree(_,r,_))=r

letsimple_link(Tree(k1,r,c1)ast1)(Tree(k2,r,c2)ast2)=
ifk1<=k2then
Tree(k1,r+1,t2::c1)
else
Tree(k2,r+1,t1::c2)

letskew_linkk1(Tree(k2,r,c2)ast2)(Tree(k3,r,c3)ast3)=
ifk1<=k2&&k1<=k3then(* type A *)
Tree(k1,r+1,[t2;t3])
else(* type B *)
lett1=Tree(k1,0,[])in
ifk2<=k3then
Tree(k2,r+1,t1::t3::c2)
else
Tree(k3,r+1,t1::t2::c3)

From these properties, it can be deduced that the root of a rankskew binomial tree has up tochildren. The number of nodes in a skew binomial treeof rankis also bounded by.Since trees of the same rank may have different numbers of nodes, there may be more than one way to distribute the ranks in the heap.

These constructions may be seen as a generalisation ofbinary treesand binomial trees. A skew binomial tree constructed using onlysimple linksis an ordinary binomial tree, and using onlytype A skew linksresults in a perfectly balanced binary tree.

Operations

[edit]

Find-min

[edit]

Search the list of roots to find the node containing the minimum key. This takestime.

In an imperative setting, one can maintain a pointer to the root containing the minimum key, allowing access intime. This pointer must be updated after every operation, adding only a constant overhead in time complexity.

In a functional setting without random access to nodes, one can instead represent the heap as a single tree with skew binomial trees as its children. The root of this tree is the minimum of the heap, allowingaccess. Note that this tree will not necessarily be a skew binomial tree itself. The other operations must be modified to deal with this single tree. This concept of a global root is used in theoptimizationsdescribed below, albeit slightly differently.

Merge

[edit]

To merge two skew binomial heaps together, first eliminate any duplicate rank trees in each heap by performingsimple links.Then, merge the heaps in the same fashion as ordinary binomial heaps, which is similar to binary addition. Trees with the same ranks are linked with asimple link,and a 'carry' tree is passed upwards if necessary. Because the rank of trees in each heap is now unique, at most three trees of the same rank are considered, which is sufficient to establish abound.

letrecunique=function
|t1::t2::tswhenrankt1=rankt2->
unique(simple_linkt1t2::ts)
|ts->ts

letrecmerge_uniqh1h2=matchh1,h2with
|h1,[]->h1
|[],h2->h2
|t1::ts1,t2::ts2->
ifrankt1<rankt2then
t1::merge_uniqts1h2
elseifrankt1>rankt2then
t2::merge_uniqh1ts2
else
unique(simple_linkt1t2::merge_uniqts1ts2)

letmergeh1h2=merge_uniq(uniqueh1)(uniqueh2)

Insert

[edit]

Create a skew binomial tree of rank 0 (a singleton node), containing the key to be inserted. The smallest two trees in the heap are then considered:

  • If they are both of rank,then perform askew linkwith these two trees and the singleton node. The resulting tree is of rank.Since there can only have been at most one ranktree in the original heap, the invariant is preserved.
  • If they are of different ranks, simply add the rank 0 tree to the front of the list of roots. Since the list of roots did not have duplicate rank trees before, the invariant is not violated, as there will be at most two rank 0 trees after.

As up to one link is performed, this operation executes in worst casetime, improving on the binomial heap which relies onamortizedanalysis for itsbound, with a worst case of.

letinsertk=function
|t2::t3::tswhenrankt2=rankt3->skew_linkkt2t3::ts
|ts->Tree(k,0,[])::ts

Delete-min

[edit]

Find and discard the node containing the minimum key. This node must be the root of a tree. Divide its children into two groups, those with rank 0, and those with rank > 0. Note that there may be more than two children with rank 0, due toskew links.The children whose rank > 0 form a valid skew binomial heap, as they are already ordered, and have no duplicates. Merging these children into the heap takestime. Afterwards, reinsert each of the rank 0 children into the heap at a cost ofeach. The total time required is.

Decrease-key

[edit]

This operation is unchanged from binomial heaps. Decreasing the key of a node may cause it to be smaller than its parent. Repeatedly exchange it with its parent until theminimum-heapproperty is satisfied, at a cost oftime complexity. This operation requires a pointer to the node containing the key in question, and is easiest done in an imperative setting.

Optimizations

[edit]

Brodal and Okasaki showed how the time complexity of the merge operation can be reduced to,by applying the 'bootstrapping' technique of Buchsbaum andTarjan.[2]

Let the type of a primitive skew binomial heap containing elements of typebe.Instead of the forest of trees representation described above, we mainintain a single tree with a global root as its minimum.

Let the type of arootedskew binomial heap be

,

that is, a pair containing an element of typeand a primitive heap of rooted heaps.

Finally, we define the type of abootstrapped heapby enclosing rooted heaps in anoption type:

which permits the empty heap.

The operations on this bootstrapped heap are redefined accordingly. In the following OCaml code, the prime symbol'denotes operations for bootstrapped heaps.

type'abootstrapped=
|Empty
|Rootof'arooted

letfind_min'(Root(k,h))=k

letmerge'bh1bh2=matchbh1,bh2with
|_,Empty->bh1
|Empty,_->bh2
|Root(k1,h1),Root(k2,h2)->
ifk1<=k2then
Root(k1,insertbh2h1)
else
Root(k2,insertbh1h2)

letinsert'kh=merge'(Root(k,[]))h

letdelete_min'(Root(x,h))=
letRoot(y,h1)=find_minhin
leth2=delete_minhin
Root(y,mergeh1h2)

The new merge operation uses only insert operations on primitive heaps. Thus, it executes intime. This technique can be applied to any priority queue with constant time insertion, and logarithmic merging.

Summary of running times

[edit]

Here aretime complexities[3]of various heap data structures. The abbreviationam.indicates that the given complexity is amortized, otherwise it is a worst-case complexity. For the meaning of "O(f) "and"Θ(f) "seeBig O notation.Names of operations assume a min-heap.

Operation find-min delete-min decrease-key insert meld make-heap[a]
Binary[3] Θ(1) Θ(logn) Θ(logn) Θ(logn) Θ(n) Θ(n)
Skew[4] Θ(1) O(logn)am. O(logn)am. O(logn)am. O(logn)am. Θ(n)am.
Leftist[5] Θ(1) Θ(logn) Θ(logn) Θ(logn) Θ(logn) Θ(n)
Binomial[3][7] Θ(1) Θ(logn) Θ(logn) Θ(1)am. Θ(logn)[b] Θ(n)
Skew binomial[8] Θ(1) Θ(logn) Θ(logn) Θ(1) Θ(logn)[b] Θ(n)
2–3 heap[10] Θ(1) O(logn)am. Θ(1) Θ(1)am. O(logn)[b] Θ(n)
Bottom-up skew[4] Θ(1) O(logn)am. O(logn)am. Θ(1)am. Θ(1)am. Θ(n)am.
Pairing[11] Θ(1) O(logn)am. o(logn)am.[c] Θ(1) Θ(1) Θ(n)
Rank-pairing[14] Θ(1) O(logn)am. Θ(1)am. Θ(1) Θ(1) Θ(n)
Fibonacci[3][15] Θ(1) O(logn)am. Θ(1)am. Θ(1) Θ(1) Θ(n)
Strict Fibonacci[16][d] Θ(1) Θ(logn) Θ(1) Θ(1) Θ(1) Θ(n)
Brodal[17][d] Θ(1) Θ(logn) Θ(1) Θ(1) Θ(1) Θ(n)[18]
  1. ^make-heapis the operation of building a heap from a sequence ofnunsorted elements. It can be done inΘ(n) time whenevermeldruns inO(logn) time (where both complexities can be amortized).[4][5]Another algorithm achievesΘ(n) for binary heaps.[6]
  2. ^abcForpersistentheaps (not supportingdecrease-key), a generic transformation reduces the cost ofmeldto that ofinsert,while the new cost ofdelete-minis the sum of the old costs ofdelete-minandmeld.[9]Here, it makesmeldrun inΘ(1) time (amortized, if the cost ofinsertis) whiledelete-minstill runs inO(logn). Applied to skew binomial heaps, it yields Brodal-Okasaki queues, persistent heaps with optimal worst-case complexities.[8]
  3. ^Lower bound of[12]upper bound of[13]
  4. ^abBrodal queues and strict Fibonacci heaps achieve optimal worst-case complexities for heaps. They were first described as imperative data structures. The Brodal-Okasaki queue is apersistentdata structure achieving the same optimum, except thatdecrease-keyis not supported.

References

[edit]
  1. ^Brodal, Gerth Stølting; Okasaki, Chris (November 1996), "Optimal purely functional priority queues",Journal of Functional Programming,6(6): 839–857,doi:10.1017/s095679680000201x
  2. ^Buchsbaum, A.L.; Tarjan, R.E. (May 1995)."Confluently Persistent Deques via Data-Structural Bootstrapping".Journal of Algorithms.18(3): 513–547.doi:10.1006/jagm.1995.1020.ISSN0196-6774.
  3. ^abcdCormen, Thomas H.;Leiserson, Charles E.;Rivest, Ronald L.(1990).Introduction to Algorithms(1st ed.). MIT Press and McGraw-Hill.ISBN0-262-03141-8.
  4. ^abcSleator, Daniel Dominic;Tarjan, Robert Endre(February 1986)."Self-Adjusting Heaps".SIAM Journal on Computing.15(1): 52–69.CiteSeerX10.1.1.93.6678.doi:10.1137/0215004.ISSN0097-5397.
  5. ^abTarjan, Robert(1983). "3.3. Leftist heaps".Data Structures and Network Algorithms.pp. 38–42.doi:10.1137/1.9781611970265.ISBN978-0-89871-187-5.
  6. ^Hayward, Ryan; McDiarmid, Colin (1991)."Average Case Analysis of Heap Building by Repeated Insertion"(PDF).J. Algorithms.12:126–153.CiteSeerX10.1.1.353.7888.doi:10.1016/0196-6774(91)90027-v.Archived fromthe original(PDF)on 2016-02-05.Retrieved2016-01-28.
  7. ^"Binomial Heap | Brilliant Math & Science Wiki".brilliant.org.Retrieved2019-09-30.
  8. ^abBrodal, Gerth Stølting; Okasaki, Chris (November 1996), "Optimal purely functional priority queues",Journal of Functional Programming,6(6): 839–857,doi:10.1017/s095679680000201x
  9. ^Okasaki, Chris(1998). "10.2. Structural Abstraction".Purely Functional Data Structures(1st ed.). pp. 158–162.ISBN9780521631242.
  10. ^Takaoka, Tadao(1999),Theory of 2–3 Heaps(PDF),p. 12
  11. ^Iacono, John(2000), "Improved upper bounds for pairing heaps",Proc. 7th Scandinavian Workshop on Algorithm Theory(PDF),Lecture Notes in Computer Science, vol. 1851, Springer-Verlag, pp. 63–77,arXiv:1110.4428,CiteSeerX10.1.1.748.7812,doi:10.1007/3-540-44985-X_5,ISBN3-540-67690-2
  12. ^Fredman, Michael Lawrence(July 1999)."On the Efficiency of Pairing Heaps and Related Data Structures"(PDF).Journal of the Association for Computing Machinery.46(4): 473–501.doi:10.1145/320211.320214.
  13. ^Pettie, Seth (2005).Towards a Final Analysis of Pairing Heaps(PDF).FOCS '05 Proceedings of the 46th Annual IEEE Symposium on Foundations of Computer Science. pp. 174–183.CiteSeerX10.1.1.549.471.doi:10.1109/SFCS.2005.75.ISBN0-7695-2468-0.
  14. ^Haeupler, Bernhard; Sen, Siddhartha;Tarjan, Robert E.(November 2011)."Rank-pairing heaps"(PDF).SIAM J. Computing.40(6): 1463–1485.doi:10.1137/100785351.
  15. ^Fredman, Michael Lawrence;Tarjan, Robert E.(July 1987)."Fibonacci heaps and their uses in improved network optimization algorithms"(PDF).Journal of the Association for Computing Machinery.34(3): 596–615.CiteSeerX10.1.1.309.8927.doi:10.1145/28869.28874.
  16. ^Brodal, Gerth Stølting;Lagogiannis, George;Tarjan, Robert E.(2012).Strict Fibonacci heaps(PDF).Proceedings of the 44th symposium on Theory of Computing - STOC '12. pp. 1177–1184.CiteSeerX10.1.1.233.1740.doi:10.1145/2213977.2214082.ISBN978-1-4503-1245-5.
  17. ^Brodal, Gerth S.(1996),"Worst-Case Efficient Priority Queues"(PDF),Proc. 7th Annual ACM-SIAM Symposium on Discrete Algorithms,pp. 52–58
  18. ^Goodrich, Michael T.;Tamassia, Roberto(2004). "7.3.6. Bottom-Up Heap Construction".Data Structures and Algorithms in Java(3rd ed.). pp. 338–341.ISBN0-471-46983-1.