Consider pairs (x,y) of integers. We impose a partial order such that (x,y) ≤ (x',y') wrt. that partial order of x ≤ x' and y ≤ y'. If neither (x,y) ≤ (x',y') nor (x',y') ≤ (x,y), we say that (x,y) and (x',y') are incomparable.
Now, I have a list ls of such pairs. I want to check that the order of that list refines the partial order defined above; i.e., I need to check that to the right of any element in the list, there come no smaller elements.
Of course I could do the following:
def leq_po(u,v):
return all(a <= b for a, b in zip(u,v))
def le_po(u,v):
return leq_po(u,v) and u != v
def list_refines_po(ls):
return all(not le_po(ls[j], ls[i])
for i in range(len(ls)) for j in range(i+1, len(ls)))
However, then gives me some kind of O(n²)-complexity, while checking that a list is sorted wrt. total order takes O(n)-time.
Now, probably, that's what I've got to expect: partial orders generalise total orders, so probably, I should expect that checking the more general notion has a worse complexity. But:
Can I do better than O(n²)?
Edit To try, you can use the following data:
ls_in_order = [(0, 1), (2, 1), (3, 0), (2, 1)]
ls_not_in_order = [(0, 1), (2, 1), (3, 0), (1, 1)]
Here, no pair of consecutive entries in ls_not_in_order is descending, but the list is not in order (because (2,1) ≥ (1,1)).