Permutation using recursion

This is a discussion on Permutation using recursion within the C++ Programming forums, part of the General Programming Boards category; Hello all. I'm supposed to store a set of permutations (type list<int>) inside a set of list<int>'s. I can't figure ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    5

    Permutation using recursion

    Hello all. I'm supposed to store a set of permutations (type list<int>) inside a set of list<int>'s. I can't figure out how to use recursion inside my permutate method. Can anyone help? I'm completely lost and this is due Monday.

    Code:
    
    
    
    
    #include <iostream>
    #include <algorithm>
    #include <list>
    #include <string>
    #include <cctype>
    #include<set>
    
    
    using namespace std;
    
    
    class perm{
     
     public: 
     
     void permutate();
     void setList(int size);
     void display();
    
    
     
     
     private: 
     int max;
     list<int> numbers;
     set<list<int> > permutations;    
          
          
          
    };
    
    
    
    
    void perm::setList(int size)
    {
       for (int i = 1; i <= size; i++)
       {
           numbers.push_back(i);
       }
      
    }
       
    void perm :: permutate(list<int> numbers)
    {
         
         
         if (numbers.size() == 1)
         {
            permutations.insert(numbers);
    
    
         }
         else{
              
         list<int>::iterator it;
         for (it = numbers.begin(); it != numbers.end(); it++)
         {
             int c;
             c = numbers.front();
             numbers.pop_front();
             numbers.front() = c;
             
             set<list<int> > subPerms;
             subPerms = permutate(
             
             
             /*
             for (it2 = subPerms.begin(); it2 != subPerms.end(); it2++)
             {
                 subPerms.insert(it2,numbers);
                 numbers.pop_front();;
            
             }
             */
               
            }
            }
         
         
    }
                   
    
    
    void perm :: display()
    {
         set<list<int> >::iterator it;
         for (it = permutations.begin(); it != permutations.end(); it++)
         {
             list<int>::iterator j;
             cout << "( ";
             list<int> temp = *it;
             for (j = temp.begin(); j != temp.end(); j++) {
                 cout << *j << " ";
             }
             cout << ")";
             cout << endl;
         }
    }
    
    
    
    
    int main()
    {
        
        perm test;
        test.setList(2);
        test.permutate();
        test.display();
        
        
        
        
        
        
        system("pause");
        return 0;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    I think this method lends itself well to recursion:
    Permutation - Wikipedia, the free encyclopedia

    Step one can be used as the base case for the recursion. Until the algorithm is done, you can just have your function call itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Permutation
    By JoshR in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2005, 11:13 PM
  2. Permutation?
    By koolguysj in forum C Programming
    Replies: 1
    Last Post: 04-02-2005, 08:24 AM
  3. permutation
    By cerin in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2005, 07:22 PM
  4. Permutation Calculation
    By Eric Hansen in forum C++ Programming
    Replies: 21
    Last Post: 06-11-2003, 04:03 PM
  5. permutation
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-01-2001, 04:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21