Open In App

Python Program to find all Palindromic Bitlists in length

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn to generate all Palindromic Bitlists of a given length using Backtracking in Python. Backtracking can be defined as a general algorithmic technique that considers searching every possible combination in order to solve a computational problem. Whereas, Bitlists are lists of bits(0 and 1). A bit list that is a palindrome is called Palindromic Bitlists. To generate all Palindromic Bitlists of length N, the best way is to generate all possible bit lists of length N/2 and then form palindromic Bitlist using this list of length N/2. Here we will discuss 2 cases:

Case 1: When the length of the list is odd

Let’s consider the value of N = 3. So, N/2 = 1 (floor value).

All possible bit lists of length 1 are [0], [1]. In this case, we can not do exact same thing which we have done for the even length case because doing that thing will give a bit list of length 2 and not 3. Here we will add [0] or [1] in mid of bit list. Consider [0], its reverse will be [0]. To generate bit lists we have to combine 3 lists that are generated bit lists of length 1, [0], Or [1], the reverse of the generated list. Here every bit list will produce 2-bit lists. For [0] there will be [0] + [0] +[0] and [0] + [1] +[0].

Example: The value of N is 5

Python3




# list to store all the possible solutions
fianl_ans = []
 
# this function generates all possible bitlists
# of length N//2 and then saves the final answer
# in the list
 
 
def generate_palindromic(n, is_even, lst=[]):
    # n==0 is the base condition and after
    # reaching here we don't go forward
    if n == 0:
        if is_even:
            # even case
            # final_ans = generated list + reverse
            # of generated list
            fianl_ans.append((lst + lst
                              [-1::-1]))
        else:
            # odd case
            # final_ans_1 = generated list +
            # [0] + reverse of generated list
            fianl_ans.append(lst + [0] +
                             lst[-1::-1])
            # final_ans_1 = generated list
            # + [1] + reverse of generated list
            fianl_ans.append(lst + [1] +
                             lst[-1::-1])
        return
    # store 0 in the list to provide the value of
    # this step to the next step
    lst.append(0)
    # call recursive function and decrease
    # the value of n by 1
    generate_palindromic(n-1, is_even, lst)
    # remove 0 from the list (Backtrack)
    lst.pop()
    # store 1 in the list to provide the value
    # of this step to the next step
    lst.append(1)
    # call recursive function and decrease
    # the value of n by 1
    generate_palindromic(n-1, is_even, lst)
    # remove 1 from the list (Backtrack)
    lst.pop()
 
 
def check(n):
    if n % 2 == 0:
        # if n is even then pass 1 to the
        # recursive function
        generate_palindromic(n/2, 1)
    else:
        # if n is odd then pass 0 to the
        # recursive function
        generate_palindromic(n//2, 0)
 
 
if __name__ == "__main__":
    # calling function to generate all
    # bitlists of length 5
    check(5)
    # printing final answers
    for i in fianl_ans:
        print(i)


Output:

Use Backtracking to find all Palindromic Bitlists of a given length in Python

 

Case 2: When the length of the list is even

Let’s consider the value of N = 4. So, N/2 = 2.

All possible bit lists of length 2 are [0,0], [0,1], [1,0], [1,1]. Consider [0,1], its reverse will be [1,0]. Combining both of them we will get [0,1,1,0] which is a Palindromic bit list of length 4. In this way, we can generate all Palindromic Bitlists of length N where N is even.

Example: The value of N is 4

Python3




# list to store all the possible solutions
fianl_ans = []
 
# this function generates all possible bitlists
# of length N//2 and then saves the final answer in the list
 
 
def generate_palindromic(n, is_even, lst=[]):
    # n==0 is the base condition and after
    # reaching here we don't go forward
    if n == 0:
        if is_even:
            # even case
            # final_ans = generated list + reverse
            # of generated list
            fianl_ans.append((lst +
                              lst[-1::-1]))
        else:
            # odd case
            # final_ans_1 = generated list +
            # [0] + reverse of generated list
            fianl_ans.append(lst + [0] +
                             lst[-1::-1])
            # final_ans_1 = generated list + 
            # [1] + reverse of generated list
            fianl_ans.append(lst + [1] +
                             lst[-1::-1])
        return
    # store 0 in the list to provide the value of
    # this step to the next step
    lst.append(0)
    # call recursive function and decrease the
    # value of n by 1
    generate_palindromic(n-1, is_even, lst)
    # remove 0 from the list (Backtrack)
    lst.pop()
    # store 1 in the list to provide the
    # value of this step to the next step
    lst.append(1)
    # call recursive function and decrease
    # the value of n by 1
    generate_palindromic(n-1, is_even, lst)
    # remove 1 from the list (Backtrack)
    lst.pop()
 
 
def check(n):
    if n % 2 == 0:
        # if n is even then pass 1 to
        # the recursive function
        generate_palindromic(n/2, 1)
    else:
        # if n is odd then pass 0 to
        # the recursive function
        generate_palindromic(n//2, 0)
 
 
if __name__ == "__main__":
    # calling function to generate all
    # bitlists of length 5
    check(4)
    # printing final answers
    for i in fianl_ans:
        print(i)


Output:

Use Backtracking to find all Palindromic Bitlists of a given length in Python

 

The time complexity of both Case 1 and Case 2 of the Python program to find all Palindromic Bitlists of a given length using backtracking is O(2^(N/2)), where N is the length of the bitlist.The space complexity of both Case 1 and Case 2 algorithms is O(2^(N/2)), where N is the length of the palindromic bitlist.

This is because the algorithms generate all possible palindromic bitlists of length N, which is equal to generating all possible bitlists of length N/2 and then combining them in a palindromic manner. Therefore, the space required to store all possible bitlists of length N/2 is O(2^(N/2)), which is the maximum space required by both the algorithms.

In both cases, the bitlists are generated recursively and stored in a list, which is then appended to the final_ans list. The maximum size of this list would be O(2^(N/2)), as there are 2^(N/2) possible bitlists of length N/2. Hence, the space complexity of both algorithms is O(2^(N/2)).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads