Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df3b00e

Browse files
committedJan 2, 2025·
Update Traveling Salesman Problem implementation with detailed doctest and solve function comments
1 parent aa46f1a commit df3b00e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed
 

‎dynamic_programming/travelling_salesman.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22

3-
43
def tsp_dp(distances: list[list[float]]) -> tuple[float, list[int]]:
54
"""
65
Solves Traveling Salesman Problem using dynamic programming.
@@ -28,6 +27,13 @@ def tsp_dp(distances: list[list[float]]) -> tuple[float, list[int]]:
2827
parent = {}
2928

3029
def solve(mask: int, pos: int) -> float:
30+
"""
31+
Recursive helper function for solving the TSP using dynamic programming.
32+
33+
:param mask: Bitmask representing visited nodes.
34+
:param pos: Current position in the tour.
35+
:return: Minimum cost to complete the tour.
36+
"""
3137
if mask == all_points:
3238
return distances[pos][0]
3339

0 commit comments

Comments
 (0)
Please sign in to comment.