Tutorials › Graph Theory (Undergraduate) › Why Greedy Coloring Depends on Vertex Order

Vertex Coloring and Chromatic Number · Tutorial 347 of 799

Why Greedy Coloring Depends on Vertex Order

See how vertex order changes greedy colorings, and why some orders use more colors than an optimal coloring.

Intermediate 10 min read

What You'll Learn

  • Apply the first-fit rule to a specified vertex order.
  • Trace and check greedy colorings using explicit edge lists.
  • Compare greedy color counts with the chromatic number.
  • Identify how an early choice can force a later vertex to use a new color.
  • Prove that an order exists for which greedy coloring is optimal.

Why the Order Matters

A greedy coloring makes a local decision at each step: give the next vertex the smallest color not already used by its colored neighbors. The rule is simple, but its result can depend on which vertex comes next. A vertex may receive a color because of an earlier neighbor that would not have been colored yet in a different order. Later vertices must work with the choices already made; the algorithm does not go back and revise them.

We consider finite simple graphs. A proper vertex coloring assigns colors to vertices so that adjacent vertices receive different colors. The chromatic number \(\chi(G)\) is the smallest number of colors in any proper coloring of \(G\). A vertex order is a list containing every vertex exactly once. To compare orders fairly, we use the same rule each time and change only the list.

Definition: Given an order \(v_1,v_2,\dots,v_n\), the greedy coloring processes the vertices in that order. At each step it assigns \(v_i\) the smallest positive integer not assigned to any neighbor of \(v_i\) among \(v_1,\dots,v_{i-1}\). Colors are named \(1,2,3,\dots\); the number used is the largest color assigned, or \(0\) if the graph has no vertices.

Only already-colored neighbors restrict the choice at a step. A neighbor appearing later in the order has no effect yet. This is the key distinction between greedy coloring and a search that can revise earlier decisions: greedy coloring commits to each first available color immediately.

What the Rule Guarantees

Theorem: For every finite simple graph and every vertex order, the greedy coloring is proper.

Proof. Consider any edge \(uv\), and suppose \(u\) appears before \(v\). When the algorithm colors \(v\), the vertex \(u\) has already been colored. The rule assigns \(v\) a color different from the color of every already-colored neighbor, including \(u\). Thus the endpoints of every edge receive different colors, which is exactly the definition of a proper coloring. If the graph has no edges, the condition is vacuous, so the conclusion also holds. \(\square\)

This guarantee concerns validity, not optimality. A proper coloring may use more colors than necessary. In particular, the fact that the algorithm always chooses the smallest available color does not mean it uses the smallest possible total number of colors: its earlier choices can affect later vertices.

Worked Example: Two Orders of the Same Path

Let \(G\) have vertex set \(V(G)=\{1,2,3,4\}\) and edge set \(E(G)=\{12,23,34\}\). Thus \(G\) is the path \(P_4\). First use the order \(1,2,3,4\).

StepVertexColors on earlier neighborsAssigned color
11None1
22Neighbor 1 has color 12
33Neighbor 2 has color 21
44Neighbor 3 has color 12

The resulting color classes are \(\{1,3\}\) and \(\{2,4\}\). Now keep exactly the same graph but use the order \(1,4,2,3\).

StepVertexColors on earlier neighborsAssigned color
11None1
24None1
32Neighbor 1 has color 12
43Neighbor 2 has color 2; neighbor 4 has color 13

At the last step, colors 1 and 2 are both forbidden, so the first available color is 3. The coloring is proper: edge \(12\) has endpoint colors 1 and 2; edge \(23\) has colors 2 and 3; edge \(34\) has colors 3 and 1.

Check: the graph has a proper 2-coloring with classes \(\{1,3\}\) and \(\{2,4\}\). Since the graph has the edge \(12\), a proper coloring cannot use just one color. Hence \(\chi(G)=2\). The first order uses \(\chi(G)=2\) colors, while the second uses 3, so this order is not optimal.

How an Early Choice Can Force an Extra Color

In the second order above, vertices 1 and 4 are not adjacent, so both can be assigned color 1. Vertex 2 is adjacent to 1 but not 4, so it receives color 2. The final vertex, 3, is adjacent to both 2 and 4. Those two neighbors have different colors, leaving neither color 1 nor color 2 available. The extra color is forced by the combination of earlier choices around vertex 3, not by the graph needing three colors globally.

This also explains why inspecting the final coloring alone is not enough to understand the algorithm. To check a greedy calculation, reconstruct each step in the stated order and check exactly which earlier neighbors constrain the choice. Later neighbors do not block a color when a vertex is processed, though properness will still be ensured when those later vertices are colored.

Worked Example: A Graph Where Greedy Uses Four Colors but Three Suffice

Let \(G\) have vertex set \(V(G)=\{1,2,3,4,x\}\) and edge set \(E(G)=\{12,23,34,x1,x2,x3,x4\}\). The vertices \(1,2,3,4\) form a path, and \(x\) is adjacent to all four of them. Process the vertices in the order \(1,4,2,3,x\).

StepVertexColors on earlier neighborsAssigned color
11None1
24None1
32Neighbor 1 has color 12
43Neighbor 2 has color 2; neighbor 4 has color 13
5\(x\)Neighbors 1, 2, 3, 4 use colors 1, 2, 34

