In computer science, the Brodal queue is a heap/priority queue structure with very low worst case time bounds: O(1) for insertion, find-minimum, meld (merge two queues) and decrease-key and O(log(n)) for delete-minimum and general deletion. They are the first heap variant to achieve these bounds without resorting to amortization of operational costs. Brodal queues are named after their inventor Gerth Stølting Brodal. While having better asymptotic bounds than other priority queue structures, they are, in the words of Brodal himself, "quite complicated" and "[not] applicable in practice." Brodal and Okasaki describe a persistent (purely functional) version of Brodal queues.
Definition A Brodal queue is a set of two trees T1 and T2 and 5 guides. The definition of the guide data structure can be found in the following section. For both trees, each node has a rank, this rank is useful for later operations and intuitively corresponds to the logarithm of the size of the subtree rooted in the node. We note arityi(x) the number of children of the node x with rank i. We will also use t1 for the root of tree T1 and t2 for the root of tree T2. At each given time, every subtree rooted in a node needs to fulfill these 5 invariants (which will be later called RANK invariants):
LEAF-RANK : If x is a leaf, then rank(x)=0, PARENT-RANK : rank(x)<rank(parent(x)), NEXT-RANK-ARITY : If rank(x)>0, then arityrank(x)−1(x)⩾2, ARITY-BOUND: arityi(x)∈{0,2,3,…,7} we highlight that arityi(x)≠1, ROOT-RANK : T2=∅︀ or rank(t1)⩽rank(t2). Here NEXT-RANK-ARITY guarantees us that the size of the subtree rooted in a node is at least exponential to the rank of that node. In addition, ARITY-BOUND bounds the number of children of each rank for a given node, this implies that all nodes have rank and degrees in O(logn). In a Brodal queue, not every node will have a bigger value than its parent, the nodes violating this condition will be called violating nodes. However, we want to keep the number of violating nodes relatively small. To keep track of violating nodes, we create for each node two sets V(x) and W(x) of nodes larger than x. Intuitively, V(x) are the nodes larger of x with large rank (such that y∈V(x) if rank(y)⩾rank(t1)), and W(x) are the nodes with small rank (rank(y)<rank(t1)). These sets are implemented using doubly linked list meaning they have an order. In particular, all violating nodes added to V(x) are added at the front of the list, and all violating nodes added to W(x) are inserted next to a node of same rank. We let wi(x) denote the number of nodes in W(x) of rank iThe V(x) and W(x) lists fulfill these 5 invariants (we will call the SETS invariants):
MINIMUM-NODE : t1=min(T1∪T2) VIOLATING-CONDITION : If y∈V(x)∪W(x) then y⩾x PARENT-VIOLATING: If y<parent(y) then there exist a node x≠y such that y∈V(x)∪W(x) W-RANK-BOUND: wi(x)⩽6 V-RANK-BOUND: By denoting V(x)=(v|V(x)|,…,v2,v1), we have: rank(vi)⩾⌊i−1α⌋ for a certain constant α. Since all nodes have rank in O(logn) the W-RANK-BOUND and V-RANK-BOUND, all V(x) and W(x) are in size O(logn). We also have some invariants of the roots of the trees T1 and T2: t1 and t2 (called the ROOTS invariants).
ROOT-ARITY : ti∈{2,3,…,7} for i∈{0,1,…,rank(ti)−1}, V-SIZE-BOUND: |V(x)|⩽α rank(t1), W-ELEMENTS-RANK: if y∈W(t1), then rank(y)<rank(t1). The V-SIZE-BOUND invariant essentially tells us that if we increase the rank of t1 by one, we have at most α new "large" violations (here large means having a high rank) without violating the V-RANK-BOUND invariant. On the other hand, the W-ELEMENTS-RANKinvariant tells us that all violations in W(x) are "small", this invariant is true per the definition of W. Maintaining the invariants W-RANK-BOUND and ROOT-ARITY is not trivial, to maintain these we will use the DecreaseKey operation which can be implemented using a guide as defined in the next section. Each time we will call the DecreaseKey operation, we will essentially :
Add the new violation to V(t1) or W(t1) depending on the rank of that violation. To avoid V(t1) and W(t1) from getting too large, we incrementally do two kinds of transformations: Moving the sons of t2 to t1 to increase the rank of t1 Reducing the number of violations in W(t1) by replacing two violations of rank k to one violation of rank k+1
The guide data structure This definition is based on the definition from Brodal's paper. We assume that we have a sequence of variables xk,…,x1 and we want to make sure that ∀i⩽k,xi⩽T for some threshold T. The only operation allowed is REDUCE(i) which decreases xi by at least 2 and increases xi+1 by at most 1. We can assume without loss of generality that REDUCE(i) reduces xi by 2 and increases xi+1by 1. If a xj is increased by one, the goal of the guide is to tell us on which indices i to apply REDUCE(i) in order to respect the threshold. The guide is only allowed to make O(1) calls to the REDUCE function for each increase. The guide has access to another sequence x'k,…,x'1 such that xi⩽x'i and x'i∈{T−2,T−1,T}. As long as after the increase of xj we have xj⩽x'j we do not need to ask help from our guide since xj is "far" bellow T. However, if xj=x'j before the increase, then we have xj+1>x'j after the change. To simplify the explanation, we can assume that T=2, so that x'i∈{0,1,2}. The guide will create blocks in the sequence of x'i of form 2,1,1,…,1,0 where we allow there to be no 1. The guide maintains the invariant that each element which isn't in a block is either a 1 or a 0. For example, here are the blocks for a sequence of x'i. 1,2,1,1,0_,1,1,2,0_,2,0_,1,0,2,1,0_ The guide is composed of 3 arrays :
x the array of xk,…,x1 x′the array of x'k,…,x'1 p an array of pointers where all the pi for which x'i are in the same block will point to the same memory cell containing a value. If a x'i is not in a block, then pi points to a memory cell containing ⊥. With this definition, a guide has two important properties :
… excerpt ends here. Continue reading the full article.