计算prolog中的地图着色数

时间:2012-02-26 06:37:23

标签: prolog combinatorics

在prolog中,我们可以获得地图着色问题的解决方案,如下所示:

different(red, green). different(red, blue). 
different(green, red). different(green, blue). 
different(blue, red). different(blue, green). 

coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :-
  different(Mississippi, Tennessee), 
  different(Mississippi, Alabama), 
  different(Alabama, Tennessee), 
  different(Alabama, Mississippi), 
  different(Alabama, Georgia), 
  different(Alabama, Florida), 
  different(Georgia, Florida), 
  different(Georgia, Tennessee). 

?- coloring(Alabama, Mississippi, Georgia, Tennessee, Florida).
Alabama = blue
Florida = green
Georgia = red
Mississippi = red
Tennessee = green

是否可以计算可能的地图着色总数?

2 个答案:

答案 0 :(得分:1)

使用Anders解决方案时,请注意“着色”一词应为小写,这将给出正确的答案 - 6个解决方案。

aggregate_all(count, coloring(Alabama, Mississippi, Georgia, Tennessee, Florida), Count).
Count = 6.

答案 1 :(得分:0)

是。 findall/3将枚举所有解决方案,并且(至少在SWI-Prolog中)aggregate_all/3可用于执行计数:

?- aggregate_all(count, Coloring(A,M,G,T,F), Num).
Num = 4711.