Skip to content

Commit 6d11817

Browse files
Saatvik RaosaatvikraoIITGN
authored andcommitted
implemented function to generate upsets
1 parent 15fcf52 commit 6d11817

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
@@ -5881,6 +5881,10 @@ REFERENCES:
58815881
Journal of Algorithms, Volume 26, Issue 2, Pages 275 - 290, 1998.
58825882
(https://doi.org/10.1006/jagm.1997.0891)
58835883
5884+
.. [Sq2002] Matthew B. Squire.
5885+
*Enumerating the Ideals of a Poset*.
5886+
(https://citeseer.ist.psu.edu/465417.html)
5887+
58845888
.. [SS1983] Shorey and Stewart. "On the Diophantine equation a
58855889
x^{2t} + b x^t y + c y^2 = d and pure powers in recurrence
58865890
sequences." Mathematica Scandinavica, 1983.

src/sage/categories/posets.py

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

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

0 commit comments

Comments
 (0)