Exemplo n.º 1
0
        public KruskalMST(EdgeWeightedGraph G)
        {
            _mst = new Queue <Edge>();
            var pq = new MinPQ <Edge>(G.Edges().Count());
            var uf = new WeightedQuickUnion(G.V);

            foreach (var edge in G.Edges())
            {
                if (edge is null)
                {
                    continue;
                }
                pq.Insert(edge);
            }
            while (!pq.IsEmpty && _mst.Count() < G.V - 1)
            {
                var e = pq.DelMin(); // Get min weight edge on pq
                var v = e.Either();
                var w = e.Other(v);  // and its vertices.
                if (uf.Connected(v, w))
                {
                    continue;    // Ignore ineligible edges.
                }
                uf.Union(v, w);  // Merge components.
                _mst.Enqueue(e); // Add edge to mst.
            }
        }
Exemplo n.º 2
0
 public WeightedQuickUnionTest()
 {
     _connectedAfterUnionWeightedQuickUnionSut = new WeightedQuickUnion(10);
     Thread.CurrentThread.CurrentCulture       = CultureInfo.GetCultureInfo("en-US");
     _connectedWeightedQuickUnionSut           = new WeightedQuickUnion(10);
     //7-9  0 1 2 3 4 5 6 7 8 7  1 1 1 1 1 1 1 2 1 1
     _connectedWeightedQuickUnionSut.Union(7, 9);
     //6-9  0 1 2 3 4 5 7 7 8 7  1 1 1 1 1 1 1 3 1 1
     _connectedWeightedQuickUnionSut.Union(6, 9);
     //2-0  2 1 2 3 4 5 7 7 8 7  1 1 2 1 1 1 1 3 1 1
     _connectedWeightedQuickUnionSut.Union(2, 0);
     //5-8  2 1 2 3 4 5 7 7 5 7  1 1 2 1 1 2 1 3 1 1
     _connectedWeightedQuickUnionSut.Union(5, 8);
     //7-4  2 1 2 3 7 5 7 4 5 7  1 1 2 1 1 2 1 4 1 1
     _connectedWeightedQuickUnionSut.Union(7, 4);
     //6-1  2 7 2 3 7 5 7 4 5 7  1 1 2 1 1 2 1 5 1 1
     _connectedWeightedQuickUnionSut.Union(6, 1);
     //0-5  2 7 2 3 7 2 7 7 5 7  1 1 4 1 1 2 1 5 1 1
     _connectedWeightedQuickUnionSut.Union(0, 5);
     //4-3  2 7 2 7 7 2 7 7 5 7  1 1 4 1 1 2 1 6 1 1
     _connectedWeightedQuickUnionSut.Union(4, 3);
     //2-4  2 7 7 7 7 2 7 7 5 7  1 1 4 1 1 2 1 10 1 1
     _connectedWeightedQuickUnionSut.Union(2, 4);
     //2-4  2 7 7 7 7 2 7 7 5 7  1 1 4 1 1 2 1 10 1 1
     _connectedWeightedQuickUnionSut.Union(2, 2);
     //2-7  2 7 7 7 7 2 7 7 5 7  1 1 4 1 1 2 1 10 1 1
     _connectedWeightedQuickUnionSut.Union(2, 7);
 }
