使用GLPK输出图形的所有可能路径

时间:2015-11-11 18:21:00

标签: math graph enumeration linear-programming glpk

我正在尝试使用glpk枚举从源节点到目标节点的所有可能路径,但我遇到了一些语法问题。这是我当前的代码(改编自最短路径示例):

param n, integer, > 0;
/* number of nodes */

set E, within {i in 0..n, j in 0..n};
/* set of edges */

param s, in {0..n};
/* source node */

param t, in {0..n};
/* target node */

var x{(i,j) in E}, >= 0;
/* x[i,j] = 1 means that edge (i,j) belong to shortest path;
   x[i,j] = 0 means that edge (i,j) does not belong to shortest path;
   note that variables x[i,j] are binary, however, there is no need to
   declare them so due to the totally unimodular constraint matrix */


s.t. r{i in 1..n}: sum{(j,i) in E} x[j,i] + (if i = s then 1) =
                   sum{(i,j) in E} x[i,j] + (if i = t then 1);
/* conservation conditions for unity flow from s to t; every feasible
   solution is a path from s to t */

 var test, integer, =0;

minimize Z: sum{(i,j) in E} x[i,j];
/* objective function is the path length to be minimized */

solve;

for {(i,j) in E: x[i,j]>0}{
   printf "%d --> %d ", i, j;

}

#printf " tamanho do caminho %d ", count;

0 个答案:

没有答案