Arrange pairs of numbers so that members of adjacent pairs are equal

前端 未结 5 1120
北海茫月
北海茫月 2021-01-01 17:38

I would like to arrange the following items, forming the longest chain possible starting with 12-8 and matching the numbers end to end.

My items are 7-4, 11-8, 11-11

5条回答
  •  一生所求
    2021-01-01 18:31

    I've looked at the problem in a graph-theoretic perspective, which gives some insights about the problem, and provides some heuristics that may be used to solve the problem efficiently.

    First, construct a graph such that every item you are given corresponds to an edge of the graph. For example, if your input given as: 1-2, 2-3; you construct a graph with nodes: 1, 2, 3; and edges (1, 2), (2, 3).

    Thereafter, you can see that your problem is identical to finding the longest trail, i.e., the longest path that does not contain any edge more than one. Unfortunately, this problem is known to be NP-hard, as discussed in this question. So, we cannot hope to find a polynomial algorithm to solve this problem.

    However, this problem is actually very similar to the problem of Eularian Path. However, in an Eularian path you traverse all edges. And it has a very simple solution:

    An undirected graph has an Eulerian path if and only if exactly zero or two vertices have odd degree, and if all of its vertices with nonzero degree belong to a single connected component.

    So in order to solve your problem, you take the connected component of the graph that contains the item you want to start with. You cannot possibly reach the items that are not in this connected component. Therefore, you can forget about all of the remaining edges of the graph.

    Then, you simply count the degrees of each node, and check if that graph has an Eulerian path by the preceding definition. If it has, then you're lucky. Because you can't possibly have a chain longer than this path.

    And you can compute this chain easily by Fleury's Algorithm.

    However, if this component does not have an Eualirian path, then you at least know that there does not exist a chain of the size of the edges of this component or more.

    Handshaking Lemma tells us:

    Every undirected graph has an even number of vertices with odd degree.

    If there does not exists an Eulerian path, then we know that we have 2k nodes with odd degrees where k > 1. So we need to remove minimum number of edges edges so that we have k = 1. However, you need to take into account that when you remove some of the edges, remaining edges may not be connected.

    So the best heuristic that comes to my mind is to find edges such that both of its vertices have odd degrees, and removing it doesn't tear the connected component apart. If we can find such k - 1 vertices, then when we remove them we will stil have a connected component and we will have only 2 vertices with odd degrees. Therefore we can find the longest chain easily, again by finding an Eulerian path using Fleury's algorithm.

提交回复
热议问题