Пример #1
0
        public static int FindCircleNum(int[,] M)
        {
            var uf = new UnionFind(M.GetLength(0));

            for (var i = 0; i < M.GetLength(0); i++)
            {
                for (var j = 0; j < M.GetLength(1); j++)
                {
                    if (M[i, j] == 1)
                    {
                        uf.UnionElements(i, j);
                    }
                }
            }
            return(uf.Count);
        }
Пример #2
0
        public int RegionsBySlashes(string[] grid)
        {
            int n        = grid.Length;
            var unionSet = new UnionFind(4 * n * n);

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    int start = 4 * (i * n + j);
                    switch (grid[i][j])
                    {
                    case '/':
                        unionSet.UnionElements(start, start + 3);
                        unionSet.UnionElements(start + 1, start + 2);
                        break;

                    case '\\':
                        unionSet.UnionElements(start, start + 1);
                        unionSet.UnionElements(start + 2, start + 3);
                        break;

                    case ' ':
                        unionSet.UnionElements(start, start + 1);
                        unionSet.UnionElements(start + 1, start + 2);
                        unionSet.UnionElements(start + 2, start + 3);
                        break;
                    }
                    if (i > 0)
                    {
                        unionSet.UnionElements(start, start - 4 * n + 2);
                    }
                    if (j > 0)
                    {
                        unionSet.UnionElements(start + 3, start - 3);
                    }
                }
            }
            return(unionSet.Count);
        }
        public KruskalMST(IGraph <TWeight> graph)
        {
            G        = graph;
            ipq      = new IndexMinHeap <Edge <TWeight> >(2 * G.E);
            marked   = new bool[G.V];
            MSTEdges = new List <Edge <TWeight> >();

            for (var i = 0; i < G.V; i++)
            {
                foreach (Edge <TWeight> e in G.GetAdjIterator(i))
                {
                    ipq.Add(e);
                }
            }

            var uf = new UnionFind(G.V);

            while (!ipq.IsEmpty() && MSTEdges.Count < graph.V - 1)
            {
                var e = ipq.ExtractMin();
                if (uf.IsConnected(e.V, e.W))
                {
                    continue;
                }

                MSTEdges.Add(e);
                uf.UnionElements(e.V, e.W);
            }

            var value     = 0.0;
            var converter = TypeDescriptor.GetConverter(typeof(TWeight));

            foreach (var edge in MSTEdges)
            {
                value += (double)converter.ConvertTo(edge.Weight, typeof(double));
            }
            MSTWeight = (TWeight)converter.ConvertTo(value, typeof(TWeight));
        }