# 贪心着色

**贪心着色**是一种给图的顶点着色的方法：顶点按某个固定顺序处理，每个顶点都获得其已着色邻居尚未使用的最小号码颜色。它也被称为首次适应算法（first-fit algorithm），是图着色问题最简单的启发式方法之一；图着色问题即给顶点分配颜色，使任意两个相邻顶点都不同色。<sup>[1](https://arxiv.org/abs/1407.5336)</sup><sup> • </sup><sup>[2](https://ndl.ethernet.edu.et/bitstream/123456789/77242/1/25.pdf)</sup>

| 关键事实 | 细节 |
|---|---|
| 规则 | 按选定顺序处理每个顶点，赋予其已着色邻居未使用的最小颜色<sup>[1](https://arxiv.org/abs/1407.5336)</sup> |
| 通用上界 | 任何贪心着色最多使用 Δ + 1 种颜色，其中 Δ 是最大度；完全图和奇圈可达到该上界<sup>[3](https://fabiofurini.github.io/combinatorial-optimization/heuristic-algorithms/constructive-heuristics/vertex-coloring-greedy-and-dsatur/)</sup> |
| 退化度上界 | 如果顶点可排序，使每个顶点之前至多出现 d 个它的邻居，则贪心最多使用 d + 1 种颜色，因此 χ(G) ≤ d(G) + 1<sup>[4](https://onlinelibrary.wiley.com/doi/10.1002/jgt.70070)</sup> |
| 最坏情况 | 在具有 2k 个顶点、色数为 2 的冠图上，一个坏排序会迫使贪心使用 k = n/2 种颜色<sup>[3](https://fabiofurini.github.io/combinatorial-optimization/heuristic-algorithms/constructive-heuristics/vertex-coloring-greedy-and-dsatur/)</sup> |
| Grundy数 | 贪心在所有排序上可使用的最多颜色数；判定其是否至少为 k，即使在二分图上也是 NP 完全的<sup>[1](https://arxiv.org/abs/1407.5336)</sup> |
| 最优性条件 | 对每个图，至少存在一个排序使贪心最优，即依次取一个最优着色的各个颜色类<sup>[3](https://fabiofurini.github.io/combinatorial-optimization/heuristic-algorithms/constructive-heuristics/vertex-coloring-greedy-and-dsatur/)</sup> |
| 应用 | 编译器中的寄存器分配、排课、频率分配和调度<sup>[5](https://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-506.pdf)</sup> |

## 算法

给定一个图 G，其顶点按选定顺序为 v₁, …, vₙ，算法给每个顶点 vᵢ 赋予最小的正整数，该正整数未出现在任何已着色的邻居 vⱼ（j < i）上。<sup>[1](https://arxiv.org/abs/1407.5336)</sup> 一种等价表述按相反顺序处理顶点：为每个未着色顶点维护一张可用颜色列表，并在给某个顶点选好颜色后，从其邻居的列表中删去该颜色；如果该排序见证退化度 d，则每张列表都保持非空，该过程产生一个合法的 (d + 1)-着色。<sup>[4](https://onlinelibrary.wiley.com/doi/10.1002/jgt.70070)</sup>

运行时间并不大。基本贪心算法的实现在有 n 个顶点、m 条边的图上以 O(n + m) 时间运行；威尔士-鲍威尔（Welsh-Powell）变体先按度数递减对顶点排序，以 O(n log n + m) 时间运行。<sup>[6](https://www.rhydlewis.eu/gcol/)</sup>

## 排序决定了什么

贪心使用的颜色数完全取决于顶点顺序。对每一种排序都有两个保证。第一，贪心使用的颜色从不超过 Δ + 1：当一个顶点被着色时，它已着色的邻居至多占用 deg(v) ≤ Δ 种颜色，因此颜色 1, …, Δ + 1 中总有一种空闲。<sup>[3](https://fabiofurini.github.io/combinatorial-optimization/heuristic-algorithms/constructive-heuristics/vertex-coloring-greedy-and-dsatur/)</sup> 第二，总存在某种排序是最优的：若 Q₁, …, Q_χ 是一个最优着色的颜色类，先处理 Q₁ 的顶点，再处理 Q₂ 的顶点，依此类推，则 Q_c 中每个顶点得到的颜色至多为 c，因此至多使用 χ(G) 种颜色。困难完全在于找到那种排序。<sup>[3](https://fabiofurini.github.io/combinatorial-optimization/heuristic-algorithms/constructive-heuristics/vertex-coloring-greedy-and-dsatur/)</sup>

**坏排序可能非常糟糕。** 在具有 2k 个顶点的冠图上——即完全二分图 K_{k,k} 去掉一个完美匹配——色数为 2，但按 u₁, v₁, u₂, v₂, …, u_k, v_k 的顺序，贪心会给 uᵢ 和 vᵢ 都赋予颜色 i，并使用 k = n/2 种颜色。所用颜色数与色数之比为 k/2 = n/4，会无界增长，因此对于任意排序，贪心没有常数性能保证。<sup>[3](https://fabiofurini.github.io/combinatorial-optimization/heuristic-algorithms/constructive-heuristics/vertex-coloring-greedy-and-dsatur/)</sup> 随机排序同样表现不佳：对每个 n，都存在一个图，其色数至多 n^ε，但随机选取的排序使贪心所用颜色数低于 (1 − ϑ)n log₂ n 的概率为 O(n^{−Δ})。<sup>[7](https://www.sciencedirect.com/science/article/abs/pii/0196677491900406)</sup>

**排序启发式。** 威尔士和鲍威尔在 1967 年将图着色与排课联系起来的论文中，提出在应用贪心之前按度数递减对顶点排序，并由此推导出一个用顶点度数表示的新色数上界。<sup>[8](https://doi.org/10.1093%2Fcomjnl%2F10.1.85)</sup> 后来的启发式动态地改进了这一思想：DSATUR（布雷拉兹（Brélaz），1979）下一步给邻域中不同颜色数量——即饱和度——最大的未着色顶点着色，并且在二分图上总是至多使用 2 种颜色；而在二分图上，普通贪心遇到坏排序可能表现得很糟。<sup>[3](https://fabiofurini.github.io/combinatorial-optimization/heuristic-algorithms/constructive-heuristics/vertex-coloring-greedy-and-dsatur/)</sup> 其他标准排序包括最大度优先和最小度最后；经训练以产生排序的图神经网络已被证明在着色质量上优于这两种排序，同时在运行时间上仍有竞争力。<sup>[9](https://arxiv.org/abs/2408.05054)</sup>

## Grundy数

一个图的 **Grundy数** Γ(G) 是最大的 k，使得存在某种顶点排序能让首次适应算法产生一个合法的 k-着色；等价地说，它是贪心可能被迫使用的最坏情况颜色数。<sup>[1](https://arxiv.org/abs/1407.5336)</sup> 这一概念可追溯到帕特里克·迈克尔·格伦迪（Patrick Michael Grundy）1939 年关于博弈与有向图的工作，并由克劳德·A·克里斯滕（Claude A. Christen）和斯坦利·M·塞尔科夫（Stanley M. Selkow）于 1979 年正式提出；他们将 Grundy n-着色定义为：颜色为 i 的每个顶点，对于每一种比 i 小的颜色，都至少与一个该颜色的顶点相邻，且不与任何同色顶点相邻。<sup>[1](https://arxiv.org/abs/1407.5336)</sup><sup> • </sup><sup>[10](https://www.sciencedirect.com/science/article/pii/0095895679900674)</sup> 西蒙斯（Simmons）于 1982 年独立地将其定义为赭色数（ochromatic number），二者的等价性于 1987 年得到证明。<sup>[1](https://arxiv.org/abs/1407.5336)</sup>

Grundy数总是满足 χ(G) ≤ Γ(G) ≤ Δ(G) + 1，但即使对二分图，差值 Γ(G) − χ(G) 也可以任意大；一棵色数为 2 的树可以有 Grundy数 4。<sup>[1](https://arxiv.org/abs/1407.5336)</sup> 计算 Γ(G) 一般是困难的：判定问题 GRUNDY着色问题——问是否 Γ(G) ≥ k——在二分图、二分图的补图、弦图和线图上都是 NP 完全的，不过树有线性时间算法，有界树宽图有多项式时间算法。<sup>[1](https://arxiv.org/abs/1407.5336)</sup><sup> • </sup><sup>[11](https://arxiv.org/abs/2412.10082)</sup> 精确指数算法对 GRUNDY着色问题以 O*(2.443ⁿ) 时间运行，对弱变体（其中着色不必是合法的）以 O*(2.716ⁿ) 时间运行。<sup>[1](https://arxiv.org/abs/1407.5336)</sup> 以颜色数为参数时，该问题一般是 W[1]难的，但在弦图、无爪图以及排除某个固定子式的图上固定参数可处理；一个连通变体——要求已着色顶点在每一步都保持连通——在 k = 7 种颜色时就已经是 NP 完全的。<sup>[1](https://arxiv.org/abs/1407.5336)</sup><sup> • </sup><sup>[11](https://arxiv.org/abs/2412.10082)</sup>

## 退化度与弱退化度

一个图的退化度 d(G) 是最小的 d，使得可以给顶点排序，每个顶点之前至多出现 d 个它的邻居；它给出保证 χ(G) ≤ d(G) + 1，但这个上界很少是紧的：每个 d-正则图的退化度都恰为 d，然而根据布鲁克斯（Brooks）定理，唯一色数为 d + 1 的连通 d-正则图是完全图和奇圈。<sup>[4](https://onlinelibrary.wiley.com/doi/10.1002/jgt.70070)</sup>

为了刻画贪心方法的真正威力，伯恩施泰因（Bernshteyn）和李（Lee）引入了**弱退化度**；在这个参数中，当一个顶点自己的列表更大时，它可以从某个邻居的列表之外选择一种颜色，从而为该邻居“省下”一种颜色。如果一个图是弱 d-退化的，那么 wd(G) + 1 不仅是色数的上界，也是列表色数、DP色数和涂色数的上界。<sup>[4](https://onlinelibrary.wiley.com/doi/10.1002/jgt.70070)</sup> 在正则图上，这一参数严格强于退化度：除圈和完全图外，所有连通 d-正则图都是弱 (d − 1)-退化的；并且对每个 d，都存在弱退化度恰为 ⌊d/2⌋ + 1 的 d-正则图。<sup>[4](https://onlinelibrary.wiley.com/doi/10.1002/jgt.70070)</sup> 对于平面图，退化度只给出上界 6，而修正后的结果是平面图是弱 4-退化的，这重新得到了托马森（Thomassen）定理：平面图是 5-列表可着色的。<sup>[4](https://onlinelibrary.wiley.com/doi/10.1002/jgt.70070)</sup>

## 应用

**寄存器分配。** 编译器通过构建干涉图，把程序变量映射到一组有限的机器寄存器上；若两个变量的活跃范围重叠，就在它们之间连一条边，然后用一种颜色代表一个寄存器来给图着色。柴廷（Chaitin）及其同事首先表明，图着色可以作为有效寄存器分配器的基础；尽管着色是 NP 完全的，但这一场景容忍近似，因为值可以被溢出到内存，而且简单的启发式就能达到接近最优的分配。<sup>[5](https://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-506.pdf)</sup> 贪心思想在实践中占主导：LLVM 的默认寄存器分配器是贪心分配器，它是对其基本分配器的高度调优实现，带有全局活跃范围拆分，旨在最小化溢出代价；其发布说明报告，与线性扫描分配相比，代码小 1–2%，运行速度最多快 10%。<sup>[12](https://llvm.org/docs/CodeGenerator.html)</sup><sup> • </sup><sup>[13](https://blog.llvm.org/2011/09/greedy-register-allocation-in-llvm-30.html)</sup> 在 SSA 形式的干涉图上（这类图是弦图），贪心着色在线性时间内就是最优的。<sup>[14](https://homepages.dcc.ufmg.br/~fernando/projects/APLAS2005/)</sup>

**调度与排课。** 把考试分配到时间段，使任何学生都不会同时有两场考试，正是在给冲突图着色；威尔士和鲍威尔的贪心上界正是为排课而提出的。<sup>[8](https://doi.org/10.1093%2Fcomjnl%2F10.1.85)</sup> 无线网络中的频率分配和分布式对等网络配置是进一步的应用领域；在区块链网络实验中，包括 DSATUR 在内的贪心变体被用来平衡资源分配，其中 DSATUR 实现了最均匀的颜色分布。<sup>[15](https://www.mdpi.com/2227-7080/13/1/33/xml)</sup>

**在线着色。** 在在线场景中，顶点逐个到达，并且必须在看到后续顶点之前不可撤销地着色；FirstFit 是把贪心规则应用于到达顺序的算法，是概念上最简单的在线算法。它在最坏情况下表现很差；近期工作建立了已揭示图的结构与 FirstFit 所用颜色数之间的关系，并将其扩展到 FirstFitPredictions；后者纳入机器学习得到的颜色预测，既具有一致性，又具有平滑性。<sup>[16](https://arxiv.org/abs/2312.00601)</sup>

## References

1. [Edouard Bonnet, Florent Foucaud, Eun Jung Kim, Florian Sikora. Complexity of Grundy coloring and its variants. arXiv:1407.5336.](https://arxiv.org/abs/1407.5336)
2. [Algorithmic Random Graph Coloring, lecture text.](https://ndl.ethernet.edu.et/bitstream/123456789/77242/1/25.pdf)
3. [Fabio Furini. Vertex coloring: greedy and DSATUR, Combinatorial Optimization lecture notes.](https://fabiofurini.github.io/combinatorial-optimization/heuristic-algorithms/constructive-heuristics/vertex-coloring-greedy-and-dsatur/)
4. [Antoni Bernshteyn et al. Weak Degeneracy of Planar Graphs. Journal of Graph Theory (2026).](https://onlinelibrary.wiley.com/doi/10.1002/jgt.70070)
5. [Applications of graph coloring, MIT LCS Technical Report TR-506.](https://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-506.pdf)
6. [R. M. R. Lewis. Graph colouring algorithm implementations (GCol).](https://www.rhydlewis.eu/gcol/)
7. [The greedy coloring is a bad probabilistic algorithm. Journal of Algorithms.](https://www.sciencedirect.com/science/article/abs/pii/0196677491900406)
8. [D. J. A. Welsh, M. B. Powell (1967). An upper bound for the chromatic number of a graph and its application to timetabling problems. The Computer Journal 10(1): 85–86.](https://doi.org/10.1093%2Fcomjnl%2F10.1.85)
9. [Graph Neural Networks as Ordering Heuristics for Parallel Graph Coloring. arXiv:2408.05054.](https://arxiv.org/abs/2408.05054)
10. [Claude A. Christen, Stanley M. Selkow (1979). Some perfect coloring properties of graphs. Journal of Combinatorial Theory, Series B 27(1): 49–59.](https://www.sciencedirect.com/science/article/pii/0095895679900674)
11. [Fast FPT Algorithms for Grundy Number on Dense Graphs. arXiv:2412.10082.](https://arxiv.org/abs/2412.10082)
12. [The LLVM Target-Independent Code Generator, LLVM documentation.](https://llvm.org/docs/CodeGenerator.html)
13. [Jakob Stoklund Olesen (2011). Greedy Register Allocation in LLVM 3.0. LLVM Project Blog.](https://blog.llvm.org/2011/09/greedy-register-allocation-in-llvm-30.html)
14. [The implementation of three different register allocators, UFMG APLAS 2005 project.](https://homepages.dcc.ufmg.br/~fernando/projects/APLAS2005/)
15. [Švarcmajer, Ivanović, Rudec, Lukić (2025). Application of Graph Theory and Variants of Greedy Graph Coloring Algorithms for Optimization of Distributed Peer-to-Peer Blockchain Networks. Technologies 13(1): 33.](https://www.mdpi.com/2227-7080/13/1/33/xml)
16. [Online Graph Coloring with Predictions. arXiv:2312.00601.](https://arxiv.org/abs/2312.00601)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Graph theory › Computational graph problems and algorithms › Graph coloring algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/greedy-coloring

English original: https://www.edgepedia.org/greedy-coloring
