public void UnionTest()
        {
            // Assemble
            var hs1 = new HashedSet(new long[] { 1, 2, 3, 4, 5, 6, 7 });
            var hs2 = new HashedSet(new long[] { 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            // Act
            var actual = hs1.Union(hs2);

            // Assert
            var expected = new UnionSet(hs1, hs2);

            Assert.AreEqual(expected, actual);
        }
        static int MinCostUsingKruskal(WeightedGraph <int> g, int numEdges)
        {
            int minCost = 0;

            //As it is a undirected graph, each edge will be added twice, hence size will be twice the number of edges.
            Tuple <int, int, int>[] edgeList = new Tuple <int, int, int> [2 * numEdges];

            // Get Iterartor to traverse adjList
            Dictionary <int, List <Tuple <int, int> > > .Enumerator it = g.adjList.GetEnumerator();
            int index = 0;

            while (it.MoveNext())
            {
                //Get iterator to traverse neighbour list of current node
                List <Tuple <int, int> > .Enumerator nbrIterator = it.Current.Value.GetEnumerator();
                while (nbrIterator.MoveNext())
                {
                    int curVtx = it.Current.Key;
                    int curNbr = nbrIterator.Current.Item1;
                    int curWt  = nbrIterator.Current.Item2;
                    edgeList[index++] = new Tuple <int, int, int>(curVtx, curNbr, curWt);
                }
            }

            Array.Sort(edgeList, delegate(Tuple <int, int, int> t1, Tuple <int, int, int> t2) {
                if (t1.Item3 < t2.Item3)
                {
                    return(-1);
                }
                else
                {
                    return(1);
                }
            });

            UnionSet edgeSet = new UnionSet(g.numVertices);

            for (int i = 0; i < 2 * numEdges; ++i)
            {
                var curEdge = edgeList[i];
                int vtx1    = curEdge.Item1;
                int vtx2    = curEdge.Item2;
                if (edgeSet.IsSameSet(vtx1, vtx2) == false)
                {
                    // curEdge is significant
                    minCost += curEdge.Item3;
                    edgeSet.MakeUnion(vtx1, vtx2);
                }
            }
            return(minCost);
        }
Ejemplo n.º 3
0
        public int NumIslands(char[][] grid)
        {
            int rows = grid.Length;

            if (rows == 0)
            {
                return(0);
            }
            int cols = grid[0].Length;

            if (cols == 0)
            {
                return(0);
            }
            int      n    = rows * cols;
            UnionSet uset = new UnionSet(n);

            for (int row = 0; row < rows; row++)
            {
                int index = row * cols;
                for (int col = 0; col < cols; col++)
                {
                    if (grid[row][col] == '1')
                    {
                        if (row > 0 && grid[row - 1][col] == '1')
                        {
                            uset.Union(index + col, index - cols + col);
                        }
                        if (col > 0 && grid[row][col - 1] == '1')
                        {
                            uset.Union(index + col, index + col - 1);
                        }
                    }
                }
            }

            HashSet <int> count = new HashSet <int>();

            for (int row = 0; row < rows; row++)
            {
                int index = row * cols;
                for (int col = 0; col < cols; col++)
                {
                    if (grid[row][col] == '1')
                    {
                        count.Add(uset.Find(index + col));
                    }
                }
            }
            return(count.Count);
        }
Ejemplo n.º 4
0
        public void CompareToUnionSetIsEqualToRangedSetTest()
        {
            // Assemble
            var hs1 = new HashedSet(new long[] { -1, 0, 1, 2, 3 });
            var hs2 = new HashedSet(new long[] { -1, 0, 1, 2, 3, 4, 5 });
            var us  = new UnionSet(hs1, hs2);

            var rs3 = new RangeSet(-1, 5);

            // Act
            var expected = 0;
            var actual   = us.CompareTo(rs3);

            //Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void CompareToIndeterminableTest()
        {
            // Assemble
            var hs1 = new HashedSet(new long[] { -1, 0, 1, 2, 3 });
            var hs2 = new HashedSet(new long[] { 4, 5, 6, 7, 8 });
            var us  = new UnionSet(hs1, hs2);

            var rs3 = new RangeSet(long.MinValue, long.MaxValue);

            // Act
            var expected = 2;
            var actual   = us.CompareTo(rs3);

            //Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 6
0
        public void CompareToNoPureSubSetsTest()
        {
            // Assemble
            var hs1 = new HashedSet(new long[] { -1, 0, 1, 2, 3 });
            var hs2 = new HashedSet(new long[] { 4, 5, 6, 7, 8 });
            var hs3 = new HashedSet(new long[] { 10, 11, 12 });

            var us = new UnionSet(hs1, hs2);


            // Act
            var expected = 2;
            var actual   = us.CompareTo(hs3);

            //Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 7
0
        public void CompareToUnionSetIsPureSupersetToHashedSetTest()
        {
            // Assemble
            var hs1 = new HashedSet(new long[] { -1, 0, 1, 2, 3 });
            var hs2 = new HashedSet(new long[] { 4, 5, 6, 7, 8 });
            var hs3 = new HashedSet(new long[] { 1, 2, 3 });

            var us = new UnionSet(hs1, hs2);


            // Act
            var expected = 1;
            var actual   = us.CompareTo(hs3);

            //Assert
            Assert.AreEqual(expected, actual);
        }
    public int FindCircleNum(int[][] isConnected)
    {
        if (isConnected.Length == 0 || isConnected == null)
        {
            return(0);
        }

        UnionSet set = new UnionSet(isConnected.Length);

        for (int row = 0; row < isConnected.Length; row++)
        {
            for (int col = 0; col < isConnected[row].Length; col++)
            {
                if (isConnected[row][col] == 1 && row != col)
                {
                    set.Union(row, col);
                }
            }
        }
        return(set.GetSets());
    }
Ejemplo n.º 9
0
 public MazeGraphWithUnionSet(int rows, int columns) : base(rows, columns)
 {
     unionSet = new UnionSet(cellNum);
 }
Ejemplo n.º 10
0
 public MazeGraphWithUnionSet()
 {
     unionSet = new UnionSet(cellNum);
 }