Tutorials › Graph Theory (Undergraduate) › Greedy Coloring by Vertex Order

Vertex Coloring and Chromatic Number · Tutorial 345 of 799

Greedy Coloring by Vertex Order

Follow a fixed vertex order, choose the least available color at each step, and check why the completed assignment is proper.

Intermediate 10 min read

What You'll Learn

  • Define a vertex order and the colors available when a vertex is processed.
  • Apply the greedy rule one vertex at a time.
  • Record which already colored neighbors forbid each color.
  • Verify the output by checking every edge.
  • Explain why the algorithm always produces a proper coloring.
  • Identify the error in a calculation that overlooks an earlier colored neighbor.

The Greedy Rule for a Fixed Order

A vertex-coloring procedure needs a rule for deciding what to do next. In greedy coloring, we first choose an order of the vertices, then process them one by one. Each vertex receives the smallest positive integer that has not already been assigned to any of its previously colored neighbors. The rule is simple, but the result is determined by the local information available at each step: unprocessed neighbors do not yet restrict the current choice.

Throughout, graphs are finite and simple. A proper vertex coloring assigns colors to vertices so that the endpoints of every edge have different colors. We represent colors by positive integers \(1,2,3,\ldots\); the integers are labels, not quantities to be compared in any way other than their order. A vertex order of a graph with \(n\) vertices is a list \(v_1,v_2,\ldots,v_n\) in which every vertex occurs exactly once.

Definition: Given a graph \(G=(V,E)\) and a specified vertex order \(v_1,v_2,\ldots,v_n\), the greedy coloring algorithm processes \(v_1,\ldots,v_n\) in that order. When processing \(v_i\), it assigns to \(v_i\) the least positive integer not already assigned to a neighbor \(v_j\) with \(j<i\). Such neighbors are the already colored neighbors of \(v_i\).

More formally, let \(c(v_j)\) denote the color assigned at step \(j\). The colors forbidden at step \(i\) form the set

$$ F_i=\{c(v_j): j<i \text{ and } v_jv_i\in E\}. $$

The algorithm assigns \(c(v_i)=\min(\{1,2,3,\ldots\}\setminus F_i)\). This least color always exists: \(F_i\) is finite, since it comes from at most \(i-1\) vertices, while the positive integers form an infinite set. The choice is based only on neighbors that have already been processed. After the last step, the output is a coloring of all vertices.

Why the Procedure Produces a Proper Coloring

Theorem: For every finite simple graph and every vertex order, the greedy coloring algorithm terminates and produces a proper coloring.

Proof. At each step \(i\), the set \(F_i\) is finite, so the least positive integer outside it exists and the algorithm can assign a color. There are \(n\) vertices, and each is processed once; therefore the algorithm terminates after \(n\) steps. To check that its output is proper, take any edge \(v_iv_j\), and suppose \(i<j\). When \(v_j\) is processed, \(v_i\) is already colored and is a neighbor, so \(c(v_i)\in F_j\). The algorithm chooses \(c(v_j)\) outside \(F_j\), which gives \(c(v_j)\ne c(v_i)\). This holds for every edge, so the completed coloring is proper. \(\square\)

This argument also explains why the order matters to the calculation: when a vertex is reached, the algorithm consults its earlier neighbors, not every neighbor indiscriminately. A later neighbor cannot forbid a color at the current step, because it has not been colored yet. If \(V\) is empty, there are no steps and the empty assignment is proper, since there are no edges to check.

Proposition: If the algorithm uses at least one color, the set of colors it uses is \(\{1,2,\ldots,q\}\) for some positive integer \(q\). In particular, the algorithm never uses a color label while skipping a smaller label entirely.

Proof. Let \(q\) be the largest color assigned, and consider the step at which color \(q\) is assigned. Since the algorithm chooses the least available color, each positive integer \(k<q\) must be forbidden at that step. Thus, for each such \(k\), some already colored neighbor has color \(k\), so the algorithm has used \(k\). It has also used \(q\) at the step under consideration. No color larger than \(q\) is used by the definition of \(q\), so the set of colors used is exactly \(\{1,2,\ldots,q\}\). \(\square\)

Tracing the Algorithm

A careful trace records four things at each step: the vertex being processed, its already colored neighbors, the colors those neighbors forbid, and the color assigned. The examples below specify each graph by its vertex and edge sets, so the adjacency information does not depend on a drawing. In each table, a dash means that the vertex has no already colored neighbors.

Worked Example: A Four-Cycle in Consecutive Order

Let \(G=(V,E)\), where \(V=\{1,2,3,4\}\) and \(E=\{12,23,34,14\}\). Use the order \(1,2,3,4\).

StepVertexAlready colored neighbors and their colorsForbidden colorsAssigned color
11None\(\varnothing\)1
221 has color 1\(\{1\}\)2
332 has color 2\(\{2\}\)1
441 has color 1; 3 has color 1\(\{1\}\)2

At step 1 no color is forbidden, so the least available color is 1. At step 2, color 1 is forbidden by vertex 1, and the least available color is 2. At step 3, its only earlier neighbor is vertex 2, so color 1 is available and is the least choice. At step 4, both earlier neighbors, 1 and 3, have color 1; color 1 is forbidden, and color 2 is the least available choice. The final assignment is \(c(1)=1,\ c(2)=2,\ c(3)=1,\ c(4)=2\).

Check: The four edges have differently colored endpoints: \(12\) has colors 1 and 2; \(23\) has colors 2 and 1; \(34\) has colors 1 and 2; \(14\) has colors 1 and 2. Thus the output is proper. It uses colors 1 and 2, as expected from the steps.

