ljsh_king's blog

By ljsh_king, history, 6 weeks ago, In English

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
 
 
 
 
  • Vote: I like it
  • +3
  • Vote: I do not like it

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ljsh_king (previous revision, new revision, compare).

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Is it because bottom-up approach checked to many unimportant states?

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.

        • »
          »
          »
          »
          »
          6 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          no no, do the same dp, but only with middle nodes, don't do it for 1 and n

          • »
            »
            »
            »
            »
            »
            6 weeks ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            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$$$.

            • »
              »
              »
              »
              »
              »
              »
              6 weeks ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              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