|
| 1 | +using System.Collections.Generic; |
| 2 | + |
| 3 | +namespace CodeJam.Collections |
| 4 | +{ |
| 5 | + /// <summary>Disjoint sets implementation base</summary> |
| 6 | + /// <remarks> |
| 7 | + /// See http://en.wikipedia.org/wiki/Disjoint-set_data_structure |
| 8 | + /// </remarks> |
| 9 | + public class DisjointSetsBase<T> where T : BasicNode |
| 10 | + { |
| 11 | + /// <summary>All nodes</summary> |
| 12 | + protected readonly List<T> Nodes_ = new List<T>(); |
| 13 | + |
| 14 | + /// <summary>Creates an empty base</summary> |
| 15 | + protected DisjointSetsBase() { } |
| 16 | + |
| 17 | + /// <summary>The number of nodes</summary> |
| 18 | + public int Count => Nodes_.Count; |
| 19 | + |
| 20 | + /// <summary>The number of disjoint sets</summary> |
| 21 | + public int SetsCount { get; protected set; } |
| 22 | + |
| 23 | + /// <summary>Finds a set identifier for the element</summary> |
| 24 | + /// <param name="index">The element index</param> |
| 25 | + /// <returns>The identifier of the containing set</returns> |
| 26 | + /// <remarks> |
| 27 | + /// The set identifier is the index of a single element representing the set. |
| 28 | + /// The Union operation may lead to a choice of a different representative for a set. |
| 29 | + /// In this case IndexToSetId(oldSetId) may be called to get the new set id. |
| 30 | + /// </remarks> |
| 31 | + public int IndexToSetId(int index) |
| 32 | + { |
| 33 | + // First, find a root element of a tree containing the passed element |
| 34 | + var rootIndex = index; |
| 35 | + for (;;) |
| 36 | + { |
| 37 | + var parentIndex = Nodes_[rootIndex].ParentIndex; |
| 38 | + if (parentIndex == -1) |
| 39 | + { |
| 40 | + break; |
| 41 | + } |
| 42 | + rootIndex = parentIndex; |
| 43 | + } |
| 44 | + |
| 45 | + // Then, do the path compression: |
| 46 | + // walk from the passed element upto the root replacing the the ParentIndex with the root index |
| 47 | + while (index != rootIndex) |
| 48 | + { |
| 49 | + var node = Nodes_[index]; |
| 50 | + index = node.ParentIndex; |
| 51 | + node.ParentIndex = rootIndex; |
| 52 | + } |
| 53 | + return rootIndex; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary>Combines to distjoint sets into a single set</summary> |
| 57 | + /// <param name="elementOfSet1Index">Index of an element of the first set</param> |
| 58 | + /// <param name="elementOfSet2Index">Index of an element of the second set</param> |
| 59 | + public void Union(int elementOfSet1Index, int elementOfSet2Index) |
| 60 | + { |
| 61 | + elementOfSet1Index = IndexToSetId(elementOfSet1Index); |
| 62 | + elementOfSet2Index = IndexToSetId(elementOfSet2Index); |
| 63 | + if (elementOfSet1Index == elementOfSet2Index) |
| 64 | + { |
| 65 | + return; // Already the single set |
| 66 | + } |
| 67 | + |
| 68 | + var set1Root = Nodes_[elementOfSet1Index]; |
| 69 | + var set2Root = Nodes_[elementOfSet2Index]; |
| 70 | + var rankDifference = set1Root.Rank - set2Root.Rank; |
| 71 | + // Attach the tree with a smaller rank to the tree with a higher rank. |
| 72 | + // The resulting tree rank is equal to the higher rank |
| 73 | + // except the case when initial ranks are equal. |
| 74 | + // In the latter case the new rank will be increased by 1 |
| 75 | + if (rankDifference > 0) // 1st has higher rank |
| 76 | + { |
| 77 | + set2Root.ParentIndex = elementOfSet1Index; |
| 78 | + } |
| 79 | + else if (rankDifference < 0) // 2nd has the higher rank |
| 80 | + { |
| 81 | + set1Root.ParentIndex = elementOfSet2Index; |
| 82 | + } |
| 83 | + else // ranks are equal and the new root choice is arbitrary |
| 84 | + { |
| 85 | + set2Root.ParentIndex = elementOfSet1Index; |
| 86 | + ++set1Root.Rank; |
| 87 | + } |
| 88 | + |
| 89 | + // we have joined 2 sets, so we have to decrease the count |
| 90 | + --SetsCount; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary>Node base class</summary> |
| 95 | + public class BasicNode |
| 96 | + { |
| 97 | + /// <summary>Parent node index</summary> |
| 98 | + /// <remarks>Points to the root after a path compression</remarks> |
| 99 | + public int ParentIndex; |
| 100 | + |
| 101 | + /// <summary>Estimated height of the tree (i.e. maximum length of the path from the root to a node. Path compression is not taken into account)</summary> |
| 102 | + public int Rank; |
| 103 | + } |
| 104 | +} |
0 commit comments