Travelling Salesman Problem

Given a list of cities numbered from 0 to N-1 and a matrix DISTANCE consisting of 'N' rows and 'N' columns, representing the distances between each pair of cities, find the shortest possible route that visits each city exactly once and returns to the starting city.

Input:

The first line contains a single integer 'T' denoting the number of test cases. Each test case is as follows:

The first line contains an integer 'N', representing the number of cities.
The next 'N' lines contain 'N' space-separated integers 'DISTANCE[i][j]', where DISTANCE[i][j] denotes the distance from the ith city to the jth city.

Output:

For each test case, return the minimum distance of the shortest possible route that visits each city exactly once and returns to the starting city.

Example:

Input:
2
3
0 10 15
10 0 20
15 20 0
4
0 29 20 21
29 0 15 17
20 15 0 28
21 17 28 0

Output:
35
60

Constraints:

  • 1 <= T <= 5
  • 2 <= N <= 16
  • 0 <= DISTANCE[i][j] <= 109
  • Time Limit: 1 sec
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
AnswerBot
2mo

The Traveling Salesman Problem seeks the shortest route visiting each city once and returning to the start.

  • Brute Force Approach: Check all permutations of cities to find the shortest path, but this is...read more

Help your peers!
Select
Add answer anonymously...

American Express Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Left View of a Binary Tree Problem Statement Given a binary tree, your task is t...read more
A Software Developer Intern was asked Q. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
A Software Developer Intern was asked Q. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Left View of a Binary Tree Problem Statement Given a binary tree, your task is t...read more
A Software Developer Intern was asked Q2. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
A Software Developer Intern was asked Q3. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more
American Express Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits