I have a directed graph which has edges between every vertices $i$ and $j$ such that $i < j$ and the edge is from i->j and every vertex needs to be visited. I need to divide the graph into two parts such that the travel distance on both the parts i.e. sum of edge weights while traveling from the lowest index to the highest index in each of the group is minimum. For example there is a graph of 4 vertices where each edge weight is the following
- $w(1, 3) = 2$
- $w(1, 4) = 1$
- $w(3, 4) = 3$
- $w(1, 2) = 100$
- $w(2, 3) = 100$
- $w(2, 4) = 100$
The solution would be $\{2\}, \{1, 3, 4\}$. In the first group {2} doesn't have any vertex so no edges between any of the vertices in that group. In the second group we travel from 1->3 and 3->4 so the edges we travelled would be 2 + 3. Hence the ans would be 0 (from the first group) + (2+3) (from the second group). We start from the lowest index vertex and move to the highest index vertex as the edges are from lower index to higher index vertex.
The algorithm I came up was a brute force i.e. considering each combination and return the minimum of it i.e. to take one vertex in one group or include in other which leads to complexity $O(2^n)$.
I want to get the above problem to run in $O(n^2)$ solution. I did draw a decision tree for the above problem and can see nodes being overlapped i.e. $\{1,2\}, \{3,4\}$ and $\{3,4\}, \{1,2\}$ so I figured this is a dynamic programming problem but I am not able to define the states.
Edit - It can be done using a 1d dp array