For finite intervals on a line such that numbers , are distinct, answer the following questions.
From each vertex, we just have to check all the possible (directed) paths and remember them. The longest will denote a maximum-size subset of mutually non-intersecting intervals. How do we actually look for it?
There is a val array of integers associated with the set of intervals. There is also a dad array of integers to remember the father of each node in a longest path. We select the first node and put is val value to 0. dad is initializes to 0 too. We check all the adjacent nodes and put their val values to 1, or, more precisely, to val of the predecessor plus 1. Their dad values are given the number associated with the predecessor node. Then we perform the same operations for any following nodes taken in the increasing order. Let us consider node . If val has not been yet initialized, we initialize val and dad to 0. We look for the adjacent nodes and determine their val and dad values: if val is greater than val, we update val and dad (the latter is given the value of ); if not, it means that is reachable through a longer path that we prefer to keep track of.
The node with the higher val value is the last node of a longest path. Reading the dad array in reverse order gives us the element of that path. We need to go through all nodes onces and for each of them we look at a subset of a partition of the edges, leading to a algorithm. and cannot exceed . Thus, we have a algorithm.
We can represent the problem of coloring as in the figure above (b). Each vertex represents an interval that is linked to another vertex if and only if they overlap. Our problem can be understood as the problem of coloring the graph constructed this way.