简体   繁体   中英

How do you determine whether a graph G has a spanning tree of weight k?

给定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:

  • vertices v 1 , …, v n corresponding to the elements of the set, plus two special vertices x and y .
  • an edge from x to each v i , with weight w i .
  • an edge from y to each v i , with weight 0.
  • an edge from x to y , with weight 0.

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:

  • If your algorithm returns "yes", then there is a spanning tree with weight W , meaning that the edges in the spanning tree add up to W . But the nonzero edge-weights are all distinct elements in the set, so this means we have a subset that sums to W .
  • Conversely, if there is a subset that sums to W , then we can construct a spanning tree with weight W by taking the edges from x to the vertices corresponding to that subset, plus the (zero-weight) edges from y to all other vertices, plus the (zero-weight) edge from x to y .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM