Skip to content

Commit c7be8c0

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 345d58f commit c7be8c0

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

graphs/tests/test_traveling_salesman_problem.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ def sample_graph_2() -> list[list[int]]:
1818
[20, 25, 30, 0],
1919
]
2020

21+
2122
def test_brute_force() -> None:
2223
graph = sample_graph_1()
2324
assert tsp_brute_force(graph) == 64
2425

26+
2527
def test_dp() -> None:
2628
graph = sample_graph_1()
2729
assert tsp_dp(graph) == 64
2830

31+
2932
def test_greedy() -> None:
3033
graph = sample_graph_1()
3134
# The greedy algorithm does not guarantee an optimal solution;
@@ -35,6 +38,7 @@ def test_greedy() -> None:
3538
assert isinstance(result, int)
3639
assert result >= 64
3740

41+
3842
def test_dp_larger_graph() -> None:
3943
graph = sample_graph_2()
4044
assert tsp_dp(graph) == 80
@@ -44,6 +48,7 @@ def test_brute_force_larger_graph() -> None:
4448
graph = sample_graph_2()
4549
assert tsp_brute_force(graph) == 80
4650

51+
4752
def test_greedy_larger_graph() -> None:
4853
graph = sample_graph_2()
4954
result = tsp_greedy(graph)

graphs/traveling_salesman_problem.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ def tsp_greedy(graph: list[list[int]]) -> int:
104104
for _ in range(n - 1):
105105
# Find the nearest city to the current location that has not been visited.
106106
next_city = min(
107-
((city, cost) for city, cost in enumerate(graph[current]) if not visited[city] and city != current),
107+
(
108+
(city, cost)
109+
for city, cost in enumerate(graph[current])
110+
if not visited[city] and city != current
111+
),
108112
key=lambda cost: cost[1],
109-
default=(None, float('inf'))
113+
default=(None, float("inf")),
110114
)[0]
111115

112116
# If no such city exists, break the loop.

0 commit comments

Comments
 (0)