Build a graph ordering where each element is an head, and the order is given by the sequence of elements
.
Build a graph ordering where each element is an head, and the order is given by the sequence of elements
.
Implementation is inefficient and can be improved using memoization