# Trying to write my own Greedy/ Nearest Neighbor Algorithm

So I'm trying to write my own function and compare it to the results I have from using FindShortestTour.

Does anyone know how to find the distance from each city to each of the others without tedious code. I have 50 cities and I want to find the distance between all the cities so then I can write a Greedy Algorithm.

I know I have to use GeoDistance but I'm not sure how to do all 50 cities.

These are the cities I have:

{GeoPosition[{18.96, 72.82}], GeoPosition[{12.97, 77.56}], GeoPosition[{27.19, 78.01}], GeoPosition[{19.89, 75.32}], GeoPosition[{24.85, 79.93}], GeoPosition[{32.71, 74.85}], GeoPosition[{15.42, 73.78}], GeoPosition[{26.92, 75.8}], GeoPosition[{24.58, 73.69}], GeoPosition[{26.92, 70.9}], GeoPosition[{34.17, 77.58}], GeoPosition[{32.27, 77.17}], GeoPosition[{31.1033, 77.1722}], GeoPosition[{27.34, 88.61}], GeoPosition[{27.05, 88.26}], GeoPosition[{8.078, 77.541}], GeoPosition[{29.98, 78.16}], GeoPosition[{29.38, 79.45}], GeoPosition[{28.6, 77.22}], GeoPosition[{30.34, 78.05}], GeoPosition[{31.64, 74.87}], GeoPosition[{27.1, 77.67}], GeoPosition[{17.4, 78.48}], GeoPosition[{12.88, 74.84}], GeoPosition[{25.57, 91.87}], GeoPosition[{17.73, 83.3}], GeoPosition[{30.73, 79.07}], GeoPosition[{13.63, 79.41}], GeoPosition[{10.23, 77.48}], GeoPosition[{22.57, 88.36}], GeoPosition[{18.98, 73.27}], GeoPosition[{25.32, 83.01}], GeoPosition[{17.92, 73.67}], GeoPosition[{24.71, 84.98}], GeoPosition[{26.85, 80.92}], GeoPosition[{9.92, 78.12}], GeoPosition[{23.17, 79.94}], GeoPosition[{24.6, 72.7}], GeoPosition[{29.4, 79.12}], GeoPosition[{11.4117, 76.6947}], GeoPosition[{8.51, 76.95}], GeoPosition[{13.09, 80.27}], GeoPosition[{12.3, 76.6}], GeoPosition[{13.21, 75.99}], GeoPosition[{9.61, 77.15}], GeoPosition[{10.08, 77.0597}], GeoPosition[{15.33, 76.46}], GeoPosition[{12.62, 80.1994}], GeoPosition[{26.35, 92.67}], GeoPosition[{11.58, 75.59}]}

Thanks

• This question appear to relate to another one; which "50 cities" are you referring to? – Mr.Wizard Apr 21 '15 at 16:05
• @DanielLichtblau I tried to make a do loop in which I tell the program to look at what the nearest location is and then connect to it, if the nearest location is already a location that has been visited then to go to the next nearest location. however, I am unable to connect it to the geoPostitions and geoDistances – Sarah Apr 22 '15 at 2:41

If you want the minimal set of distances start with Subsets:

cities = {Entity["City", {"Bombay", "Maharashtra", "India"}],
Entity["City", {"Bengaluru", "Karnataka", "India"}],

# -> GeoDistance @@ # & /@ Subsets[cities, {2}]


For a complete set try Tuples or Outer.
Outer produces an extra level of bracketing that may be useful.

# -> GeoDistance @@ # & /@ Tuples[cities, {2}]

Outer[{##} -> GeoDistance[##] &, cities, cities]


• Thanks for the info! I just put all my GeoPositions in the info. Well I'm trying to create an algorithm to find the best route so i would want to know all the distances between each city and then I want it to choose the closest city and continue to iterate – Sarah Apr 21 '15 at 16:30
• I got all the distances for the cities. I'm going to write a do loop so it chooses the nearest city – Sarah Apr 21 '15 at 16:39
• @Sarah Consider possibly sorting the table, e.g. dist = Outer[{##} -> GeoDistance[##] &, cities, cities]; then SortBy[QuantityMagnitude@*Last] /@ dist – Mr.Wizard Apr 21 '15 at 16:53