Exemplo n.º 1
0
        public static PairPoint brutforce(int N, List <Point> points)
        {
            PairPoint pp       = new PairPoint();
            double    distance = Double.MaxValue;

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    PairPoint temp = new PairPoint();
                    temp.Point1 = points[i];
                    temp.Point2 = points[j];
                    if (temp.Distance() < distance)
                    {
                        distance = temp.Distance();
                        pp       = temp;
                    }
                    //double currDistance = Math.Sqrt(Math.Pow(points[i].X - points[j].X, 2) + Math.Pow(points[i].Y - points[j].Y, 2));
                    //if(currDistance<distance)
                    //{
                    //    distance = currDistance;
                    //    pp.Point1 = points[i];
                    //    pp.Point2 = points[j];
                    //}
                }
            }
            return(pp);
        }
        private PairPoint MyClosestRec(List <Point> sortedByX)
        {
            int count = sortedByX.Count;

            if (count <= 4)
            {
                return(Program.brutforce(count, sortedByX));
            }
            var leftByX     = sortedByX.Take(count / 2).ToList();
            var leftResult  = MyClosestRec(leftByX);
            var rightByX    = sortedByX.Skip(count / 2).ToList();
            var rightResult = MyClosestRec(rightByX);
            var result      = rightResult.Distance() < leftResult.Distance() ? rightResult : leftResult;
            var midX        = leftByX.Last().X;
            var bandWidth   = result.Distance();
            var inBandByX   = sortedByX.Where(p => Math.Abs(midX - p.X) <= bandWidth);
            var inBandByY   = inBandByX.OrderBy(p => p.Y).ToArray();
            int last        = inBandByY.Length - 1;

            for (int i = 0; i < last; i++)
            {
                var point1 = inBandByY[i];
                for (int j = i + 1; j <= last; j++)
                {
                    var point2 = inBandByY[j];
                    if ((point1.Y - point2.Y) >= result.Distance())
                    {
                        break;
                    }
                    PairPoint temp = new PairPoint();
                    temp.Point1 = point1;
                    temp.Point2 = point2;
                    if (temp.Distance() < result.Distance())
                    {
                        result = temp;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            int[]        iters    = { 5, 10, 20, 30, 100, 1000 };
            List <Point> pointsst = init(1000);
            PairPoint    ppst     = brutforce(1000, pointsst);

            Console.WriteLine("Алгоритм грубой силы");
            Console.WriteLine("Время работы алгоритма");
            Console.WriteLine("Первая точка");
            Console.WriteLine("(" + ppst.Point1.X + "," + ppst.Point1.Y + ")");
            Console.WriteLine("Вторая точка");
            Console.WriteLine("(" + ppst.Point2.X + "," + ppst.Point2.Y + ")");
            Console.WriteLine("Расстояние между точками");
            Console.WriteLine(ppst.Distance());
            Console.ReadLine();
            DivideAndConquer dacst = new DivideAndConquer(1000, pointsst);
            PairPoint        pp1st = dacst.MyClosestDivide();

            Console.WriteLine("Алгоритм декомпозиции");
            Console.WriteLine("Время работы алгоритма");
            Console.WriteLine("Первая точка");
            Console.WriteLine("(" + pp1st.Point1.X + "," + pp1st.Point1.Y + ")");
            Console.WriteLine("Вторая точка");
            Console.WriteLine("(" + pp1st.Point2.X + "," + pp1st.Point2.Y + ")");
            Console.WriteLine("Расстояние между точками");
            Console.WriteLine(pp1st.Distance());
            Console.ReadLine();
            foreach (int i in iters)
            {
                for (int j = 0; j < 100; j++)
                {
                    //List<Point> points = new List<Point>();
                    //Point point1 = new Point();
                    //point1.X = 3;
                    //point1.Y = 1;
                    //points.Add(point1);
                    //Point point2 = new Point();
                    //point2.X = 4;
                    //point2.Y = 2;
                    //points.Add(point2);
                    //Point point3 = new Point();
                    //point3.X = 5;
                    //point3.Y = 6;
                    //points.Add(point3);
                    //Point point4 = new Point();
                    //point4.X = 7;
                    //point4.Y = 8;
                    //points.Add(point4);
                    //Point point5 = new Point();
                    //point5.X = 6;
                    //point5.Y = 4;
                    //points.Add(point5);
                    List <Point> points = init(i);
                    Stopwatch    timer  = new Stopwatch();
                    timer.Start();
                    PairPoint pp = brutforce(i, points);
                    timer.Stop();
                    Console.WriteLine("Алгоритм грубой силы");
                    long time = timer.ElapsedMilliseconds;
                    Console.WriteLine("Время работы алгоритма");
                    Console.WriteLine(time);
                    Console.WriteLine("Первая точка");
                    Console.WriteLine("(" + pp.Point1.X + "," + pp.Point1.Y + ")");
                    Console.WriteLine("Вторая точка");
                    Console.WriteLine("(" + pp.Point2.X + "," + pp.Point2.Y + ")");
                    Console.WriteLine("Расстояние между точками");
                    Console.WriteLine(pp.Distance());
                    DivideAndConquer dac = new DivideAndConquer(i, points);
                    timer.Reset();
                    timer.Start();
                    PairPoint pp1 = dac.MyClosestDivide();
                    timer.Stop();
                    long time2 = timer.ElapsedMilliseconds;
                    Console.WriteLine("Алгоритм декомпозиции");
                    Console.WriteLine("Время работы алгоритма");
                    Console.WriteLine(time2);
                    Console.WriteLine("Первая точка");
                    Console.WriteLine("(" + pp1.Point1.X + "," + pp1.Point1.Y + ")");
                    Console.WriteLine("Вторая точка");
                    Console.WriteLine("(" + pp1.Point2.X + "," + pp1.Point2.Y + ")");
                    Console.WriteLine("Расстояние между точками");
                    Console.WriteLine(pp1.Distance());
                }
                Console.ReadLine();
            }
        }