如何找到覆盖有向循环图的所有节点的最短路径

如何找到覆盖有向循环图的所有节点的最短路径

本文介绍了如何找到覆盖有向循环图的所有节点的最短路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个有向循环图的最短路径的一例从一个节点(应当达到从一个节点的图形,这将是对输入的所有节点)。

I need an example of the shortest path of a directed cyclic graph from one node(it should reach to all nodes of the graph from a node that will be the input).

请,如果有一个例子,我需要在C ++或算法。

Please if there is an example, I need it in C++, or the algorithm.

推荐答案

编辑:哎呀,误解的问题。感谢@jfclavette采摘这件事。旧的答案是在末尾。

Oops, misread the question. Thanks @jfclavette for picking this up. Old answer is at the end.

您正在试图解决这个问题被称为旅行商问题。有许多潜在的解决方案的,但它的NP完全问题,所以你将不能够解决了大图。

The problem you're trying to solve is called the Travelling salesman problem. There are many potential solutions, but it's NP-complete so you won't be able to solve for large graphs.

老回答:

你在试图找到被称为图的周长。它可以通过从一个节点的距离设置为本身为无穷大并且使用弗洛伊德-沃肖尔算法。从节点i的最短周期的长度为然后就在位置II中的条目。

What you're trying to find is called the girth of a graph. It can be solved by setting the distances from a node to itself to infinity and using the Floyd-Warshall algorithm. The length of the shortest cycle from node i is then just the entry in position ii.

这篇关于如何找到覆盖有向循环图的所有节点的最短路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!