Elements of Power Set

Write a program to print the elements of power set of a given set.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace PowerSet

{

    class Program

    {

        static void Main(string[] args)

        {

            //Define the given set

            string[] set = { "1", "2", "3", "4", "5", "6" };

            //Condition to check that the set contains atleast one element

            if (set.Length > 0)

            {

                double subSetCount = 0;

                //Defining the counter array to keep track of set elements

                int[] counter = new int[set.Length];

                int i;

                //Initializing the counter array to -1

                for (i = 0; i < counter.Length; i++)

                {

                    counter[i] = -1;

                }

                do

                {

                    i = counter.Length – 1; //Setting i to last row

                label1:

                    counter[i]++;

                    //Check for array bound

                    if (counter[i] == i+1)

                    {

                        counter[i] = 0;

                        i–;

                        goto label1;

                    }

                    for (; i < counter.Length – 1 && i!= 0; i++ )

                    {

                        counter[i + 1] = counter[i – 1] + 1;

                    }

                    //Check for counter validity

                    int flag = 0;

                    for (i = 0; i < counter.Length – 1; i++)

                    {

                        if (counter[i] != -1 && counter[i] >= counter[i + 1])

                        {

                            flag = 1;

                            break;

                        }

                    }

                    //Printing the elements corresponding to valid counter array

                    if (flag == 0)

                    {

                        Console.Write("(");

                        for (i = 0; i < counter.Length; i++)

                        {

                            if (counter[i] != -1)

                            {

                                if (i != counter.Length – 1)

                                {

                                    Console.Write(set[counter[i]] + ",");

                                }

                                else

                                {

                                    Console.Write(set[counter[i]]);

                                }

                            }

                        }

                        subSetCount++;

                        Console.Write(")");

                        Console.WriteLine();

                    }

                }

                while (subSetCount != Math.Pow(2, set.Length) – 1);

            }

            Console.WriteLine("(NULL)");

        }

    }

}

 

Program Output:

================

(1)

(2)

(3)

(4)

(5)

(6)

(1,2)

(1,3)

(1,4)

(1,5)

(1,6)

(2,3)

(2,4)

(2,5)

(2,6)

(3,4)

(3,5)

(3,6)

(4,5)

(4,6)

(5,6)

(1,2,3)

(1,2,4)

(1,2,5)

(1,2,6)

(1,3,4)

(1,3,5)

(1,3,6)

(1,4,5)

(1,4,6)

(1,5,6)

(2,3,4)

(2,3,5)

(2,3,6)

(2,4,5)

(2,4,6)

(2,5,6)

(3,4,5)

(3,4,6)

(3,5,6)

(4,5,6)

(1,2,3,4)

(1,2,3,5)

(1,2,3,6)

(1,2,4,5)

(1,2,4,6)

(1,2,5,6)

(1,3,4,5)

(1,3,4,6)

(1,3,5,6)

(1,4,5,6)

(2,3,4,5)

(2,3,4,6)

(2,3,5,6)

(2,4,5,6)

(3,4,5,6)

(1,2,3,4,5)

(1,2,3,4,6)

(1,2,3,5,6)

(1,2,4,5,6)

(1,3,4,5,6)

(2,3,4,5,6)

(1,2,3,4,5,6)

(NULL)

Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s