/* Actual powerset generation function * Returns a PowersetList object containing each possible subset * possible to generate from the given set */ static PowersetList PowerSet(SetList S) { /* How many values do we have in original set? * This will affect length of powerset * We create an array with this length to hold values from the set * which simplifies our algorithm later */ int len = size(S); int[] vals = new int[len]; int counter = 0; SetNode node = S.first; /* Iterate through values in set, * adding each to the array */ while (node != null) { vals[counter++] = node.data; node = node.next; } /* At this point, vals[] contains all our set values. * We set up our list container class, and our first node. * Even if there are no values in given set, we still want the empty set {} * so we set that as our first subset. */ PowersetList powerset = new PowersetList(); PowersetNode prev_node = new PowersetNode(); prev_node.data = new SetList(); powerset.first = prev_node; /* Calculate power-set length using 2^N where N = number of values. * Math.Pow isn't C-Core so we created a simple power function for this */ int n = pow(2, len); /* Our empty set {} is at location 0, so we start the loop at 1 * then loop through until we have n values. */ for (int x = 1; x < n; x++) { /* Set up this sub-set's containers */ PowersetNode New_Node = new PowersetNode(); New_Node.data = new SetList(); /* Now iterate through each value in our vals[] array */ for (int i = 0; i < len; i++) { /* If it should be in this subset, then we add it to the current subset. * This is done by using the bitwise operators & and << to check if * the binary conversion of x AND 1 << i is greater than 0. */ if ((x & (1 << i)) > 0) { add(ref New_Node.data, vals[i]); } } /* Finally, add our completed subset to the previous node's next */ prev_node.next = New_Node; prev_node = prev_node.next; } return powerset; }
/* * Task 2 - Powerset Functions * Implemented using a nested linked list data structure */ /* * Print: Prints the powerset in a user-readable format. * Overrides standard print function for powersets only. */ static void print(PowersetList S) { Console.Write("{ "); PowersetNode node = S.first; /* Loop through each node, printing it's value */ while (node != null) { print_subset(node.data); Console.Write(", "); node = node.next; } Console.WriteLine(" }"); }
static void Main() { //variable declaration SetList S = new SetList(); Console.Write("Set S: "); print(S); Console.WriteLine(); //checks whether the list is empty (should be "true") System.Console.WriteLine("is_empty(S): " + is_empty(S)); Console.WriteLine(); add(ref S, 1); add(ref S, 2); add(ref S, 3); add(ref S, 4); add(ref S, 5); add(ref S, 6); add(ref S, 7); add(ref S, 19); System.Console.Write("After adding " + size(S) + " values to set S: "); print(S); Console.WriteLine(); System.Console.WriteLine("Size of S: " + size(S)); Console.WriteLine(); System.Console.WriteLine("is_empty(S): " + is_empty(S)); Console.WriteLine(); SetList T = new SetList(); add(ref T, 1); add(ref T, 2); add(ref T, 4); add(ref T, 5); add(ref T, 8); System.Console.Write("Set T: "); print(T); Console.WriteLine(); System.Console.WriteLine("Size of T: " + size(T)); Console.WriteLine(); Console.WriteLine("Is 3 in set T? " + is_element_of(3, T)); Console.WriteLine(); Console.Write("Set S after removing some elements:"); remove(ref S, 1); remove(ref S, 4); remove(ref S, 6); print(S); Console.WriteLine(); Console.WriteLine("Is 7 in set S? " + is_element_of(7, S)); Console.WriteLine(); Console.WriteLine("X = copy_set(S). Set X:"); SetList X = copy_set(S); print(X); Console.WriteLine(); Console.WriteLine("Is S a subset of T? " + is_subset(S, T)); Console.WriteLine(); Console.WriteLine("union(S, T): "); print(union(S, T)); Console.WriteLine("intersection(S, T): "); print(intersection(S, T)); Console.WriteLine("difference(S, T): "); print(difference(S, T)); Console.WriteLine("difference(T, S): "); print(difference(T, S)); Console.WriteLine("symmetricDifference(S, T): "); print(symmetricDifference(S, T)); SetList Y = new SetList(); PowersetList test = new PowersetList(); Console.Write("Powerset({}): "); PowersetList PS = new PowersetList(); PS = PowerSet(Y); print(PS); add(ref Y, 1); add(ref Y, 2); add(ref Y, 3); add(ref Y, 4); add(ref Y, 5); add(ref Y, 6); Console.Write("Powerset({1,2,3,4,5,6}): "); print(PowerSet(Y)); Console.ReadLine(); }