简体   繁体   English

使用最小生成树算法

[英]Using a minimum spanning tree algorithm

Suppose I have a weighted non-directed graph G = (V,E). 假设我有一个加权无向图G =(V,E)。 Each vertex has a list of elements. 每个顶点都有一个元素列表。

We start in a vertex root and start looking for all occurances of elements with a value x . 我们从顶点开始,并开始寻找值x的所有元素的出现。 We wish to travel the least amount of distance (in terms of edge weight) to uncover all occurances of elements with value x . 我们希望走最少的距离(就边缘权重而言)以发现所有出现的值x的元素。

The way I think of it, a MST will contain all vertices (and hence all vertices that satisfy our condition). 我认为,MST将包含所有顶点(并因此包含满足我们条件的所有顶点)。 Therefore the algorithm to uncover all occurances can just be done by finding the shortest path from root to all other vertices (this will be done on the MST of course). 因此,可以通过找到从到所有其他顶点的最短路径来完成发现所有事件的算法(当然,这将在MST上完成)。

Edit : As Louis pointed out, the MST will not work in all cases if the root is chosen arbitrarily. 编辑:正如路易斯指出的那样, 如果任意选择根,则MST在所有情况下都不会起作用。 However, to make things clear, the root is part of the input and therefore there will be one and only one MST possible (given that the edges have distinct weights). 但是,为了清楚起见,根是输入的一部分,因此只有一个MST可能(假设边具有不同的权重)。 This spanning tree will indeed have all minimum-cost paths to all other vertices in the graph starting from the root. 实际上,该生成树将从根开始具有到图中所有其他顶点的所有最小开销路径。

I don't think this will work. 我认为这行不通。 Consider the following example: 考虑以下示例:

 x
 |
 3
 |
 y--3--root
 |     /
 5    3
 |   /
 |  /
  x

The minimum spanning tree contains all three edges with weight 3, but this is clearly not the optimum solution. 最小生成树包含权重为3的所有三个边,但这显然不是最佳解决方案。

If I understand the problem correctly, you want to find the minimum-weight tree in the graph which includes all vertices labeled x. 如果我正确理解问题,则希望在图中找到最小权重树,其中包括标记为x的所有顶点。 (That is, the correct answer would have total weight 8, and would be the two edges drawn vertically in this drawing.) But this does not include your arbitrarily selected root at all. (也就是说,正确答案的总权重为8,并且是该图中垂直绘制的两个边。)但是,这根本不包括您任意选择的根。

I am pretty confident that the following variation on Prim's algorithm would work. 我非常有信心Prim算法的以下变体将起作用。 Not sure if it's optimal, though. 不过,不确定是否最佳。

Let's say the label we are looking for is called L. 假设我们要查找的标签称为L。

  1. Use an all-pairs shortest path algorithm to compute d(v, w) for all v, w. 使用全对最短路径算法为所有v,w计算d(v,w)。
  2. Pick some node labeled L; 选择一些标记为L的节点; call this the root. 称其为根。 (We can be sure that this will be in the result tree, since we are including all nodes labeled L.) (由于包含了所有标记为L的节点,因此我们可以确定这将在结果树中。)
  3. Initialize a priority queue with the root initialized to 0. (The priority queue will consist of vertices labeled L, and their minimum distance from any node in the tree, including vertices not labeled L.) 初始化优先级队列,并将其根节点初始化为0。(优先级队列将由标记为L的顶点以及它们与树中任何节点的最小距离组成,包括未标记为L的顶点。)
  4. While the priority queue is nonempty, do the following: 当优先级队列为非空时,请执行以下操作:
    1. Pick out the top vertex in the queue; 选择队列中的最高顶点; call it v, and its distance from the tree d. 称其为v,其与树的距离为d。
    2. For each vertex w on the path from v to the tree, v inclusive, find the nearest L-labeled node x to w, and add x to the priority queue, or update its priority. 对于从v到树(包括v)的路径上的每个顶点w,找到距离w最近的L标记节点x,并将x添加到优先级队列,或更新其优先级。 Add w to the tree. 将w添加到树中。

The answer is no , if I'm understanding correctly. 如果我的理解正确,答案是“ 否” Finding the minimum spanning tree will contain all vertices V, but you only want to find the vertices with value x . 查找最小生成树将包含所有顶点V,但是您只想查找值为x的顶点。 Thus, your MST result may have unneeded vertices adding extra path length and therefore be sub-optimal. 因此,您的MST结果可能具有不需要的顶点,从而增加了额外的路径长度,因此是次优的。

An example has been given where the MST M1 from Root differs from an MST M2 containing all x nodes but not containing Root. 给出了一个示例,其中来自根的MST M1与包含所有x节点但不包含根的MST M2不同。

Here's an example where Root is in both MST's: Let graph G contain nodes R,S,T,U,V (R=Root), and a clockwise path RSTUVR, with edge weights 1,1,3,2,2 going clockwise, and x at R, S, T, U. The first MST, M1, will have subtrees ST and VU below R, with cost 6 = 2+4, and cost-3 edge TU not included in M1. 这是两个MST中都存在Root的示例:让图G包含节点R,S,T,U,V(R = Root)和顺时针路径RSTUVR,边权重1,1、3、2、2顺时针,和在R,S,T,U的第一MST,M1,x将具有子树ST和VU以下R,与成本6 = 2 + 4,和具有成本3边缘TU不包括在M1。 But M2 has subtree STU (only) below R, at cost 5. 但是M2的子树STU(仅)低于R,成本为5。

Negative. 负。 If the idea is to find for every node that contains 'x' a separate path from root to it, and minimize the total cost of the paths, then you can just use simple shortest-path calculation separately for every node starting from the root, and put the paths together. 如果您要为每个包含'x'的节点找到一个从根到它的单独路径,并最大程度地减少路径的总成本,那么您可以对从根开始的每个节点分别使用简单的最短路径计算,并把路径放在一起。

Some of those shortest paths will not be in the minimum spanning tree, so if this is your goal, the MST solution does not work. 这些最短路径中的某些不会出现在最小生成树中,因此,如果这是您的目标,则MST解决方案将不起作用。 MST optimizes the cost of the tree, not the sum of costs of paths from root to the nodes. MST优化了树的成本,而不是从根到节点的路径总成本。

If your idea is to find one path that starts from root and traverses through all nodes that contain 'x', then this is the traveling salesman problem and it is an NP-complete optimization problem, ie very hard. 如果您的想法是找到一条从根开始并遍历所有包含“ x”的节点的路径,那么这就是旅行商问题 ,并且是NP完全优化问题,即非常困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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