prologgraph-coloring

Graph Coloring Problem in Prolog : Program not terminating


vertex(1).
vertex(2).
vertex(3).
vertex(4).

color(1).
color(2).
color(3).

edge(1,2).
edge(1,3).
edge(1,4).
edge(2,4).
edge(3,4).
edge(X,Y):-edge(Y,X).

getelement(1,[Z|_],Z).
getelement(X,[Z|Zs],M):-K is X-1,getelement(K,Zs,M).

check(Z):-edge(X,Y),getelement(X,Z,M),getelement(Y,Z,N),M=:=N,!,fail.


getcolor(1,red).
getcolor(2,blue).
getcolor(3,green).


colorit([A,B,C,D]):-color(A),color(B),color(C),color(D),write([A,B,C,D]),check([A,B,C,D]),write([A,B,C,D]).

This is my code. The problem it is not getting terminated. But the logic in code is clear, if you find where problem exists please help me to correct.

input:- colorit(Z).

output:- [1, 1, 1, 1]
[1, 1, 1, 2]
[1, 1, 1, 3]
[1, 1, 2, 1]
[1, 1, 2, 2]
[1, 1, 2, 3]
[1, 1, 3, 1]
[1, 1, 3, 2]
[1, 1, 3, 3]
[1, 2, 1, 1]
[1, 2, 1, 2]
[1, 2, 1, 3]
[1, 2, 2, 1]
[1, 2, 2, 2]
[1, 2, 2, 3]

it came till here, [1,2,2,3] which is the true solution it is in loop and not terminating by returning true.There's just a small correction that need to be made. Please help me out.


Solution

  • Some notes on your code:

    Based on these observations, your program could be rewritten to (I'm skipping the facts):

    hasEdge(X,Y):-edge(X,Y) ; edge(Y,X).
    
    getelement(1,[Z|_],Z).
    getelement(X,[Z|Zs],M):-K is X-1,getelement(K,Zs,M).
    
    check(Z):-hasEdge(X,Y),getelement(X,Z,M),getelement(Y,Z,N),M=:=N,!,fail.
    check(Z).
    
    colorit([A,B,C,D]):-color(A),color(B),color(C),color(D),check([A,B,C,D]).
    

    Or using not:

    hasEdge(X,Y):-edge(X,Y) ; edge(Y,X).
    
    getelement(1,[Z|_],Z).
    getelement(X,[Z|Zs],M):-K is X-1,getelement(K,Zs,M).
    
    check(Z):-hasEdge(X,Y),getelement(X,Z,M),getelement(Y,Z,N),M=:=N.
    
    colorit([A,B,C,D]):-color(A),color(B),color(C),color(D),not(check([A,B,C,D])).