How can I limit my powerset function?

Seth Kitchen

I found a powerset function from a StackOverflow Post:

    public static T[][] FastPowerSet<T>(T[] seq)
    {
        var powerSet = new T[1 << seq.Length][];
        powerSet[0] = new T[0]; // starting only with empty set
        for (int i = 0; i < seq.Length; i++)
        {
            var cur = seq[i];
            int count = 1 << i; // doubling list each time
            for (int j = 0; j < count; j++)
            {
                var source = powerSet[j];
                var destination = powerSet[count + j] = new T[source.Length + 1];
                for (int q = 0; q < source.Length; q++)
                    destination[q] = source[q];
                destination[source.Length] = cur;
            }
        }
        return powerSet;
    }

But I am not interested in the entire powerset. It uses too much memory, is limited to the maxvalue of integer, and takes too long. Imagine I have an unlimited length string that I'm taking the powerset of, I couldn't store it. I only want 10 values that are located 25% of the way through the powerset.

For instance, if I send an array of 22 values to the powerset function:

var toTakePowerSetOf = {3,-3,39,-39,392,-392,3920,-3920, 9, -9, 92, -92, 920, -920, 9203, -9203, 2, -2, 20, -20, 203, -203}

I normally get a powerset of 4194304 elements. But I only care about the values at index @ 1049089/4194304 which is {3, -9, 203} . How can I edit the fast powerset function to use unlimited precision, go to a specific index, and only store ~10 values.

The index is calculated as 2^n/4 where n is the number of elements in the toTakePowerSetOf. For my example there are 22 elements: 2^22=4194304 . 4194304/4 = 1048576 . The actual value I was seeking is at 1049089 which is 513 away. In my question I said I would like to check the surrounding 10 values, but I guess I should really say I'd like to check about 513 values (.0122 %) of numbers around it.

This algorithm I'm trying to use may lead to faster factoring, but I need to find out if its the case that factors always appear near 25% of the way through the powerset. It may not make much sense, but if it works I'll share it with you and we can solve P=NP lol

Niels V

The problem with the given FastPowerSet algorithm is that you need to calculate all the preceding power sets for a new one. Another Algo is presentend on the Wikipedia page for power sets.

Here is a short implementation:

    public static T[][] FastPowerSet2<T>(T[] seq)
    {
        var powerSet = new T[1 << seq.Length][];
        powerSet[0] = new T[0]; // starting only with empty set
        for (uint i = 1; i < powerSet.Length; i++)
        {
            powerSet[i] = PowerSetItem(i, seq, powerSet.Length);
        }
        return powerSet;
    }

    private static T[] PowerSetItemBig<T>(BigInteger neededSetIndex, T[] p)
    {
        byte[] neededSetBytes = neededSetIndex.ToByteArray();
        BitArray b = new BitArray(neededSetBytes);
        return p.Take(b.Length).Where((s, j) =>  b[j]).ToArray();
    }

    private static T[] PowerSetItem<T>(uint powersetIndex, T[] sequence, int powersetLength)
    {
        BitArray b = new BitArray(BitConverter.GetBytes(powersetIndex));
        if (b.Length < powersetLength)
        {
            var prefix = new BitArray(powersetLength - b.Length);
            b = Append(prefix, b);
        }

        return sequence.Where((s, j) => b[j]).ToArray();
    }
    public static BitArray Append(BitArray current, BitArray after)
    {
        var bools = new bool[current.Count + after.Count];
        current.CopyTo(bools, 0);
        after.CopyTo(bools, current.Count);
        return new BitArray(bools);
    }

With these functions, you also have the possibility to generate the specific powerset item with:

int[] P = { 3, -3, 39, -39, 392, -392, 3920, -3920, 9, -9, 92, -92, 920, -920, 9203, -9203, 2, -2, 20, -20, 203, -203 };
var p1049089 = PowerSetItem(1049089, P, (2^P.Length));
Console.WriteLine(string.Join(", ", p1049089));

The BigInteger version works with

var p1049089 = PowerSetItemBig(new BigInteger(1049089), P);

NB: I took some shortcuts with linq, and omitted some bound checks on incoming arrays, but this will work for this specific usecase

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I specify the function type in my type hints?

From Java

How can I improve my spiral index function?

From Dev

How can I make my function run in constant time?

From Dev

How can I inline my inner function?

From Dev

How can I pass all arguments to my callback function?

From Dev

How can I add the value of a Typescript enum to my javascript function?

From Dev

How can i limit the size of my availability calender by adjusting my existing CSS?

From Dev

How can i know my remaining Soundcloud API Limit?

From Dev

Can I limit my Bluemix utilization and charges?

From Dev

How can I get my bootstrap column to extend indefinitely (no height limit)?

From Dev

How can I ignore certain strings in my string centring function?

From Dev

How can I limit a table to fit inside 90% of my web page and no more?

From Dev

How can I rate-limit my Flask application per user?

From Dev

How can I limit the size of my ~/.cache folder

From Dev

How do I limit the size of my syslog?

From Dev

How can I give a limit to append in my Javascript Code?

From Dev

How do I limit only certain users to access specific function in my application?

From Dev

How can I limit the size of my ~/.cache folder

From Dev

How can I limit this number?

From Dev

How to I limit the net speed of my computer

From Dev

Time Limit Exceeded in Codeforces solution. How can I improve my solution?

From Dev

How can I inline my inner function?

From Dev

How can I limit my textbox text length takes 9 digits only using regex

From Dev

How can I limit the number of rows to show at a time for my gallery

From Dev

How can i limit my blogs per page on my website?

From Dev

How do I limit my user input

From Dev

How do I limit the size of my syslog?

From Dev

How can I limit my entire regular expression to 254 characters?

From Dev

Why does my powerset function run out of memory?

Related Related

  1. 1

    How can I specify the function type in my type hints?

  2. 2

    How can I improve my spiral index function?

  3. 3

    How can I make my function run in constant time?

  4. 4

    How can I inline my inner function?

  5. 5

    How can I pass all arguments to my callback function?

  6. 6

    How can I add the value of a Typescript enum to my javascript function?

  7. 7

    How can i limit the size of my availability calender by adjusting my existing CSS?

  8. 8

    How can i know my remaining Soundcloud API Limit?

  9. 9

    Can I limit my Bluemix utilization and charges?

  10. 10

    How can I get my bootstrap column to extend indefinitely (no height limit)?

  11. 11

    How can I ignore certain strings in my string centring function?

  12. 12

    How can I limit a table to fit inside 90% of my web page and no more?

  13. 13

    How can I rate-limit my Flask application per user?

  14. 14

    How can I limit the size of my ~/.cache folder

  15. 15

    How do I limit the size of my syslog?

  16. 16

    How can I give a limit to append in my Javascript Code?

  17. 17

    How do I limit only certain users to access specific function in my application?

  18. 18

    How can I limit the size of my ~/.cache folder

  19. 19

    How can I limit this number?

  20. 20

    How to I limit the net speed of my computer

  21. 21

    Time Limit Exceeded in Codeforces solution. How can I improve my solution?

  22. 22

    How can I inline my inner function?

  23. 23

    How can I limit my textbox text length takes 9 digits only using regex

  24. 24

    How can I limit the number of rows to show at a time for my gallery

  25. 25

    How can i limit my blogs per page on my website?

  26. 26

    How do I limit my user input

  27. 27

    How do I limit the size of my syslog?

  28. 28

    How can I limit my entire regular expression to 254 characters?

  29. 29

    Why does my powerset function run out of memory?

HotTag

Archive