MRo47 / algorithms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Algorithms

When you have no life other than solving these on weekends.

Travelling salesman problem (TSP)

| C | O | O | C |
| O | S | O | O |
| O | O | O | H |
| C | O | O | O |

Finding the optimal path (minimum distance) a Salesman (S) has to travel between multiple cities (C) in a grid before getting home (H).

Solved using dynamic programming approach where a solution is guranteed. The python code should be able to solve for any grid size, for any number of cities.

The names are mapped to integer values for keeping the solution general to any such problem.

The problem can be represented as an undirected weighted graph since every city is connected to every other city where the cities can be represented as nodes of the graph and the distances between them as the weights.

Graph key

S = 1

H = 2

Cities (C) = 3,4,5

tsp_graph

The solution can then be formulated as below.

About


Languages

Language:Python 100.0%