Classic problems
By Afshine Amidi and Shervine Amidi
Traveling salesman
Given cities , the traveling salesman problem (TSP) is a classic problem that aims at finding the shortest path that visits all cities exactly once and then returns to the starting city.
The distance between each pair of cities is noted and is known.
A naive approach would consist of enumerating all possible solutions and finding the one that has the minimum cumulative distance. Given a starting city, there are possible paths, so this algorithm would take time. This approach is impracticable even for small values of .
Luckily, the Held-Karp algorithm provides a bottom-up dynamic programming approach that has a time complexity of and a space complexity of .
Suppose is the starting city. This arbitrary choice does not influence the final result since the resulting path is a cycle. We define the following quantities:
- contains all sets of distinct cities in :We note that has a size of .
- is the distance of the shortest path that starts from , goes through each city in exactly once and ends at .
The solution is found iteratively:
Initialization: The shortest path between the starting city and each of the other cities with no intermediary city is directly given by :
Compute step: Suppose that for all and , we know the distance of the shortest path between and via the cities in .
Let's take . For all , we note that and that (naturally) , which means that is known.
We can deduce the distance of the shortest path between and via cities by taking the minimum value over all second-to-last cities :
Each step has the following complexities:
Time Space Final step: The solution is given by .
Since , the last step of the algorithm gives:
Knapsack
The 0/1 knapsack problem is a classic problem where the goal is to maximize the sum of values of items put in a bag that has a weight limit. Here, "0/1" means that for each item, we want to know whether we should include it (1) or not (0).
More formally, we have a bag of capacity and items where each item has value and weight . We want to find a subset of items such that:
A naive approach would consist of trying out every combination of the items and take the one with the maximum value which also satisfies the cumulative weight constraint. Such an approach has a time complexity of .
Luckily, this problem can be solved with a bottom-up dynamic programming approach that has a time complexity of and a space complexity of .
We note the maximum value of a bag of capacity that contains items among . Our objective is to get .
Initialization: The maximum values of the following bags are known:
Bags of capacity 0: .
Bags with 0 item: .
Compute maximum value: Starting from and , we iteratively fill the 2D array of maximum values from left to right, top to bottom.
In order to determine the maximum value of a bag of capacity containing items among , we need to choose between two hypothetical bags:
Bag 1: contains item . is found by adding the value of item to the maximum value of the bag of capacity containing items among .
Bag 2: does not contain item . is already known: it is the maximum value of the bag of capacity containing items among .
We choose the best potential bag:
When the 2D array is filled, the desired value is .
Get final items: In order to know which items were selected, we start from position of the 2D array and traverse it iteratively:
Case : This means that item was included. We go to position and include item in the set of included items.
Case : This means that item was not included. We go to position .
We repeat this process until retrieving all items.
-Queens
Given a chessboard of size , the -Queens problem aims at finding a configuration of queens such that no two queens attack each other.
A queen is a chess piece that can move horizontally, vertically or diagonally by any number of steps.
The solution can be found using a backtracking approach:
Step 1: Put the queen on the chessboard.
Step 2: Put the queen in the second column of the chessboard. As long as the partial configuration is invalid, keep changing its position until conditions are satisfied.
Step 3: If there is nowhere to place the next queen, go back one step and try the next partial configuration.
Step 4: Continue this process until finding a valid solution.
Coin change
Given an unlimited number of coins of values , the coin change problem aims at finding the minimum number of coins that sum up to a given amount . By convention, we assume that .
In other words, we want to find the minimum value of such that
The bottom-up dynamic programming solution iteratively computes the minimum amount of coins for each amount .
This approach runs in time and takes space:
Initialization:
Initialize array with zeros.
For each , set to since we already know that the corresponding amount only needs 1 coin.
Compute step: Starting from , we iteratively fill array .
In order to obtain amount , we distinguish two cases:
Case : We look back at valid values and see which coin minimizes the total number of coins needed to obtain amount .
Case : We cannot obtain amount using coins .
At the end of this step, amounts with cannot be obtained with the given coins.
The answer to this problem is given by .
Subscribe here to be notified of new Super Study Guide releases!