Ejemplo n.º 1
0
    private static void Compute()
    {
        var size               = 200;
        var quickFind          = new QuickFindUf(size);
        var quickUnion         = new QuickUnionUf(size);
        var weightedQuickUnion = new WeightedQuickUnionUf(size);
        var connections        = ErdosRenyi.Generate(size);

        var quickFindResult = new int[size];
        var quickUnionResult = new int[size];
        var weightedQuickUnionResult = new int[size];
        int p, q;

        for (var i = 0; i < size; i++)
        {
            p = connections[i].P;
            q = connections[i].Q;

            quickFind.Union(p, q);
            quickUnion.Union(p, q);
            weightedQuickUnion.Union(p, q);
            quickFindResult[i]          = quickFind.ArrayVisitCount;
            quickUnionResult[i]         = quickUnion.ArrayVisitCount;
            weightedQuickUnionResult[i] = weightedQuickUnion.ArrayParentVisitCount + weightedQuickUnion.ArraySizeVisitCount;

            quickFind.ResetArrayCount();
            quickUnion.ResetArrayCount();
            weightedQuickUnion.ResetArrayCount();
        }

        Draw(quickFindResult, "Quick-Find");
        Draw(quickUnionResult, "Quick-Union");
        Draw(weightedQuickUnionResult, "Weighted Quick-Union");
    }
Ejemplo n.º 2
0
        public void Open(int row, int col)
        {
            Validate(row, col);
            if (IsOpen(row, col))
            {
                return;
            }
            var index = FindIndex(row, col);

            _grid[row - 1, col - 1] = true;
            _openSites++;

            if (row == 1)
            {
                _wquGrid.Union(_virtualTop, index);
                _wquGridFull.Union(_virtualTop, index);
            }
            else if (row == _gridSize)
            {
                _wquGrid.Union(_virtualBottom, index);
            }

            // Check up site
            if (IsInGrid(row - 1, col) && IsOpen(row - 1, col))
            {
                _wquGrid.Union(index, index - _gridSize);
                _wquGridFull.Union(index, index - _gridSize);
            }
            // Check down site
            if (IsInGrid(row + 1, col) && IsOpen(row + 1, col))
            {
                _wquGrid.Union(index, index + _gridSize);
                _wquGridFull.Union(index, index + _gridSize);
            }
            // Check left site
            if (IsInGrid(row, col - 1) && IsOpen(row, col - 1))
            {
                _wquGrid.Union(index, index - 1);
                _wquGridFull.Union(index, index - 1);
            }
            // Check right site
            if (IsInGrid(row, col + 1) && IsOpen(row, col + 1))
            {
                _wquGrid.Union(index, index + 1);
                _wquGridFull.Union(index, index + 1);
            }
        }