Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public static UnionFind CopyUnionFind(UnionFind input)
        {
            var n      = input.Nodes.Length;
            var output = new UnionFind
            {
                Nodes = new int[n],
                Sizes = new int[n]
            };

            input.Nodes.CopyTo(output.Nodes, 0);
            input.Sizes.CopyTo(output.Sizes, 0);

            return(output);
        }
Exemplo n.º 3
0
        public int Execute(string filepath)
        {
            int maxVertex = 199999;

            // Sample dict format <234123, "1001010010101001001~23,779, ..">
            Dictionary <int, string> graph = BuildGraph(filepath);

            // Will initialize Union Find to max vertex as 0-199999 already known
            _uf = new UnionFind(maxVertex);

            GetMaxClustersPossible(graph);

            // At the end, simply count the UF components
            return(_uf.Components);
        }