forked from petkofff/p_vs_np_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnearest_neighbour.py
54 lines (44 loc) · 1.57 KB
/
nearest_neighbour.py
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
import json
# Nearest neighbour algorithm (greedy algorithm) done non-recursively
# It doesn't guarantee a solution, will return True if solution is found,
# otherwise will return False
def shortest_path_nn(firstNode, cities, path = []):
# use cities by value, not by referance
cities = dict(cities)
for city in cities:
cities[city] = dict(cities[city])
distance = 0
# append first node
path.append(firstNode)
while len(path) < len(cities):
# choose nearest nonvisited city
if cities[path[-1]]:
nearestCity = min(cities[path[-1]], key=cities[path[-1]].get)
else:
return False
while nearestCity in path:
del cities[path[-1]][nearestCity]
if cities[path[-1]]:
nearestCity = min(cities[path[-1]], key=cities[path[-1]].get)
else:
return False
# if such city is found update distance and path
distance += cities[path[-1]][nearestCity]
path.append(nearestCity)
if firstNode in cities[path[-1]]:
distance += cities[path[-1]][firstNode]
path.append(firstNode)
else:
return False
print("Solution found: ", path)
print("Distance: ", distance)
return True
if __name__ == '__main__':
#load cities
with open('cities.json') as json_data:
cities = json.load(json_data)
print("Nearest neighbour algorithm:")
for city in cities:
print("Start: ", city)
if not shortest_path_nn(city, cities, []):
print("Solution not found")