예제 #1
0
    // Complete the maxRegion function below.
    static int maxRegion(int[][] matrix)
    {
        var xOffset = new[] { -1, 0, 1, -1 };
        var yOffset = new[] { -1, -1, -1, 0 };

        var ds = new DisjointSets();

        var rows = matrix.Length;
        var cols = matrix[0].Length;

        var max = 0;

        Func <int, int, bool> isValid = (int x, int y) =>
        {
            return(x >= 0 && x < cols && y >= 0 && y < rows);
        };

        for (int y = 0; y < rows; y++)
        {
            for (int x = 0; x < cols; x++)
            {
                if (matrix[y][x] == 0)
                {
                    continue;
                }
                var current = new CellKey(x, y);
                ds.AddNew(current);
                max = Math.Max(max, 1);

                for (int index = 0; index < xOffset.Length; index++)
                {
                    var ox = x + xOffset[index];
                    var oy = y + yOffset[index];

                    if (!isValid(ox, oy))
                    {
                        continue;
                    }

                    if (matrix[oy][ox] == 0)
                    {
                        continue;
                    }

                    var offset = new CellKey(ox, oy);

                    if (ds.Find(current) != ds.Find(offset))
                    {
                        max = Math.Max(max, ds.Union(current, offset));
                    }
                }
            }
        }

        return(max);
    }
예제 #2
0
        public List <Edge> GetMST(Graph graph)
        {
            List <Edge> mst = new List <Edge>();

            // Sort the edges in ascending order of their weights
            List <Edge> sortedEdges = graph.AllEdges.OrderBy(e => e.Weight).ToList();

            int totalNumOfVertices = graph.AllVertices.Count();
            DisjointSets <int> ds  = new DisjointSets <int>();


            foreach (Edge e in sortedEdges)
            {
                if (mst.Count() >= totalNumOfVertices)
                {
                    // total number of edges in an mst will be total number of vertices in a graph -1
                    break;
                }
                if (!ds.Find(e.StId, e.EndId))
                {
                    // the stId and endId vertex of the edge are not in the same set
                    // hence adding this edge wont create a cycle
                    ds.Union(e.StId, e.EndId);
                    mst.Add(e);
                }
            }

            return(mst);
        }
예제 #3
0
    /*
     * Complete the 'kruskals' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts WEIGHTED_INTEGER_GRAPH g as parameter.
     */

    /*
     * For the weighted graph, <name>:
     *
     * 1. The number of nodes is <name>Nodes.
     * 2. The number of edges is <name>Edges.
     * 3. An edge exists between <name>From[i] and <name>To[i]. The weight of the edge is <name>Weight[i].
     *
     */

    public static int kruskals(int gNodes, List <int> gFrom, List <int> gTo, List <int> gWeight)
    {
        var ds = new DisjointSets(gNodes);

        var edges = new Edge[gFrom.Count];

        for (var index = 0; index < gFrom.Count; index++)
        {
            edges[index] = new Edge
            {
                From   = gFrom[index],
                To     = gTo[index],
                Weight = gWeight[index]
            };
        }

        Array.Sort(edges);

        var edgesUsed = 0;
        var sum       = 0;

        foreach (var edge in edges)
        {
            if (edgesUsed == gNodes - 1)
            {
                break;
            }

            if (ds.Find(edge.From) == ds.Find(edge.To))
            {
                continue;
            }

            edgesUsed++;
            sum += edge.Weight;

            ds.Union(edge.From, edge.To);
        }

        return(sum);
    }
예제 #4
0
        public MST <V> Execute()
        {
            var mst = new MST <V>();

            var orderedEdges = _g.AllEdges().OrderBy(e => e.Weight);

            var ds = new DisjointSets <Node <V> >();

            foreach (var edge in orderedEdges)
            {
                var head = ds.Find(edge.Head);
                var tail = ds.Find(edge.Tail);

                if (!head.Equals(tail))
                {
                    mst.Edges.Add(edge);
                    ds.Union(head, tail);
                }
            }

            return(mst);
        }
