问题
Here's the problem.
A weighted undirected connected graph G is given. The weights are constant. The task is to come up with an algorithm that would find the total weight of a spanning tree for G that fulfills these two conditions (ordered by priority):
- The spanning tree has to have maximum number of edges with the same weight (the actual repeated weight value is irrelevant);
- The total spanning tree weight should be minimized. That means, for example, that the spanning tree T1 with weight 120 that has at most 4 edges with the same weight (and the weight of each of those four is 15) should be preferred over the spanning tree T2 with weight 140 that has at most 4 edges with the same weight (and the weight of each of those four is 8).
I've been stuck on that for quite a while now. I've already implemented Boruvka's MST search algorithm for the graph, and now I'm not sure whether I should perform any operations after MST is found, or it's better to modify MST-search algorithm itself.
Any suggestions welcome!
回答1:
This can be done naively in O(m^2)
, and without much effort in O(mn)
. It seems like it is possible to do it even faster, maybe something like O(m log^2(n))
or even O(m log(n))
with a little work.
The basic idea is this. For any weight k
, let MST(k)
be a spanning tree which contains the maximum possible number of edges of weight k
, and has minimum overall weight otherwise. There may be multiple such trees, but they will all have the same total weight, so it doesn't matter which one you pick. MST(k)
can be found by using any MST algorithm and treating all edges of weight k
as having weight -Infinity
.
The solution to this problem will be MST(k)
for some k
. So the naive solution is to generate all the MST(k)
and picking the one with the max number of identical edge weights and then minimum overall weight. Using Kruskal's algorithm, you can do this in O(m^2)
since you only have to sort the edges once.
This can be improved to O(mn)
by first finding the MST using the original weights, and then for each k
, modifying the tree to MST(k)
by reducing the weight of each edge of weight k
to -Infinity
. Updating an MST for a reduced edge weight is an O(n)
operation, since you just need to find the maximum weight edge in the corresponding fundamental cycle.
To do better than O(mn)
using this approach, you would have to preprocess the original MST in such a way that these edge weight reductions can be performed more quickly. It seems that something like a heavy path decomposition should work here, but there are some details to work out.
来源:https://stackoverflow.com/questions/46792311/find-a-spanning-tree-with-maximum-number-of-edges-with-same-weight