Generating permutations using Bitmasking

5.3k views Asked by At

I was answering some programming problems in the internet and this problem interests me. The problem is defined as follows:

This code prints all the permutations of the string lexicographically. Something is wrong with it. Find and fix it by modifying or adding one line!

Input:

The input consists of a single line containing a string of lowercase characters with no spaces in between. Its length is at most 7 characters, and its characters are sorted lexicographically.

Output:

All permutations of the string printed one in each line, listed lexicographically.

def permutations():
global running
global characters
global bitmask
if len(running) == len(characters):
    print(''.join(running))
else:
    for i in xrange(len(characters)):
        if ((bitmask>>i)&1) == 0:
            bitmask |= 1<<i
            running.append(characters[i])
            permutations()
            running.pop()

raw = raw_input()
characters = list(raw)
running = []
bitmask = 0
permutations()

Can somebody answer it for me and explain how it works? I am not really familiar in the applications of bitmasking. Thank you.

1

There are 1 answers

2
Abhishek Bansal On BEST ANSWER

You should make the bitmask bit 0 again by adding the line:

bitmask ^= 1<<i

Code:

def permutations():
    global running
    global characters
    global bitmask
    if len(running) == len(characters):
        print(''.join(running))
    else:
        for i in xrange(len(characters)):
            if ((bitmask>>i)&1) == 0:
                bitmask |= 1<<i
                running.append(characters[i])
                permutations()
                bitmask ^= 1<<i  #make the bit zero again.
                running.pop()

raw = raw_input()
characters = list(raw)
running = []
bitmask = 0
permutations()

Explanation:
Bitmask is an integer that is treated as a string of bits. In your case the length of this string is equal to the length of the input string.

Each position in this string signifies whether the corresponding character has already added in the partially built string or not.

The code works by building a new string starting from an empty string. Whenever any character is added, the bitmask records it. Then the string is sent deeper into recursion for further addition of characters. When the code returns from recursion, then the added character is to be removed and the bitmask value has to be made to its original value.

More information about masking can be found here.http://en.wikipedia.org/wiki/Mask_%28computing%29

EDIT:
Say the input string is "abcde" and the bitmask at any point in the execution of the code is "00100". This means that only the character 'c' has been added so far to the partially built string. Hence we should not add the character 'c' again.
The "if" condition ((bitmask >> i) & 1) == 0 checks whether the i'th bit in bitmask has been set, ie., whether the i'th character has already been added in the string. If it is not added, only then the character gets appended, otherwise not.

If the bit operations are new to you then I suggest you look up on this topic on the internet.