Worked Example: A Graph with Two Disconnected Parts and an Isolated Vertex

Let \(G=(V,E)\), where \(V=\{1,2,3,4,5,6\}\) and \(E=\{12,23,13,45\}\). The vertices \(1,2,3\) form one component, vertices \(4,5\) form another component, and vertex 6 is isolated. Use the order \(4,1,5,2,6,3\).

StepVertexAlready colored neighbors and their colorsForbidden colorsAssigned color
14None\(\varnothing\)1
21None\(\varnothing\)1
354 has color 1\(\{1\}\)2
421 has color 1\(\{1\}\)2
56None\(\varnothing\)1
631 has color 1; 2 has color 2\(\{1,2\}\)3

Vertices 4 and 1 have no earlier neighbors when processed, so each receives color 1. Vertex 5 is adjacent to 4, which forbids color 1, and therefore receives 2. Vertex 2 is adjacent to 1, also forbidding 1, so it receives 2. The isolated vertex 6 has no neighbors and receives 1. Finally, vertex 3 is adjacent to both 1 and 2. Their colors are 1 and 2, so both are forbidden; the least available color is 3. The algorithm's output is \(c(1)=1,\ c(2)=2,\ c(3)=3,\ c(4)=1,\ c(5)=2,\ c(6)=1\).

Check: The edge \(12\) has colors 1 and 2, \(23\) has colors 2 and 3, \(13\) has colors 1 and 3, and \(45\) has colors 1 and 2. These are all the edges, and each has differently colored endpoints. There are no edges between the components or incident with vertex 6, so no further checks are needed.

Worked Example: A Vertex Receives Color 3

Let \(G=(V,E)\), where \(V=\{a,b,c,d\}\) and \(E=\{ab,bc,ca,ad,cd\}\). Use the order \(b,d,a,c\).

StepVertexAlready colored neighbors and their colorsForbidden colorsAssigned color
1\(b\)None\(\varnothing\)1
2\(d\)None\(\varnothing\)1
3\(a\)\(b\) has color 1; \(d\) has color 1\(\{1\}\)2
4\(c\)\(b\) has color 1; \(a\) has color 2; \(d\) has color 1\(\{1,2\}\)3

At the first two steps there are no earlier neighbors, so both vertices receive 1. Vertex \(a\) is adjacent to \(b\) and \(d\), both colored 1; it receives the least color not forbidden, which is 2. Vertex \(c\) is adjacent to all three earlier vertices: \(b\) and \(d\) forbid 1, while \(a\) forbids 2. Thus the least available color is 3. The output is \(c(b)=1,\ c(d)=1,\ c(a)=2,\ c(c)=3\).

Check: The five edges are \(ab,bc,ca,ad,cd\). Their endpoint colors are respectively \(1,2;\ 1,3;\ 2,3;\ 2,1;\ 3,1\). Every pair differs, so the output is proper. In particular, the final vertex cannot use either 1 or 2 because each is used by one of its already colored neighbors.

Common Mistake: Looking at Only Some Earlier Neighbors

In the disconnected-graph example, vertex 3 was processed last. A mistaken trace might inspect only its neighbor 1, notice color 1, and assign color 2. But vertex 2 is also a neighbor of 3 and already has color 2. This incorrect assignment would make edge \(23\) have color 2 at both endpoints. The correction is to list all already colored neighbors before choosing: vertex 1 forbids 1 and vertex 2 forbids 2, so vertex 3 must receive 3.

Another potential confusion is to treat every neighbor as a restriction at every step. Only already colored neighbors belong in the forbidden set. For instance, when vertex \(a\) is processed in the third example, \(c\) is a neighbor but has not yet been processed. It therefore does not forbid a color at that step. The algorithm will account for the edge \(ac\) when \(c\) is processed, as the proper-coloring proof guarantees.

Using and Checking a Greedy Coloring

The procedure is useful whenever a coloring must be constructed directly from local information. It can be carried out from an edge list: maintain the colors already assigned, inspect the earlier neighbors of the next vertex, and select the first positive integer missing from their colors. The output is a valid coloring regardless of the order, because the theorem applies to every specified order. The number of colors used is a separate feature of the particular output; the proof of validity does not claim that this number is the chromatic number.

For a small graph, a reliable hand calculation can follow this checklist:

  • Write the complete vertex order before assigning any colors.
  • At each step, identify all neighbors that occur earlier in that order.
  • Form the set of their assigned colors, removing duplicates if several neighbors share a color.
  • Choose the least positive integer outside that set.
  • At the end, check every edge against the two endpoint colors.

The last check is valuable even when the procedure has been followed carefully: it can expose a missed neighbor or a copied color from the wrong step. A coloring is proper precisely when every edge has differently colored endpoints; checking only some edges is not enough.

Check Your Understanding

For each question, specify the earlier neighbors before deciding which color is available.

  1. Let \(V=\{1,2,3\}\), \(E=\{12,23\}\), and use the order \(2,1,3\). Trace the greedy algorithm and check both edges.
  2. For the graph with \(V=\{a,b,c,d\}\), \(E=\{ab,bc,ca,ad,cd\}\), use the order \(a,b,c,d\). Give the color assigned at each step.
  3. Explain why the greedy algorithm can always choose a color at a step, even if every color used so far appears on an already colored neighbor.
  4. In the graph \(V=\{1,2,3,4\}\), \(E=\{12,23,34,14\}\), process the vertices in order \(1,3,2,4\). Trace the algorithm and verify the final assignment.
  5. State why an unprocessed neighbor cannot forbid a color at the current step, and explain how the algorithm still ensures that their edge is properly colored.