Which Data Structure Is Used in Travelling Salesman Problem?


Heather Bennett

When it comes to solving the Travelling Salesman Problem (TSP), choosing the right data structure is crucial. The TSP is a classic optimization problem where the goal is to find the shortest possible route that visits all given cities and returns to the starting city. This problem is NP-hard, meaning that as the number of cities increases, finding an optimal solution becomes exponentially more difficult.

The Importance of Data Structures in TSP

To efficiently solve the TSP, we need a data structure that can store information about cities and their connections while allowing for quick access and manipulation.

Graphs as a Data Structure

In most cases, graphs are used to represent the connections between cities in the TSP. A graph consists of a set of vertices (cities) and edges (connections between cities). There are two main types of graphs commonly used in TSP:

  • Complete Graph: In a complete graph, each city is connected to every other city. This means that there are n(n-1)/2 edges in a complete graph with n cities.

    Complete graphs provide an accurate representation of the problem but can be computationally expensive for large-scale instances.

  • Sparse Graph: In a sparse graph, only some cities are connected directly to others. Sparse graphs reduce computational complexity by eliminating unnecessary edges but may not accurately represent real-world scenarios.

Adjacency Matrix

An adjacency matrix is a common way to represent graphs in computer science. It uses a 2D matrix to represent connections between vertices. For example, if there are n vertices, then an n x n matrix is used.

In TSP, an adjacency matrix can be used where each cell represents the distance between two cities. If two cities are not connected directly, a special value (such as infinity) can be used to indicate that there is no edge between them.

Adjacency List

Another popular data structure for representing graphs is an adjacency list. In this approach, each vertex maintains a list of its neighboring vertices. For TSP, each city’s adjacency list stores the cities it is connected to along with their corresponding distances.

The adjacency list representation is more memory-efficient than the adjacency matrix for sparse graphs since it only stores information about connected vertices.

Choosing the Right Data Structure

The choice between using an adjacency matrix or an adjacency list depends on the specific requirements of the TSP instance.

If the TSP instance involves a complete graph or dense connections, an adjacency matrix may be more suitable as it allows for constant-time access to any edge. However, this comes at the cost of increased memory usage and slower initialization time due to initializing all possible edges.

On the other hand, if the TSP instance involves a sparse graph with only a few connections per city, an adjacency list would be more efficient in terms of memory usage and initialization time.


In conclusion, choosing the right data structure is crucial when solving the Travelling Salesman Problem. Graphs are commonly used to represent city connections in TSP instances, and both adjacency matrices and adjacency lists have their advantages depending on the problem’s characteristics.

Remember, understanding different data structures and their implications can greatly impact the efficiency and performance of your TSP solution.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy