Friday, 27 January 2023

Other examples: The Travelling Salesperson Problem

 


Other examples: The Travelling Salesperson Problem

Given a set of locations V and a matrix of distances dij between each pair of locations i and j in V, is it possible to visit all locations exactly once and then return to your starting point while covering a total distance less than k.

You may assume, without loss of generality, that you can start from any one of the locations (it actually makes no difference).

     Think about how to solve a yes instance

     Think about how to solve a no instance

To solve a "yes" instance of the problem, where it is possible to visit all locations exactly once and then return to the starting point while covering a total distance less than k, we can use the following algorithm:

1.       Initialize a list of visited locations and a variable total_distance to 0.

2.       Iterate through the locations in V.

3.       At each location, mark it as visited and add the distance from the previous location to the total distance.

4.       If the total distance is greater than or equal to k, return False.

5.       If all locations have been visited, return True.

This algorithm has a time complexity of O(n), where n is the number of locations in V, because it requires one pass through the list of locations.

To solve a "no" instance of the problem, where it is not possible to visit all locations exactly once and then return to the starting point while covering a total distance less than k, we can use the following algorithm:

1.       Initialize a list of visited locations and a variable total_distance to 0.

2.       Iterate through the locations in V.

3.       At each location, mark it as visited and add the distance from the previous location to the total distance.

4.       If the total distance is greater than or equal to k, return False.

5.       If all locations have been visited and the total distance is less than k, return True.

This algorithm has a time complexity of O(n), where n is the number of locations in V, because it requires one pass through the list of locations.

The Python code for the "yes" instance algorithm with detailed line-by-line comments:

# Initialize list of visited locations and variable total_distance to 0

visited = []

total_distance = 0

 

# Iterate through locations in V

for i in V:

    # Mark location as visited

    visited.append(i)

    # Add distance from previous location to total distance

    total_distance += dij[i][visited[-2]]

    # If total distance is greater than or equal to k, return False

    if total_distance >= k:

        return False

 

# If all locations have been visited, return True

return True

the Python code for the "no" instance algorithm:

# Initialize list of visited locations and variable total_distance to 0

visited = []

total_distance = 0

 

# Iterate through locations in V

for i in V:

    # Mark location as visited

    visited.append(i)

    # Add distance from previous location to total distance

    total_distance += dij[i][visited[-2]]

    # If total distance is greater than or equal to k, return False

    if total_distance >= k:

        return False

 

# If all locations have been visited and total distance is less than k, return True

if len(visited) == len(V) and total_distance < k:

    return True

else:

    return False

Another example

Consider this problem:

Find out if a Boolean expression in disjunctive normal form (DNF) is a tautology.

     A tautology is an expression that evaluates to true for all combinations of inputs 

     E.g. (a ¬a) (b ¬b)

     Disjunctive normal form is an or of ands

     e.g. (a ¬b c) (a b ¬c)

Think about yes and no instances again - is it the same as the previous examples?

The problem of determining whether a Boolean expression in disjunctive normal form (DNF) is a tautology is similar to the previous examples of evaluating "yes" and "no" instances. A "yes" instance would be a tautology that evaluates to true for all combinations of inputs, while a "no" instance would be a Boolean expression that does not evaluate to true for all combinations of inputs.

To solve a "yes" instance of the problem, we can iterate through all possible combinations of inputs and check if the Boolean expression evaluates to true for each combination. If the expression evaluates to true for all combinations, we can conclude that it is a tautology.

To solve a "no" instance of the problem, we can also iterate through all possible combinations of inputs and check if the Boolean expression evaluates to true for each combination. If the expression does not evaluate to true for at least one combination, we can conclude that it is not a tautology.

Both solutions have a time complexity of O(2^n), where n is the number of variables in the Boolean expression, because they require checking all possible combinations of inputs.

No comments:

Post a Comment