Optimizing length of network cable

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Collections;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.Write("Enter number of computers to connect: ");

            int num = int.Parse(Console.ReadLine());

            int[,] coordinates = new int[2, num];

            int[] pointer = new int[num];

            int[] finalPointer = new int[num];

            double length = 0;

            for (int i = 0; i < num; i++)

            {

                Console.Write("Enter coordinates for computer" + (i + 1) + ": ");

                string str = Console.ReadLine();

                string[] tempStr = str.Split(‘ ‘);

                coordinates[0, i] = int.Parse(tempStr[0]);

                coordinates[1, i] = int.Parse(tempStr[1]);

            }

            //Initializing the pointer array

            for (int i = 0; i < num; i++)

            {

                pointer[i] = i;

            }

            pointer[num – 1]–;

            //Permutation loop

            do

            {

                int i = num – 1;

            label1:

                pointer[i]++;

                if (pointer[i] == num && i != 0)

                {

                    pointer[i] = 0;

                    i–;

                    goto label1;

                }

                else if (pointer[i] == num && i == 0)

                {

                    break;

                }

                else

                {

                    //Check pointer validity

                    int[] counter = new int[num];

                    int flag = 0;

                    for (i = 0; i < num; i++)

                    {

                        counter[pointer[i]]++;

                        if (counter[pointer[i]] > 1)

                        {

                            flag = 1;

                            break;

                        }

                    }

                    if (flag == 0)

                    {

                        double temp = 0;

                        for (i = 0; i < num – 1; i++)

                        {

                            temp = temp + distance(coordinates[0, pointer[i]], coordinates[1, pointer[i]], coordinates[0, pointer[i + 1]], coordinates[1, pointer[i + 1]]);

                        }

                        if (length == 0)

                        {

                            length = temp;

                            for (i = 0; i < num; i++)

                            {

                                finalPointer[i] = pointer[i];

                            }

                        }

                        else if(length > temp)

                        {

                            length = temp;

                            for (i = 0; i < num; i++)

                            {

                                finalPointer[i] = pointer[i];

                            }

                        }

                    }

                }

            } while (pointer[0] != num);

            Console.WriteLine("Sequence of computers:");

            length = 0;

            for (int i = 0; i < num – 1; i++)

            {

                Console.WriteLine("Cable requirement to connect (" + coordinates[0, finalPointer[i]].ToString() + "," + coordinates[1, finalPointer[i]].ToString() + ") to (" + coordinates[0, finalPointer[i + 1]].ToString() + "," + coordinates[1, finalPointer[i + 1]].ToString() + ") is " + (distance(coordinates[0, finalPointer[i]], coordinates[1, finalPointer[i]], coordinates[0, finalPointer[i + 1]], coordinates[1, finalPointer[i+1]]) + 16) + " units");

                length = length + distance(coordinates[0, finalPointer[i]], coordinates[1, finalPointer[i]], coordinates[0, finalPointer[i + 1]], coordinates[1, finalPointer[i + 1]]) + 16;

            }

            Console.WriteLine("Optimized length of the wire: " + length);

        }

        public static double distance(double x, double y, double x1, double y1)

        {

            return (Math.Sqrt(((x – x1) * (x – x1)) + (y – y1) * (y – y1)));

        }

    }

}

 

Enter number of computers to connect: 6
Enter coordinates for computer1: 5 19
Enter coordinates for computer2: 55 28
Enter coordinates for computer3: 38 101
Enter coordinates for computer4: 28 62
Enter coordinates for computer5: 111 84
Enter coordinates for computer6: 43 116
Sequence of computers:
Cable requirement to connect (5,19) to (55,28) is 66.8035431835222 units
Cable requirement to connect (55,28) to (28,62) is 59.4165866921848 units
Cable requirement to connect (28,62) to (38,101) is 56.2616442783948 units
Cable requirement to connect (38,101) to (43,116) is 31.8113883008419 units
Cable requirement to connect (43,116) to (111,84) is 91.1531769122237 units
Optimized length of the wire: 305.446339367167
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s