Treat positions as vertices and direct an edge from the position that must have the smaller value to the one that must have the larger value. Thus < gives , and > gives . This is an orientation of a path, so it has no directed cycle.
Let be lower and upper bounds on the value assigned to position . Initialize an unknown position with , and a fixed position with .
In topological order, apply for every edge . In reverse topological order, apply . If any , the instance is impossible. Otherwise, every edge satisfies and .
Now assign distinct values to positions, with position receiving a value in . Process values . Add positions with to a priority queue, then assign to the position currently in the queue with the smallest . If the queue is empty or its smallest upper bound is below , no assignment exists.
Any assignment produced this way also satisfies the original inequalities. If all values are assigned, output the resulting permutation.
Topological sorting and bound propagation take time. Each position enters and leaves the queue once, giving total time and space .
Proof
Every valid permutation satisfies all propagated bounds. The initial intervals encode the original conditions. Since the values are integers, implies . Thus propagating a lower bound cannot exclude a valid permutation, and the same holds for upper bounds.
Solution written by GPT6
Consider the greedy interval assignment. Suppose some complete interval assignment agrees with the greedy choices for values smaller than . Let be the position chosen by the greedy rule, and let hold in this complete assignment. Let be the value at .
Swap the values at and . Since is eligible, . Since originally held , we have . The greedy choice gives , so . Both positions still satisfy their intervals. Therefore each greedy choice preserves the existence of a completion. An empty queue or an expired upper bound makes such a completion impossible.
Finally, consider an edge . Suppose were assigned before . At that moment, means that has already entered the queue and is still unassigned. But , so the greedy rule would prefer to , a contradiction. Hence .
Each value is used exactly once, and every fixed position has a singleton interval containing its fixed value. Thus the output satisfies all conditions. Conversely, a valid permutation would survive bound propagation and interval assignment, so every impossibility report is correct.