Store a complete binary tree in a one-indexed array , level by level. Index has parent in a rooted tree for and possible children in a rooted tree and , present only when their indices are at most . The max-heap condition is for every .
Following these inequalities from the root of a tree to any entry proves that is a maximum. The heap order does not sort siblings or unrelated branches. The complete binary tree has height , which bounds how far any single repair can travel. Store the current heap size separately from the array's capacity.
Assume the subtrees rooted at the children in a rooted tree of already obey the max-heap condition. Compare with each present child and choose an index of the largest of these at most three keys. If that index is , stop. Otherwise exchange the two keys, continue at the chosen child, and repeat. This is sift down, with the inequality reversed from a minimum-oriented binary heap.
The exchange repairs the old root position because its new key dominates both child roots; only the subtree receiving the displaced key can still be defective. This recursion invariant proves correctness. Each exchange descends one level and costs constant work, so the worst-case running time is for subtree height , hence . A small key at the root of a suitably ordered heap must descend to a leaf, so the logarithmic worst-case bound is attained.
Leaves already obey the heap order. Process indices , calling the preceding repair at each. When an index is processed, its descendants have already been processed, so the repair's precondition holds. The resulting array is a max-heap.
The bound obtained by charging to every call is unnecessarily large. The number of nodes with height at least is at most . ThereforeThe total repair work is thus , and the loop itself has a linear number of iterations, giving for this bottom-up heap construction. Most nodes are near the leaves and require little repair.
A maximum priority queue stores keyed entries and supports insertion, inspection and removal of an entry with greatest key, and key increases. A max-heap gives the following running times, with the size/capacity cost of the backing array accounted for separately:
maximum: return the root key in time.insert: append at the next available leaf, then use sift up until the heap order holds, in time.extractMaximum: save the root, replace it by the last entry, reduce the size, and use sift down, in time.increaseKey(i,k): reject ; otherwise set the new key and use sift up, in time.isEmptyandsize: inspect the stored size in time; construct a queue from keys by bottom-up heap construction in time.
Empty-queue inspections and removals need an explicit failure result. The index argument to
increaseKey must identify the entry's current location: if callers use item handles, update their location records during swaps.Given the target's current heap index , remove it as follows. If , discard the last entry. Otherwise replace by and reduce by one. If the replacement is larger than its parent in a rooted tree, use sift up; otherwise use the maximum-oriented sift down from .
Only comparisons incident to the replacement can initially violate the heap order. If it exceeds its parent, it also dominates the old children because the old key dominated those children and was no larger than the parent; an upward repair suffices. Otherwise the upward comparison is valid and only a downward repair can be needed. Thus removal takes worst-case time after the location is known. Searching an unindexed heap for an arbitrary specified key can instead require time; the logarithmic bound does not include such a search.
Only arrange switches towards
A. For every vertex , compute its degree and the current number of neighbours preferring A. Put the B vertices in a max-heap with key . Maintain each vertex's current heap array index during exchanges.While the queue is nonempty and its maximum key is positive, extract that vertex , show it its neighbours' preferences, append to the answer sequence, and set its preference to
A. For every neighbour still preferring B, increase by one and use increaseKey to increase its heap key by two. Stop when every remaining key is nonpositive.A positive key means precisely a strict majority of
A neighbours, so every listed move is legal. Degrees never change; A neighbours never revert in this chosen sequence. An isolated B vertex has key zero and cannot be converted. Initialization costs , there are at most extractions, and each edge causes at most two neighbour inspections and at most a constant number of key updates. The required bound is therefore . A simple work queue of newly eligible vertices can even implement the same monotone threshold activation in time, since a vertex needs to be enqueued only once.Let be the final
A set produced by the algorithm. It contains the initial A set, and every vertex outside has at most half its neighbours in : otherwise its final key would still be positive. Thus is closed under the activation rule.Now consider any legal sequence, including sequences which change some
A preferences back to B. We prove by induction that its current A set is always a subset of . This is true initially. A switch to B preserves the inclusion. If a vertex outside were to switch to A, its A neighbours would all lie in , so their number could not exceed half its degree. Such a switch is forbidden. A switch inside also preserves the inclusion.Consequently every attainable final
A set has size at most . The algorithm supplies a legal sequence attaining itself, so its sequence is optimal. This closure dominance for asynchronous threshold switching proves more than a local-greedy claim: temporary losses of A vertices cannot unlock an additional vertex outside the activation closure. Articles by others on the same topic
There are currently no matching articles.