简体   繁体   中英

Finding shortest cycle in undirected graph that includes 2 specified vertices

I am given a dataset of philosophers that resembles an undirected graph. The philosophers are stored in a hash map where the key is the name of the philosopher and the value is a list of his neighbors.

The user Gives 2 philosopher names as input and I need to find the shortest cycle (if one exists that is) that includes those 2 philosophers in it.

At first I thought that this problem is quite similar to the traveling salesman problem however in that problem we also have traveling distances and each vertex can only be visited once which doesn't seem to be the case here. The solution to this problem seems to be graph related but I am not quite sure which algorithm to use or even which problem resembles it.

A solution I thought about involved finding all cycles in the given graph, reducing the results to only the cycles that contain the philosophers given by the user and picking only the shortest cycle out of those since I would score the cycles based on how many "jumps" they make which would essentially be a simple counter. The problem with this is that I'm worried it might not be an optimal solution considering how big the dataset is and how many cycles can potentially be formed (a lot).

The output needs to be in this format: {u1, u2, u3, u4, u5, u1} where u is the name of each philosopher that creates the cycle I'm looking for. If such a cycle doesn't exist then I just need to print an appropriate message.

You can use a variant of Suurballe's algorithm. As Wikipedia explains (at https://en.wikipedia.org/wiki/Suurballe's_algorithm ):

Suurballe's algorithm is an algorithm for finding two disjoint paths in a nonnegatively-weighted directed graph, so that both paths connect the same pair of vertices and have minimum total length. […] The main idea of Suurballe's algorithm is to use Dijkstra's algorithm to find one path, to modify the weights of the graph edges, and then to run Dijkstra's algorithm a second time. The output of the algorithm is formed by combining these two paths, discarding edges that are traversed in opposite directions by the paths, and using the remaining edges to form the two paths to return as the output.

and later:

The version of Suurballe's algorithm as described above finds paths that have disjoint edges, but that may share vertices. It is possible to use the same algorithm to find vertex-disjoint paths, by replacing each vertex by a pair of adjacent vertices, one with all of the incoming adjacencies u-in of the original vertex, and one with all of the outgoing adjacencies u-out . Two edge-disjoint paths in this modified graph necessarily correspond to two vertex-disjoint paths in the original graph, and vice versa, so applying Suurballe's algorithm to the modified graph results in the construction of two vertex-disjoint paths in the original graph.

So you'll need to translate your unweighted, undirected graph of | V | vertices and | E | unweighted, undirected edges to a weighted, directed graph of 2| V | vertices and 2| E | + | V | weighted, directed edges; then apply Suurballe's algorithm; then translate the result back to a cycle on the original graph.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM