# Chapter 22. 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.

See also:

`` | igraph_is_vertex_coloring() to check if a coloring is valid, i.e. if all edges connect vertices of different colors.

Example 22.1. File `examples/simple/coloring.c`

    #include <igraph.h>

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

        /* Initialize the library. */
        igraph_setup();

        /* 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_SIMPLE_SW, IGRAPH_EDGE_UNLABELED);

        /* 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_is_vertex_coloring(&graph, &colors, &valid_coloring);
        IGRAPH_ASSERT(valid_coloring);

        /* 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_vertex_coloring` — Checks whether a vertex coloring is valid.

    igraph_error_t igraph_is_vertex_coloring(
            const igraph_t *graph,
            const igraph_vector_int_t *types,
            igraph_bool_t *res);

This function checks whether the given vertex type/color assignment is a valid vertex coloring, i.e., no two adjacent vertices have the same color. Self-loops are ignored.

Arguments:

`graph`: | The input graph.
`types`: | The vertex types/colors as an integer vector.
`res`: | Pointer to a boolean, the result is stored here.

Returns:

`` | Error code.

Time complexity: O(|E|), linear in the number of edges.

Example 22.2. File `examples/simple/coloring.c`

    #include <igraph.h>

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

        /* Initialize the library. */
        igraph_setup();

        /* 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_SIMPLE_SW, IGRAPH_EDGE_UNLABELED);

        /* 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_is_vertex_coloring(&graph, &colors, &valid_coloring);
        IGRAPH_ASSERT(valid_coloring);

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

        return 0;
    }

## 4. `igraph_is_bipartite_coloring` — Checks whether a bipartite vertex coloring is valid.

    igraph_error_t igraph_is_bipartite_coloring(
            const igraph_t *graph,
            const igraph_vector_bool_t *types,
            igraph_bool_t *res,
            igraph_neimode_t *mode);

This function checks whether the given vertex type assignment is a valid bipartite coloring, i.e., no two adjacent vertices have the same type. Additionally, for directed graphs, it determines the mode of edge directions. Self-loops are ignored.

Arguments:

`graph`: | The input graph.
`types`: | The vertex types as a boolean vector.
`res`: | Pointer to a boolean, the result is stored here.
`mode`: | Pointer to store the edge direction mode. Can be `NULL` if not needed. If all edges go from false to true vertices, `IGRAPH_OUT` is returned. If all edges go from true to false vertices, `IGRAPH_IN` is returned. If edges go in both directions or graph is undirected, `IGRAPH_ALL` is returned.

Returns:

`` | Error code.

Time complexity: O(|E|), linear in the number of edges.

See also:

`` | igraph_is_bipartite() to determine whether a graph is bipartite, i.e. 2-colorable, and find such a coloring.

## 5. `igraph_is_edge_coloring` — Checks whether an edge coloring is valid.

    igraph_error_t igraph_is_edge_coloring(
            const igraph_t *graph,
            const igraph_vector_int_t *types,
            igraph_bool_t *res);

This function checks whether the given edge color assignment is a valid edge coloring, i.e., no two adjacent edges have the same color. Note that this function does not consider self-edges (loops) as being adjacent to themselves, so graphs with self-loops may still be considered to have a valid edge coloring.

Arguments:

`graph`: | The input graph.
`types`: | The edge colors as an integer vector.
`res`: | Pointer to a boolean, the result is stored here.

Returns:

`` | Error code.

Time complexity: O(|V|*d*log(d)), where d is the maximum degree.

## 6. `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.