Exemplo n.º 3
0
 public void OneTimeSetup()
 {
     _connectedWeightedQuickUnionSut = new WeightedQuickUnion(10);
     //7-9  0 1 2 3 4 5 6 7 8 7  1 1 1 1 1 1 1 2 1 1
     _connectedWeightedQuickUnionSut.Union(7, 9);
     //6-9  0 1 2 3 4 5 7 7 8 7  1 1 1 1 1 1 1 3 1 1
     _connectedWeightedQuickUnionSut.Union(6, 9);
     //2-0  2 1 2 3 4 5 7 7 8 7  1 1 2 1 1 1 1 3 1 1
     _connectedWeightedQuickUnionSut.Union(2, 0);
     //5-8  2 1 2 3 4 5 7 7 5 7  1 1 2 1 1 2 1 3 1 1
     _connectedWeightedQuickUnionSut.Union(5, 8);
     //7-4  2 1 2 3 7 5 7 4 5 7  1 1 2 1 1 2 1 4 1 1
     _connectedWeightedQuickUnionSut.Union(7, 4);
     //6-1  2 7 2 3 7 5 7 4 5 7  1 1 2 1 1 2 1 5 1 1
     _connectedWeightedQuickUnionSut.Union(6, 1);
     //0-5  2 7 2 3 7 2 7 7 5 7  1 1 4 1 1 2 1 5 1 1
     _connectedWeightedQuickUnionSut.Union(0, 5);
     //4-3  2 7 2 7 7 2 7 7 5 7  1 1 4 1 1 2 1 6 1 1
     _connectedWeightedQuickUnionSut.Union(4, 3);
     //2-4  2 7 7 7 7 2 7 7 5 7  1 1 4 1 1 2 1 10 1 1
     _connectedWeightedQuickUnionSut.Union(2, 4);
     //2-4  2 7 7 7 7 2 7 7 5 7  1 1 4 1 1 2 1 10 1 1
     _connectedWeightedQuickUnionSut.Union(2, 2);
     //2-7  2 7 7 7 7 2 7 7 5 7  1 1 4 1 1 2 1 10 1 1
     _connectedWeightedQuickUnionSut.Union(2, 7);
 }
Exemplo n.º 4
0
        public void IsConnected_NodesAreNotConnected_ReturnsFalse()
        {
            WeightedQuickUnion weightedQuickUnion = new WeightedQuickUnion(5);

            weightedQuickUnion.Union(1, 2);

            Assert.IsFalse(weightedQuickUnion.IsConnected(2, 3));
        }
Exemplo n.º 5
0
        public void IsConnected_NodesAreConnected_ReturnsTrue()
        {
            WeightedQuickUnion weightedQuickUnion = new WeightedQuickUnion(5);

            weightedQuickUnion.Union(1, 2);

            Assert.IsTrue(weightedQuickUnion.IsConnected(1, 2));
        }
Exemplo n.º 6
0
        public void IsConnected_MultipleUnions_ReturnsTrue()
        {
            WeightedQuickUnion weightedQuickUnion = new WeightedQuickUnion(5);

            weightedQuickUnion.Union(1, 2);
            weightedQuickUnion.Union(2, 3);

            Assert.IsTrue(weightedQuickUnion.IsConnected(1, 3));
        }
Exemplo n.º 7
0
        public void IsConnected_UnionOfConnectedNodes_ReturnsTrue()
        {
            WeightedQuickUnion weightedQuickUnion = new WeightedQuickUnion(5);

            weightedQuickUnion.Union(1, 2);
            weightedQuickUnion.Union(4, 5);
            weightedQuickUnion.Union(1, 4);

            Assert.IsTrue(weightedQuickUnion.IsConnected(2, 4));
        }
