Given a graph G with red and blue edges and a constant K, devise a deterministic, linear time algorithm that finds a spanning tree of G with exactly K red edges (or returns False
if such a spanning tree does not exist).
What we have done so far:
Let every red edge have weight of -1 and every blue edge have weight of 0.
Find a minimum spanning tree (using a standard linear time algorithm). So, we have a spanning tree T with minimal weight, meaning we used as many red edges as we can because red edges will only decrease the weight.
If there are less than K red edges in T, we return False
.
If there are exactly K red edges, we are done, T is the answer.
If there are more than K red edges, we need to replace them with blue ones.
This is our problem, how do we do that in linear time?
Every blue edge added will create a cycle, so removing one red edge from the cycle will work but how can we ensue linearity in this way? Is this even a good approach?