Skip to content

Commit 9cc0448

Browse files
committed
Fix: tsp-greedy
1 parent 3042b37 commit 9cc0448

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

graphs/traveling_salesman_problem.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ def tsp_greedy(graph: list[list[int]]) -> int:
9696
Example:
9797
>>> tsp_greedy([[0, 29, 20], [29, 0, 15], [20, 15, 0]])
9898
64
99-
>>> tsp_greedy([
100-
[0, 10, 15, 20],
101-
[10, 0, 35, 25],
102-
[15, 35, 0, 30],
103-
[20, 25, 30, 0]
104-
])
105-
80
10699
"""
107100
n = len(graph)
108101
visited = [False] * n # Mark whether each city has been visited.
@@ -143,27 +136,22 @@ def tsp_greedy(graph: list[list[int]]) -> int:
143136

144137

145138
def test_tsp_example() -> None:
146-
graph = [
147-
[0, 10, 15, 20],
148-
[10, 0, 35, 25],
149-
[15, 35, 0, 30],
150-
[20, 25, 30, 0],
151-
]
139+
graph = [[0, 29, 20], [29, 0, 15], [20, 15, 0]]
152140

153141
result = tsp_brute_force(graph)
154-
if result != 80:
142+
if result != 64:
155143
raise Exception("tsp_brute_force Incorrect result")
156144
else:
157145
print("Test passed")
158146

159147
result = tsp_dp(graph)
160-
if result != 80:
148+
if result != 64:
161149
raise Exception("tsp_dp Incorrect result")
162150
else:
163151
print("Test passed")
164152

165153
result = tsp_greedy(graph)
166-
if result != 80:
154+
if result != 64:
167155
if result < 0:
168156
raise Exception("tsp_greedy Incorrect result")
169157
else:

0 commit comments

Comments
 (0)