Prolog中的图形着色:我做错了什么?

时间:2014-05-07 20:49:39

标签: graph prolog graph-coloring

我正在为一项简单的技能编写Prolog程序,为图表着色。我已经完成了清单上的所有操作:使用边缘列表制作图形,制作颜色事实,制作颜色绑定,指定颜色,检查冲突等等。它总是返回false!我一直试图稍微调整一下,但没有任何效果:

color(red).
color(green).
color(blue).
color(yellow).
color(purple).
color(brown).

edge(A,A) :- [A,A].

cb(Item,Color).

graph([edge(A,A)]).

vertex(A,B) :- edge(A,B) ; edge(B,A).

colorItem([],[]).
colorItem([Item|Items], [Binding|Bindings]) :-
    colorItem(Items, Bindings),
    Binding = cb(Item, Color),
    color(Color),
    noconflict(Binding, Bindings).

colorGraph(Graph, Bindings) :- colorItem(Graph,Bindings).

noconflict(_,[]).
noconflict(Cb1,[Cb2|Cbs]) :- not(conflict(Cb1,Cb2)), noconflict(Cb1,Cbs).

conflict(cb(N1,Color), cb(N2,Color)) :- vertex(N1,N2).

看起来应该像IIRC这样:

?- colorGraph([edge(ny,nj)], Bindings).
Bindings = [cb(nj, green), cb(ny, red)] ;
Bindings = [cb(nj, blue), cb(ny, red)] ;
Bindings = [cb(nj, yellow), cb(ny, red)] .

但我明白了:

?- colorGraph([edge(ny,nj)], Bindings).
Bindings = [cb(edge(ny, nj), red)] 
Bindings = [cb(edge(ny, nj), green)] 
Bindings = [cb(edge(ny, nj), blue)] 
Bindings = [cb(edge(ny, nj), yellow)] 
Bindings = [cb(edge(ny, nj), purple)] 
Bindings = [cb(edge(ny, nj), brown)].

我的边缘和图形都缺少一些明显而重要的东西。任何建议或提示将不胜感激。

1 个答案:

答案 0 :(得分:3)

替换:

colorGraph(graph, bindings) :- colorItem(graph,bindings).

使用:

colorGraph(Graph, Bindings) :- colorItem(Graph, Bindings).

变量以Prolog中的下划线或大写字母开头。显然与其他一些谓词相同。