For using the igraph C library
igraph_vertex_coloring_greedy
— Computes a vertex coloring using a greedy algorithm.igraph_coloring_greedy_t
— Ordering heuristics for greedy graph coloring.igraph_is_vertex_coloring
— Checks whether a vertex coloring is valid.igraph_is_bipartite_coloring
— Checks whether a bipartite vertex coloring is valid.igraph_is_edge_coloring
— Checks whether an edge coloring is valid.igraph_is_perfect
— Checks if the graph is perfect.
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:
|
The input graph. |
|
Pointer to an initialized integer vector. The vertex colors will be stored here. |
|
The vertex ordering heuristic to use during greedy coloring.
See |
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; }
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:
|
Choose the vertex with largest number of already colored neighbors. |
|
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 |
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:
|
The input graph. |
|
The vertex types/colors as an integer vector. |
|
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; }
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:
|
The input graph. |
|
The vertex types as a boolean vector. |
|
Pointer to a boolean, the result is stored here. |
|
Pointer to store the edge direction mode. Can be |
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. |
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:
|
The input graph. |
|
The edge colors as an integer vector. |
|
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.
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:
|
The input graph. It is expected to be undirected and simple. |
|
Pointer to an integer, the result will be stored here. |
Returns:
Error code. |
Time complexity: worst case exponenital, often faster in practice.
← Chapter 21. Graph isomorphism | Chapter 23. Maximum flows, minimum cuts and related measures → |