I want to list all TSP route combinations.
There are 5 vertices and thus 10 edges:
All the edges are as follows:
edges = [('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'D'), ('C', 'E'), ('D', 'E')]
Note: ('A', 'B')
is the same as ('B', 'A')
, same goes for the other edges.
I want to list all route combinations where you start at A and visit each other number and end at A.
This is what I got so far:
edges = [('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'D'), ('C', 'E'), ('D', 'E')]
x = list(itertools.permutations(['A','B','C','D','E', 'A'], 6))
b = 1
for i in x:
if i[-1] == 'A' and i[0] == 'A':
print(i, b)
b += 1
However, I don't want duplicate routes. How do I sort those out? Eg. A->B->C->A is the same as A->C->B->A, and should not be counted/listed twice.