Esempio n. 1
0
        /// <summary>
        /// Creates a new set with given member
        /// </summary>
        /// <param name="member"></param>
        public void MakeSet(T member)
        {
            var newSet = new DisJointSetNode <T>()
            {
                Data = member,
                Rank = 0
            };

            //Root's Parent is Root itself
            newSet.Parent = newSet;

            set.Add(member, newSet);
        }
Esempio n. 2
0
        /// <summary>
        /// Recursively move up in the set tree till root
        /// and return the Root.
        /// Does path Compression on all visited members on way to root
        /// by pointing their parent to Root.
        /// </summary>
        private DisJointSetNode <T> findSet(DisJointSetNode <T> node)
        {
            var parent = node.Parent;

            if (node != parent)
            {
                //compress path by setting parent to Root
                node.Parent = findSet(node.Parent);
                return(node.Parent);
            }

            //reached root so return the Root (reference Member)
            return(parent);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new set with given member.
        /// Time complexity: log(n).
        /// </summary>
        public void MakeSet(T member)
        {
            if (set.ContainsKey(member))
            {
                throw new Exception("A set with given member already exists.");
            }

            var newSet = new DisJointSetNode <T>()
            {
                Data = member,
                Rank = 0
            };

            //Root's Parent is Root itself
            newSet.Parent = newSet;
            set.Add(member, newSet);

            Count++;
        }