C3 linearisation is an algorithm to linearise a special kind of DAG (Directed Acyclic Graph). In DAG, the parent-child relationship exists, and linearisation is to give an 1D order of the nodes following that correct relationship. For this algorithm, the parents of a node has an predefined order itself, and the linearisation must follow this order as well.
Inputs: A DAG
Outputs: An order of all parent nodes for each node
Preservation of local precedence order: the order of each
must be consistent withMonotonicity: if
is a parent node of , then must be consistent withPreservation of extended local precedence order: if
in a certain local precedence order , then for any child node as to , as to , must keep the order that .
Yes, it is unique as long as it exists.
There are conditions on
- The graph is cyclic, i.e. there is a cycle in the graph.
- There are conflicting local precedence orders among sibling nodes.
C3 linearisation is originally designed for resolving the problem of multiple inheritance in object-oriented programming languages, such as Dylan and Python. It is used to determine the method resolution order (MRO) in these languages.
The relationship among classes with multiple inheritance can be represented as a DAG, where each class is a node and the inheritance relationship is a directed edge. C3 linearisation provides a way to determine the order in which methods should be resolved when there are multiple parent classes.
For example, in the above figure, if a method appears in multiple parent classes of class
Python uses C3 linearisation to determine the method resolution order (MRO) for classes with multiple inheritance. You can use built-in methods C.mro()
or C.__mro__
to get the MRO of a class super()
in Python also find the first parent following the MRO.
However, many other languages such as C++ don’t have this feature.