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

Feature/travelling salesman #12494

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
76 changes: 76 additions & 0 deletions dynamic_programming/travelling_salesman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
def tsp_dp(distances: list[list[float]]) -> tuple[float, list[int]]:
"""
Solves Traveling Salesman Problem using dynamic programming.

>>> distances = [[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]]
>>> cost, path = tsp_dp(distances)
>>> float(cost)
80.0
>>> path[0]
0

>>> distances = [[0, 5], [5, 0]]
>>> cost, path = tsp_dp(distances)
>>> float(cost)
10.0
>>> path
[0, 1]
"""
if not distances:
raise ValueError("Empty distance matrix")

n = len(distances)
all_points = (1 << n) - 1
dp = {}
parent = {}

def solve(mask: int, pos: int) -> float:

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.py, please provide doctest for the function solve

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.py, please provide doctest for the function solve

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.py, please provide doctest for the function solve

"""
Recursive helper function for solving the TSP using dynamic programming.

:param mask: Bitmask representing visited nodes.
:param pos: Current position in the tour.
:return: Minimum cost to complete the tour.
"""
if mask == all_points:
return distances[pos][0]

state = (mask, pos)
if state in dp:
return dp[state]

minimum = float("inf")
min_next = -1

for next_city in range(n):
if mask & (1 << next_city) == 0:
new_mask = mask | (1 << next_city)
new_dist = distances[pos][next_city] + solve(new_mask, next_city)

if new_dist < minimum:
minimum = new_dist
min_next = next_city

dp[state] = minimum
parent[state] = min_next
return minimum

optimal_cost = solve(1, 0)

path = [0]
mask = 1
pos = 0

for _ in range(n - 1):
next_pos = parent[(mask, pos)]
path.append(next_pos)
mask |= 1 << next_pos
pos = next_pos

return optimal_cost, path


if __name__ == "__main__":
import doctest

doctest.testmod()
16 changes: 16 additions & 0 deletions graphics/butterfly_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def butterfly_pattern(n):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: butterfly_pattern. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file graphics/butterfly_pattern.py, please provide doctest for the function butterfly_pattern

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: butterfly_pattern. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file graphics/butterfly_pattern.py, please provide doctest for the function butterfly_pattern

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: butterfly_pattern. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file graphics/butterfly_pattern.py, please provide doctest for the function butterfly_pattern

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: n

# Upper part
for i in range(1, n + 1):
print("*" * i, end="")
print(" " * (2 * (n - i)), end="")
print("*" * i)

# Lower part
for i in range(n - 1, 0, -1):
print("*" * i, end="")
print(" " * (2 * (n - i)), end="")
print("*" * i)


n = int(input("Enter the size: "))
butterfly_pattern(n)
Loading