algorithm - 我需要一种算法,既能找到最少的颜色来为图形着色,又能确保没有两个相邻顶点具有相同的颜色
问题描述
我需要一个回溯算法,通过尊重没有相邻顶点可以具有相同颜色的事实来为图形着色。我们正在谈论一个无向连通图。我还需要相同的算法来确定为图形着色所需的不同颜色的最少数量。这基本上意味着我需要找到色数。
我已经找到了如何使用回溯方法为图形着色,但我还没有找到如何找到色数。是否有众所周知的算法(使用回溯,我已经知道一种贪婪的方法)。
解决方案
使用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
直到找不到解决方案。
推荐阅读
- reactjs - 错误不变违规:requireNativeComponent:在 UIManager 中找不到“RNSVGLinearGradient”。IOS React Native
- firebase - 火力基地 | 在functions.pubsub.schedule onRun上获取RemoteConfig值时出错
- dns - 如果 /etc/hosts 有负载均衡器的条目 0.0.0.0/0 将调用哪个 IP
- python - 在 Pandas 中扩展列的所有类别中的缺失值
- vb.net - 值不能为空。(参数'baseName')
- javascript - 无法在 ASP.NET MVC 中使用 Ajax 提交表单
- python - 更快(矢量化)的方式来使用日期执行此 pandas 公式
- python - 使用 Beautiful Soup 从 Tableau 仪表板中抓取悬停框
- python - 从列表 Python 中删除项目
- c# - 所需的防伪 cookie“__RequestVerificationToken_...”不存在