I am trying to find all the paths in a graph with minimum distance using DLV. Say I have the following graph:
I am expecting to obtain the predicates (I hope I don't skip any):
- path(a, b, 1), path(a, d, 1), path(a, e, 1), path(a, c, 2)
- path(b, a, 1), path(b, c, 1), path(d, d, 2), path(b, e, 2)
- path(c, b, 1), path(c, e, 1), path(c, a, 2), path(c, d, 3)
- path(d, a, 1), path(d, b, 2), path(d, e, 2), path(d, c, 3)
- path(e, a, 1), path(e, c, 1), path(e, d, 2), path(e, b, 2)
I assume that you can travel an arch both left or right. So, I tried the following:
path(X, Y, 1) :- arc(X, Y).
path(Y, X, 1) :- arc(X, Y).
path(X, Z, L) :- path(X, Y, M), path(Y, Z, N),
X!=Z,
L = M + N,
not path(X, Z, V), V < L, #int(V)
The idea of the third rule was to add 2 existing paths if they are not going back (X!=Z) and there is not already a path connecting the same edges with a shorter distance (not path(X, Z, V), V < L, #int(V)). I had to add #int(V) because otherwise the rule was not safe. I don't know if there is a better way of resolving this safety issue with an integer value.
When I run this code (with the flag -N=5 to set #maxint=5) I get paths that should not be there, for example, path(d,a,5). I don't know if the problem is with the #int(V) or something else but I wouldn't expect these paths to appear since I already have a path(d,a,1). Probably it is because of #int(V) but I can't figure out how to do this right.
Can anyone help me solve this? Thanks in advance.