Monday, June 15, 2015

String Permutations Algorithm with example code in C/C++

Permutations of 3 balls
Permutations of 3 balls (Photo credit: Wikipedia)


Hello Everyone,

Today I will be telling you how to print all the possible permutations of a string provided by the user. I will show you the recursive way to do this. The programming paradigm that we use in case of Recursion is backtracking.

What do you mean by Permutations ?

A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.

For more info visit here --> http://mathworld.wolfram.com/Permutation.html

Recursion

This is a bad method for printing permutations since its time complex i.e. it takes more time to execute than the iterative method this is because of the extensive use of the call stack. Now the following image explains the recursion technique. The image presents the recursion tree of the method of permutations generation. We take the example string that we permute as "GOD".

In order to find all possible combinations for a given string, then start at a position i, then find and place all possible letters in position i. Every time we put a new letter in position i, we should then find all the possible combinations at position i+1 – this would be the recursive call that we make.

[Image: permute_zps699764b7.png]

Here's the C++ implementation string permutation.


Now the above code will print all the permutations of the string "GOD" without repetition. But if you want to print them lexicographic manner i.e where the repetitions of the characters are included then read the matter below.

Since we are going in lexicographic order, so we have to do the following ,its pretty much understandable. (Note : the numbers represent index and 1 means the starting index , we will not use 0 for the first index as we do it in programming.)
  • Start with index '1' and then recurse over rest of the 'n - 1' numbers.
                     
    • Start with '2' and then recurse over rest of the 'n - 2' numbers.
    • Start with '3' and then recurse over rest of the 'n - 2' numbers.
      :
      :
    • Start with 'i' and then recurse over rest of the 'n - 2' numbers.
      :
      :
    • Start with 'n' and then recurse over rest of the 'n - 2' numbers.
  • Start with a '2' and then recurse over rest of the 'n - 1' numbers.

    • Start with '1' and then recurse over rest of the 'n - 2' numbers.
    • Start with '3' and then recurse over rest of the 'n - 2' numbers.
      :
      :
    • Start with 'i' and then recurse over rest of the 'n - 2' numbers.
      :
      :
    • Start with 'n' and then recurse over rest of the 'n - 2' numbers.
  • :
    :
  • Start with 'i' and then recurse over rest of the 'n - 1' numbers.
    :
    :
  • Start with 'n' and then recurse over rest of the 'n - 1' numbers.

Important thing to note is, in every recursion you start with the minimum numbers left, to be printed, and then keep picking the next minimum, and so on, till you exhaust all the 'n' numbers.

Here's the C Code for Lexicographical Permutations.

Related articles

0 comments :

Post a Comment

Follow Me!

Blog Archive

Followers

Visitor Map