Thread: Better algorithm for this ? (generating combinations)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Better algorithm for this ? (generating combinations)

    The problem is to generate all possible unique selections of n numbers from a given set of size > n.
    I've pretty much done it by brute force, as the number of final results should be same whichever way I choose. If a better way is not possible, I'd like suggestions about simple optimizations.
    Code:
    std::deque<std::set<int>> find_combinations(const std::set<int>& U,unsigned int n)
    {
        std::deque<std::set<int>> result;
        if(n<1 || U.size()<n)//Nothing Happens Here !
            {}
        else if(n==1) // The Universal input set is broken up and redistributed.
        {
            for(auto x:U)
                result.push_back(std::set<int>({x}));
        }
        else // Recursion FTW, no comments !!
        {
            std::set<int> temp(U);
            for(auto x:U)
            {
                temp.erase(x);
                auto partial_result = find_combinations(temp,n-1);
                for(auto& y:partial_result)
                    y.insert(x);
                result.insert
                (
                    std::begin(result),
                    std::begin(partial_result),
                    std::end(partial_result)
                );
            }
        }
        return result;
    }
    I noticed that having the return container a deque makes this much faster than if a vector or list is used. Why ?
    Also, is it worth the trouble trying to do it iteratively?
    Finally, can anyone help me determining the complexity of this function ?
    (I think it is something near (log base n U.size() )^ n) but I'm probably very very wrong about that.)

    Btw, here is a main you can use to test it.
    Code:
    int main(int argc,char** argv)
    {
        std::set<int> input;
        for(int i=0;i<atoi(argv[1]);i++)
            input.insert(i);
        
        auto foo = find_combinations(input,atoi(argv[2]));
        
    //     for(auto x:foo) //displaying results
    //     {
    //         for(auto y:x)
    //             std::cout<<y<<'\t';
    //         std::cout<<std::endl;
    //     }
    }
    Last edited by manasij7479; 05-10-2012 at 04:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating number combinations
    By litzkrieg in forum C Programming
    Replies: 23
    Last Post: 03-01-2011, 11:25 AM
  2. Trouble Generating Game Algorithm
    By justlivelife15 in forum Game Programming
    Replies: 3
    Last Post: 06-13-2007, 09:58 AM
  3. Combinations
    By Cmuppet in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 07:39 AM
  4. Combinations
    By GaPe in forum C Programming
    Replies: 16
    Last Post: 01-09-2002, 05:38 AM
  5. Replies: 10
    Last Post: 01-07-2002, 04:03 PM