Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("data file name for QuickFind:");
            string file = Console.ReadLine();

            string[] RawSet = File.ReadAllLines(file);
            int      N      = Convert.ToInt32(RawSet[0]);

            string[] ProbSet = new string[RawSet.Length - 1];
            for (int i = 1; i < RawSet.Length; i++)
            {
                ProbSet[i - 1] = RawSet[i];
            }

            QuickFind uf = new QuickFind(N);

            foreach (string word in ProbSet)
            {
                string[] Pair = word.Split(" ");
                int      p    = Convert.ToInt32(Pair[0]);
                int      q    = Convert.ToInt32(Pair[1]);
                if (uf.IsConnected(p, q))
                {
                    continue;
                }
                uf.Union(p, q);
                Console.WriteLine(p + " " + q + " are now connected");
            }

            Console.WriteLine(uf.Count() + " total components");
        }
Esempio n. 2
0
        public void QuickFindTest()
        {
            //Arrange
            var quickFind = new QuickFind(10);
            //Act

            //7-9 0-3 5-0 5-6 0-8 4-1
            quickFind.Union(7, 9);
            quickFind.Union(0, 3);
            quickFind.Union(5, 0);
            quickFind.Union(5, 6);
            quickFind.Union(0, 8);
            quickFind.Union(4, 1);
            //Assert
            Assert.IsTrue(quickFind.IsConnected(7, 9));
            Assert.IsTrue(quickFind.IsConnected(0, 3));
            Assert.IsTrue(quickFind.IsConnected(5, 0));

            Assert.IsTrue(quickFind.IsConnected(5, 6));
            Assert.IsTrue(quickFind.IsConnected(0, 8));
            Assert.IsTrue(quickFind.IsConnected(4, 1));

            foreach (var component in quickFind.Components)
            {
                Trace.WriteLine(component);
            }
        }
Esempio n. 3
0
        public void UnionTests()
        {
            var union = new QuickFind(10);

            union.Union(5, 8);

            Assert.IsTrue(union.Connected(5, 8));
            Assert.IsFalse(union.Connected(5, 9));
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            string theInputFile = @"F:\Holidays Project\Algorithms\UnionFind\UnionFind\data.txt";
             int wordPointer=0;

             if (File.Exists(theInputFile))
             {

             System.IO.StreamReader myFile = new System.IO.StreamReader(theInputFile);
             string fileContents = myFile.ReadToEnd();

             myFile.Close();
             string[] words=fileContents.Split(' ','\n');

             //create objects
             QuickFind quickFindObj = new QuickFind(Convert.ToInt32(words[wordPointer++]));

             while (wordPointer < words.Length)
             {
                 if (!quickFindObj.connected(Convert.ToInt32(words[wordPointer]),Convert.ToInt32(words[wordPointer + 1])))
                 {
                     quickFindObj.Union(Convert.ToInt32(words[wordPointer]), Convert.ToInt32(words[wordPointer + 1]));
                 }
                 wordPointer=wordPointer+2;

             }

             //Check whether Nodes 4 and 7 are connected
             Console.Out.WriteLine(quickFindObj.connected(4, 7));

             //Check whether Nodes 0 and 9 are connected
             Console.Out.WriteLine(quickFindObj.connected(0, 9));

             }
             else
             {

             Console.Out.Write("Reconfigure the File path in Program.cs file");

             }
            Console.Read();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            // Note: The text file unions do not make a fully connected tree.
            var pathToSmallFile  = "/home/adam/Downloads/tinyUF.txt";
            var pathToMediumFile = "/home/adam/Downloads/mediumUF.txt";
            var pathToBigFile    = "/home/adam/Downloads/largeUF.txt";

            var qf = new QuickFind(pathToBigFile);

            qf.ConnectFromTextFile();
            Console.WriteLine(qf.ToString());

            // To make a unified array of the small file uncomment the two lines bellow
            qf.Union(8, 1);
            Console.WriteLine(qf.ToString());

            // var qu = new QuickUnion(pathToBigFile);
            // qu.ConnectFromTextFile();

            // var wqu = new WeightedQuickUnion(pathToBigFile);
            // wqu.ConnectFromTextFile();
        }
        private void Button_Print_Click(object sender, RoutedEventArgs e)
        {
            if (connections.Count == 0)
            {
                return;
            }
            int       n  = connections.Count;
            QuickFind qf = new QuickFind(10);

            foreach (Connection con in connections)
            {
                if (qf.Connected(con.Point1, con.Point2))
                {
                    outConnections.Add(con);
                    DrawLine(con);
                    continue;
                }
                qf.Union(con.Point1, con.Point2);
                //System.Diagnostics.Debug.WriteLine($"X:{con.Point1.X},Y:{con.Point1.Y} Union X:{con.Point2.X},Y:{con.Point2.Y}\n");
                //for (int i = 0; i <= 4; i++)
                //{
                //    System.Diagnostics.Debug.Write($"[{i},1]{qf.id[i, 1]}   ");
                //}
                //for (int i = 0; i <= 4; i++)
                //{
                //    System.Diagnostics.Debug.Write($"[{i},0]{qf.id[i, 0]}   ");
                //}
                //System.Diagnostics.Debug.WriteLine("");
            }

            MyOutDataGrid.ItemsSource = outConnections;
            System.Diagnostics.Debug.WriteLine(qf.Count + "Components");

            //foreach(Connection c in connections)
            //{
            //    System.Diagnostics.Debug.WriteLine($"{c.Point1},{c.Point2}");
            //}
        }