[英]Algorithm for finding distinct paths from A to B in weighted, directed, cyclic graph
Suppose we have a DIRECTED , WEIGHTED and CYCLIC graph. 假设我们有一个DIRECTED , WEIGHTED和CYCLIC图表。
Suppose we are only interested in paths with a total weight of less than MAX_WEIGHT 假设我们只对总重量小于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? 找到总重量小于MAX_WEIGHT的两个节点A和B之间的不同路径数量的最合适(或任何)算法是什么?
PS: It's not my homework. PS:这不是我的功课。 Just personal, non-commercial project.
只是个人的非商业项目。
If the number of nodes and MAX_WEIGHT
aren't too large (and all weights are integers), you can use dynamic programming 如果节点数和
MAX_WEIGHT
不是太大(并且所有权重都是整数),则可以使用动态编程
unsigned long long int num_of_paths[MAX_WEIGHT+1][num_nodes];
initialize to 0, except num_of_paths[0][start] = 1;
初始化为0,但
num_of_paths[0][start] = 1;
除外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
. 解是
num_of_paths[w][target], 0 <= w <= MAX_WEIGHT
总和num_of_paths[w][target], 0 <= w <= MAX_WEIGHT
。
Simple recursion. 简单的递归。 You have it in exponential time.
你有指数时间。 Obviously, no zero-weight cycles allowed.
显然,不允许零重量循环。
function noe(node N, limit weight W) 功能noe(节点N,极限重量W)
no. 没有。 of path is zero if all outgoing edges have weight > W
如果所有输出边都具有权重> W,则路径为零
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.
路径是由和(noe(C1,W-W1),noe(C2,W-W2),... noe(Cn,W-Wn))得到的路径数之和,其中C1 ... Cn是连接到N的节点,其中W-Wi不是负数,其中Wi是连接边缘的权重,用您喜欢的语言编写。
More eficient solution should exist, along the lines of Dijkstra's algorithm, but I think this is enough for homework. 根据Dijkstra的算法,应该存在更有效的解决方案,但我认为这对于家庭作业来说已经足够了。
Your problem is more general case of K-Disjoint Path In directed planar graphs , with not fixed K. 你的问题是K-不相交路径的更一般情况在有向平面图中 ,没有固定的K.
K
disjoint paths problem for directed planar graphs is as this: 定向平面图的
K
不相交路径问题如下:
given : a directed planar graph G = (V;E) and k pairs (r 1 ; s 1 ); 给定 :有向平面图G =(V; E)和k对(r 1 ; s 1 ); .... ;
....; (r k ; s k ) of vertices of G;
(r k ; s k )G的顶点;
find : k pairwise vertex-disjoint directed paths P 1 ; find :k成对顶点不相交的有向路径P 1 ; ... ;
......; P k in G, where P i runs from r i to s i (i = 1; .... ; k).
G中的P k ,其中P i从r i到s i (i = 1; ......; k)。
In k-disjoint path you can draw arc from all s i to B, and Also arc from A to all r i by this way you create graph G' from G. 在k-disjoint路径中,您可以从所有s i到B绘制弧,并且通过这种方式从A到所有r i ,从G创建图G'。
Now if you can solve your problem in G' in P you can solve k-disjoint Path in G, So P=NP. 现在,如果你能用P中的G'解决你的问题,你就可以解决G中的k-disjoint路径,所以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. 但是,如果你阅读链接的论文,它给出了一般图形的一些想法(用固定的k求解k-不相交路径),你可以用它来得到一些好的近似。
Also there is more complicated algorithm which solves this problem in P (for fixed k) in general graphs. 还有更复杂的算法在一般图中解决了P(对于固定k)的这个问题。 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. 编辑:因为MAXWEIGHT独立于你的输入大小(你的图形大小)它不会影响这个问题,也因为它是NP-Hard的无向未加权图,你仍然可以简单地得出它的NP-Hard。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.