I understand the MWIS Algorithm with its subproblem
A[i]=max(A[i-1], A[i-2]+W[i])
But when it adds the condition about making the sequence to circular, means that the 1st element is connected to the last node.
I got stuck and couldn't figure out the proper subproblems..
The answer's actually quite simple. Assume you have a list of node weights in the from of A
, where A[i]
is connected to A[i-1]
and A[i+1]
.
Also assume that you have a function solve()
that solves the MWIS problem where the first and last node are not connected.
The only additional criteria for the circular variation is that if the first node is in the set, then the last node can't be in it. If the last node is in the set, the first node can't be in it.
Therefore the answer is just the maximum of solve(A[1:])
(solution for the array without the first node) and solve(A[:-1])
(solution for the array without the last node).
Why is this correct? Consider the optimal answer. It must be missing either A[0]
or A[-1]
, so it must be a subset of A[1:]
or A[:-1]
.