[Introduction to Algorithms] Postman Problem

postman problem

Traveling Salesman Problem : Given a list of cities and the distance between each pair of cities, find the shortest circuit that visits each city once and returns to the starting city. (Traversing points, returning to the starting point)
Hamiltonian path Hamiltonian diagram
Chinese postman problem : The postman must try to find a shortest path that can walk through all the streets in this area, and finally return to the starting point. (Traversing edges, returning to the starting point)
The Euler Path Euler Diagram
Traveling Salesman Problem cannot be turned back. Once a node has been visited, it cannot be returned. It does not need to go all the way, but the Chinese postman problem can visit a node multiple times , because the Chinese postman problem requires that all streets be visited, ie, every street must be visited.

This problem is a kind of graph traversal problem.
The Chinese postal route problem of the undirected graph is easy to solve, and it is a P problem;
the Chinese postal route problem of the directed graph is an NP-complete problem.

Euler circuit

Just imagine, assuming that every road is walked once, and it happens to be able to return to the starting point in the end, what kind of structure should such a topology map satisfy?
Each node has to go in and out, so the in-degree and out-degree of each point must be equal (the same is true for the starting point).
The degree of each node should be an even number, and a graph satisfying such a topology is an Euler circuit.

Solutions for undirected graphs

G is an Euler graph:
since an Euler circuit passes through all edges, any Euler circuit is the solution to this problem.
Any Euler circuit of G is an optimal circuit, so it can be solved by Floyd's algorithm.

G is not an Euler graph:
any cycle in G must pass through some edges more than once.
Among them, there must be endpoints with an odd number of sides, and such endpoints must be greater than or equal to 2.
So some sides need to be repeated again, so that the endpoints of the odd sides become the endpoints of the even sides.
When the two endpoints of edge e are connected by a new edge with weight w(e), the edge e is called repeated.

Assume that there are 14 roads and 9 intersections in a town (the intersections are numbered 1, 2, ..., 9 respectively), in the map corresponding to the roads and intersections (the first picture on the right, the numbers on the side are the cost of each side) There are 4 endpoints (numbered 1, 3, 6 and 9) through which an odd number of edges passes, so there is no Euler circuit in this graph.
The next thing to do is to repeat several edges in the graph, so that all the endpoints in the graph have even-numbered edges passing through .
For example, if {1,3} and {6.9} edges are repeated in the figure, all endpoints have even-numbered edges passing through. However, the increased length is not necessarily the least.
insert image description here
In order to determine which side to repeat can make the end points of the original graph have even-numbered sides through, and increase the length the least.
First draw the complete graph K4 of the endpoints of all odd-numbered edges, and the number on the edge is the shortest length from one endpoint to the other endpoint (which can pass through the points omitted in other complete graphs K4).
Points with odd degrees are 1,3,6,9. Adding an edge makes the odd-degree point become even-degree, making the weight sum of the added edge minimum.
If you choose sides {1,6} and {3,9}, all endpoints pass through once, and the total length 4+2=6 is the shortest.
insert image description here

Find the shortest path, and the original path must be, so the edge of the construction of the Euler circuit determines the final result.

Determine whether it is an Euler circuit.
If it is an Euler circuit , directly add the result and return it.
If it is not an Euler circuit , there must be an even number of nodes with odd degrees.
(The sum of the degrees of all vertices in an undirected graph must be an even number and equal to twice the number of edges. Because of the handshake theorem, one edge has two degrees (out-degree and in-degree).)

1. According to the number of degrees of each node, mark the nodes with odd degrees.
2. There must be an even number of odd nodes. Therefore, the final distance should be: the shortest distance between two points, and the shortest distance between two points can be obtained by Floyd or Dijkstra or Bellman-ford algorithm.
I personally recommend using the Floyd algorithm, which is less prone to errors.
3. Go through all combinations and find the shortest combination,
for example, compare d(2,8)+d(4,6), d(2,6)+d(4,8), d(2,4 )+d(6,8),
and then take the minimum value as the final optimization scheme: d(2,8)+d(4,6).
When solving this step, you can use DFS to find the shortest path solution, or use the idea of ​​DFS to implement dynamic programming. Construct
a dp table, the row value is 1, and the column value represents the elements contained in the set.

Solution steps:

1. Generate an adjacency matrix, and use the Floyd algorithm to find the shortest distance between every two points. (Floyd)

2. Determine whether the whole process is an Euler circuit, if not construct an Euler circuit.

3. Use dynamic programming or DFS to calculate the optimal solution for constructing Euler circuits.

4. Calculate the sum of the lengths of the original path and the newly constructed path as the final result.

See the Floyd algorithm for the shortest path problem for detailed code

Guess you like

Origin blog.csdn.net/Trance95/article/details/115919197