在加权有向循环图中查找从 A 到 B 的不同路径的算法

发布于 2024-12-27 13:13:31 字数 244 浏览 1 评论 0原文

假设我们有一个有向加权循环图。

假设我们只对总权重小于 MAX_WEIGHT 的路径感兴趣,

找到两个节点 A 之间不同路径的数量的最合适(或任何)算法是什么?和 B 的总重量小于 MAX_WEIGHT?

PS:这不是我的作业。只是个人、非商业项目。

Suppose we have a DIRECTED, WEIGHTED and CYCLIC graph.

Suppose we are only interested in paths with a total weight of less than MAX_WEIGHT

What is the most appropriate (or any) algorithm to find the number of distinct paths between two nodes A and B that have a total weight of less than MAX_WEIGHT?

P.S: It's not my homework. Just personal, non-commercial project.

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

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

发布评论

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

评论(3

瑾兮 2025-01-03 13:13:31

如果节点数和 MAX_WEIGHT 不太大(且所有权重均为整数),可以使用动态规划

unsigned long long int num_of_paths[MAX_WEIGHT+1][num_nodes];

初始化为 0,除非 num_of_paths[0][start] = 1; 。

for(w = 0; w < MAX_WEIGHT; ++w){
    for(n = 0; n < num_nodes; ++n){
        if (num_of_paths[w][n] > 0){
            /* for each child c of node n
             * if w + weight(n->c) <= MAX_WEIGHT
             * num_of_paths[w+weight(n->c)][c] += num_of_paths[w][n];
             */
        }
    }
}

解决方案是 num_of_paths[w][target], 0 <= w <= MAX_WEIGHT 的总和。

If the number of nodes and MAX_WEIGHT aren't too large (and all weights are integers), you can use dynamic programming

unsigned long long int num_of_paths[MAX_WEIGHT+1][num_nodes];

initialize to 0, except num_of_paths[0][start] = 1;.

for(w = 0; w < MAX_WEIGHT; ++w){
    for(n = 0; n < num_nodes; ++n){
        if (num_of_paths[w][n] > 0){
            /* for each child c of node n
             * if w + weight(n->c) <= MAX_WEIGHT
             * num_of_paths[w+weight(n->c)][c] += num_of_paths[w][n];
             */
        }
    }
}

solution is sum of num_of_paths[w][target], 0 <= w <= MAX_WEIGHT .

微暖i 2025-01-03 13:13:31

简单的递归。你在指数时间内就拥有了它。显然,不允许零重量循环。

函数 noe(节点 N, 限制权重 W)

  1. no.如果所有传出边的权重均大于 1,则路径的 0 为零。没有

  2. 否则不行。路径的数量是通过 sum(noe(C1,W-W1),noe(C2,W-W2),... noe(Cn,W-Wn)) 获得的路径数的总和,其中 C1 ... Cn 是连接到 N 的节点,其中 W-Wi 不为负,其中 Wi 是连接边的权重,用您最喜欢的语言编写。

按照 Dijkstra 算法的思路,应该存在更有效的解决方案,但我认为这对于家庭作业来说已经足够了。

Simple recursion. You have it in exponential time. Obviously, no zero-weight cycles allowed.

function noe(node N, limit weight W)

  1. no. of path is zero if all outgoing edges have weight > W

  2. otherwise no. of path is sum of numbers of path obtained by sum(noe(C1,W-W1),noe(C2,W-W2),... noe(Cn,W-Wn)) where C1 ... Cn are the nodes connected to N for which W-Wi is not negative where Wi is weight of the connecting edge, written in your favorite language.

More eficient solution should exist, along the lines of Dijkstra's algorithm, but I think this is enough for homework.

尐偏执 2025-01-03 13:13:31

您的问题是K-不相交路径在有向平面图中的更一般情况,有向平面图的

K 不相交路径问题如下:

给定:有向平面图 G = (V;E) 和 k 对(r1; s1); ....; G 顶点的 (rk; sk);

查找:k个成对顶点不相交的有向路径P1; ...; G 中的 Pk,其中 Pi 从 ri 到 si (i = 1; .. ..;k)。

在k-不相交路径中,您可以绘制从所有 si 到 B 的弧,也可以从 A 到所有 ri 的弧,通过这种方式您可以从 G 创建图 G'。

现在,如果您可以解决 P 中 G' 中的问题,您就可以解决 G 中的 k 不相交路径,因此 P=NP。

但是,如果您阅读链接的论文,它会给出一些关于一般图的想法(用固定 k 求解 k 不相交路径),您可以使用它来获得一些好的近似值。

还有更复杂的算法可以在一般图中解决 P(对于固定 k)中的这个问题。但总而言之,这并不容易(作者:Seymour)。

所以目前你最好的选择是使用暴力算法。

编辑:因为 MAXWEIGHT 与你的输入大小(你的图形大小)无关,所以它不会影响这个问题,而且因为它对于无向无权图来说是 NP-Hard,所以你仍然可以简单地得出结论它是 NP-难的。

Your problem is more general case of K-Disjoint Path In directed planar graphs, with not fixed K.

K disjoint paths problem for directed planar graphs is as this:

given: a directed planar graph G = (V;E) and k pairs (r1; s1); .... ; (rk; sk) of vertices of G;

find: k pairwise vertex-disjoint directed paths P1; ... ; Pk in G, where Pi runs from ri to si (i = 1; .... ; k).

In k-disjoint path you can draw arc from all si to B, and Also arc from A to all ri by this way you create graph G' from G.

Now if you can solve your problem in G' in P you can solve k-disjoint Path in G, So P=NP.

But if you read the paper linked it gives some idea for general graph (solving k-disjoint path with fixed k) and you can use it to have some good approximation.

Also there is more complicated algorithm which solves this problem in P (for fixed k) in general graphs. but in all it's not easy (it's by Seymour ).

So your best choice currently is to use brute force algorithms.

Edit: Because MAXWEIGHT is independent to your input size (your graph size) It doesn't affect to this problem, Also because it's NP-Hard for undirected unweighted graph, still you simply can conclude it's NP-Hard.

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