Question

I am trying to make a program in Clingo to solve a Euler path. So far this is what I have come up with. I want my graph to appear as follows. Graphical Representation My input.

    edge(a,b).
    edge(b,c).
    edge(c,d).
    edge(d,e).
    edge(a,d).
    edge(b,d).
    %edge(x,y):-edge(y,x).
    num(1..6).

My program so far.

    %Generates paths with X and Ys equal to edges in the input, N can be in the range of 'num'
    1{path(X,Y,N):edge(X,Y)}1:-num(N).

    %Next edges Y and X1 are the same/connect. Where N is in the range of indices.
    :-path(X,Y,N), path(X1,Y1,N+1), Y!=X1, num(N).

Am I incorrect in my comments in my program? I would think that the program is it should always connect edges together. As it is right now I do not get an answer, however when the number of steps "num" is ranged from 1..4 I get a solution. I believe a Euler path should have 6 steps, however with a possible solution of:

path(b,d,1) 
path(d,a,2) 
path(a,b,3) 
path(b,c,4) 
path(c,d,5) 
path(d,e,6) 
Was it helpful?

Solution

Figured it out!

Here is the input file.

    edge(a,b).
    edge(b,c).
    edge(c,d).
    edge(d,e).
    edge(a,d).
    edge(b,d).
    edge(X,Y):-edge(Y,X).
    num(1..6).

And the output file.

    1{path(X,Y,N):edge(X,Y)}1:-num(N).

    %Next edges Y and X1 are the same/connect. Where N is in the range of indices.
    :-path(X,Y,N), path(X1,Y1,N+1), Y!=X1, num(N).

    %Accept no duplicate paths
    :-path(X,Y,N), path(X1,Y1,N2), Y==Y1, X==X1, num(N), num(N2), N!=N2.
    :-path(X,Y,N), path(X1,Y1,N2), Y==X1, X==Y1, num(N), num(N2), N!=N2.

    #hide.
    #show path/3.

Which produces an output as follows on my machine (reversed for readability):

    path(e,d,1)
    path(d,b,2)
    path(b,c,3)
    path(c,d,4)
    path(d,a,5)
    path(a,b,6)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top