Skip to content

Commit b264dc2

Browse files
Saatvik RaoSaatvik Rao
authored andcommitted
implemented function to generate upsets
1 parent 8ea5214 commit b264dc2

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/doc/en/reference/references/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5872,6 +5872,10 @@ REFERENCES:
58725872
Journal of Algorithms, Volume 26, Issue 2, Pages 275 - 290, 1998.
58735873
(https://doi.org/10.1006/jagm.1997.0891)
58745874
5875+
.. [Sq2002] Matthew B. Squire.
5876+
*Enumerating the Ideals of a Poset*.
5877+
(https://citeseer.ist.psu.edu/465417.html)
5878+
58755879
.. [SS1983] Shorey and Stewart. "On the Diophantine equation a
58765880
x^{2t} + b x^t y + c y^2 = d and pure powers in recurrence
58775881
sequences." Mathematica Scandinavica, 1983.

src/sage/categories/posets.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,56 @@ def order_filter(self, elements):
304304
[3, 7, 8, 9, 10, 11, 12, 13, 14, 15]
305305
"""
306306

307+
def generate_upsets(U, Y, Poset):
308+
r"""
309+
Enumerates the ideals (upsets) of a given poset.
310+
311+
ALGORITHM:
312+
313+
The algorithm is based on [Sq2002]_.
314+
Current algorithms for genertating ideals require an amortized
315+
time of `O(n)` per ideal in the worst case, where `n` is the number
316+
of elements in the poset. This algorithm generates ideals in an
317+
amortized time of `O(log n)` per ideal.
318+
319+
The function recursively generates all possible upsets (subsets) of
320+
a given set `Y` based on a partially ordered set (Poset), ensuring that
321+
if `Xi < Xm < Xj`, then Xi must also be less than `Xj`. It iterates through
322+
`Y`, partitioning it into two subsets based on the relationship between
323+
elements and yields the resulting upsets.
324+
325+
INPUT:
326+
327+
- ``U`` -- a list of the current upset being constructed.
328+
- ``Y`` -- a list of remaining elements to be considered for inclusion.
329+
- ``Poset`` -- a dictionary representing the partial order.
330+
331+
OUTPUT:
332+
333+
- A subset of Y that satisfies the upset condition.
334+
335+
EXAMPLES::
336+
337+
"""
338+
if not Y:
339+
yield U
340+
else:
341+
Xm = Y[len(Y) // 2]
342+
Y1 = []
343+
Y2 = []
344+
for Xi in Y:
345+
if Poset[(Xi, Xm)] == 1:
346+
U[Y.index(Xi)] = 0
347+
else:
348+
Y1.append(Xi)
349+
yield from Posets.generate_upsets(U.copy(), Y1, Poset)
350+
for Xj in Y:
351+
if Poset[(Xm, Xj)] == 1:
352+
U[Y.index(Xj)] = 1
353+
else:
354+
Y2.append(Xj)
355+
yield from Posets.generate_upsets(U.copy(), Y2, Poset)
356+
307357
def directed_subset(self, elements, direction):
308358
r"""
309359
Return the order filter or the order ideal generated by a

0 commit comments

Comments
 (0)