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]]]]):
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):
print(f'\x1b[4;1m|S| = {k}:\x1b[0m');
for (i, S) in keys:
if len(S) != k:
continue;
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:
print(f'optimal path: {" -> ".join(map(str, paths[0]))}');
print(f'optimal way: {" -> ".join(map(str, map(addone, paths[0])))}');
else:
print('optimal paths:');
print('optimal ways:');
for path in paths:
print(f'* {" -> ".join(map(str, path))}');
print(f'* {" -> ".join(map(str, map(addone, path)))}');
print('');
return;