Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Recursive Permutation Generator.

Recursive Permutation Generator.

Scheduled Pinned Locked Moved C#
algorithmsdata-structureshelpquestion
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shivamkalra
    wrote on last edited by
    #1

    Hi, I've an assignment to make a program that should generate all the permutations of a string one by one. I know many algorithm of generating permutations using recursion, but question has asked to generate it one by one. So deliverable should contain a class called "PermutationIterator" which should have a method "hasNextPermutation" and "nextPermutation". They have given one hint, make a "tailIterator" object of type "PermutationIterator" inside a "PermutationIterator" class (like a node of LinkedList) and then do some funny things with it. I can't figure out where should I start....any help would be appreciated. Note: You can't generate all permutations at one time, then store it into an array and then call each of them one by one. Please don not suggest this way. Thank you Shivam Kalra

    P L 2 Replies Last reply
    0
    • S shivamkalra

      Hi, I've an assignment to make a program that should generate all the permutations of a string one by one. I know many algorithm of generating permutations using recursion, but question has asked to generate it one by one. So deliverable should contain a class called "PermutationIterator" which should have a method "hasNextPermutation" and "nextPermutation". They have given one hint, make a "tailIterator" object of type "PermutationIterator" inside a "PermutationIterator" class (like a node of LinkedList) and then do some funny things with it. I can't figure out where should I start....any help would be appreciated. Note: You can't generate all permutations at one time, then store it into an array and then call each of them one by one. Please don not suggest this way. Thank you Shivam Kalra

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I dunno; I don't do it[^] recursively.

      L S 2 Replies Last reply
      0
      • P PIEBALDconsult

        I dunno; I don't do it[^] recursively.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Nonetheless, generating permutations is something that suits recursion. And the yield keyword would be useful too, see a little experiment here[^]. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        P 1 Reply Last reply
        0
        • S shivamkalra

          Hi, I've an assignment to make a program that should generate all the permutations of a string one by one. I know many algorithm of generating permutations using recursion, but question has asked to generate it one by one. So deliverable should contain a class called "PermutationIterator" which should have a method "hasNextPermutation" and "nextPermutation". They have given one hint, make a "tailIterator" object of type "PermutationIterator" inside a "PermutationIterator" class (like a node of LinkedList) and then do some funny things with it. I can't figure out where should I start....any help would be appreciated. Note: You can't generate all permutations at one time, then store it into an array and then call each of them one by one. Please don not suggest this way. Thank you Shivam Kalra

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          smells a lot like homework, and we don't do other people's homework... :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          S 1 Reply Last reply
          0
          • L Luc Pattyn

            smells a lot like homework, and we don't do other people's homework... :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            S Offline
            S Offline
            shivamkalra
            wrote on last edited by
            #5

            Thank you guys. I'm very excited right now because I've just finished the question, and I did it all by myself :-\ . It took 1/2 hour to think, 1 hour to write and 1 hour to correct it using hit and trial..But finally I succeeded. I'm attaching code below, it is in java but it will still run in C# because all syntax-es are same.

            public class PermutationIterator
            {
            private String s;
            private int index = 0;
            private PermutationIterator tailIterator = null;

            public PermutationIterator(String s)
            {
            	this.s = s;
            	if(s.length() > 0)
            		tailIterator = new PermutationIterator(s.substring(1));
            }
            
            public boolean hasMorePermutations()
            {	
            	if(tailIterator == null)
            		return false;
            	
            	if(s.length() == 1)
            		return true;
            	
            	return  index != s.length() - 1 || tailIterator.hasMorePermutations();
            }
            
            public String nextPermutation()
            {
            	if(s.length() == 1)
            	{
            		tailIterator = null;
            		return s;
            	}
            	
            	if(!tailIterator.hasMorePermutations())
            		tailIterator = new PermutationIterator(s.substring(0, ++index) + 
            				s.substring(index + 1));
            	
            	return s.charAt(index) + tailIterator.nextPermutation();
            }
            

            }

            It is still less readable because I just used hit trial thing to correct array out of bound exceptions. I've started to love recursion..There are only few things in world that gives to pain and pleasure and one of them is recursion :-D, lol I just made that up.. Anyways, if you could improve my code and I would really appreciate it. Thanks Shivam K

            L 2 Replies Last reply
            0
            • P PIEBALDconsult

              I dunno; I don't do it[^] recursively.

              S Offline
              S Offline
              shivamkalra
              wrote on last edited by
              #6

              Very nice article. I've bookmarked it. But in my case, if I do it non recursively then I'll get 0 marks because question clearly says USE RECURSION.

              1 Reply Last reply
              0
              • S shivamkalra

                Thank you guys. I'm very excited right now because I've just finished the question, and I did it all by myself :-\ . It took 1/2 hour to think, 1 hour to write and 1 hour to correct it using hit and trial..But finally I succeeded. I'm attaching code below, it is in java but it will still run in C# because all syntax-es are same.

                public class PermutationIterator
                {
                private String s;
                private int index = 0;
                private PermutationIterator tailIterator = null;

                public PermutationIterator(String s)
                {
                	this.s = s;
                	if(s.length() > 0)
                		tailIterator = new PermutationIterator(s.substring(1));
                }
                
                public boolean hasMorePermutations()
                {	
                	if(tailIterator == null)
                		return false;
                	
                	if(s.length() == 1)
                		return true;
                	
                	return  index != s.length() - 1 || tailIterator.hasMorePermutations();
                }
                
                public String nextPermutation()
                {
                	if(s.length() == 1)
                	{
                		tailIterator = null;
                		return s;
                	}
                	
                	if(!tailIterator.hasMorePermutations())
                		tailIterator = new PermutationIterator(s.substring(0, ++index) + 
                				s.substring(index + 1));
                	
                	return s.charAt(index) + tailIterator.nextPermutation();
                }
                

                }

                It is still less readable because I just used hit trial thing to correct array out of bound exceptions. I've started to love recursion..There are only few things in world that gives to pain and pleasure and one of them is recursion :-D, lol I just made that up.. Anyways, if you could improve my code and I would really appreciate it. Thanks Shivam K

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                I haven't checked it at all, however it looks sufficiently clean for me to trust you either have it right or could easily get it right. one comment: I would put some parentheses in this line, to make it even more obvious:

                return index != s.length() - 1 || tailIterator.hasMorePermutations();
                ( )

                Now here is something to think about: assume there could be some duplicate characters (say street), now generate all permutations without any duplicates. I'm not claiming this is easy! I could offer a hint if you were to need one. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                S 1 Reply Last reply
                0
                • S shivamkalra

                  Thank you guys. I'm very excited right now because I've just finished the question, and I did it all by myself :-\ . It took 1/2 hour to think, 1 hour to write and 1 hour to correct it using hit and trial..But finally I succeeded. I'm attaching code below, it is in java but it will still run in C# because all syntax-es are same.

                  public class PermutationIterator
                  {
                  private String s;
                  private int index = 0;
                  private PermutationIterator tailIterator = null;

                  public PermutationIterator(String s)
                  {
                  	this.s = s;
                  	if(s.length() > 0)
                  		tailIterator = new PermutationIterator(s.substring(1));
                  }
                  
                  public boolean hasMorePermutations()
                  {	
                  	if(tailIterator == null)
                  		return false;
                  	
                  	if(s.length() == 1)
                  		return true;
                  	
                  	return  index != s.length() - 1 || tailIterator.hasMorePermutations();
                  }
                  
                  public String nextPermutation()
                  {
                  	if(s.length() == 1)
                  	{
                  		tailIterator = null;
                  		return s;
                  	}
                  	
                  	if(!tailIterator.hasMorePermutations())
                  		tailIterator = new PermutationIterator(s.substring(0, ++index) + 
                  				s.substring(index + 1));
                  	
                  	return s.charAt(index) + tailIterator.nextPermutation();
                  }
                  

                  }

                  It is still less readable because I just used hit trial thing to correct array out of bound exceptions. I've started to love recursion..There are only few things in world that gives to pain and pleasure and one of them is recursion :-D, lol I just made that up.. Anyways, if you could improve my code and I would really appreciate it. Thanks Shivam K

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Shivamkalra wrote:

                  tailIterator = new PermutationIterator(s.substring(0, ++index) + s.substring(index + 1));

                  While fine in C#, I don't like it a bit; in other languages (e.g.C, C++) this statement's result would be undefined, as the big + operator is commutative (*) and nothing forces the left operator to be evaluated before the right one, so the right operand can't be sure the auto-increment has already been executed. Again, C# guarantees it, but a casual reader could port it to another language and inadvertently introduce something that goes wrong on some compilers or under some circumstances! :) [ADDED] (*) well, not fully commutative like an arithmetic addition, however both string operands could be evaluated in any order before they get concatenated. [/ADDED]

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  modified on Friday, February 25, 2011 11:34 PM

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    I haven't checked it at all, however it looks sufficiently clean for me to trust you either have it right or could easily get it right. one comment: I would put some parentheses in this line, to make it even more obvious:

                    return index != s.length() - 1 || tailIterator.hasMorePermutations();
                    ( )

                    Now here is something to think about: assume there could be some duplicate characters (say street), now generate all permutations without any duplicates. I'm not claiming this is easy! I could offer a hint if you were to need one. :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    S Offline
                    S Offline
                    shivamkalra
                    wrote on last edited by
                    #9

                    It sounds interesting and more practical. Give me hint, I'll try to work on it. Shivam :)

                    L 1 Reply Last reply
                    0
                    • S shivamkalra

                      It sounds interesting and more practical. Give me hint, I'll try to work on it. Shivam :)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      think of a way to get all permutations in a simple ordered fashion, before you tackle the duplicates! It is very cheap, however it may well influence a lot of your code... :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      S 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        think of a way to get all permutations in a simple ordered fashion, before you tackle the duplicates! It is very cheap, however it may well influence a lot of your code... :)

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        S Offline
                        S Offline
                        shivamkalra
                        wrote on last edited by
                        #11

                        It still very hard to do. Let say you have string "aaabbbccc", you will get 9! combination..then you need to find repetitions using linear search for each of the element..it's gonna take too much time..is there any way of handling repetition within code so that it can be ignored and final result we get is list of length (9!/3!3!3!).

                        L 1 Reply Last reply
                        0
                        • S shivamkalra

                          It still very hard to do. Let say you have string "aaabbbccc", you will get 9! combination..then you need to find repetitions using linear search for each of the element..it's gonna take too much time..is there any way of handling repetition within code so that it can be ignored and final result we get is list of length (9!/3!3!3!).

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          Shivamkalra wrote:

                          It still very hard to do

                          not really. all you need to do is, at each nesting level, not choose a letter you have already chosen at that level (in a previous iteration). Now think. very hard, if you must. :)

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          S 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            Shivamkalra wrote:

                            It still very hard to do

                            not really. all you need to do is, at each nesting level, not choose a letter you have already chosen at that level (in a previous iteration). Now think. very hard, if you must. :)

                            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                            S Offline
                            S Offline
                            shivamkalra
                            wrote on last edited by
                            #13

                            oh ya, that's actually easy and makes sense too. I found a good way..if u arrange letter so that repeated letters are together then it will even easier to skips letters in each iteration ex. "aabbcc". This way you can skip alternating alphabet in each recursion..this is really interesting..I'm gonna give it a try right now. Thanks :) Shivam

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Nonetheless, generating permutations is something that suits recursion. And the yield keyword would be useful too, see a little experiment here[^]. :)

                              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              Luc Pattyn wrote:

                              generating permutations is something that suits recursion

                              I respectfully disagree.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups