Link to the problem I use bottom up DP, where $$$dp[\text{mask of visited vertices}][\text{ending vertex}]$$$. I'm pretty sure my time complexity is $$$O(N \cdot 2^N + 2^N \cdot N^2)$$$ however it gives TLE. Are there any way to optimize my code, and what's the reason for TLE (probably big constant factor?)
My code
Your dp idea could work, if you eliminate the N^2 factor. Try using a top-bottom approach, so start with the full mask and ending, and work to the bottom (recursion should be an easy implementation). don't forget to use memoization in that case, since you don't want to compute the same value more than once.
Is it because bottom-up approach checked to many unimportant states?
Scratch that, looking at my submission I think I'm using N^2*2^N as well, the problem is probably you sorting the masks.
Also quick tip for speeding up your code by about x5 is only doing dp on the middle nodes (2,3,...,n-1) because you know for sure when you'll reach 1 and n.
Sorting is only $$$O(N \cdot 2^N)$$$ which is way smaller than $$$O(2^N \cdot N^2)$$$. Also, only do dp on the middle nodes like this $$$dp[\text{first_node}][\text{visited_nodes}][\text{ending_node}]$$$ ? However it requires $$$O(2^N \cdot N^2)$$$ memory which is too much.
no no, do the same dp, but only with middle nodes, don't do it for 1 and n
Not sure how. If we only do the middle ones, what will be the final answer? Some of the $$$\text{full_mask}$$$ could be starting from some nodes that isn't directly connected to node $$$1$$$.
the only difference is the starting values, which will be 1 for each direct neighbor of node 1 (for the 1 bit mask).
The calculation at the end will be the sum of full masks over all nodes that connect to n