I have a program for coloring graphs with 3 colors, neighbouring nodes need to have different colors.
My problem is, it is working only for directed graph, when I use non-directed graph it fails on stack overflow. I know there are some mistakes, could you help me to make it work for non-directed graph?
There is also problem with that findall/3
at the end. I need to change it to finding all nodes, not only nodes with edge(V,_)
but I don't know exactly how to do that.
I'm beginner and I need the solution to be simple. Thanks.
edge(1,2).
edge(2,3).
edge(2,4).
edge(3,4).
%for making the non-oriented graph I tried to use nonedge(X, Y) :- edge(X, Y).
% nonedge(X, Y) :- edge(Y, X).
color(blue).
color(red).
color(green).
coloring([V-C]) :-
color(C),
\+ edge(V,_).
coloring([V-C,V1-C1|Coloring]) :-
color(C),
edge(V, V1),
V \== V1,
coloring([V1-C1|Coloring]),
C1 \== C.
colors(X) :-
coloring(X),
findall(V, edge(V,_), List),
length(List, Len),
length(X, Len).