예제 #5
0
        /// <summary>
        /// Procedurally generates the 2-dimensional data for a maze with the dimensions set by the
        /// <see cref="MazeGenerator(int, int)"/> constructor.<para />
        /// Calling this method consecutively will always generate an entirely new maze but of same Rows by Cols
        /// dimensions of the given object instance. To change the dimensions, a new object must be instantiated.
        /// </summary>
        public void GenerateMaze()
        {
            int  visitedCells = 1;
            int  currCell     = rand.Next(CellNum);
            int  adjCell;
            bool adjacentFound;

            // Index - 0: above, 1: below, 2: left, 3: right
            // If adjacentCells[Index] = -1 then that adjacent cell does not exist.
            int[] adjCells = new int[Max_Adjacency];

            Stack <int> cellStack = new Stack <int>();

            while (visitedCells < CellNum)
            {
                adjCell       = -1;
                adjacentFound = false;

                if (currCell < Cols)
                {
                    adjCells[0] = -1;
                }
                else
                {
                    adjCells[0] = currCell - Cols;
                }

                if (currCell >= (CellNum - Cols))
                {
                    adjCells[1] = -1;
                }
                else
                {
                    adjCells[1] = currCell + Cols;
                }

                if (currCell % Cols == 0)
                {
                    adjCells[2] = -1;
                }
                else
                {
                    adjCells[2] = currCell - 1;
                }

                if ((currCell + 1) % Cols == 0)
                {
                    adjCells[3] = -1;
                }
                else
                {
                    adjCells[3] = currCell + 1;
                }

                for (int i = 0; i < adjCells.Length; ++i)
                {
                    if (adjCells[i] >= 0 &&
                        (sets.Find(currCell) != sets.Find(adjCells[i])))
                    {
                        adjacentFound = true;
                    }
                    else
                    {
                        adjCells[i] = -1;
                    }
                }

                if (adjacentFound)
                {
                    int wallIndex;
                    do
                    {
                        wallIndex = rand.Next(Max_Adjacency);
                        adjCell   = adjCells[wallIndex];
                    }while (adjCell < 0);

                    sets.Union(currCell, adjCell);

                    // Adjacent cell is - 0: above, 1: below, 2: left, 3: right
                    switch (wallIndex)
                    {
                    case 0:     // Connect currentCell with adjacentCell above.
                        grid[currCell] = new Cell(false, grid[currCell].belowWall, grid[currCell].leftWall, grid[currCell].rightWall);
                        grid[adjCell]  = new Cell(grid[adjCell].aboveWall, false, grid[adjCell].leftWall, grid[adjCell].rightWall);
                        break;

                    case 1:     // Connect currentCell with adjacentCell below.
                        grid[currCell] = new Cell(grid[currCell].aboveWall, false, grid[currCell].leftWall, grid[currCell].rightWall);
                        grid[adjCell]  = new Cell(false, grid[adjCell].belowWall, grid[adjCell].leftWall, grid[adjCell].rightWall);
                        break;

                    case 2:     // Connect currentCell with adjacentCell on left.
                        grid[currCell] = new Cell(grid[currCell].aboveWall, grid[currCell].belowWall, false, grid[currCell].rightWall);
                        grid[adjCell]  = new Cell(grid[adjCell].aboveWall, grid[adjCell].belowWall, grid[adjCell].leftWall, false);
                        break;

                    case 3:     // Connect currentCell with adjacentCell on right.
                        grid[currCell] = new Cell(grid[currCell].aboveWall, grid[currCell].belowWall, grid[currCell].leftWall, false);
                        grid[adjCell]  = new Cell(grid[adjCell].aboveWall, grid[adjCell].belowWall, false, grid[adjCell].rightWall);
                        break;

                    default:
                        Debug.LogError
                            ("GenerateMaze() error: invalid wallIndex value.");
                        break;
                    }

                    cellStack.Push(currCell);
                    currCell = adjCell;
                    ++visitedCells;
                }
                else
                {
                    if (cellStack.Count > 0)
                    {
                        currCell = cellStack.Pop();
                    }
                }
            }
        }