igraph Reference Manual

For using the igraph C library

Search the manual:

Chapter 18. Graph coloring

1. igraph_vertex_coloring_greedy — Computes a vertex coloring using a greedy algorithm.

igraph_error_t igraph_vertex_coloring_greedy(const igraph_t *graph, igraph_vector_int_t *colors, igraph_coloring_greedy_t heuristic);

This function assigns a "color"—represented as a non-negative integer—to each vertex of the graph in such a way that neighboring vertices never have the same color. The obtained coloring is not necessarily minimal.

Vertices are colored greedily, one by one, always choosing the smallest color index that differs from that of already colored neighbors. Vertices are picked in an order determined by the speified heuristic. Colors are represented by non-negative integers 0, 1, 2, ...

Arguments: 

graph:

The input graph.

colors:

Pointer to an initialized integer vector. The vertex colors will be stored here.

heuristic:

The vertex ordering heuristic to use during greedy coloring. See igraph_coloring_greedy_t for more information.

Returns: 

Error code.

Example 18.1.  File examples/simple/igraph_coloring.c

#include <igraph.h>

int main(void) {
    igraph_t graph;
    igraph_vector_int_t colors;

    /* Setting a seed makes the result of erdos_renyi_game_gnm deterministic. */
    igraph_rng_seed(igraph_rng_default(), 42);

    /* IGRAPH_UNDIRECTED and IGRAPH_NO_LOOPS are both equivalent to 0/FALSE, but
       communicate intent better in this context. */
    igraph_erdos_renyi_game_gnm(&graph, 1000, 10000, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS);

    /* As with all igraph functions, the vector in which the result is returned must
       be initialized in advance. */
    igraph_vector_int_init(&colors, 0);
    igraph_vertex_coloring_greedy(&graph, &colors, IGRAPH_COLORING_GREEDY_COLORED_NEIGHBORS);

    /* Verify that the colouring is valid, i.e. no two adjacent vertices have the same colour. */
    {
        igraph_integer_t i;
        /* Store the edge count to avoid the overhead from igraph_ecount in the for loop. */
        igraph_integer_t no_of_edges = igraph_ecount(&graph);
        for (i = 0; i < no_of_edges; ++i) {
            if ( VECTOR(colors)[ IGRAPH_FROM(&graph, i) ] == VECTOR(colors)[ IGRAPH_TO(&graph, i) ]  ) {
                printf("Inconsistent coloring! Vertices %" IGRAPH_PRId " and %" IGRAPH_PRId " are adjacent but have the same color.\n",
                       IGRAPH_FROM(&graph, i), IGRAPH_TO(&graph, i));
                abort();
            }
        }
    }

    /* Destroy data structure when we are done. */
    igraph_vector_int_destroy(&colors);
    igraph_destroy(&graph);

    return 0;
}


2. igraph_coloring_greedy_t — Ordering heuristics for greedy graph coloring.

typedef enum {
    IGRAPH_COLORING_GREEDY_COLORED_NEIGHBORS = 0,
    IGRAPH_COLORING_GREEDY_DSATUR = 1
} igraph_coloring_greedy_t;

Ordering heuristics for igraph_vertex_coloring_greedy().

Values: 

IGRAPH_COLORING_GREEDY_COLORED_NEIGHBORS:

Choose the vertex with largest number of already colored neighbors.

IGRAPH_COLORING_GREEDY_DSATUR:

Choose the vertex with largest number of unique colors in its neighborhood, i.e. its "saturation degree". When multiple vertices have the same saturation degree, choose the one with the most not yet colored neighbors. Added in igraph 0.10.4. This heuristic is known as "DSatur", and was proposed in Daniel Brélaz: New methods to color the vertices of a graph, Commun. ACM 22, 4 (1979), 251–256. https://doi.org/10.1145/359094.359101

3. igraph_is_perfect — Checks if the graph is perfect.

igraph_error_t igraph_is_perfect(const igraph_t *graph, igraph_bool_t *perfect);

A perfect graph is an undirected graph in which the chromatic number of every induced subgraph equals the order of the largest clique of that subgraph. The chromatic number of a graph G is the smallest number of colors needed to color the vertices of G so that no two adjacent vertices share the same color.

Warning: This function may create the complement of the graph internally, which consumes a lot of memory. For moderately sized graphs, consider decomposing them into biconnected components and running the check separately on each component.

This implementation is based on the strong perfect graph theorem which was conjectured by Claude Berge and proved by Maria Chudnovsky, Neil Robertson, Paul Seymour, and Robin Thomas.

Arguments: 

graph:

The input graph. It is expected to be undirected and simple.

perfect:

Pointer to an integer, the result will be stored here.

Returns: 

Error code.

Time complexity: worst case exponenital, often faster in practice.