Skip to content
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

Closed
wants to merge 30 commits into from
Closed
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
63e4798
Create travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
92e121f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
b2d0c34
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
d76d039
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
7f43fa4
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
f178fa9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
b82d1b4
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
4013b48
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
5ad7c44
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
90d830a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
bacc47c
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
1b31c73
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
801f476
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
dba4f07
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
7815581
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
33dbb7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
e9adddd
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
1769cb9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
9d8a653
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
5d02d22
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
6de700e
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
006a79e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
be956ab
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
612b116
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
acf647e
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
20c297f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
a1bd736
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
32f76b3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
ce0f65e
Update travelling_salesman_problem.py
OmMahajan29 Oct 10, 2024
4bf30ce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions dynamic_programming/travelling_salesman_problem.py
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:

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 function tsp

"""
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:

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

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 function visit

"""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()
Loading