-
-
Notifications
You must be signed in to change notification settings - Fork 46.2k
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
base: master
Are you sure you want to change the base?
Feature/travelling salesman #12494
Changes from all commits
d2b0527
d8a7179
459613e
a9703e1
e95c476
f3379c3
39fbc03
aa46f1a
df3b00e
5fb415f
9799ea5
1605f55
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,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: | ||
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 |
||
""" | ||
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def butterfly_pattern(n): | ||
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. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: Please provide type hint for the parameter: 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. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: Please provide type hint for the parameter: 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. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: Please provide type hint for the parameter: |
||
# 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) |
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.py
, please provide doctest for the functionsolve