algorithm - 如何 - Prolog 中的图形着色

标签 algorithm prolog graph-theory graph-coloring

我正在尝试在 Prolog 中制作简单的图形着色算法,但我在理解该语言方面遇到了一些困难。我知道我想要做什么 - 我想要去一个顶点,找到连接到它的所有其他顶点,检查我的顶点的颜色,并根据情况,用不同的颜色为其他顶点着色。我只是很难将其翻译成 Prolog。如果它是 C 语言或 Java,这对我来说是小菜一碟,但这让我很不舒服。

这是我到目前为止所拥有的:

main:- graph_coloring.

%color_list([blue, red, green, yellow, white], colors).
%vertex_list([a, b, c, d], vertices).
%edge_list([(a,b),(b,c),(c,d)], edges).

%Our graph
color(blue).
color(red).
color(green).
color(black).
color(white).

%graph([a-b, b-c, b-d, c-d]).

vertex(a).
vertex(b).
vertex(c).
vertex(d).

%Subject to changing, so are asserted into listener at runtime.
init_dynamic_facts:-
    assertz(vertex_color(a, none)),
    assertz(vertex_color(b, none)),
    assertz(vertex_color(c, none)),
    assertz(vertex_color(d, none)),
    assertz(location(a)).

edge(a,b).
edge(b,c).
edge(b,d).
edge(c,d).

is_connect(A,B):-
    edge(A,B).
is_connect(A,B):-
    edge(B,A).

connections(Vertex):-
    edge(Vertex,X).
connections(Vertex):-
    edge(X,Vertex).

move(Vertex):-
    retract(location(_)),
    asserta(location(Vertex)).

paint_vertex(Vertex, Color):-
    retract(vertex_color(Vertex,_)),
    asserta(vertex_color(Vertex, Color)).

find_vertex_color(Vertex):-
    vertex_color(Vertex, X).


