-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackTracking.py
More file actions
48 lines (43 loc) · 1.43 KB
/
backTracking.py
File metadata and controls
48 lines (43 loc) · 1.43 KB
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
import matplotlib.pyplot as plt
import math
import networkx as nx
from scipy.spatial.distance import *
from util import generateGraph, generateGraph2, pathDistance
def BackTracking(g, A, l, lengthSoFar, minCost, bestSol):
n = len(A)
if(l == n-1):
if(minCost > lengthSoFar + g[A[-1]][A[0]]['weight']):
bestSol = A
minCost = min(minCost, lengthSoFar + g[A[-1]][A[0]]['weight'])
else:
for i in range(l+1, n):
tmp = A[l+1]
A[l+1] = A[i]
A[i] = tmp
newLength = lengthSoFar + g[A[l]][A[l+1]]['weight']
if (newLength >= minCost) :
A[i] = A[l+1]
A[l+1] = tmp
continue
else:
newSol = BackTracking(g, A, l+1, newLength, minCost, bestSol)
if(newSol[0] < minCost):
print(newSol[1])
bestSol = newSol[1]
minCost = min(minCost, newSol[0])
A[i] = A[l+1]
A[l+1] = tmp
return minCost, bestSol
def main() :
maxNode = 5
g = generateGraph2('./instances/test.tsp', 5, maxNode)
A = range(1, maxNode+1)
sol = BackTracking(g, A, 0, 0, float("inf"), [])
print(sol[0])
print(sol[1])
print(pathDistance(g,sol[1]))
print(pathDistance(g, [1, 3, 2, 4, 5]))
nx.draw(g, with_labels=True, font_weight='bold')
plt.show()
if __name__ == "__main__":
main()