-
Notifications
You must be signed in to change notification settings - Fork 2
/
NearestNeighbour.cs
66 lines (61 loc) · 2.42 KB
/
NearestNeighbour.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Travelling_SalesMan_Problem
{
class NearestNeighbour
{
public ShortestPath display;
public NearestNeighbour(ShortestPath display)
{
this.display = display;
}
public Route FindShortestRoute(ArrayList cities)//calculate the total distance
{
ArrayList ShortestRouteCities = new ArrayList();
City city = new City("Bahria University", 24.893277, 67.08818145);// cities will be picked from here
updatesRoutes(ShortestRouteCities, cities, city);
while (cities.Count >= 1)
{
city = getNextCity(cities, city);
updatesRoutes(ShortestRouteCities, cities, city);
}
printShortestRout(ShortestRouteCities);
return new Route(ShortestRouteCities);
}
private void printShortestRout(ArrayList shortestRouteCities)
{
this.display.output.Text = null;
foreach (City city in shortestRouteCities)// iterrate on cities to print next name of city
{
this.display.output.Text = this.display.output.Text + city.getName() + "\n";
}
this.display.output.Text = this.display.output.Text + "Bahria University" + "\n";
}
private void updatesRoutes(ArrayList shortestRouteCities, ArrayList cities, City city)//we update the list after picking any city from the list
{
shortestRouteCities.Add(city);
cities.Remove(city);
}
private City getNextCity(ArrayList cities, City city) // find the minimum distance from list of cities and pick the city with minimum distance.
{
int min_index = 0;
double minDistance = city.measureDistance((City)cities[0]);
for (int row = 0; row < cities.Count - 1; row++)
{
double minDistanceNext = city.measureDistance((City)cities[row + 1]);
if (minDistanceNext == 0)
continue;
if (minDistance == 0 || minDistanceNext < minDistance)
{
min_index = row + 1;
minDistance = minDistanceNext;
}
}
return (City)cities[min_index];
}
}
}