Python Program to find all Palindromic Bitlists in length
Last Updated :
28 Mar, 2023
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
fianl_ans = []
def generate_palindromic(n, is_even, lst = []):
if n = = 0 :
if is_even:
fianl_ans.append((lst + lst
[ - 1 :: - 1 ]))
else :
fianl_ans.append(lst + [ 0 ] +
lst[ - 1 :: - 1 ])
fianl_ans.append(lst + [ 1 ] +
lst[ - 1 :: - 1 ])
return
lst.append( 0 )
generate_palindromic(n - 1 , is_even, lst)
lst.pop()
lst.append( 1 )
generate_palindromic(n - 1 , is_even, lst)
lst.pop()
def check(n):
if n % 2 = = 0 :
generate_palindromic(n / 2 , 1 )
else :
generate_palindromic(n / / 2 , 0 )
if __name__ = = "__main__" :
check( 5 )
for i in fianl_ans:
print (i)
|
Output:
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
fianl_ans = []
def generate_palindromic(n, is_even, lst = []):
if n = = 0 :
if is_even:
fianl_ans.append((lst +
lst[ - 1 :: - 1 ]))
else :
fianl_ans.append(lst + [ 0 ] +
lst[ - 1 :: - 1 ])
fianl_ans.append(lst + [ 1 ] +
lst[ - 1 :: - 1 ])
return
lst.append( 0 )
generate_palindromic(n - 1 , is_even, lst)
lst.pop()
lst.append( 1 )
generate_palindromic(n - 1 , is_even, lst)
lst.pop()
def check(n):
if n % 2 = = 0 :
generate_palindromic(n / 2 , 1 )
else :
generate_palindromic(n / / 2 , 0 )
if __name__ = = "__main__" :
check( 4 )
for i in fianl_ans:
print (i)
|
Output:
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)).
Share your thoughts in the comments
Please Login to comment...