Click here to Skip to main content
12,762,714 members (32,501 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
Hi, I'm new to C# and need help with a search algorithm, I have created a windows form application where you select a city from a combobox, this program has to go through all the cities without repeating the same city twice and it should select the shortest route.
It should then display the shortest routes and the total distances on a listbox.

I have the following code so far, please, I need help.

The button1 is what will initialize the search.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CycleTour
    public partial class Form1 : Form
        //Adding the list of arrays to be used
        string[] ArrayCities = new string[7];
        int[] arrDistance1 = new int[7];
        int[] arrDistance2 = new int[6];
        int[] arrDistance3 = new int[5];
        int[] arrDistance4 = new int[5];
        int[] arrDistance5 = new int[3];
        int[] arrDistance6 = new int[2];
        int[] arrDistance7 = new int[1];
        int[] arrDistance8 = new int[1];
        int[] arrBestDis = new int[6];

        //Variables declared
        int counter1 = 0;
        int counter2 = 0;
        int counter3 = 0;
        int counter4 = 0;
        int counter5 = 0;
        int counter6 = 0;
        int input = 0;
        int total = 0;

        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            //List cities index
            ArrayCities[0] = "Aberdeen";
            ArrayCities[1] = "Ayr";
            ArrayCities[2] = "Fort William";
            ArrayCities[3] = "Glasgow";
            ArrayCities[4] = "Inverness";
            ArrayCities[5] = "St Andrews";
            ArrayCities[6] = "Stirling";
            //Add the cities to the combobox

            arrDistance1[0] = 179; //Aberdeen to Ayr
            arrDistance1[1] = 129; //Aberdeen to Edinburgh
            arrDistance1[2] = 157; //Aberdeen to Fort William
            arrDistance1[3] = 146; //Aberdeen to Glasgow
            arrDistance1[4] = 105; //Aberdeen to Inverness
            arrDistance1[5] = 79;  //Aberdeen to St Andrews
            arrDistance1[6] = 119; //Aberdeen to Stirling

            arrDistance2[0] = 79;  //Ayr to Edinburgh
            arrDistance2[1] = 141; //Ayr to Fort William
            arrDistance2[2] = 33;  //Ayr to Glasgow
            arrDistance2[3] = 207; //Ayr to Inverness
            arrDistance2[4] = 118; //Ayr to St Andrews
            arrDistance2[5] = 64;  //Ayr to Stirling

            arrDistance3[0] = 131; //Edinburgh to Fort William
            arrDistance3[1] = 43;  //Edinburgh to Glasgow
            arrDistance3[2] = 154; //Edinburgh to Inverness
            arrDistance3[3] = 50;  //Edinburgh to St Andrews
            arrDistance3[4] = 36;  //Edinburgh to Stirling

            arrDistance4[0] = 116; //Fort William to Glasgow
            arrDistance4[1] = 64;  //Fort William to Inverness, roadworks
            arrDistance4[2] = 74;  //Fort William to Inverness
            arrDistance4[3] = 134; //Fort William to St Andrews
            arrDistance4[4] = 96;  //Fort William to Stirling

            arrDistance5[0] = 175; //Glasgow to Inverness
            arrDistance5[1] = 81;  //Glasgow to St Andrews
            arrDistance5[2] = 27;  //Glasgow to Stirling

            arrDistance6[0] = 145; //Inverness to St Andrews
            arrDistance6[1] = 143; //Inverness to Stirling

            arrDistance7[0] = 52;  //St Andrews to Stirling

            arrDistance8[0] = 0;   //Stirling

        private void button1_Click(object sender, EventArgs e)
            if (comboBox1.SelectedIndex != -1)//== ArrayCities.ToString())
                while(counter1 <= 7)

                if (arrDistance1[counter1] < arrDistance2[counter1])

                    counter1 = counter1 + 1;

                MessageBox.Show("Please select a city", "Select city", MessageBoxButtons.OK, MessageBoxIcon.Information);


        private void button3_Click(object sender, EventArgs e)
            //Creates a MessageBox confirming an exit
            DialogResult result = MessageBox.Show("Would you like to exit the program?", "Continue?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            //if Yes is selected the program will close
            if (result == DialogResult.Yes)
                DialogResult = MessageBox.Show("Thank you for using the program", "Goodbye!", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //if No is selected the program will remain running
            if (result == DialogResult.No)


        private void button4_Click(object sender, EventArgs e)
            comboBox1.SelectedIndex = -1;
            comboBox1.SelectedText = "";
            label6.Text = "Cities";
            label9.Text = "0000";

Posted 14-Oct-12 5:41am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Can I suggest a couple of changes, before you go any further?

Firstly, don't use the Visual Studio default names for anything! When you have an application full of button1, button2, button3, textBox1, textBox2, etc., it is very difficult to work out what is going on. If you use descriptive names instead, then your code is both easier to read, and (generally) more reliable, because you don't use the textbox containing the city name as a departure date! :laugh:

Secondly, Setting up your city - city distances via separate arrays is a bit of a pain for you to work with - it is dfifficult to automate via loops if you =have they all as separate arrays. Consider changing to a two dimensional array, with indexes for cities:

0     1     2
0    0   100   150
1  100     0    50
2  150    50     0
Then when you are looking for distances, it's just cityDistance[city1, city2] rather than having to work out which array to use for city1, then offset into it for city2.

When you have thought about that for a while, you should find it relatively simple to complete your homework! :D
Sergey Alexandrovich Kryukov 14-Oct-12 22:40pm
Monjurul Habib 15-Oct-12 16:41pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You may look at Travelling Salesman Problem at Wikipedia[^] for some insight.
Old "Numerical recipes in C", freely available online[^] shows how to use Simulated Annealing method to deal with such a problem.
Sergey Alexandrovich Kryukov 14-Oct-12 22:40pm
This matter needs some annealing, I guess :-}, a 5.
CPallini 15-Oct-12 3:31am
Thank you.
Monjurul Habib 15-Oct-12 16:41pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 14 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100