-
-
Notifications
You must be signed in to change notification settings - Fork 46.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create travelling_salesman_problem.py #11939
Changes from 25 commits
63e4798
92e121f
b2d0c34
d76d039
7f43fa4
f178fa9
b82d1b4
4013b48
5ad7c44
90d830a
bacc47c
1b31c73
801f476
dba4f07
7815581
33dbb7b
e9adddd
1769cb9
9d8a653
5d02d22
6de700e
006a79e
be956ab
612b116
acf647e
20c297f
a1bd736
32f76b3
ce0f65e
4bf30ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python3 | ||
|
||
|
||
def tsp(distances: list[list[int]]) -> int: | ||
""" | ||
Solves the Travelling Salesman Problem (TSP) | ||
using dynamic programming and bitmasking. | ||
Args: | ||
distances: 2D list where distances[i][j] | ||
is the distance between city i and city j. | ||
Returns: | ||
Minimum cost to complete the | ||
tour visiting all cities. | ||
Raises: | ||
ValueError: If any distance is negative. | ||
|
||
>>> tsp([[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], | ||
[20, 25, 30, 0]]) | ||
80 | ||
>>> tsp([[0, 29, 20, 21], [29, 0, 15, 17], [20, 15, 0, 28], | ||
[21, 17, 28, 0]]) | ||
69 | ||
>>> tsp([[0, 10, -15, 20], [10, 0, 35, 25], [15, 35, 0, 30], | ||
[20, 25, 30, 0]]) # doctest: +ELLIPSIS | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Distance cannot be negative | ||
""" | ||
n = len(distances) | ||
if any(distances[i][j] < 0 for i in range(n) for j in range(n)): | ||
raise ValueError("Distance cannot be negative") | ||
# Memoization table | ||
memo = [[-1] * (1 << n) for _ in range(n)] | ||
visited_all = (1 << n) - 1 # All cities visited mask | ||
|
||
def visit(city: int, mask: int) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
"""Recursively calculates the minimum cost to visit all cities.""" | ||
if mask == visited_all: | ||
return distances[city][0] # Return to the starting city | ||
if memo[city][mask] != -1: # Return cached result if exists | ||
return memo[city][mask] | ||
min_cost = float("inf") # Use infinity for initial comparison | ||
for next_city in range(n): | ||
if not (mask & (1 << next_city)): # If unvisited | ||
new_cost = distances[city][next_city] + visit( | ||
next_city, mask | (1 << next_city) | ||
) | ||
min_cost = min(min_cost, new_cost) | ||
memo[city][mask] = int(min_cost) # Store result as an integer | ||
return memo[city][mask] # Return the cached result | ||
return visit(0, 1) # Start from city 0 with city 0 visited | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
dynamic_programming/travelling_salesman_problem.py
, please provide doctest for the functiontsp