data_structures/graphs/opt_transformations.py
def optimization2opt(graph, path, n):
    """ Search for a better path using 2-opt swaps """
    best_distance = getPathLength(graph, path)
    best_path = path

    for i in range(1, n):
        for j in range(i, n):
            new_path = swap2opt(path, i, j)
            new_distance = getPathLength(graph, new_path)
            if new_distance < best_distance:
                best_path = optimization2opt(graph, new_path, n)
    return best_path
 Search for a better path using 2-opt swaps 
Search for a better path using 2-opt swaps
Search for a better path using 2 - opt swaps
def optimization2opt(graph, path, n):
    best_distance = getPathLength(graph, path)
    best_path = path
    for i in range(1, n):
        for j in range(i, n):
            new_path = swap2opt(path, i, j)
            new_distance = getPathLength(graph, new_path)
            if new_distance < best_distance:
                best_path = optimization2opt(graph, new_path, n)
    return best_path
def optimization2opt ( graph , path , n ) : best_distance = getPathLength ( graph , path ) best_path = path for i in range ( 1 , n ) : for j in range ( i , n ) : new_path = swap2opt ( path , i , j ) new_distance = getPathLength ( graph , new_path ) if new_distance < best_distance : best_path = optimization2opt ( graph , new_path , n ) return best_path
Search for a better path using 2-opt swaps
Search for a better path using 2 - opt swaps
""" Search for a better path using 2-opt swaps """
graph
path
n