sequential_vertex_coloring

sequential_vertex_coloring#

graph_tool.structure.sequential_vertex_coloring(g, order=None, color=None, strategy=None)[git][source]#

Returns a vertex coloring of the graph.

Parameters:
gGraph

Graph to be used.

orderVertexPropertyMap (optional, default: None)

Scalar vertex property map with the rank of each vertex in the order with which the vertices will be colored: vertices with smaller values are colored first, and ties are broken by the vertex index. If not given, the vertices are colored in the order of their indices.

colorVertexPropertyMap (optional, default: None)

Integer-valued vertex property map to store the colors.

strategystr (optional, default: None)

If given, the order is determined by one of the strategies "largest_first", "smallest_last", "independent_set", "connected_sequential_bfs", "connected_sequential_dfs", "saturation_largest_first", or "random_sequential" (see below), and order is ignored.

Returns:
colorVertexPropertyMap

Integer-valued vertex property map with the vertex colors.

See also

dsatur_vertex_coloring

Coloring with the DSatur heuristic

equitable_vertex_coloring

Coloring with color classes of nearly equal size

edge_coloring

Proper edge coloring

chromatic_polynomial

Number of proper colorings

Notes

Each vertex receives the smallest color not used by its neighbors that have already been colored. The direction of the edges is ignored, so that adjacent vertices always receive different colors (except for vertices with self-loops).

The time complexity is \(O(V(d+k))\), where \(V\) is the number of vertices, \(d\) is the maximum degree of the vertices in the graph, and \(k\) is the number of colors used.

Every vertex gets the smallest color not used by its neighbors colored before it. The strategies order the vertices of the simple undirected graph underlying g as follows, with ties broken by the smallest vertex index:

Strategy

Order

"largest_first"

Decreasing degree [welsh-upper-1967].

"smallest_last"

Reverse of the order in which a vertex of smallest degree is removed repeatedly [matula-smallest-1983], which uses at most one color more than the degeneracy.

"independent_set"

Maximal independent sets, each built by taking repeatedly a vertex of smallest degree among the remaining candidates, are removed in turn.

"connected_sequential_bfs"

Breadth-first order of every connected component, from its smallest vertex.

"connected_sequential_dfs"

Depth-first order of every connected component, from its smallest vertex.

"saturation_largest_first"

The DSatur order, in which the next vertex has the most distinct colors among its neighbors (see dsatur_vertex_coloring()).

"random_sequential"

Uniformly random order.

The strategies take time \(O((V + E)\log V)\), except "independent_set", which takes \(O(k(V + E)\log V)\) for \(k\) colors.

References

[welsh-upper-1967]

D. J. A. Welsh and M. B. Powell, “An upper bound for the chromatic number of a graph and its application to timetabling problems”, The Computer Journal 10, 85-86 (1967), DOI: 10.1093/comjnl/10.1.85 [sci-hub, @tor]

[matula-smallest-1983]

David W. Matula and Leland L. Beck, “Smallest-last ordering and clustering and graph coloring algorithms”, Journal of the ACM 30, 417-427 (1983), DOI: 10.1145/2402.322385 [sci-hub, @tor]

Examples

>>> g = gt.lattice([10, 10])
>>> colors = gt.sequential_vertex_coloring(g)
>>> print(colors.a)
[0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1
 0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0
 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0]
>>> g = gt.collection.data["football"]
>>> for s in ["largest_first", "smallest_last", "independent_set"]:
...     c = gt.sequential_vertex_coloring(g, strategy=s)
...     print(s, c.a.max() + 1)
largest_first 9
smallest_last 9
independent_set 10

The coloring with the largest-first strategy can be shown in a drawing:

>>> c = gt.sequential_vertex_coloring(g, strategy="largest_first")
>>> palette = np.array(["#9bbedc", "#f28c8e", "#a6d7a4", "#cba6d1",
...                     "#ffbf7f", "#ffff99", "#d2aa93", "#fbc0df",
...                     "#cccccc"])
>>> vcolor = g.new_vp("string", vals=palette[c.a])
>>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=vcolor,
...               edge_color="#999999",
...               output="sequential_vertex_coloring-football.pdf")
<...>
../_images/sequential_vertex_coloring-football.png

Vertex coloring with nine colors of the network of American college football games obtained with the largest-first strategy.#