图中最长的路径

发布于 2024-12-14 13:21:10 字数 122 浏览 3 评论 0原文

过去两天以来,我试图找到一些计算图中最长路径的逻辑。我知道我可以轻松地为 DAG 找到它,并且一般来说它是多项式时间算法。形式上,我想实现启发式来计算最长路径,此外,如果给出图中存在边的概率 p,我们如何解决这个问题..帮助...

Since last 2 days,i'm trying to find some logic for calculating longest path in graph.I know i can find it easily for DAGs and in general it is polynomial time algorithm.Formally,I want to implement heuristic for computing longest path,morever,if probability p is given with which an edge is present in graph,how can we solve the problem..help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

潦草背影 2024-12-21 13:21:10

据我所知,计算最长路径不能在多项式时间内完成。下面的最长路径算法的 java 实现找到给定源的正加权图的最长路径,但在最坏的情况下需要指数时间。

public class LongestPath {
    static int times;

    public double initLongestPath(ArrayList<Vertex> V, Vertex source) {
        for (Vertex u : V) {
            u.setVisited(false);
        }
        return getLongestPath(source);
    }

    public double getLongestPath(Vertex v) {
        ++times;
        System.out.println(times);
        double w, dist, max = 0;
        v.setVisited(true);
        for (Edge e : v.getOutGoingEdges()) {
            if (!e.getToNode().isVisited()) {
                dist = e.getWeight() + getLongestPath(e.getToNode());
                if (dist > max)
                    max = dist;
            }
        }

        v.setVisited(false);
        return max;
    }
}

Calculating longest path cannot be done in polynomial time as far as I know. Following java implementation of the longest path algorithm finds the longest path of a positive weighted graph for a given source but it takes exponential time in its worst case.

public class LongestPath {
    static int times;

    public double initLongestPath(ArrayList<Vertex> V, Vertex source) {
        for (Vertex u : V) {
            u.setVisited(false);
        }
        return getLongestPath(source);
    }

    public double getLongestPath(Vertex v) {
        ++times;
        System.out.println(times);
        double w, dist, max = 0;
        v.setVisited(true);
        for (Edge e : v.getOutGoingEdges()) {
            if (!e.getToNode().isVisited()) {
                dist = e.getWeight() + getLongestPath(e.getToNode());
                if (dist > max)
                    max = dist;
            }
        }

        v.setVisited(false);
        return max;
    }
}
七分※倦醒 2024-12-21 13:21:10

Dijkstra 不能用于负权重的图 - 有关 Dijkstra 的 Wiki 文章

Dijkstra 算法,由荷兰计算机科学家 Edsger Dijkstra 于 1956 年提出并于 1959 年发布,1 是一种图搜索算法,用于解决图的单源最短路径问题非负边缘路径成本...

所以你不能否定所有边权重并使用 Dijkstra 算法,你能做的就是否定所有边权重并使用 Bellman-Ford 算法 - 有关 Bellman-Ford 的 Wiki 文章

贝尔曼-福特算法是一种计算从单个源顶点到加权有向图中所有其他顶点的最短路径的算法。1 对于同一问题,它比 Dijkstra 算法慢,但更通用,因为它能够处理其中某些边权重是负数

编辑:最短路径(具有最大负值)就是原始图中的最长路径。

注意:如果图中存在正循环,您将找不到解决方案,因为最长路径在此类图中不存在。

Dijkstra's can't be used on graphs with negative weights - Wiki article on Dijkstra's

Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1956 and published in 1959,1 is a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs ...

So you can't negate all edge weights and use Dijkstra's, what you can do is negate all edge weights and use Bellman-Ford algorithm - Wiki article on Bellman-Ford

The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph.1 It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers

EDIT: The shortest path (with the most negative value) is then the longest path in your original graph.

NOTE: if you have positive cycles in your graph, you will not find a solution since the longest path doesn't exist in such a graph.

忆伤 2024-12-21 13:21:10

您始终可以使用 广度优先搜索 (BFS),并且每当您添加在图的边缘,你可以得到它的成本作为加法逆元(乘以-1)。这样,您就可以使用最长的边找到“最短路径”。因为您正在进行标量变换,所以您不会失去在组内添加的能力(如果您使用乘法逆元,则会失去这种能力)。

You could always just use a breadth first search (BFS) and, whenever you are adding an edge to the graph you have it's cost as the additive inverse (multiply it be -1). This way you are finding the 'shortest path' by using the longest edges. Because you're doing a scalar transform, you're not losing the ability to add within the group (which you do lose if you use the multiplicative inverse).

不知在何时 2024-12-21 13:21:10

反转路径的权重并运行最短路径算法。您得到的最小数字(最负数)是最长的路径。

Invert the weights of the paths and run a shortest path algorithm. The lowest number you get (most negative) is the longest path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文