Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7617467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:09:27+00:00 2026-05-31T03:09:27+00:00

i am using the following C# function to get a powerset limited to subsets

  • 0

i am using the following C# function to get a powerset limited to subsets of a minimal length

string[] PowerSet(int min_len, string set)
{
    IEnumerable<IEnumerable<string>> seed = 
                    new List<IEnumerable<string>>() { Enumerable.Empty<string>() };

    return set.Replace(" ", "")
              .Split(',')
              .Aggregate(seed, (a, b) => a.Concat(a.Select(x => x.Concat(new[] { b }))))
              .Where(subset => subset.Count() >= min_len)
              .Select(subset => string.Join(",", subset))
              .ToArray();
}

the problem is that when the original set is large, the algorithm has to work very hard even if the minimal length is large as well.

e.g:

    PowerSet(27, "1,11,12,17,22,127,128,135,240,254,277,284,292,296,399,309,322,326,333,439,440,442,447,567,580,590,692,697");

should be very easy, but is too lengthily for the above function. i am looking for a concise modification of my function which could efficiently handle these cases.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:09:28+00:00Added an answer on May 31, 2026 at 3:09 am

    Taking a quick look at your method, one of the inefficiencies is that every possible subset is created, regardless of whether it has enough members to warrant inclusion in the limited super set.

    Consider implementing the following extension method instead. This method can trim out some unnecessary subsets based on their count to avoid excess computation.

    public static List<List<T>> PowerSet<T>(List<T> startingSet, int minSubsetSize)
    {
        List<List<T>> subsetList = new List<List<T>>();
    
        //The set bits of each intermediate value represent unique 
        //combinations from the startingSet.
        //We can start checking for combinations at (1<<minSubsetSize)-1 since
        //values less than that will not yield large enough subsets.
        int iLimit = 1 << startingSet.Count;
        for (int i = (1 << minSubsetSize)-1; i < iLimit; i++)
        {
            //Get the number of 1's in this 'i'
            int setBitCount = NumberOfSetBits(i);
    
            //Only include this subset if it will have at least minSubsetSize members.
            if (setBitCount >= minSubsetSize)
            {
                List<T> subset = new List<T>(setBitCount);
    
                for (int j = 0; j < startingSet.Count; j++)
                {
                    //If the j'th bit in i is set, 
                    //then add the j'th element of the startingSet to this subset.
                    if ((i & (1 << j)) != 0)
                    {
                        subset.Add(startingSet[j]);
                    }
                }
                subsetList.Add(subset);
            }
        }
        return subsetList;
    }
    

    The number of set bits in each incremental i tells you how many members will be in the subset. If there are not enough set bits, then there is no point in doing the work of creating the subset represented by the bit combination. NumberOfSetBits can be implemented a number of ways. See How to count the number of set bits in a 32-bit integer? for various approaches, explanations and references. Here is one example taken from that SO question.

    public static int NumberOfSetBits(int i)
    {
        i = i - ((i >> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
        return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
    }
    

    Now, while this solution works for your example, I think you will run into long runtimes and memory issues if you lower the minimum subset size too far or continue to grow the size of the startingSet. Without specific requirements posted in your question, I can’t judge if this solution will work for you and/or is safe for your range of expected input cases.

    If you find that this solution is still too slow, the operations can be split up for parallel computation, perhaps using PLINQ features.

    Lastly, if you would like to dress up the extension method with LINQ, it would look like the following. However, as written, I think you will see slower performance without some changes to it.

    public static IEnumerable<List<T>> PowerSet<T>(List<T> startingSet, int minSubsetSize)
    {
        var startingSetIndexes = Enumerable.Range(0, startingSet.Count).ToList();
    
        var candidates = Enumerable.Range((1 << minSubsetSize)-1, 1 << startingSet.Count)
                                   .Where(p => NumberOfSetBits(p) >= minSubsetSize)
                                   .ToList();
    
        foreach (int p in candidates)
        {
            yield return startingSetIndexes.Where(setInd => (p & (1 << setInd)) != 0)
                                           .Select(setInd => startingSet[setInd])
                                           .ToList();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to get this following url using the downloadURL function as follows: http://www.ncbi.nlm.nih.gov/nuccore/27884304
I'm using the following function that changes a calendar selection at a set time
I'm using the following function to get a URL parameter. function gup(name, url) {
I can get the r,g,b values using the following functions. int rgb=bImg.getRGB(i, j); int
I'm using the following function to get a JSON Array from a php webpage.
I am using the following cut/paste function to get the URL parameters. function getUrlVars()
I am using the following function ..instead of CFolderDialog.. to get the folder path...check
Currently I am using following function to get the temporary folder path for current
Hello I am using the following GeoLocation function to get the location of a
Consider the following function using jQuery: function getVal() { jQuery.get('/relative/url/', function (data) { return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.