External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Another Characteristics of Disk Storage We know already Access time is limited by mechanics How about access size ? defined by operating system/hardware design; typically large “chunks”, called blocks , of data can be read very fast, once the disk head has reached the correct location Reading and writing in blocks Reading and writing is typically done in large blocks to take advantage of this feature of disk storage CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 19
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Search Trees—Revisited Setup We would like to quickly find out if a given data item is included in a collection. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 20
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Search Trees—Revisited Setup We would like to quickly find out if a given data item is included in a collection. Example In an underground carpark, a system captures the licence plate numbers of incoming and outgoing cars. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 21
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Search Trees—Revisited Setup We would like to quickly find out if a given data item is included in a collection. Example In an underground carpark, a system captures the licence plate numbers of incoming and outgoing cars. Problem: Find out if a particular car is in the carpark. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 22
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Binary Search Setup Keep items in a tree. Each node holds one data item. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 23
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Binary Search Setup Keep items in a tree. Each node holds one data item. Idea The left subtree of a node V only contains items smaller than V and the right subtree only contains items larger than V . CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 24
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Binary Search Setup Keep items in a tree. Each node holds one data item. Idea The left subtree of a node V only contains items smaller than V and the right subtree only contains items larger than V . Search can then proceed top-down, starting at the root. If the search item is smaller than the item at the root, go down to the left, and if it is larger, go right. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 25
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Example Both trees are binary trees, but only the left tree is a search tree. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 26
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Insertion Idea Proceed like in search. If item is found, do nothing. If not, insert it in the last visited position. Example CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 27
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Deletion Idea Proceed like in search. If item is not found, do nothing. If item is found, take action depending on node. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 28
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Deletion Idea Proceed like in search. If item is not found, do nothing. If item is found, take action depending on node. Leaf If the node is leaf, delete it from parent. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 29
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Deletion Idea Proceed like in search. If item is not found, do nothing. If item is found, take action depending on node. Leaf If the node is leaf, delete it from parent. One child If the node has one child, move the child to parent. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 30
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Average-case Analysis Average Depth If all insertion sequences are equally likely, the average depth of any node is O ( log N ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 31
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Average-case Analysis Average Depth If all insertion sequences are equally likely, the average depth of any node is O ( log N ) Deletion introduces imbalance Deletion favours right subtree, and therefore trees become “left-heavy” on the long run. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 32
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler A Cure: AVL Trees Worst-case depth We want to restrict all operations to O ( log N ) in the worst case. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 33
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler A Cure: AVL Trees Worst-case depth We want to restrict all operations to O ( log N ) in the worst case. AVL Trees Make sure that the height of the subtrees of any node differ by at most one (Adelson-Velskii and Landis), using rebalancing if necessary CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 34
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler A Cure: AVL Trees Worst-case depth We want to restrict all operations to O ( log N ) in the worst case. AVL Trees Make sure that the height of the subtrees of any node differ by at most one (Adelson-Velskii and Landis), using rebalancing if necessary Bound The height of an AVL tree is at most 1 . 44 log ( N + 2 ) − 1 . 328, thus O ( log N ) . In practice, the height is only slightly more than log N . CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 35
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Search Trees with External Storage Main issue When data does not fit in main memory, the number of block accesses needs to be minimized CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 36
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Search Trees with External Storage Main issue When data does not fit in main memory, the number of block accesses needs to be minimized Overall idea Put more data into each node; use n − ary trees instead of binary trees CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 37
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Example of 5-ary Tree CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 38
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler B-tree Definition A B-tree of order M is an M -ary tree with the following properties: Data items are stored at leaves 1 Nonleaf nodes store up to M − 1 keys to guide search; key 2 i represents smallest key in subtree i + 1 Root is either a leaf or has between two and M children 3 Non-leaf non-root nodes have between ⌈ M / 2 ⌉ and M 4 children Leaves are at same depth, have between ⌈ L / 2 ⌉ and L 5 children CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 39
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Example of B-Tree of Order 5 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 40
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler B-Tree Before Insertion of 57 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 41
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler B-Tree After Insertion of 57 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 42
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Insertion of 55 Causes Split CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 43
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Insertion of 40 Causes Two Splits CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 44
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler What if a Split Reaches the Root? Splitting root is allowed Create a new root, and have the two halves as children CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 45
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler What if a Split Reaches the Root? Splitting root is allowed Create a new root, and have the two halves as children Exception in definition makes sense Root can have between 2 and M children CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 46
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler What if a Split Reaches the Root? Splitting root is allowed Create a new root, and have the two halves as children Exception in definition makes sense Root can have between 2 and M children Growing B-trees Splitting root as result of insertion is the only way that a B-tree can gain height CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 47
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler Before Deletion of 99 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 48
External Search Trees: B-Trees External Sorting Motivation Disjoint Sets Definition of B-Trees Java API Support for Data Structures Insertion and Deletion Another Puzzler After Deletion of 99 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 49
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler External Search Trees: B-Trees 1 External Sorting 2 Model for External Sorting The Simple Algorithm Multiway Merge Disjoint Sets 3 Java API Support for Data Structures 4 Another Puzzler 5 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 50
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Tapes as Storage Similar to disks Access time many orders of magnitude slower than main memory CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 51
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Tapes as Storage Similar to disks Access time many orders of magnitude slower than main memory Additional characteristics Large amounts of data can be read sequentially quite efficiently CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 52
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Tapes as Storage Similar to disks Access time many orders of magnitude slower than main memory Additional characteristics Large amounts of data can be read sequentially quite efficiently Access of previous locations CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 53
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Tapes as Storage Similar to disks Access time many orders of magnitude slower than main memory Additional characteristics Large amounts of data can be read sequentially quite efficiently Access of previous locations is extremely slow, as it requires re-winding the tape! CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 54
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler External Sorting Main idea Use tapes sequentially, and read one block from each input tape tape CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 55
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler External Sorting Main idea Use tapes sequentially, and read one block from each input tape tape Merge blocks Sort the blocks Use merge procedure from mergesort to merge CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 56
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler The Simple Algorithm: Overview Four tapes Two input tapes; two output tapes CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 57
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler The Simple Algorithm: Overview Four tapes Two input tapes; two output tapes Read and write runs Read runs from input tape, sort them and write alternatively to output tapes CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 58
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler The Simple Algorithm: Overview Four tapes Two input tapes; two output tapes Read and write runs Read runs from input tape, sort them and write alternatively to output tapes Continue, writing larger runs Read two runs from each “output” tape, and merge them on the fly, writing alternatively to “input” tapes CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 59
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler The Simple Algorithm: Overview Four tapes Two input tapes; two output tapes Read and write runs Read runs from input tape, sort them and write alternatively to output tapes Continue, writing larger runs Read two runs from each “output” tape, and merge them on the fly, writing alternatively to “input” tapes Continue until one tape has all sorted data CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 60
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Multiway Merge Why only four tapes? If we have more than four tapes, we can take advantage of them by using multiway merge CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 61
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Multiway Merge Why only four tapes? If we have more than four tapes, we can take advantage of them by using multiway merge How finding the smallest element during merge? CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 62
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Multiway Merge Why only four tapes? If we have more than four tapes, we can take advantage of them by using multiway merge How finding the smallest element during merge? Priority queue! CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 63
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Multiway Merge Why only four tapes? If we have more than four tapes, we can take advantage of them by using multiway merge How finding the smallest element during merge? Priority queue! Each iteration of inner loop deleteMin to find smallest element CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 64
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Multiway Merge Why only four tapes? If we have more than four tapes, we can take advantage of them by using multiway merge How finding the smallest element during merge? Priority queue! Each iteration of inner loop deleteMin to find smallest element insert new element from tape from which element was deleted CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 65
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Polyphase Merge and Replacement Selection Polyphase merge: main idea Make use of fewer tapes, by re-using tapes for reading and writing CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 66
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Polyphase Merge and Replacement Selection Polyphase merge: main idea Make use of fewer tapes, by re-using tapes for reading and writing Leading to tape organization using k th order Fibonacci numbers CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 67
External Search Trees: B-Trees External Sorting Model for External Sorting Disjoint Sets The Simple Algorithm Java API Support for Data Structures Multiway Merge Another Puzzler Polyphase Merge and Replacement Selection Polyphase merge: main idea Make use of fewer tapes, by re-using tapes for reading and writing Leading to tape organization using k th order Fibonacci numbers Replacement selection: main idea Make use of input tape as output tape, reusing the tapes “on the fly” CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 68
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler External Search Trees: B-Trees 1 External Sorting 2 Disjoint Sets 3 Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants Java API Support for Data Structures 4 Another Puzzler 5 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 69
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Equivalence Relations Definition An equivalence relation is a relation R that satisfies three properties: (Reflexive) aRa , for all a ∈ S . 1 (Symmetric) aRb if and only if bRa . 2 (Transitive) aRb and bRc implies aRc . 3 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 70
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Equivalence Relations Definition An equivalence relation is a relation R that satisfies three properties: (Reflexive) aRa , for all a ∈ S . 1 (Symmetric) aRb if and only if bRa . 2 (Transitive) aRb and bRc implies aRc . 3 Examples Electrical connectivity (metal wires between points) Cities belonging to same country CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 71
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler The Dynamic Equivalence Problem Initial setup Collection of N disjoint sets, each with one element CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 72
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler The Dynamic Equivalence Problem Initial setup Collection of N disjoint sets, each with one element Operations find ( a ) : return the set of which x is element union ( a , b ) : merge the sets to which a and b belong, so that find ( a ) = find ( b ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 73
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Strategies Fast Find, Slow Union Use array repres to store equivalence class for each element CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 74
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Strategies Fast Find, Slow Union Use array repres to store equivalence class for each element find ( a ) : return repres [ a ] union ( a , b ) : if repres [ x ] = repres [ b ] then set repres [ x ] to repres [ a ] CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 75
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Strategies Fast Find, Slow Union Use array repres to store equivalence class for each element find ( a ) : return repres [ a ] union ( a , b ) : if repres [ x ] = repres [ b ] then set repres [ x ] to repres [ a ] Fast Union, Reasonable Find Union/find data structure CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 76
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Basic Data Structure Idea Maintain forest corresponding to equivalence relation CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 77
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Basic Data Structure Idea Maintain forest corresponding to equivalence relation Union Merge trees CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 78
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Basic Data Structure Idea Maintain forest corresponding to equivalence relation Union Merge trees Find Return root of tree CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 79
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Basic Data Structure Idea Maintain forest corresponding to equivalence relation Union Merge trees Find Return root of tree Observe Only upward direction needed! CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 80
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Example Initial setup: CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 81
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Example Initial setup: After union ( 4 , 5 ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 82
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Example After union ( 4 , 5 ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 83
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Example After union ( 4 , 5 ) After union ( 6 , 7 ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 84
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Example After union ( 6 , 7 ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 85
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Example After union ( 6 , 7 ) After union ( 4 , 6 ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 86
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Representation CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 87
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Representation Idea Remember parent node only; mark root with − 1 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 88
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Representation Idea Remember parent node only; mark root with − 1 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 89
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Variants Problem How to choose root for union? Bad choice can lead to long paths CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 90
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Variants Problem How to choose root for union? Bad choice can lead to long paths Union-by-size Always make the smaller tree a subtree of the larger tree CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 91
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Variants Problem How to choose root for union? Bad choice can lead to long paths Union-by-size Always make the smaller tree a subtree of the larger tree Analysis When depth increases, the tree is smaller than the other side. Thus, after union, it is at least twice as large. CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 92
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Variants Problem How to choose root for union? Bad choice can lead to long paths Union-by-size Always make the smaller tree a subtree of the larger tree Analysis When depth increases, the tree is smaller than the other side. Thus, after union, it is at least twice as large. Height less than or equal to log N CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 93
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Variants Union-by-height Always make the shorter tree a subtree of the higher tree CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 94
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Variants Union-by-height Always make the shorter tree a subtree of the higher tree Height As with union-by-size: O ( log N ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 95
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Path Compression During find make every node point to root CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 96
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Path Compression During find make every node point to root CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 97
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Path Compression During find make every node point to root after find ( 14 ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 98
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler A Very Slowly Growing Function Definition log ∗ N is the number of times log needs to be applied to N until N ≤ 1. Examples log ∗ 2 = 1 log ∗ 4 = 2 log ∗ 16 = 3 log ∗ 65536 = 4 ... CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 99
External Search Trees: B-Trees Equivalence Relations External Sorting The Dynamic Equivalence Problem Disjoint Sets Basic Data Structure Java API Support for Data Structures Variants Another Puzzler Runtime Consider variant Union-by-height combined with path compression CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 100
Recommend
More recommend