CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 21
Image: ubuntu2204
# Question 1 # Example 1: Creating a relation as a set of ordered pairs relation1 = {(1, 2), (2, 3), (3, 4),(1,3)} relation = {(a,b) for a in range(1,4) for b in range (1,4) if a<=b} print(relation1) print(relation)
{(2, 3), (1, 2), (1, 3), (3, 4)} {(1, 2), (1, 1), (2, 3), (3, 3), (2, 2), (1, 3)}
# Q2 def is_reflexive(r,elements): return all((a,a) in r for a in elements) # to check if it is antisymetric def is_antisymmetric(r): return all (a==b or (b,a) not in r for (a,b) in r) # to check if it is transitive def is_transitive(r): return all((a, c) in r for (a, b) in r for (_, c) in r if b == _) # to check if it is reflexive def is_partial_order(r, elements): return is_reflexive(r, elements) and is_antisymmetric(r) and is_transitive(r) r = {(1, 2), (2, 3), (1, 3)} # Set of elements elements = {1, 2, 3} # Check properties print("Is the relation reflexive?", is_reflexive(r, elements)) print("Is the relation antisymmetric?", is_antisymmetric(r)) print("Is the relation transitive?", is_transitive(r)) print("Is the relation a partial order?", is_partial_order(r, elements))
Is the relation reflexive? False Is the relation antisymmetric? True Is the relation transitive? True Is the relation a partial order? False
#Q3 p=Poset((divisors(24), attrcall("divides")), linear_extension=True) p.cover_relations() p.maximal_elements() p.minimal_elements() p.show()
[[1, 2], [1, 3], [2, 4], [2, 6], [3, 6], [4, 8], [4, 12], [6, 12], [8, 24], [12, 24]] [24] [1]
#Q4 P1=Poset((divisors(12),attrcall("divides")),linear_extension=True) P1.show() S1=Poset((divisors(6),attrcall("divides")),linear_extension=True) S1.show() #upperBound & LowerBound def is_upper_bound(element,S): return all(s <= element for s in S) def is_lower_bound(element,S): return all(element <= s for s in S) is_lower_bound(1,P1) is_upper_bound(12,P1) upper_bounds = [u for u in P1 if is_upper_bound(u,S1)] lower_bounds = [l for l in P1 if is_lower_bound(l,S1)] lower_bounds upper_bounds LUB = min(upper_bounds) if upper_bounds else None GLB = max(lower_bounds) if lower_bounds else None LUB GLB
True True [1] [6, 12] 6 1
#Q5 A1=Poset((divisors(24),attrcall("divides")),linear_extension=True) A1.show() A1.is_lattice()
True
A=Poset({0:[1,2],2:[3,4]}) A.show() A.is_lattice()
False