Reputation: 13
given k, a positive integer, and a connected graph G=(V, E), each e of E has a weight w(e), can anyone come up with an algo to determine if G has a spanning tree of weight k?
Upvotes: 0
Views: 1068
Reputation: 183504
Your problem is NP-hard. We can show this by reduction from the following formulation of the subset sum problem:
Given the […] natural numbers w1, …, wn, does any subset of them sum to precisely W?
To wit, suppose that we have an algorithm for solving your problem. We can then solve the subset sum problem (in the formulation above) by creating a graph G consisting of the following:
The original set has a subset with sum W if and only if your algorithm returns "yes" for graph G and k = W.
To see why:
Of course, NP-hardness doesn't mean you can't do it. It just means that there's no known algorithm that's correct and efficient for all possible inputs.
One inefficient algorithm is to generate all possible spanning trees, and see if any of them have the correct weight.
You can also check some likely cases more efficiently; in particular, you can use Kruskal's algorithm to find the minimal spanning tree, and (with slight modification) to find the maximal spanning tree. This will let you quickly eliminate any k outside the range of possible spanning tree weights.
Another possible way to eliminate many values of k is to use some of the algorithms for subset sum problems (at the Wikipedia article I linked to above) in order to determine whether the graph has any subgraph with weight k.
Upvotes: 3