2.5 Shortest Path Data Structures
2.5.4 Trinomial heap
The trinomial heap uses the idea of a bad child or inconsistent node, where a limited number of inconsistent nodes are allowed to be in the tree. In this thesis the words “inconsistent” and “active” are used interchangeably for convenience. Precisely speaking, active encompasses inconsistent. A node can become inconsistent and then consistent passively by some operation at the parent. It is expensive to check an active node is consistent with its parent. Definition of this heap is similar to the 2-3 heap. A very simple trinomial heap structure is given in Figure 2.4. In Figure 2.4, node a has two children b and c. Node b and c are called partner nodes, and normally sorted in non-decreasing order, unless they are active. Nodes b and d are called siblings. Sibling nodes are distinguished by dimensions. Node d is a child of a. Node d is a higher dimensional child of a than b is. A trunk that has nodes a and d is called the main trunk in this tree. In general the trunk of the highest dimension is the main trunk. Node b is an active node, where a black circle is used to represent this node in the tree. Node b is called the first child and node c denotes as the second child on the trunk that connects a, b and c. The first child and second child terms are used to describe the nodes’s position on the trunk.
An active node is not allowed in the main trunk; if the wrong order occurs, the two nodes are swapped together with the underlying trees.
b
c
a
d
main trunk
Figure 2.4: An example of a trinomial tree structure
In this heap, two internal operations are introduced. These operations are called reordering and rearrangement. Sometimes, reordering is called a promotion process. When a trunk has inconsistent nodes, that means the key value of the head node might be lower than one or two nodes that are located on the same trunk. If this happens, reordering will reorder the position of the nodes to maintain the heap property. With this technique, the number of inconsistent nodes will be reduced.
Another internal operation is rearrangement. Rearrangement is a process for rearranging nodes that are located on two different trunks. This is done when there are two trunks of the same dimensions have inconsistent nodes. Each might have one inconsistent node. The rearrangement process will rearrange the position of all nodes in these two trunks so that the heap property is maintained. Rearranging the nodes by its key values will help in maintaining the heap structure, thus reducing the number of inconsistent nodes.
The insertion process works in a similar manner to the 2-3 heap. When a new node is inserted into the heap, the node will be merged to the right most tree in the heap. If the results of the merge operation is a carry tree, the carry tree will propagate to the left. The delete-min operation is quite tricky in the trinomial heap. This is mainly because the trinomial heap allows the heap to have inconsistent nodes. To search for the minimum node means, not only the root nodes are scanned, but also the inconsistent nodes list should also be examined. This takes O(log n) time to search for the minimum node as the number of active nodes is bounded by O(log n).
During the deletion process, the number of active nodes may decrease. If the minimum node was an active node, it must be made active. This is to make sure that the number of active
2.5 Shortest Path Data Structures
a
d
b
c
e
g
f
h
j
i
k
(a) A trinomial tree structure before the break-up operation
a
d
b
c
f
g
h
j
i
k
(b) The tree during the break-up operation
Figure 2.5: The resulting sub trees when node e is removed from the tree. Break-up operation will result in sub trees rooted at a, f and g
nodes is decreased at least by one when the minimum node is removed.
When the minimum node is chosen to be deleted, operation break-up must be performed. The resulting break-up depends on whether or not the minimum node is a root node or an active node. In the break-up operation, the higher dimension parts of the tree will be broken apart, producing trunks to be merged back into the heap at the root level. The child trunk will also be merged to the root level after the link between the minimum node and the child trunk is broken. Nodes in the break-up may go from active to inactive. If the first child is an active node, the second child may be possible an active node as well. However, if the first child is not an active node, the second child activeness does not have to be checked. After the break up, the length of the main trunk decreases by one. The current tree position will become empty unless the minimum node has a partner node.
Figure 2.5 shows an example of break-up operation at node e. When e is removed from the tree, the length of the main trunk becomes 2, where nodes a and i are treated as the first and the second child node on the main trunk. Node a is also considered as a new partner of node i and the same the other way around. As a child node of the removed node, node g becomes a nonactive node. The second node on the trunk that connected nodes e, g, h, that is node h is also made nonactive. Thus, with the break-up operation, the number of active nodes is decreased by three; one from the removed node, e and another two from g and g. The new trees, rooted at a, f and g, will be merged back to the root level with the existing trees in the heap. The delete-min operation is obviously done in O(log n) time.
The decrease-key operation is quite relax in the trinomial heap. The position of the node which the key is decreased on a trunk is important. The decreased node might have to be swapped with a higher location node on the same trunk if the new key is smaller than the higher node. This is to ensure that the heap order is correct. The decreased node is made a new inconsistent node if the number of inconsistent nodes in the heap is still in tolerance. Otherwise, reordering and rearrangement process has to be performed to keep the number of inconsistent nodes under control. To describe the decrease-key process, Algorithm 3 is given. Let v be the node that the key value is decreased.
Algorithm 3 Decrease-key procedure in the trinomial heap
1: if v is a root node OR v is an active node then
2: rearrangement is not necessary;
3: if v has a parent node AND v is the second child then
4: if key(v) < key(first child) then
5: swap v with the first child to maintain the correct ordering;
6: else
7: if the first child is active then
8: make v as a new active node;
9: if the number of active nodes reaches its limit then
10: rearrangement is needed;
11: else . v is the first child
12: v will be made active ;
13: if the number of active nodes reaches its limit then
14: rearrangement is needed;
15: if key(v) < key(first child) then . v is the second node on the main trunk
16: swap v with the first child to maintain the correct ordering;
In (2), the decrease-key operation can be implemented in O(1) for both worst case and amortized time.