Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
'''The following code implements a graph coloring in Sage.
+ The first line loads a package for Sage specifically to deal with graph coloring.
+ Replace g with any graph. The default below is a randomly generated graph with 10
nodes and a 0.5 chance of two nodes being adjacent.
+ vertex_coloring(g) generates a partition of the nodes according to color.
+ The partition=colors option in the last line will actually color the nodes according to the partition.
from sage.graphs.graph_coloring import *
g = graphs.RandomGNP(10, 0.5) # Or your favorite graph, entered as a dictionary, matrix, special graph, etc.
colors = vertex_coloring(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment