private void AddToUnion(int u, int v)
 {
     if (_uf.Find(u) != _uf.Find(v))
     {
         _uf.Union(u, v);
     }
 }
Example #2
0
        private int KruskalRoutine(List <Edge1> edges)
        {
            /* Start picking and adding edges to the UnionFind
             * without creating cycles. Stop when UnionFind components
             * become 'k'-clusters.
             */

            // Initialize UnionFind
            UnionFind uf         = new UnionFind(EdgeBuilder.ExtractVertices(edges));
            int       maxSpacing = 0;

            foreach (Edge1 edge in edges)
            {
                // If the vertices (clusters) are in different components, add.
                if (uf.Find(edge.U) != uf.Find(edge.V))
                {
                    // Check if k-cluster reached
                    if (uf.Components == _k)
                    {
                        maxSpacing = edge.Cost;
                        break;
                    }
                    uf.Union(edge.U, edge.V);
                }
            }
            return(maxSpacing);
        }