Exemplo n.º 8
0
        public void WeightedQuickUnionTest()
        {
            var pqPairs = CreatePairs();

            var wqu = new WeightedQuickUnion(10);

            wqu.DoSearch(pqPairs);

            //all items are now connected - all indexes are 1
            //Assert.IsFalse(arr.Any(i => i != 1));
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var amount = 10;
            var qf     = new WeightedQuickUnion(amount);

            for (var i = 0; i < amount - 1; i++)
            {
                qf.Union(i, i + 1);
                Console.WriteLine(qf.Count());
            }
        }
        public void DemoTest()
        {
            IUnionFind uf = new WeightedQuickUnion(10);

            int[] root = new int[10];

            uf.Union(4, 3);
            root = uf.GetIds();
            Assert.AreEqual(4, root[3]);

            uf.Union(3, 8);
            root = uf.GetIds();
            Assert.AreEqual(4, root[8]);

            uf.Union(6, 5);
            root = uf.GetIds();
            Assert.AreEqual(6, root[5]);

            uf.Union(9, 4);
            root = uf.GetIds();
            Assert.AreEqual(4, root[9]);

            uf.Union(2, 1);
            root = uf.GetIds();
            Assert.AreEqual(2, root[1]);

            uf.Union(5, 0);
            root = uf.GetIds();
            Assert.AreEqual(6, root[0]);

            uf.Union(7, 2);
            root = uf.GetIds();
            Assert.AreEqual(2, root[7]);

            uf.Union(6, 1);
            root = uf.GetIds();
            Assert.AreEqual(6, root[2]);

            uf.Union(7, 3);
            root = uf.GetIds();
            Assert.AreEqual(6, root[4]);

            Assert.AreEqual(6, root[0]);
            Assert.AreEqual(2, root[1]);
            Assert.AreEqual(6, root[2]);
            Assert.AreEqual(4, root[3]);
            Assert.AreEqual(6, root[4]);
            Assert.AreEqual(6, root[5]);
            Assert.AreEqual(6, root[6]);
            Assert.AreEqual(2, root[7]);
            Assert.AreEqual(4, root[8]);
            Assert.AreEqual(4, root[9]);
        }
Exemplo n.º 11
0
    private void Solve(int startVertex)
    {
        var currentVertex = startVertex;

        while (true)
        {
            path.Add(currentVertex);
            var adjacent = graph.Adj(currentVertex);

            var filteredAdjacent = adjacent.Where(next => !visited[currentVertex, next]).ToArray();

            if (filteredAdjacent.Length == 0)
            {
                break;
            }
            for (int i = 0; i < filteredAdjacent.Length; i++)
            {
                var nextPos = filteredAdjacent[i];
                bool[,] tempVisited = (bool[, ])visited.Clone();
                // System.Console.WriteLine(test);
                tempVisited[nextPos, currentVertex] = true;
                tempVisited[currentVertex, nextPos] = true;
                var uf = new WeightedQuickUnion(graph.Vertex);

                for (int j = 0; j < this.graph.Vertex; j++)
                {
                    foreach (var to in graph.Adj(j))
                    {
                        if (tempVisited[to, j] || tempVisited[j, to])
                        {
                            continue;
                        }
                        uf.Union(j, to);
                    }
                }

                if (!uf.Connected(currentVertex, nextPos) && i != filteredAdjacent.Length - 1)
                {
                    continue;
                }

                visited[currentVertex, nextPos] = true;
                visited[nextPos, currentVertex] = true;

                currentVertex = nextPos;
                break;
            }
        }
    }
Exemplo n.º 12
0
        public void QuickFind_TinyFile_True()
        {
            var dots = new Dots(@"DataStore\tinyUF.txt");
            var weightedQuickUnion = new WeightedQuickUnion(dots.Count);

            foreach (Tuple <int, int> item in dots.Items)
            {
                if (weightedQuickUnion.Connected(item.Item1, item.Item2))
                {
                    continue;
                }
                weightedQuickUnion.Union(item.Item1, item.Item2);
            }
            Assert.Equal(2, weightedQuickUnion.Count);
        }
        public void Works()
        {
            var wqu = new WeightedQuickUnion(10);

            wqu.Union(4, 3);
            wqu.Union(3,8);
            wqu.Union(9, 4);

            wqu.Union(6, 5);
            wqu.Union(2, 1);
            wqu.Union(5,0);
            wqu.Union(7, 2);
            wqu.Union(6, 1);
            //wqu.Union(7, 3); this would connect everything

            Assert.IsTrue(wqu.Connected(4, 3));
            Assert.IsTrue(wqu.Connected(4, 8));
            Assert.IsTrue(wqu.Connected(4, 9));
            Assert.IsTrue(wqu.Connected(3, 3));
            Assert.IsTrue(wqu.Connected(3, 8));
            Assert.IsTrue(wqu.Connected(3, 9));
            Assert.IsTrue(wqu.Connected(8, 8));
            Assert.IsTrue(wqu.Connected(8, 9));

            Assert.IsFalse(wqu.Connected(4, 1));
            Assert.IsFalse(wqu.Connected(4, 7));
            Assert.IsFalse(wqu.Connected(4, 2));
            Assert.IsFalse(wqu.Connected(4, 5));
            Assert.IsFalse(wqu.Connected(4, 6));
            Assert.IsFalse(wqu.Connected(4, 0));
            Assert.IsFalse(wqu.Connected(8, 1));
            Assert.IsFalse(wqu.Connected(8, 7));
            Assert.IsFalse(wqu.Connected(8, 2));

            Assert.IsTrue(wqu.Connected(6, 0));
            Assert.IsTrue(wqu.Connected(6, 1));
            Assert.IsTrue(wqu.Connected(6, 2));
            Assert.IsTrue(wqu.Connected(6, 5));
            Assert.IsTrue(wqu.Connected(6, 7));
            Assert.IsTrue(wqu.Connected(2, 0));
            Assert.IsTrue(wqu.Connected(2, 1));
        }
