is coloring an image of a flower that has six petals organized in a circle. She needs to paint every of the six petals with precisely one of many following 4 colours: pink, orange, yellow, and blue. No two neighboring petals can have the identical shade. Not all 4 colours should be used. What number of methods are there for Rita to paint the flower petals whereas satisfying these constraints? This was the premise of downside #25 within the 2025 Cayley Contest, and it occurs to be a selected instance of a category of combinatorial knowledge science issues associated to the notion of graph coloring. Within the following sections, we’ll resolve Rita’s concrete downside, derive its normal open and closed-form options, and have a look at some attention-grabbing sensible purposes in business.
Notice: All figures and formulation within the following sections have been created by the creator of this text.
A Theoretical Puzzle
To resolve Rita’s downside, allow us to start by visualizing the flower petals as a cyclical graph consisting of 6 nodes linked by edges as proven in Determine 1:
Determine 2 reveals some legitimate colorings (additionally known as correct colorings) of the petals:

Let P(n, ok) be the variety of methods we are able to shade a cycle of n nodes with ok colours, such that no neighboring nodes have the identical shade.
Now, take into account what occurs if we break the cycle into a series of n nodes. What number of methods Pchain(n, ok) are there to paint a series of n nodes with ok colours, such that no neighboring nodes have the identical shade? For the beginning (left-most) node within the chain, we’ve got a selection of ok colours. However for every of the next n – 1 nodes, we’ve got a selection of solely ok – 1 colours, since one of many colours can have already been taken by the previous node. This instinct is illustrated in Determine 3 beneath:

Thus, we’ve got:

Nonetheless, discover that in a few of these legitimate colorings, the primary and final nodes within the chain will share the identical shade – if we subtract these instances from Pchain(n, ok), then we’d receive P(n, ok) as required. Moreover, discover that the instances to subtract are equal to P(n – 1, ok), i.e., the variety of methods to paint a cycle of n – 1 nodes with ok colours, such that no neighboring nodes have the identical shade. This so-called deletion-contraction maneuver is illustrated in Determine 4 beneath:

Determine 5 beneath reveals the bottom instances for P(n, ok), for a given worth ok:

Pulling all of those insights collectively yields the next first-order recurrence relation for constructive integers n > 3 and ok, with base instances as described above:

Now, fixing Rita’s downside quantities to evaluating the recurrence P(n, ok) for n = 6 and ok = 4. Because the numbers on this case are comparatively small, we are able to perform the analysis by increasing P(6, 4) as follows:

Utilizing the expression for the bottom case P(3, ok), word that:

Consequently:

Thus, there are precisely 732 methods for Rita to paint her flower petals whereas satisfying the given constraints.
The next Python perform (appropriate with Python variations ≥ 3.9) operationalizes the recurrence to allow us to shortly consider P(n, ok) for bigger enter values:
def num_proper_colorings(n: int, ok: int) -> int:
"""
Iteratively compute the variety of correct colorings of a cycle graph with n nodes and ok colours.
Parameters:
- n (int): Variety of nodes within the cycle graph.
- ok (int): Variety of obtainable colours.
Returns:
- int: Variety of correct colorings.
"""
if n == 1:
return ok
elif n == 2:
return ok * (ok - 1)
elif n == 3:
return ok * (ok - 1) * (ok - 2)
# Initialize base case num_proper_colorings(3, ok)
num_prev = ok * (ok - 1) * (ok - 2)
for i in vary(4, n + 1):
present = ok * (ok - 1)**(i - 1) - num_prev
num_prev = present
return num_prev
Graph coloring will also be operationalized utilizing backtracking, a helpful method for exploring the answer house of assorted sorts of knowledge science issues and incrementally establishing candidate options. This article gives an intuitive introduction to backtracking, and the next video reveals how backtracking could be utilized to graph coloring issues particularly:
Closed-Type Answer
The iterative Python perform proven above has a time complexity of O(n) with respect to variety of nodes n within the cyclical graph. Nonetheless, if we are able to discover an analytical or closed-form resolution to P(n, ok), we may consider the consequence straight; the corresponding Python perform would have a time complexity of simply O(1) and thus symbolize a considerable time saving as n grows very giant. Time complexity and the deserves of closed-form options are mentioned in additional depth in this article on algorithmic pondering for knowledge scientists.
To discover a simplified closed-form resolution, allow us to rearrange our recurrence relation as follows:

At this level, we are able to make use of a neat precept in linear algebra: the final resolution of a linear algebraic equation f(x) = y is the sum xh + xp of the final resolution xh of its homogeneous counterpart f(x) = 0 and a specific resolution xp of f(x) = y. To remind ourselves of why this works, we are able to substitute xh + xp into f(x) = y to get f(xh + xp) = y. Since f(x) is a linear perform, f(xh + xp) = f(xh) + f(xp) = y. We all know that f(xh) = 0 and f(xp) = y. By substitution, f(xh) + f(xp) = 0 + y = y. The equation y = y is trivially true, confirming that xh + xp is certainly the final resolution of f(x) = y. Thus, our job is now to:
- Discover a normal resolution xh of the homogeneous equation P(n, ok) + P(n – 1, ok) = 0
- Discover a specific resolution xp to our recurrence P(n, ok) + P(n – 1, ok) = ok(ok – 1)(n – 1)
- Mix xh and xp to derive the final closed-form resolution of our recurrence
So, allow us to begin by fixing the next homogeneous equation:

