Open DamianJaskolski95 opened 2 years ago
I presented example of creating all different paths:
0 1 2 3 0
0 1 3 2 0
0 2 1 3 0
0 2 3 1 0
0 3 1 2 0
0 3 2 1 0
in https://github.com/matthias152/algorithms/tree/permutations branch. It's called permutations in math.
Create new directory for TSP algorithms. First one will be brute force up to 8 nodes (cities). Theory of TSP: https://pl.wikipedia.org/wiki/Problem_komiwoja%C5%BCera https://en.wikipedia.org/wiki/Travelling_salesman_problem
How to approach the problem:
You need to create 'list of lists' (list[n][n]) where you can store values of passing from one node (city) to another nodes (cities). You can see structure in tsp_data/atsp/test4.atps or other files there. First line is number of cities (n). Then you can create list of lists (list[n][n]). In the above case its list[4][4]. Its best to implement reading from file, but you can generate this data. Value form city 1 to 1 is 0 (2 to 2 is also 0, 3 to 3 is 0, etc.)
You will need list[n] (list[4]) with boolean values, to track what city you already visited. You will start in first city, so list[0] always will be 0.
You will need current path, so another list[n] with number of the city you need to visit in order.
Remember to always count coming back from last city to first (value of list[x][0]).
Pseudocode:
Above pseudocode will be valid to most of algorithms. Pseudocode for brute force:
Amount of different paths will be (n-1)! In this case its (4-1)! = 6
So you need to count values for all these paths to find the best. In Brute Force there is no need to keep track of visited cities, since you will check all paths anyway.