【问题标题】:I need an algorithm that will both find the minimal number of colors for coloring a graph and ensure that no two adajcent vertices have the same color我需要一种算法,既能找到最少的颜色来为图形着色,又能确保没有两个相邻顶点具有相同的颜色
【发布时间】:2020-04-09 19:55:02
【问题描述】:

我需要一个回溯算法,通过尊重相邻顶点不能具有相同颜色的事实来为图形着色。我们谈论的是无向连通图。我还需要相同的算法来确定为图形着色所需的不同颜色的最少数量。这基本上意味着我需要找到色数。

我已经找到了如何使用回溯方法为图形着色,但我还没有找到如何找到色数。是否有众所周知的算法(使用回溯,我已经知道一种贪婪的方法)。

【问题讨论】:

  • 首先,找到一个节点的最大等级(如果grade(u) = 3,则表示u连接到其他3个节点,您至少需要4种颜色)。从那里开始。如果u 的等级最高,请尝试使用正常回溯方法仅用 4 种颜色为图形着色。如果做不到,请再试一个,依此类推。

标签: algorithm graph


【解决方案1】:

使用MiniZinc约束服务器,你可以很容易地表达这样一个问题:

%  Petersen Graph
set of int: Colors = 1..3;
set of int: Nodes = 1..10;
set of int: Edges = 1..15;
array[Edges] of Nodes: aFrom = [ 1, 2, 3, 4, 1, 1, 2, 3, 4,  5, 6,  7, 7,  8, 6 ];
array[Edges] of Nodes: aTo   = [ 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 8, 10, 9, 10, 9 ];

array[int] of string: colorName = [ "red", "green", "blue", "purple", "yellow", "brown", "black" ];

array[Nodes] of var Colors: nodeColor;

constraint
  forall(e in Edges) (
      nodeColor[aFrom[e]] != nodeColor[aTo[e]]
  );

solve satisfy;

output [ show(colorName[nodeColor[n]]) ++ "\n" | n in Nodes ]; 

要确定颜色的最小数量,您可以递减Colors,直到找不到解决方案。

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2022-10-19
    • 2015-07-16
    相关资源
    最近更新 更多