master > master: code py - tsp

This commit is contained in:
RD 2022-06-09 01:50:57 +02:00
parent 07cf57eeab
commit 53b2066e0d
1 changed files with 5 additions and 4 deletions

View File

@ -58,18 +58,19 @@ def tsp_naive_algorithm(
def display_computation(n: int, memory: Dict[tuple[int, tuple], tuple[float, list[list[int]]]]): def display_computation(n: int, memory: Dict[tuple[int, tuple], tuple[float, list[list[int]]]]):
keys = sorted(memory.keys(), key=lambda key: (len(key[1]), key[0], key[1])); keys = sorted(memory.keys(), key=lambda key: (len(key[1]), key[0], key[1]));
addone = lambda x: x + 1;
for k in range(0,n): for k in range(0,n):
print(f'\x1b[4;1m|S| = {k}:\x1b[0m'); print(f'\x1b[4;1m|S| = {k}:\x1b[0m');
for (i, S) in keys: for (i, S) in keys:
if len(S) != k: if len(S) != k:
continue; continue;
value, paths = memory[(i, S)]; value, paths = memory[(i, S)];
print(f'g({i}, {list(S)}) = {value}'); print(f'g({addone(i)}, {list(map(addone, S))}) = {value}');
if len(paths) == 1: if len(paths) == 1:
print(f'optimal path: {" -> ".join(map(str, paths[0]))}'); print(f'optimal way: {" -> ".join(map(str, map(addone, paths[0])))}');
else: else:
print('optimal paths:'); print('optimal ways:');
for path in paths: for path in paths:
print(f'* {" -> ".join(map(str, path))}'); print(f'* {" -> ".join(map(str, map(addone, path)))}');
print(''); print('');
return; return;