### ljsh_king's blog

By ljsh_king, history, 7 weeks ago,

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

• +3

 » 7 weeks ago, # |   0 Auto comment: topic has been updated by ljsh_king (previous revision, new revision, compare).
 » 7 weeks ago, # |   0 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.
•  » » 7 weeks ago, # ^ |   0 Is it because bottom-up approach checked to many unimportant states?
•  » » » 7 weeks ago, # ^ |   0 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.
•  » » » » 7 weeks ago, # ^ |   0 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.
•  » » » » 7 weeks ago, # ^ |   0 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.
•  » » » » » 7 weeks ago, # ^ |   0 no no, do the same dp, but only with middle nodes, don't do it for 1 and n
•  » » » » » » 7 weeks ago, # ^ |   0 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$.
•  » » » » » » » 7 weeks ago, # ^ |   0 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
•  » » » » » » » » 7 weeks ago, # ^ | ← Rev. 2 →   0 It worked! Finally! Thank you for your help!