This repository contains Python code for solving the Traveling Salesman Problem (TSP) using Christofides' algorithm. This project is part of my International Baccalaureate Mathematics Internal Assessment (Math IA), focusing on determining the most efficient route for a delivery vehicle among a set of addresses.
Christofides' algorithm is employed to find an approximate solution to the TSP. The algorithm is well-regarded for its performance guarantee, offering a solution within 1.5 times the optimal route length. The implementation follows these key steps:
- Graph Construction: Build a complete graph where nodes represent addresses and edges represent distances between them.
- Minimum Spanning Tree (MST): Compute an MST to minimize the sum of edge weights. Using the MSTplotting.py file you can also print a diagram of the Minimum Spanning Tree if it is necessary.
- Perfect Matching: Identify nodes with odd degrees and find a minimum weight perfect matching to achieve an Eulerian graph.
- Eulerian Circuit: Formulate an Eulerian circuit that visits each edge exactly once.
- Hamiltonian Circuit: Convert the Eulerian circuit to a Hamiltonian circuit by avoiding revisits to the already visited nodes.
- Python 3.x
- Libraries:
pandas,networkx,matplotlib
Install the required libraries:
pip install pandas networkx matplotlib- Modify the
file_pathvariable in the script to point to your Excel file containing the distance matrix. - Execute the script to compute the route and visualize the Hamiltonian circuit.
The final output includes a visual representation of the Hamiltonian circuit and the calculation of its total length, displayed through a matplotlib plot.
This project is licensed under the MIT License.
For inquiries or issues, please open an issue on this GitHub repository.