Priority Queues and Disjoint Union
Overview
The lecture develops a simplified Fibonacci-heap-style priority queue using tournament trees, showing how deferred cleanup and a potential function yield amortized O(1) insertion and decrease-key, plus O(log n) delete-min. It then analyzes disjoint-set union with union by rank and path compression, first deriving an O(log* n) amortized bound through rank thresholds and explaining how more intricate Ackermann-based thresholds give the inverse-Ackermann bound.
Key takeaways
- A tournament-tree priority queue can support O(1) amortized decrease-key by cutting the changed element's parent edge instead of restructuring a binary heap along a logarithmic path.
- Tracking trees, unary nodes, and total nodes in a potential function pays for heap cleanup, including equal-height links and pruning levels with too many unary nodes.
- Distinct tournament-tree heights and an O(log n) maximum height limit delete-min to an O(log n) root scan and O(log n) cuts.
- Union by rank alone guarantees that a rank-k set contains at least 2^k elements, which bounds find-path height by O(log n).
- Path compression combined with union by rank has inverse-Ackermann amortized cost per operation; a simpler threshold analysis first demonstrates an O(log* n) bound.
Chapters
- Amortized analysis averages operation costs over a sequence, while randomized analysis averages over an algorithm's random choices; neither averages over input distributions.
- Dijkstra's algorithm repeatedly extracts the closest unsettled vertex and decreases tentative distances as edges are explored.
- Prim's minimum-spanning-tree algorithm similarly selects the cheapest edge connecting the growing tree to an outside vertex.
- Fibonacci-heap-style decrease-key can reduce the edge-update contribution from O(m log n) to O(m), yielding an overall O(m + n log n) bound.
- Each tournament tree stores its elements at leaves, with each internal node recording the minimum among its descendants.
- Every internal node has one or two children, and all leaves in a tree share the same depth.
- A cut removes the parent edge of an element's top node, splitting a valid tournament tree into two valid trees.
- A link combines two equal-height trees beneath a new root whose value is the smaller of their root values.
- The heap is a collection of tournament trees with distinct heights, so at most O(log n) trees need to be scanned to find the global minimum.
- Unlike a perfectly binary heap, one-child nodes can make a tournament tree tall without quickly reducing the number of descendants.
- The structure monitors unary nodes at each level; when too many accumulate, it cuts off nodes at and above that level.
- Removing excessive unary nodes restores enough branching to keep tree heights O(log n).
- The potential tracks the number of trees, unary nodes, and total nodes, with constants chosen to balance their contributions.
- Linking two equal-height trees reduces the tree count by one, paying for the new internal node.
- When a level has too many unary nodes, deleting the upper structure reduces unary-node and node counts enough to pay for the new trees created.
- The potential makes both invariant-restoring procedures amortized free, letting operations temporarily violate the structural rules.
- Insertion creates a singleton tree in O(1) actual time; any resulting equal-height trees are linked during cleanup.
- For decrease-key, pointers from internal nodes to stored elements let a changed leaf value update ancestor minima without walking to the root.
- The operation cuts the element's parent edge, decreases its key, and then restores the collection invariants through cleanup.
- The cut and key update take O(1) actual time, and the constant potential increase gives O(1) amortized decrease-key.
- Because tree heights are distinct and bounded by O(log n), scanning all roots finds the global minimum in O(log n) time.
- Deleting the minimum cuts away the branches hanging from its path, producing up to O(log n) new trees.
- Each cut has O(1) amortized cost, so delete-min takes O(log n) amortized time.
- The resulting interface has O(1) amortized insert and decrease-key, with O(log n) amortized delete-min.
- A disjoint-set structure maintains a collection of nonoverlapping sets with make-set, union, and same-set queries.
- In Kruskal's minimum-spanning-tree algorithm, vertices are the elements and each set represents a connected component.
- Before accepting an edge, Kruskal checks whether its endpoints are already in the same set, which would indicate a cycle.
- Each set is represented by a rooted tree, and finding an element's root identifies its set.
- Initially, every disjoint-set element is a root with rank zero.
- A union attaches the lower-rank root beneath the higher-rank root, limiting unnecessary increases in tree height.
- When two roots have equal rank, either may become the parent, and the chosen root's rank increases by one.
- Ranks strictly increase along parent pointers toward the root, though rank need not equal the node's current height.
- A root of rank k represents a tree containing at least 2^k elements, because its rank can increase only by joining two equal-rank trees.
- Since there are at most n elements, no root can have rank greater than logâ‚‚ n.
- Ranks increase strictly along any path toward the root, so a tree's height is also O(log n).
- Union by rank alone therefore keeps find operations within O(log n) time.
- Path compression makes each visited node point directly to the root after a find operation.
- A recursive find also compresses the parent path, flattening all nodes visited on the way to the root.
- Repeated finds can therefore become much cheaper than the initial traversal, even though unions may later alter the structure.
- The challenge is to account for these changing paths across an entire sequence of operations.
- The analysis partitions ranks into thresholds and classifies a parent-pointer traversal as a big hop when it crosses a threshold.
- There are only O(k) threshold crossings on a path when the rank range is divided into k intervals.
- Path compression can cross each threshold only a bounded number of times per node, while within-interval compressions are charged to potential.
- A node's potential is based on the distance between its parent's rank and the top of its threshold interval.
- A rank-k node has at least 2^k descendants in its represented set, so there can be at most n/2^k nodes of rank k.
- The number of nodes in a rank interval therefore drops rapidly as its threshold rises.
- Choosing thresholds so each grows roughly as 2 raised to the previous threshold balances the number of nodes against their potential.
- The number of such thresholds up to n is O(log* n), the number of repeated base-2 logarithms needed to reduce n to at most one.
- More rapidly growing, recursively defined thresholds improve on the simpler tower-of-twos analysis.
- The resulting amortized cost per operation is governed by the inverse Ackermann function, which grows even more slowly than log* n.
- The lecture presents the inverse-Ackermann bound as the celebrated near-constant cost achieved by union by rank with path compression.
- The threshold details are more intricate than the O(log* n) proof, but the central technique remains charging path compression against rank-based potential.
Transcript excerpt
0:06 Okay. So, uh Okay. So, today we're going to do uh two data structures. Actually, we might only do the first one based on how it went last year. Um but they're both data structures that you've seen used in algorithms uh so far. And they're going to be like uh sort of like hardcore applications that amortize analysis. I don't think it's that important to know all the details, but it might be nice to see like one of these worked out and kind of going through the intricacies so you get some sense of of what these people in this area do. So, right, and this is sort of our our closing part of the of the class. We're kind of alternating between uh amortize analysis and randomized analysis. Both give you uh slightly different senses of the word like average, right? So amortize is kind of doing these running
0:53 times where you know you do a bunch of different operations and some might actually take longer than another. You know one one might do something like garbage collection or something and kind of pause everything. Um uh but in hindsight on average they're all going to look pretty good, right? It's as if it were fast the whole time. Uh and in in randomized algorithm the average is coming from the algorithm actually deliberately flipping coins and making random choices like in quicksort or something. Okay. And then we look at the expected running time over all kind of possible coin flips. Okay. Now, one thing that that neither of these uh notions of average give up is worst case analysis, right? So, we're always still saying no matter the input, you get such and such and such on average. Okay? So, a completely different field looks at
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Kent Quanrud.