Exemple #1
0
        public new void Union(UnionFindItem <int> x, UnionFindItem <int> y)
        {
            var xRoot = Find(x);
            var yRoot = Find(y);

            if (xRoot == yRoot)
            {
                return;
            }

            if (xRoot.Rank < yRoot.Rank)
            {
                xRoot.Parent = yRoot;
            }
            else if (xRoot.Rank > yRoot.Rank)
            {
                yRoot.Parent = xRoot;
            }
            else
            {
                xRoot.Parent = yRoot;
                yRoot.Rank++;
            }
            this.Count--;
        }
Exemple #2
0
 public UnionFindItem <T> Find(UnionFindItem <T> x)
 {
     if (x.Parent != x)
     {
         return(Find(x.Parent));
     }
     return(x.Parent);
 }
Exemple #3
0
 public void Init(int[,] nums)
 {
     Count    = nums.GetLength(0);
     ListItem = new Dictionary <int, UnionFindItem <int> >();
     for (int i = 0; i <= nums.GetUpperBound(0); i++)
     {
         var item = new UnionFindItem <int>(i);
         this.MakeSet(item);
         ListItem.Add(i, item);
     }
 }
Exemple #4
0
        public void Union(UnionFindItem <T> x, UnionFindItem <T> y)
        {
            var xRoot = Find(x);
            var yRoot = Find(y);

            if (xRoot == yRoot)
            {
                return;
            }

            if (xRoot.Rank < yRoot.Rank)
            {
                xRoot.Parent = yRoot;
            }
            else if (xRoot.Rank > yRoot.Rank)
            {
                yRoot.Parent = xRoot;
            }
            else
            {
                xRoot.Parent = yRoot;
                yRoot.Rank++;
            }
        }
Exemple #5
0
 public void MakeSet(UnionFindItem <T> item)
 {
     item.Rank   = 0;
     item.Parent = item;
 }