The greedy output uses four colors. But three colors suffice: assign color 1 to \(\{1,3\}\), color 2 to \(\{2,4\}\), and color 3 to \(\{x\}\). Every listed edge joins vertices with different colors. Also, \(x,1,2\) form a triangle: the edges \(x1\), \(12\), and \(x2\) are all present. A triangle requires three different colors, so \(\chi(G)\ge 3\). The displayed coloring gives \(\chi(G)\le 3\), and therefore \(\chi(G)=3\). The order uses 4 colors even though the chromatic number is 3.

Check: the four greedy color classes are \(\{1,4\}\), \(\{2\}\), \(\{3\}\), and \(\{x\}\). No edge has both endpoints in one of these classes. For the three-coloring, the path edges \(12,23,34\) join colors 1 and 2, 2 and 1, and 1 and 2, respectively; each edge \(xi\) joins color 3 to color 1 or 2. Thus both the greedy output and the claimed optimal coloring pass the edge check.

An Order That Attains the Chromatic Number

Order dependence does not mean that greedy coloring must be wasteful on every order. In fact, for every nonempty finite graph there is an order for which the greedy algorithm uses exactly \(\chi(G)\) colors. The existence argument gives a useful contrast: greedy coloring can be far from optimal on one order, but a suitable order can make it optimal.

Theorem: Every nonempty finite simple graph \(G\) has a vertex order for which greedy coloring uses exactly \(\chi(G)\) colors.

Proof. Write \(k=\chi(G)\), and choose a proper coloring with \(k\) colors. Such a coloring exists by the definition of \(\chi(G)\); because \(G\) is finite, assigning a different color to every vertex gives at least one proper coloring, so the set of possible color counts has a minimum. Let its nonempty color classes be \(C_1,\dots,C_k\), with the classes listed in any order, and order the vertices by listing all of \(C_1\), then all of \(C_2\), and so on through \(C_k\). We show inductively that vertices in \(C_i\) receive only colors from \(\{1,\dots,i\}\). For \(C_1\), its vertices are pairwise nonadjacent, so each has no earlier neighbor in \(C_1\) and receives color 1. Now suppose every vertex in \(C_1\cup\cdots\cup C_{i-1}\) has color at most \(i-1\). A vertex in \(C_i\) has no neighbor earlier in \(C_i\), because \(C_i\) is an independent set. Its earlier neighbors are therefore in earlier classes and have colors at most \(i-1\). Color \(i\) is available, so the greedy rule assigns a color at most \(i\). This proves the claim for every class. The resulting greedy coloring uses at most \(k\) colors, and it is proper by the preceding theorem. Since \(k=\chi(G)\), no proper coloring can use fewer than \(k\) colors. Thus this order uses exactly \(k\) colors. \(\square\)

The argument does not say that every order grouped by an arbitrary collection of color classes will work. The classes used in the proof must come from an optimal coloring, and the proof’s ordering puts each entire class before the next. Even with an optimal coloring in hand, a different order can make earlier greedy choices introduce colors that are unnecessary globally.

Worked Example: Changing the Order Need Not Change the Answer

Let \(G\) have vertex set \(V(G)=\{a,b,c\}\) and edge set \(E(G)=\{ab,ac,bc\}\), so \(G=K_3\). Compare orders \(a,b,c\) and \(b,c,a\).

OrderStep-by-step greedy colorsNumber used
\(a,b,c\)\(a\) has no earlier neighbors, so gets 1; \(b\) sees color 1, so gets 2; \(c\) sees colors 1 and 2, so gets 3.3
\(b,c,a\)\(b\) has no earlier neighbors, so gets 1; \(c\) sees color 1, so gets 2; \(a\) sees colors 1 and 2, so gets 3.3

Every pair of vertices in \(K_3\) is adjacent, so a proper coloring needs three distinct colors. Assigning one distinct color to each vertex gives a proper coloring with three colors, hence \(\chi(G)=3\). Both orders use exactly the chromatic number. This example shows that order dependence means the output can vary with the order, not that it must vary for every pair of orders.

Check: in each row, the endpoints of \(ab\), \(ac\), and \(bc\) have different colors. The lower bound of three follows from those three pairwise adjacent vertices, and the three displayed colors achieve that bound.

A Common Mistake

Wrong calculation: In the \(P_4\) example with order \(1,4,2,3\), one might say that vertex 3 gets color 1 because it is not adjacent to vertex 1, or color 2 because its most recently processed neighbor, vertex 2, has color 2. Neither calculation applies the rule: vertex 3 is adjacent to both 2 and 4, and both are already colored.

Correction: At the step for vertex 3, its earlier neighbors are 2 and 4. Their colors are 2 and 1, respectively. Thus color 1 is forbidden by neighbor 4, color 2 is forbidden by neighbor 2, and the smallest available color is 3. Check every earlier neighbor, not only the immediately preceding vertex or one neighbor selected for inspection.

When using a greedy coloring, keep the order visible and record the colors on the current vertex’s earlier neighbors before assigning its color. Then check the final result edge by edge. This separates two questions: whether the output is a proper coloring, and whether it uses the minimum possible number of colors.

Check Your Understanding

For each question, state the graph or order clearly and show the relevant greedy steps.

  1. For the path with vertex set \(\{1,2,3,4,5\}\) and edge set \(\{12,23,34,45\}\), run greedy coloring in the order \(1,3,5,2,4\). How many colors are used?
  2. Give a proper coloring of that path using two colors, and explain why one color is not enough.
  3. Explain why the greedy rule must check all earlier colored neighbors, rather than only the most recently processed vertex.
  4. In the theorem about an order attaining \(\chi(G)\), why must each color class be listed as a block before the next class?
  5. For \(K_3\), explain why no vertex order can make greedy coloring use fewer than three colors.