graph_coloring:-

    location(Current_vertex),
    vertex_color(Current_vertex, Curr_color),
    ( Curr_color =:= none ->
        connections(Current_vertex, Others),
        vertex_color(Others, Other_colors),
        paint_vertex(Current_vertex, 

如何完成这个算法?

(编辑:graph_coloring 下的更多代码)

最佳答案

我想提一下,这个问题是一个典型的约束满足问题,可以使用SWI-Prolog的CSP模块有效地解决。这是完整的算法:

:- use_module(library(clpfd)). 

color(red).
color(blue).
color(green).

vertex(a).
vertex(b).
vertex(c).
vertex(d).
vertex(e).

edge(a,b).
edge(a,c).
edge(b,c).
edge(b,d).
edge(c,d).


colorGraph(ColorList) :- 
  findall((X, Y), edge(X, Y), Edges),
  findall(X, vertex(X), Vertexes),
  findall(hasColor(X, _), member(X, Vertexes), ColorList),
  createConstraint(Edges,ColorList),
  colorNodes(ColorList).

createConstraint([],_).
createConstraint([(V1,V2)|RL],ColorList):-
  member(hasColor(V1,C1),ColorList),
  member(hasColor(V2,C2),ColorList),
  dif(C1,C2),
  createConstraint(RL,ColorList).

colorNodes([]).
colorNodes([hasColor(_,C)|Nodes]) :-
  color(C),
  colorNodes(Nodes).

color/1 表示可用的颜色,vertex/1 表示图中的顶点,edge/2 定义顶点之间的对。顶点。此外,colorGraph(?List) 确定顶点的颜色,其中 ListhasColor(Vertex, Color) 子句的列表,Vertex 是使用 Color 着色的顶点。

让我们详细介绍上面算法的每个部分,以了解会发生什么。

:- use_module(library(clpfd)).

它指示SWI-Prolog加载包含约束满足问题谓词的模块。

colorGraph(ColorList) :- 
  findall((X, Y), edge(X, Y), Edges),
  findall(X, vertex(X), Vertexes),
  findall(hasColor(X, _), member(X, Vertexes), ColorList),
  createConstraint(Edges,ColorList),
  colorNodes(ColorList).

谓词colorGraph/1是算法的入口点。它将边/顶点的子句转换为列表,约束 ColorList 以定义顶点列表,最后创建颜色约束并分配颜色(它们是两个独立的操作)。

createConstraint([],_).
createConstraint([(V1,V2)|RL],ColorList):-
  member(hasColor(V1,C1),ColorList),
  member(hasColor(V2,C2),ColorList),
  dif(C1,C2),
  createConstraint(RL,ColorList).

预测createConstraint/2只是指出两个链接的顶点必须具有不同的颜色。值得一提的是 dif/2 是一个 CSP 谓词。

colorNodes([]).
colorNodes([hasColor(_,C)|Nodes]) :-
  color(C),
  colorNodes(Nodes).

谓词colorNodes/1为顶点分配正确的颜色。 Prolog 将根据上面定义的约束来注意分配正确的颜色。

最后可以通过调用谓词colorGraph/1来查找结果,如:

?- colorGraph(L).
L = [hasColor(a, red), hasColor(b, blue), hasColor(c, green), hasColor(d, red), hasColor(e, red)] ;
L = [hasColor(a, red), hasColor(b, blue), hasColor(c, green), hasColor(d, red), hasColor(e, blue)] ;
L = [hasColor(a, red), hasColor(b, blue), hasColor(c, green), hasColor(d, red), hasColor(e, green)] ;
L = [hasColor(a, red), hasColor(b, green), hasColor(c, blue), hasColor(d, red), hasColor(e, red)] ;
L = [hasColor(a, red), hasColor(b, green), hasColor(c, blue), hasColor(d, red), hasColor(e, blue)] ;
L = [hasColor(a, red), hasColor(b, green), hasColor(c, blue), hasColor(d, red), hasColor(e, green)] ;
L = [hasColor(a, blue), hasColor(b, red), hasColor(c, green), hasColor(d, blue), hasColor(e, red)] ;
L = [hasColor(a, blue), hasColor(b, red), hasColor(c, green), hasColor(d, blue), hasColor(e, blue)] ;
L = [hasColor(a, blue), hasColor(b, red), hasColor(c, green), hasColor(d, blue), hasColor(e, green)] ;
L = [hasColor(a, blue), hasColor(b, green), hasColor(c, red), hasColor(d, blue), hasColor(e, red)] ;
L = [hasColor(a, blue), hasColor(b, green), hasColor(c, red), hasColor(d, blue), hasColor(e, blue)] ;
L = [hasColor(a, blue), hasColor(b, green), hasColor(c, red), hasColor(d, blue), hasColor(e, green)] ;
L = [hasColor(a, green), hasColor(b, red), hasColor(c, blue), hasColor(d, green), hasColor(e, red)] ;
L = [hasColor(a, green), hasColor(b, red), hasColor(c, blue), hasColor(d, green), hasColor(e, blue)] ;
L = [hasColor(a, green), hasColor(b, red), hasColor(c, blue), hasColor(d, green), hasColor(e, green)] ;
L = [hasColor(a, green), hasColor(b, blue), hasColor(c, red), hasColor(d, green), hasColor(e, red)] ;
L = [hasColor(a, green), hasColor(b, blue), hasColor(c, red), hasColor(d, green), hasColor(e, blue)] ;
L = [hasColor(a, green), hasColor(b, blue), hasColor(c, red), hasColor(d, green), hasColor(e, green)] ;

关于algorithm - 如何 - Prolog 中的图形着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10713690/

相关文章:

algorithm - 状态空间搜索 : A* and Breadth First Search

带约束的 Prolog 查询返回 false,但有答案

algorithm - Bellman-Ford 的负循环

algorithm - 查找操作序列

选择最小篮子数的算法策略

algorithm - 识别Namaz在特定国家的时间

prolog - 如果标签大于 0,Gerrit 允许提交(序言规则)

Prolog回溯

scala - 图 : Is it possible to execute a program on each vertex without receiving a message?

algorithm - 在压缩的有向图中检测循环?