# tsp

TSP or "Travelling salesman problem" is one of the well-known problems in graph theory. The goal of the problem is to find the shortest route that visits each node once, starting and finishing from the same node, given the distance between each one of them. It is an NP-hard problem in optimization and therefore there exists no exact solution. Here implemented are trivial, greedy and k-approx methods that find the solution within a k-bound of the optimal one. That means that solution is not going to be more than k times worse than the best possible. The algorithm uses the distance calculator to determine the distance between points, and works only with geographical locations, meaning each node needs to have its lat and lng property.

(location:Location {lat: 44.1194, lng: 15.2314}) TraitValue
Module typemodule
ImplementationPython
Graph directionundirected
Edge weightsunweighted
Parallelismsequential
Too slow?

If this algorithm implementation is too slow for your use case, contact us and request a rewrite to C++ !

## Procedures​

info

If you want to execute this algorithm on graph projections, subgraphs or portions of the graph, be sure to check out the guide on How to run a MAGE module on subgraphs.

### solve(points, method)​

#### Input:​

• points: List[Vertex] ➡ List of points to calculate TSP on. Required to have lng and lat properties.
• method: string (default=1.5_approx) ➡ Method used for optimization. Can be either 1.5_approx, 2_approx or greedy

#### Output:​

• sources: List[Vertex] ➡ List of elements from 1st to (n-1)-th element
• destinations: List[Vertex] ➡ List of elements from 2nd to n-th element The pairs of them represent individual edges between 2 nodes in the graph.

#### Usage:​

MATCH (n:Location)WITH COLLECT(n) as locationsCALL tsp_module.solve(points) YIELD sources, destinations;

## Example​ 