The chromatic polynomial of an undirected graph G is the polynomial f(x) such that f(n) is the number of different ways to color G using n colors.

Some examples:

• C4: x4 - 4 x3 + 6 x2 - 3 x
• K3: x3 - 3 x2 + 2 x
• K3,3: x6 - 9 x5 + 36 x4 - 75 x3 + 78 x2 - 31 x

Some basic properties:

• The largest power with a non-zero coefficient is the number of nodes in the graph
• f(x) = P(x) * (x-(k-1)) * ... * (x-1) * x, where k is the chromatic number of the graph.

Some more properties:

• The coefficient of the second-highest power is always -e, where e is the number of edges in the graph.
• Except for null graphs, the sum of the coefficients is always zero. For null graphs, the sum is, of course, 1.
• The chromatic polynomial for a tree graph on n vertices is x(x-1)n-1.
• Interestingly, the number of acyclic orientations of a graph is given by PG(-1). [Stanley, 1978]

The two most useful formulas, though, are the following:

• PG(x) = PG-e(x) - PG|e(x)
• PG(x) = PG+e(x) + PG|e(x)
where PG(x) denotes the chromatic polynomial of the graph G, G-e denotes the graph G with the edge e removed, G+e denotes the graph G with the edge e added, and G|e denotes the graph obtained from G by identifying the two vertices incident to e.

That last one (G|e) is probably the most confusing, so I'll explain it in a bit more detail. Say we have a graph G:

```    A*----*B
|    |
|    |
D*----*C
```
If we identify along the edge (C,D), we get:
```    A*----*B
\   |
\  |
\ |
C*
```
That is, we "merge" the two vertices C and D into one vertex.

Now it is easy to work out the chromatic polynomial of the original graph. Since we know that PG(x) = PG-e(x) - PG|e(x), we get (in pseudo-mathematical form):

```     *----*       *---*       *----*
|    |   =    \  |   -   |    |
|    |         \ |       |    |
*----*           *       *    *
```
This thus reduces our problem to a more simple problem. If we cannot compute the chromatic polynomial for these graphs, we simply repeat the procedure until we can.

Chromatic polynomials for common graphs:

• K3,3: x6 - 9x5 + 36x4 - 75x3 + 78x2 - 31x
• Cube graph: x8 - 12x7 + 66x6 - 214x5 + 441x4 - 572x3 + 423x2 - 133x
• Petersen graph: x10 - 15x9 + 105x8 - 455x7 + 1353x6 - 2861x5 + 4275x4 - 4305x3 + 2606x2 - 704x

Chromatic polynomials computed with GraphThing, my graph theory software, available free at http://graph.seul.org/
Cube graph chromatic polynomial fixed (s/411/441) -- June 2006

## Computing the chromatic polynomial

#### (without fancy-schmanzy special-purpose tools like GraphThing, whatever that is, dsx)

The following Perl 5 script will compute the chromatic polynomial of a graph fed to its standard input.

``` c(1,<>); \$i--; map {print s'-''?'-':'+',''x s'^1\$'',\$_,"x^\$i" if \$i++,\$_} @s; sub c {my(\$p,\$c,@q)=@_; @q or \$s[\$c]+=\$p,return; return if grep {/(.)\1/} @q; my(\$l,\$r)=split //,shift @q; c(\$p,\$c,@q); map {s/\$l/\$r/g} @q; c(-\$p,--\$c,@q)} ```

(Of course, it's possible to write such a program in legible Perl too, but .sig-length scripts that do something useful are much more fun, no?)

To use the program, prepare a data file containing the graph in the following format: name each vertex of your graph with an ASCII symbol (number, letter, whatever.) The first line of your file gives the number of vertices of the graph; each subsequent line of the file should contain a two-character string corresponding to the two ends of an edge. Thus for a square (also known as C4), whose vertices you have decided to call `A` to `D`, you would use a file such as:

``` 4 AB BC CD DA ```
(Note that it is not necessary to list the character names of the vertices explicitly.) Save the program as `chromatic` and the data file above as `square`, then invoke Perl:
``` bash\$ perl chromatic square -3x^1+6x^2-4x^3+x^4bash\$ ```

Okay, the output could be more elegant, but x4-4x3+6x2-3x is the same chromatic polynomial that Hykin's writeup gives for C4, so it's probably right.

With suitable data files, we can also check the chromatic polynomials given in the above writeups for K3:

``` 3 AB BC CA ```
K3,3:
``` 6 A1 A2 A3 B1 B2 B3 C1 C2 C3 ```
the cube graph:
``` 8 AB BC CD DA 12 23 34 41 A1 B2 C3 D4 ```
and the Petersen graph:
``` 10 AB BC CD DE EA 13 35 52 24 41 A1 B2 C3 D4 E5 ```

In fact the script finds a misprint in dsx's writeup: for the cube graph, the coefficient of x4 is not 411 but 441.

The program uses the recursive method outlined by dsx, whose running time is exponential in the number of edges. Consequently it's not practical to use it on really big (or even moderately big) graphs. The Petersen graph takes about a quarter of a second on a 3GHz Xeon system, and that's only fifteen edges.

#### Source

I wrote the above program some years ago (back when Perl 5 was the latest thing) as an undergraduate computing project.

Log in or register to write something here or to contact authors.