

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: NP
incomplete
2: Traveling Salesman Problem
incomplete
3: Verify TSP
incomplete
4: TSP Review
incomplete
5: NP-Complete
incomplete
6: Verifying Solutions
incomplete
7: Does P Equal NP?
incomplete
8: The Negative Case
incomplete
9: NP-Hard
incomplete
10: Prime Factorization
incomplete
11: Prime Factoring Review
incomplete
12: Prime Factoring Review
incomplete
13: Subset Sum Problem
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Consider the TSP algorithms that we wrote:
def tsp(cities: list[int], paths: list[list[int]], dist: int) -> bool:
perms = permutations(cities)
for perm in perms:
total_dist = 0
for i in range(1, len(perm)):
total_dist += paths[perm[i - 1]][perm[i]]
if total_dist < dist:
return True
return False
def verify_tsp(paths: list[list[int]], dist: int, actual_path: list[int]) -> bool:
total = 0
for i in range(len(actual_path)):
if i != 0:
total += paths[actual_path[i - 1]][actual_path[i]]
return total < dist