Notice that, for simplicity, we let:

Every time period appears to cancel the earlier one, so it should alternate in signal at each step, i.e., there exists a relentless time period C (which we’ll derive shortly) such that:

Subsequent, for the reason that right-hand facet of the unique recurrence is a a number of of (ok – 1)(n – 1), allow us to strive the next specific resolution P‘:

A is a continuing time period that we are able to derive by plugging P’ into the left-hand facet of the unique recurrence as follows:

This implies A = 1, so we’ve got:

Combining the actual and homogeneous options offers us:

Now, we are able to use our base case for n = 3 to derive C:

Substituting C again into the mixed resolution offers us the next closed-form resolution:

We will confirm that this certainly offers us the proper resolution for the Cayley contest downside:

Sensible Functions
Graph coloring is a elementary downside in graph concept that entails assigning labels (or “colours”) to the nodes of a graph such that no two adjoining nodes share the identical shade. In essence, graph coloring makes an attempt to partition a graph into unbiased units which may be handled uniformly (i.e., all parts of a set could also be assigned the identical shade) with out violating adjacency constraints. Such a downside framing could be utilized in a variety of knowledge science use instances involving constraint-based optimization and useful resource allocation. We have a look at a few of these purposes beneath.
Scheduling and Timetabling
Graph coloring can be utilized to unravel scheduling issues, the place duties or occasions should be organized with out conflicts. Graphs used to mannequin such situations are sometimes known as battle graphs.
Contemplate the intuitive case of designing faculty timetables. Every class could be represented by a node within the graph, and an edge connects two courses if some college students go to each courses. Time slots are represented by colours. A correct coloring of the graph – through which adjoining nodes don’t share the identical shade – ensures that no scholar is confronted with the predicament of getting to attend two courses on the similar time. The case of examination scheduling poses the same downside, since exams should be assigned time slots in such a approach that no scholar has conflicting examination instances. Graph coloring can be utilized to unravel any such downside and reduce the variety of time slots wanted general.
Graph coloring can even assist resolve issues of constraint-based scheduling in industrial settings. For instance, product meeting in a automobile manufacturing plant sometimes entails numerous duties, corresponding to portray, wiring electronics, chassis becoming, and putting in engines. Every job requires specialised tooling, workstations, and expert employees, and there could also be sure restrictions on job ordering. Portray mustn’t occur instantly earlier than wiring, since residual paint fumes may injury delicate electronics. Engine set up and chassis becoming might require a few of the similar tools (e.g., lifts or alignment rigs) that’s briefly provide and can’t be used concurrently. To use graph coloring, we are able to mannequin every job as a node in a graph, with an edge connecting two nodes if the corresponding duties are in battle (i.e., the duties can’t be scheduled consecutively). Colours symbolize distinct time slots or phases of meeting. Correct graph coloring ensures that conflicting duties will not be scheduled back-to-back; this helps optimize the manufacturing workflow, scale back downtime and useful resource bottlenecks, and forestall pricey errors.
Clustering and Function Choice
In knowledge mining and machine studying (ML), clustering algorithms group knowledge factors collectively based mostly on shared traits or relationships. Graph coloring gives a pure method to clustering by treating the info as a graph, the place nodes symbolize particular person knowledge factors and edges point out some relationship between the respective nodes (e.g., similarity, class membership). Graph coloring allows us to partition the info into unbiased units (i.e., teams of nodes that aren’t straight linked) for efficient detection of clusters; this methodology could also be significantly helpful in social community evaluation, organic knowledge modeling, and advice techniques, the place relationships between entities could be fairly complicated. Correct graph coloring helps be certain that every cluster is internally cohesive whereas being distinct from different clusters, offering a clear and interpretable construction for downstream evaluation. readers can try this text and this guide for a deep dive into graph-theoretic representations of knowledge for function engineering.
Lastly, function choice is a vital consideration in constructing environment friendly and interpretable ML fashions, particularly within the context of high-dimensional knowledge (e.g., as seen in domains corresponding to genomics and finance). Coaching a mannequin on many options is computationally costly, and extremely correlated options have a tendency to carry redundant data, which may result in inefficient coaching and overfitting. Graph coloring gives a chic resolution: assemble a graph the place every node represents a function, and edges join pairs of extremely correlated options. A correct coloring of such a graph ensures that no two extremely correlated options are assigned the identical shade, permitting the choice of one consultant function per shade. This system reduces dimensionality whereas preserving informational range, resulting in less complicated fashions that may doubtlessly generalize higher.
The Wrap
Graph coloring, whereas rooted in combinatorial arithmetic, has sensible relevance that extends properly past theoretical puzzles. Ranging from a math contest downside involving flower petals, we derived normal open and closed-form options for the correct coloring of cyclical graphs, and checked out how graph coloring could be utilized to a variety of knowledge science issues. The important thing to such sensible purposes lies in sensible downside framing: if the issue is framed as a graph in the suitable approach – with cautious consideration given to the definition of nodes, edges, and coloring constraints – then the answer method might develop into readily obvious. To borrow a quote that’s typically (mis)attributed to Einstein, “if [you] had an hour to unravel an issue, [you should] spend 55 minutes desirous about the issue and 5 minutes desirous about options.” In the end, as the sector of knowledge science continues to evolve, methods corresponding to graph coloring will doubtless develop into more and more related in each analysis and utilized settings.