且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是&QUOT的意义;从不同的顶点链"在这个近邻算法?

更新时间:2022-10-19 11:32:33

1)的说明指出,每个顶点总是属于任何一个单顶点链(即,它是单独的),或者它属于另外一个链条;一个顶点只能属于一个链。该算法表示在每一步选择每个可能的对的两个顶点这是它们所属的相应链的每个端点,并且不已经属于同一连锁。有时,他们会单身人士;有时一个或两个都已经属于非平凡链,所以你会加入两个链。

2)你重复循环的 N 的时间,让你最终选择每一个顶点;但是,实际的迭代次数不用于任何东西。所有的事情是,你运行循环足够的时间。

The following pseudo-code is from the first chapter of an online preview version of The Algorithm Design Manual (page 7 from this PDF).

The example is of a flawed algorithm, but I still really want to understand it:

[...] A different idea might be to repeatedly connect the closest pair of endpoints whose connection will not create a problem, such as premature termination of the cycle. Each vertex begins as its own single vertex chain. After merging everything together, we will end up with a single chain containing all the points in it. Connecting the final two endpoints gives us a cycle. At any step during the execution of this closest-pair heuristic, we will have a set of single vertices and vertex-disjoint chains available to merge. In pseudocode:

ClosestPair(P)
    Let n be the number of points in set P.
    For i = 1  to n − 1 do
        d = ∞
        For each pair of endpoints (s, t) from distinct vertex chains
            if dist(s, t) ≤ d then sm = s, tm = t, and d = dist(s, t)
        Connect (sm, tm) by an edge
    Connect the two endpoints by an edge

Please note that sm and tm should be sm and tm.

First of all, I don't understand what "from distinct vertex chains" would mean. Second, i is used as a counter in the outer loop, but i itself is never actually used anywhere! Could someone smarter than me please explain what's really going on here?

1) The description states that every vertex always belongs either to a "single-vertex chain" (i.e., it's alone) or it belongs to one other chain; a vertex can only belong to one chain. The algorithm says at each step you select every possible pair of two vertices which are each an endpoint of the respective chain they belong to, and don't already belong to the same chain. Sometimes they'll be singletons; sometimes one or both will already belong to a non-trivial chain, so you'll join two chains.

2) You repeat the loop n times, so that you eventually select every vertex; but yes, the actual iteration count isn't used for anything. All that matters is that you run the loop enough times.