コード例 #1
0
        /// <summary>
        /// This method returns all possible subset of the current set
        /// </summary>
        public SetClass <SetClass <T> > Powerset()
        {
            //Any implementation that can enumerate all possible subsets, here is an example
            //there is another one that uses Math.Pow method

            int n = 1 << Data.Count;             //this is one way to get 2 to the power of n using left shift operation
            SetClass <SetClass <T> > result =
                new SetClass <SetClass <T> >(new Vector <SetClass <T> >());

            //loops on the number of possible subsets, and try to construct the corresponding subset
            for (int i = 0; i < n; i++)
            {
                SetClass <T> subset = new SetClass <T>(new Vector <T>());

                //subsetIndex is the index of the subset in the list of possible subsets - 0,1,2,..
                // elementIndex is the index of the current set elements to add to the subset
                for (int subsetIndex = i, elementIndex = 0; subsetIndex != 0; subsetIndex /= 2, elementIndex++)
                {
                    //if the corresponding bit is equal to 1,
                    //it means that this set element should be added to the curret subset,
                    //based on its index
                    if ((subsetIndex & 1) != 0)
                    {
                        subset.Data.Add(Data[elementIndex]);
                    }
                }
                result.Data.Add(subset);
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// This method returns all elements in the current set as well as the input set B without duplicates
        /// </summary>
        /// <returns> a SetClass object that contains all elements in both A and B</returns></returns>
        /// <param name="B">the other set to find union with</param>
        public SetClass <T> UnionWith(SetClass <T> B)
        {
            //Any implementation that get all elements in A and B - no duplicates

            SetClass <T> result = new SetClass <T>(new Vector <T>());

            //Add all elements in current set
            var count = Data.Count;

            for (int i = 0; i < count; i++)
            {
                result.Data.Add(Data[i]);
            }

            //Add new elements from set B
            count = B.Data.Count;
            for (int i = 0; i < count; i++)
            {
                if (result.Data.Contains(B.Data[i]) == false)
                {
                    result.Data.Add(B.Data[i]);
                }
            }

            return(result);
        }
        public SetClass <SetClass <T> > Powerset()
        {
            //to get the number of elements to later apply to the (2^n) formula
            int x = Data.Count;

            //bit shifting
            int setCount = 1 << x;

            //creating a object of setclass
            SetClass <SetClass <T> > resultSetClass = new SetClass <SetClass <T> >();

            //a loop to run x times
            for (int j = 0; j < setCount; j++)
            {
                //creating an object of T
                SetClass <T> tempSet = new SetClass <T>();
                for (int i = 0; i < x; i++)
                {
                    //checking whether the elements are not null set
                    if ((j & (1 << i)) > 0)
                    {
                        //the values in the tempset is then added to the vector result
                        tempSet.result.Add(Data[i]);
                        //Console.Write(Data[i]);
                    }
                    //Console.WriteLine();
                }
                resultSetClass.powersetResult.Add(tempSet);
            }
            // addresults(resultSetClass);
            return(resultSetClass);

            ;
        }
 //a method to add values to the "result"
 public void addresults(SetClass <SetClass <T> > resultSetClass)
 {
     for (int y = 0; y < resultSetClass.result.Count; y++)
     {
         Console.Write(resultSetClass.result[y].result.ToString());
         Console.WriteLine();
     }
 }
        public SetClass <SetClass <T> > Powerset()
        {
            SetClass <SetClass <T> > power = new SetClass <SetClass <T> >(new Vector <SetClass <T> >());

            if (this.Data.Count == 0)
            {
                power.Add(this);
            }
            else
            {
                // Assume that s is a subset that contains all elements of data exept Data[0]
                // Step 1: Your are to create such a set s
                // Hint: just simply add all elements from Data[1] to Data[Count-1] to s
                SetClass <T> s = new SetClass <T>(new Vector <T>());
                for (int j = 1; j < this.Data.Count; j++)
                {
                    s.Data.Add(this.Data[j]);
                }

                // Step 2: Let powerS be the power set of s.
                // powerS can be obtained recursively, e.g.
                SetClass <SetClass <T> > powerS = s.Powerset();

                // Step 3: You need to add all elements of powerS to power.
                // This is because s is a subset created from Data[1] to Data[Count-1] and thus
                // powerS is a subset of power. In particular, you should
                // add powerS.Data[0] to power
                // add powerS.Data[1] to power
                // ...
                // add powerS.Data[powerS.Data.Count-1] to power
                for (int j = 0; j < powerS.Data.Count; j++)
                {
                    power.Add(powerS.Data[j]);
                }

                // Step 4:
                // For each set p of powerS, you create another set, called augP (i.e. augmented p) such that
                //      (i) augP is a copy of p. Note that you should not simply assign as augP = p.
                //      (ii) augP is then augmented with Data[0], i.e. augP.Add(this.Data[0]);
                //      then add augP to power
                // End for
                for (int j = 0; j < powerS.Data.Count; j++)
                {
                    SetClass <T> p    = powerS.Data[j];
                    SetClass <T> augP = new SetClass <T>(new Vector <T>());
                    for (int k = 0; k < p.Data.Count; k++)
                    {
                        augP.Add(p.Data[k]);
                    }
                    augP.Add(this.Data[0]);
                    power.Add(augP);
                }
            }
            return(power);
        }
        public SetClass <T> Complement(SetClass <T> U)
        {
            SetClass <T> tempComplementSet = new SetClass <T>();
            Vector <T>   difference        = Difference(U).result;

            for (int i = 0; i < U.Data.Count; i++)
            {
                tempComplementSet.result.Add(difference[i]);
            }
            return(tempComplementSet);
        }
 public bool IsSupersetOf(SetClass <T> B)
 {
     for (int i = 0; i < Data.Count; i++)
     {
         if (Data.Contains(B.Data[i]) == false)
         {
             return(false);
         }
     }
     return(true);
 }
        public SetClass <T> IntersectionWith(SetClass <T> B)
        {
            for (int i = 0; i < B.Data.Count; i++)
            {
                if (Data.Contains(B.Data[i]) == true)
                {
                    result.Add(B.Data[i]);
                }
            }

            return(null);
        }
        public SetClass <T> Difference(SetClass <T> B)
        {
            SetClass <T> diff = new SetClass <T>(new Vector <T>());

            for (int i = 0; i < this.Data.Count; i++)
            {
                if (!B.Membership(this.Data[i]))
                {
                    diff.Add(this.Data[i]);
                }
            }
            return(diff);
        }
        public SetClass <T> IntersectionWith(SetClass <T> B)
        {
            SetClass <T> inter = new SetClass <T>(new Vector <T>());

            for (int i = 0; i < this.Data.Count; i++)
            {
                if (B.Membership(this.Data[i]))
                {
                    inter.Add(this.Data[i]);
                }
            }
            return(inter);
        }
        public SetClass <Tuple <T, T2> > CartesianProduct <T2>(SetClass <T2> B)
        {
            SetClass <Tuple <T, T2> > product = new SetClass <Tuple <T, T2> >(new Vector <Tuple <T, T2> >());

            for (int i = 0; i < this.Data.Count; i++)
            {
                for (int j = 0; j < B.Data.Count; j++)
                {
                    product.Add(new Tuple <T, T2>(this.Data[i], B.Data[j]));
                }
            }

            return(product);
        }
        public SetClass <T> UnionWith(SetClass <T> B)
        {
            SetClass <T> union = new SetClass <T>(new Vector <T>());

            for (int i = 0; i < this.Data.Count; i++)
            {
                union.Add(this.Data[i]);
            }
            for (int i = 0; i < B.Data.Count; i++)
            {
                union.Add(B.Data[i]);
            }
            return(union);
        }
コード例 #13
0
        /// <summary>
        /// This method checks if the current set contains all elements in an input set B
        /// </summary>
        /// <returns><c>true</c>,if this set is a superset of input set B, <c>false</c> otherwise.</returns>
        /// <param name="B">B.</param>
        public bool IsSupersetOf(SetClass <T> B)
        {
            //any implementation that checks if every single element in B exists in A
            var count = B.Data.Count;

            for (int i = 0; i < count; i++)
            {
                if (Data.Contains(B.Data[i]) == false)
                {
                    return(false);
                }
            }
            return(true);
        }
        public SetClass <T> Difference(SetClass <T> B)
        {
            SetClass <T> tempDifferenceSet = new SetClass <T>();

            //same logic as union set. Checks whether the set A contains the same element in B and if not it is added to the result
            for (int i = 0; i < B.Data.Count; i++)
            {
                if (!Data.Contains(B.Data[i]))
                {
                    tempDifferenceSet.result.Add(B.Data[i]);
                }
            }
            return(tempDifferenceSet);
        }
コード例 #15
0
        /// <summary>
        /// This method returns the elements in current set but not in set B
        /// </summary>
        /// <returns> a SetClass object that contains all elements in current set, but not in B</returns></returns>
        /// <param name="B">the other set to find difference with</param>
        public SetClass <T> Difference(SetClass <T> B)
        {
            SetClass <T> result = new SetClass <T>(new Vector <T>());

            var count = Data.Count;

            for (int i = 0; i < count; i++)
            {
                if (B.Data.Contains(B.Data[i]) == false)
                {
                    result.Data.Add(Data[i]);
                }
            }

            return(result);
        }
コード例 #16
0
        /// <summary>
        /// This method finds the intersection between current set and input set B
        /// </summary>
        /// <returns>a SetClass object that contains all common elements between this set and set B</returns>
        /// <param name="B">the other set to find intersection with</param>
        public SetClass <T> IntersectionWith(SetClass <T> B)
        {
            //Any implementation that gets common elements between this set and set B

            SetClass <T> result = new SetClass <T>(new Vector <T>());
            var          count  = Data.Count;

            for (int i = 0; i < count; i++)
            {
                if (B.Data.Contains(Data[i]) == true)
                {
                    result.Data.Add(Data[i]);
                }
            }
            return(result);
        }
コード例 #17
0
        /// <summary>
        /// This method returns the cartesian product of current set (set A) and input set (set B)
        /// </summary>
        /// <returns>A SetClass object that contains pairs (a, b) of elements from A, and B respectively</returns>
        /// <param name="B">B.</param>
        /// <typeparam name="T2">the other set to find the cartesian product</typeparam>
        public SetClass <Tuple <T, T2> > CartesianProduct <T2>(SetClass <T2> B)
        {
            SetClass <Tuple <T, T2> > result =
                new SetClass <Tuple <T, T2> >(new Vector <Tuple <T, T2> >());

            var aCount = Data.Count;
            var bCount = B.Data.Count;

            for (int i = 0; i < aCount; i++)
            {
                for (int j = 0; j < bCount; j++)
                {
                    result.Data.Add(new Tuple <T, T2>(Data[i], B.Data[j]));
                }
            }
            return(result);
        }
        public SetClass <T> SymmetricDifference(SetClass <T> B)
        {
            //symmetric difference is the union of the two sets excluding the intersection set
            //a set class for symmmetric difference
            SetClass <T> tempSymDif = new SetClass <T>();
            //an object of vector called union which will get the result of the union
            Vector <T> union = UnionWith(B).result;
            //an object of vector called intersection to get the result of intersection
            Vector <T> intersection = IntersectionWith(B).result;

            for (int i = 0; i < Data.Count; i++)
            {
                if (!(union[i].Equals(intersection[i])))
                {
                    tempSymDif.result.Add(union[i]);
                }
            }
            return(tempSymDif);
        }
        public SetClass <Tuple <T, T2> > CartesianProduct <T2>(SetClass <T2> B)
        {
            //creating a vector object of type tuple
            Vector <Tuple <T, T2> > tupleVector = new Vector <Tuple <T, T2> >();

            //the first loop is to run the elements of the first set
            for (int i = 0; i < Data.Count; i++)
            {
                //a nested loop is implemented so that the same element of first set will be multiplied with all the elements in the set B
                for (int j = 0; j < B.Data.Count; j++)
                {
                    //new tuple is made to get the products of elements from both the sets and then added to the tuple vector
                    Tuple <T, T2> tempTupleSet = new Tuple <T, T2>(Data[i], B.Data[j]);
                    tupleVector.Add(tempTupleSet);
                }
            }
            //then the tuple vector is added to a set class
            SetClass <Tuple <T, T2> > tupleSetClass = new SetClass <Tuple <T, T2> >(tupleVector);

            return(tupleSetClass);
        }
        public SetClass <T> UnionWith(SetClass <T> B)
        {
            //first an object of a set class called union is created to add the values
            SetClass <T> tempUnionSet = new SetClass <T>();

            for (int i = 0; i < Data.Count; i++)
            {
                //check whether the results in the temp union set contains the values already, if not then the elements of set A are added into the tempunion set result
                if (!(tempUnionSet.result.Contains(Data[i])))
                {
                    tempUnionSet.result.Add(Data[i]);
                }
            }
            //same as above but for set B
            for (int i = 0; i < B.Data.Count; i++)
            {
                if (!(tempUnionSet.result.Contains(B.Data[i])))
                {
                    tempUnionSet.result.Add(B.Data[i]);
                }
            }
            return(tempUnionSet);
        }
 public bool IsSupersetOf(SetClass <T> B)
 {
     return(B.IsSubsetOf(this));
 }
コード例 #22
0
 /// <summary>
 /// This method find elements in current set (set A), but not in input set B AND
 /// elements in input set B, but not in current set.
 /// </summary>
 /// <returns>a SetClass object that contains elements in A but not in B, and elements in B but not in A</returns>
 /// <param name="B">B.</param>
 public SetClass <T> SymmetricDifference(SetClass <T> B)
 {
     //Any implementation that gets (A - B) union (B - A)
     return(this.Difference(B).UnionWith(B.Difference(this)));
 }
コード例 #23
0
 /// <summary>
 /// This method finds elements in U (universal set - superset that has all elements in A and more) but not in A
 /// </summary>
 /// <returns>a SetClass object that contains elements in U but not in A</returns>
 /// <param name="U">The universal set</param>
 public SetClass <T> Complement(SetClass <T> U)
 {
     return(U.Difference(this));
 }
 public bool IsEqual(SetClass <T> B)
 {
     return(this.IsSubsetOf(B) && B.IsSubsetOf(this));
 }