4

Consider the following problem: Given a simple, strongly-connected, weighted graph G=(V,E), of which every edge is colored either red or blue (in addition to having a numeric weight). Find an efficient algorithm to find an MST which is mostly red (meaning, has the most red edges possible).

I think this can be done in linear time, in O(n+m) complexity using either Kruskal or Prim's algorithms, but I can't find an algorithm to do that.

I thought about diving the edges into two groups - red and blue, and then running Kruskal's algorithm on the red edges first, and then if we still don't have |V|-1 edges in the MST we have so far, run Kruskal again on the blue edges. However, this algorithm doesn't solve the given problem since we might not end up with a tree, but rather with a forest (made up of two trees).

Please advise.

Raphael
  • 72,336
  • 29
  • 179
  • 389
The_Ben
  • 163
  • 1
  • 6

4 Answers4

2

Hint: Add $\epsilon>0$ to the weight of all blue edges. (This is equivalent to the suggestion Raphael made in his comment.)

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
2

Your problem is ambiguous. What is the first to be optimized? the number of red edges or the tree cost? If the tree cost must be the same as an MST of the uncolored graph, Yuval's solution is correct (equivalently when sorting the edges in Kruskal algorithm, break tie by their color). If you want to find the minimized tree cost among all spanning trees with the most possible red edges, your algorithm is correct. For your question, if the algorithm cannot find a tree, there is no MST, i.e., the graph is not connected. BTW, the time complexity is not linear.

Bangye
  • 458
  • 2
  • 5
  • Thanks for the answer. However, my algorithm isn't always correct. The algorithm should take the most red edges possible, but if you have to take a blue edge to make an MST, that's alright too. – The_Ben Dec 04 '14 at 08:00
1

If all of the weights are integers, then given n nodes, we know the spanning tree will have a maximum of n-1 edges. Therefore multiply all edges so the new edge weight is w_i'=n*w_i+(1 if blue else 0). If non-integer data, then it can always be ordered, discretized and preserved to its original value with a dictionary. This means that the gap between weights will be a multiple of n and we know the spanning tree cannot at more than n-1 preserving the minimum spanning tree property while minimizing the blue edges.

Otherwise the suggestion to use Kruskal and selecting red edges before blue edges on tie's seems reasonable however, it will be correct by the cut property and cycle properties of MSTs. The whole reason why the greedy algorithm works in the first place is why we can simply further weight the edges in case of ties and still get a proper minimum.

If all else fails, you can check the MST of only the R red edges in E. If you find one you are done, otherwise if it is disconnected, then add all combinations of 1 edge, 2 edges, ..., |R| edges and compute MSTs until it finds an MST and at that point you for sure know the minimum. Of course this is a very expensive O(2^n) algorithm and should only be used as a tool with small cases where n < 20 or so for example so you can verify that any algorithm you think you made right is correct. Coming up with good test cases or even exhaustive ones is also quite difficult.

-1

Approach 1: Use a disjoint set structure, every vertex will be a set, then connect all the vertices with the red edges first, and then add the blue edges. Linear time.

Approach 2: Second approach, weight of red is 0 and weight of blue is 1. Run Prim algorithm. it is linear because you don't have to sort the edges.

Ilan Aizelman WS
  • 203
  • 3
  • 10