Exemplo n.º 14
0
    public void TestWeightedQuickUnion()
    {
        var input = new List <int[]>()
        {
            new int[] { 1, 3 },
            new int[] { 1, 4 },
            new int[] { 8, 9 }
        };

        var N = 10;


        var uf = new WeightedQuickUnion(N);

        foreach (var item in input)
        {
            uf.Union(item[0], item[1]);
        }

        Assert.Equal(uf.Connected(3, 4), true);
        Assert.Equal(uf.Connected(9, 0), false);
    }
Exemplo n.º 15
0
        /// <summary>
        /// Creates n-by-n grid, with all sites initially blocked
        /// </summary>
        /// <param name="n">size of the grid</param>
        public Percolation(int n)
        {
            if (n <= 0)
            {
                throw new ArgumentException("Grid size must be more than 0");
            }

            _size   = n;
            _top    = 0;
            _bottom = _size * _size + 1;

            //add two points at top and bottom
            quickUnionAlg = new WeightedQuickUnion(_size * _size + 2);

            //connect first point to a first row
            // and last point to a last row
            for (int i = 1; i < _size; i++)
            {
                quickUnionAlg.Union(GetSitePosition(1, i), _top);
                quickUnionAlg.Union(GetSitePosition(_size, i), _bottom);
            }

            _percolationGrid = new bool[_size, _size];
        }
Exemplo n.º 16
0
        public void WeightedQuickUnion_ReflexiveCheck()
        {
            var quickFind = new WeightedQuickUnion(10);

            ReflexiveCheck(quickFind);
        }
Exemplo n.º 17
0
        public void WeightedQuickUnion_AllConnected()
        {
            var quickFind = new WeightedQuickUnion(10);

            AllConnectedCheck(quickFind);
        }
Exemplo n.º 18
0
        public void WeightedQuickUnion_TwoSites()
        {
            var quickFind = new WeightedQuickUnion(10);

            TwoSitesCheck(quickFind);
        }
Exemplo n.º 19
0
        public void WeightedQuickUnion_TransitiveCheck()
        {
            var quickFind = new WeightedQuickUnion(10);

            TransitiveCheck(quickFind);
        }
Exemplo n.º 20
0
        public void WeightedQuickUnion_SymmetricCheck()
        {
            var quickFind = new WeightedQuickUnion(10);

            SymmetricCheck(quickFind);
        }
Exemplo n.º 21
0
 public Percolation(int x, int y)
 {
     myGrid = new Grid(x,y);
     Cells = x * y;
     weightedUnion = new WeightedQuickUnion(Cells + 2);
     VirtualTopId = Cells;
     VirtualBottomId = Cells + 1;
     X = x;
     Y = y;
 }
        public void WeightedQuickUnionTest()
        {
            var dc = new WeightedQuickUnion(10);

            DynamicConnectivityTestRuner.RunTests(dc);
        }
Exemplo n.º 23
0
 public void SetUp()
 {
     _connectedAfterUnionWeightedQuickUnionSut = new WeightedQuickUnion(10);
 }