给定k,一个正整数和一个连通图G =(V,E),E的每个e都有权重w(e),有人可以提出一种算法来确定G是否具有权重为k的生成树吗?
Your problem is NP-hard. We can show this by reduction from the following formulation of the subset sum problem :
Given the […] natural numbers w 1 , …, w n , 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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.