示例#1
0
        public static void PerformConcKruskalWithThreads(string FileName, int M)
        {
            string[] input     = File.ReadAllText(FileName).Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            int      EdgeCount = input.Length;

            Thread[] threads = new Thread[M];

            for (int i = 0; i < M; i++)
            {
                threads[i] = new Thread(ThreadTaskWithThread);
            }

            buffer = new Kruskal[M];
            int ind1, ind2;

            for (int i = 0; i < M; i++)
            {
                Object[] ThreadInput = new object[2];
                ThreadInput[0] = i;
                if (i == 0)
                {
                    ind1 = 0;
                }
                else
                {
                    ind1 = EdgeCount / M * i;
                }
                if (i == M - 1)
                {
                    ind2 = EdgeCount;
                }
                else
                {
                    if ((EdgeCount / M * (i + 1)) > EdgeCount)
                    {
                        ind2 = EdgeCount;
                    }
                    else
                    {
                        ind2 = EdgeCount / M * (i + 1);
                    }
                }



                string[] tmpMas = new string[ind2 - ind1];
                for (int j = 0; j < tmpMas.Length; j++)
                {
                    tmpMas[j] = input[ind1 + j];
                }
                ThreadInput[1] = tmpMas;


                threads[i].Start(ThreadInput);


                //  input.CopyTo(tmpMas, 0);
                // ThreadInput[4] = tmpMas;
            }
            for (int i = 0; i < M; i++)
            {
                threads[i].Join();
            }


            MergeKruskal k = new MergeKruskal(buffer);



            k.BuildSpanningTree();
            Console.WriteLine("Cost: " + k.Cost);
            // k.DisplayInfo();
        }
示例#2
0
        public static void PerformConcKruskal(string FileName, int M)
        {
            string [] input     = File.ReadAllText(FileName).Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            int       EdgeCount = input.Length;

            ManualResetEvent[] events = new ManualResetEvent[M];

            for (int i = 0; i < M; i++)
            {
                events[i] = new ManualResetEvent(false);
            }

            buffer = new Kruskal[M];
            int ind1, ind2;

            for (int i = 0; i < M; i++)
            {
                Object[] ThreadInput = new object[3];
                ThreadInput[0] = i;
                if (i == 0)
                {
                    ind1 = 0;
                }
                else
                {
                    ind1 = EdgeCount / M * i;
                }
                if (i == M - 1)
                {
                    ind2 = EdgeCount;
                }
                else
                {
                    if ((EdgeCount / M * (i + 1)) > EdgeCount)
                    {
                        ind2 = EdgeCount;
                    }
                    else
                    {
                        ind2 = EdgeCount / M * (i + 1);
                    }
                }


                ThreadInput[2] = events[i];
                string[] tmpMas = new string[ind2 - ind1];
                for (int j = 0; j < tmpMas.Length; j++)
                {
                    tmpMas[j] = input[ind1 + j];
                }
                ThreadInput[1] = tmpMas;


                ThreadPool.QueueUserWorkItem(ThreadTask, ThreadInput);


                //  input.CopyTo(tmpMas, 0);
                // ThreadInput[4] = tmpMas;
            }
            WaitHandle.WaitAll(events);

            MergeKruskal k = new MergeKruskal(buffer);

            k.BuildSpanningTree();
            Console.WriteLine("Cost: " + k.Cost);
            //   k.DisplayInfo();
        }