Skip to content

Add Traveling Salesman Problem Algorithms And Tests #12820

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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
10b1a2a
Add Traveling Salesman Problem algorithms and tests
MapleBauhinia Jul 5, 2025
e39c3ce
Add TSP Problem
MapleBauhinia Jul 5, 2025
c32a022
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2025
85f1401
Fix: TSP rename lambda parameter and add type hints
MapleBauhinia Jul 5, 2025
345d58f
add-tsp-problem
MapleBauhinia Jul 5, 2025
c7be8c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2025
a766f38
Fix: format and pass all tests
MapleBauhinia Jul 5, 2025
ed8b6e3
add-tsp-problem
MapleBauhinia Jul 5, 2025
13df43e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2025
7bd83fd
Standardize output to int
MapleBauhinia Jul 5, 2025
81fcb2f
Merge branch 'add-tsp-problem' of https://github.com/MapleBauhinia/Py…
MapleBauhinia Jul 5, 2025
80cc148
Fix: Build PR
MapleBauhinia Jul 5, 2025
cef217a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2025
f61f7cf
Fix: format
MapleBauhinia Jul 5, 2025
3042b37
Merge branch 'add-tsp-problem' of https://github.com/MapleBauhinia/Py…
MapleBauhinia Jul 5, 2025
9cc0448
Fix: tsp-greedy
MapleBauhinia Jul 5, 2025
9c9a3e4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2025
4c59775
Fix: ruff check
MapleBauhinia Jul 5, 2025
d913580
Merge branch 'add-tsp-problem' of https://github.com/MapleBauhinia/Py…
MapleBauhinia Jul 5, 2025
86be481
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2025
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
Prev Previous commit
Next Next commit
Standardize output to int
  • Loading branch information
MapleBauhinia committed Jul 5, 2025
commit 7bd83fd8d045cb777e569026aa23a86869626666
6 changes: 3 additions & 3 deletions graphs/traveling_salesman_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def tsp_brute_force(graph: list[list[int]]) -> int:
total_cost = sum(graph[path[i]][path[i + 1]] for i in range(n))
min_path = min(min_path, total_cost)

return min_path
return int(min_path)


def tsp_dp(graph: list[list[int]]) -> int:
Expand Down Expand Up @@ -78,7 +78,7 @@ def tsp_dp(graph: list[list[int]]) -> int:

# After completing visits to all cities,
# return to city 0 and obtain the minimum value.
return min(dp[(1 << n) - 1][i] + graph[i][0] for i in range(1, n))
return int(min(dp[(1 << n) - 1][i] + graph[i][0] for i in range(1, n)))


def tsp_greedy(graph: list[list[int]]) -> int:
Expand Down Expand Up @@ -132,7 +132,7 @@ def tsp_greedy(graph: list[list[int]]) -> int:
total_cost += graph[current][0]
path.append(0)

return total_cost
return int(total_cost)


def test_tsp_example() -> None:
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy