13 a external algorithms disjoint sets java api support
play

13 A: External Algorithms; Disjoint Sets; Java API Support CS1102S: - PowerPoint PPT Presentation

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler 13 A: External Algorithms; Disjoint Sets; Java API Support CS1102S: Data Structures and Algorithms Martin Henz April 14, 2010


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  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 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

  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 Example of 5-ary Tree CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 38

  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 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

  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 Example of B-Tree of Order 5 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 40

  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 B-Tree Before Insertion of 57 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 41

  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 B-Tree After Insertion of 57 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 42

  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 Insertion of 55 Causes Split CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 43

  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 of 40 Causes Two Splits CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 44

  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 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

  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 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

  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 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

  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 Before Deletion of 99 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 48

  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 After Deletion of 99 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 49

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  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 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

  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 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

  51. 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

  52. 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

  53. 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

  54. 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

  55. 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

  56. 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

  57. 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

  58. 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

  59. 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

  60. 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

  61. 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

  62. 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

  63. 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

  64. 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

  65. 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

  66. 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

  67. 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

  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 Example After union ( 6 , 7 ) After union ( 4 , 6 ) CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 86

  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 Representation CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 87

  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 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

  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 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

  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 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

  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 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

  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 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

  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 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

  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 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

  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 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

  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 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

  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 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

  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 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

  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 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

  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 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