Python 与元组集的关系
Python Relations with Sets of Tuples
我正在尝试确定元组集是否具有某种类型的关系。我正在尝试找出传递关系和复合关系。
对于传递关系:
# A relation 'Relation' is called transitive when:
# ∀ (a, b) ∈ Relation, (b, c) ∈ Relation ==> (a, c) ∈ Relation
例如:
>>> {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1), (4,4)} # Should be False
>>> {(1,1), (2,1), (3,1), (3,2), (4,1), (4,2), (4,3)} # Should be True
对于复合关系:
# The composite of relations 'R1' and 'R2' is the relation consisting
# of tuples (a,c), such that (a,b) ∈ R1 and (b,c) ∈ R2
例如:
{(1,0), (1,1), (2,1), (2,2), (3,0), (3,1)} == R1={(1,1), (1,4), (2,3), (3,1), (3,4)}, R2={(1,0), (2,0), (3,1), (3,2), (4,1)}
# Should return True
我不确定如何开始编写这些函数。任何帮助我入门的帮助将不胜感激。谢谢!
编辑:
以下是我能够成功编码的其他一些关系:
# Reflexive Relation
# A relation 'Relation' on a set 'Set' is called reflexive when:
# ∀ a ∈ Set, (a,a) ∈ Relation
def is_reflexive(Set, Relation):
newSet = {(a, b) for a in Set for b in Set if a == b}
if Relation >= newSet:
return True
return False
# Symmetric Relation
# A relation 'Relation' is called symmetric when:
# ∀ (a, b) ∈ Relation, (b, a) ∈ Relation
def is_symmetric(Relation):
if all(tup[::-1] in Relation for tup in Relation):
return True
return False
我相信这是一种有效的蛮力方法。首先,我们将使用 "adjacency set" 表示,然后使用深度嵌套的 for 循环显式测试:
In [5]: r1 = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1), (4,3)}
In [6]: adjacency = {}
...: for a,b in r1:
...: adjacency.setdefault(a,set()).add(b)
...:
In [7]: transitive = True
In [8]: for a, related in adjacency.items():
...: for b in related:
...: for c in adjacency[b]:
...: if c not in related:
...: transitive = False
...: print("({},{}) and ({},{}) but not ({},{})".format(a, b, b, c, a,c))
...:
(1,4) and (4,3) but not (1,3)
(2,1) and (1,4) but not (2,4)
(4,1) and (1,2) but not (4,2)
(4,1) and (1,4) but not (4,4)
In [9]: transitive
Out[9]: False
然而,对于你的第二个例子:
In [7]: r2 = {(1,1), (2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}
In [8]: adjacency = {}
...: for a,b in r2:
...: adjacency.setdefault(a,set()).add(b)
...:
In [9]: transitive = True
In [10]: for a, related in adjacency.items():
...: for b in related:
...: for c in adjacency[b]:
...: if c not in related:
...: transitive = False
...: print("({},{}) and ({},{}) but not ({},{})".format(a, b, b, c, a,c))
...:
In [11]: transitive
Out[11]: True
使用这个数据结构应该可以减少时间复杂度 POV 的可怕程度。
关于构建组合:
In [18]: def make_adjacency_set(R):
...: a = {}
...: for x,y in R:
...: a.setdefault(x, set()).add(y)
...: return a
...:
In [19]: def make_composite(R1, R2):
...: adj1 = make_adjacency_set(R1)
...: adj2 = make_adjacency_set(R2)
...: composite = set()
...: for a, related in adj1.items():
...: for b in related:
...: for c in adj2.get(b, []):
...: composite.add((a, c))
...: return composite
...:
In [20]: R1={(1,1), (1,4), (2,3), (3,1), (3,4)}; R2={(1,0), (2,0), (3,1), (3,2), (4,1)}
In [21]: make_composite(R1, R2)
Out[21]: {(1, 0), (1, 1), (2, 1), (2, 2), (3, 0), (3, 1)}
或者,只坚持使用元组集:
In [25]: composite = set()
...: for a, b in R1:
...: for c, d in R2:
...: if b == c:
...: composite.add((a,d))
...:
...:
In [26]: composite
Out[26]: {(1, 0), (1, 1), (2, 1), (2, 2), (3, 0), (3, 1)}
对于传递测试,只需将Python中的数学定义转换为:
def is_transitive(relation):
for a,b in relation:
for c,d in relation:
if b == c and ((a,d) not in relation):
# print (a,b),(c,d) # uncomment for tests...
return False
return True
我正在尝试确定元组集是否具有某种类型的关系。我正在尝试找出传递关系和复合关系。
对于传递关系:
# A relation 'Relation' is called transitive when:
# ∀ (a, b) ∈ Relation, (b, c) ∈ Relation ==> (a, c) ∈ Relation
例如:
>>> {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1), (4,4)} # Should be False
>>> {(1,1), (2,1), (3,1), (3,2), (4,1), (4,2), (4,3)} # Should be True
对于复合关系:
# The composite of relations 'R1' and 'R2' is the relation consisting
# of tuples (a,c), such that (a,b) ∈ R1 and (b,c) ∈ R2
例如:
{(1,0), (1,1), (2,1), (2,2), (3,0), (3,1)} == R1={(1,1), (1,4), (2,3), (3,1), (3,4)}, R2={(1,0), (2,0), (3,1), (3,2), (4,1)}
# Should return True
我不确定如何开始编写这些函数。任何帮助我入门的帮助将不胜感激。谢谢!
编辑: 以下是我能够成功编码的其他一些关系:
# Reflexive Relation
# A relation 'Relation' on a set 'Set' is called reflexive when:
# ∀ a ∈ Set, (a,a) ∈ Relation
def is_reflexive(Set, Relation):
newSet = {(a, b) for a in Set for b in Set if a == b}
if Relation >= newSet:
return True
return False
# Symmetric Relation
# A relation 'Relation' is called symmetric when:
# ∀ (a, b) ∈ Relation, (b, a) ∈ Relation
def is_symmetric(Relation):
if all(tup[::-1] in Relation for tup in Relation):
return True
return False
我相信这是一种有效的蛮力方法。首先,我们将使用 "adjacency set" 表示,然后使用深度嵌套的 for 循环显式测试:
In [5]: r1 = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1), (4,3)}
In [6]: adjacency = {}
...: for a,b in r1:
...: adjacency.setdefault(a,set()).add(b)
...:
In [7]: transitive = True
In [8]: for a, related in adjacency.items():
...: for b in related:
...: for c in adjacency[b]:
...: if c not in related:
...: transitive = False
...: print("({},{}) and ({},{}) but not ({},{})".format(a, b, b, c, a,c))
...:
(1,4) and (4,3) but not (1,3)
(2,1) and (1,4) but not (2,4)
(4,1) and (1,2) but not (4,2)
(4,1) and (1,4) but not (4,4)
In [9]: transitive
Out[9]: False
然而,对于你的第二个例子:
In [7]: r2 = {(1,1), (2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}
In [8]: adjacency = {}
...: for a,b in r2:
...: adjacency.setdefault(a,set()).add(b)
...:
In [9]: transitive = True
In [10]: for a, related in adjacency.items():
...: for b in related:
...: for c in adjacency[b]:
...: if c not in related:
...: transitive = False
...: print("({},{}) and ({},{}) but not ({},{})".format(a, b, b, c, a,c))
...:
In [11]: transitive
Out[11]: True
使用这个数据结构应该可以减少时间复杂度 POV 的可怕程度。
关于构建组合:
In [18]: def make_adjacency_set(R):
...: a = {}
...: for x,y in R:
...: a.setdefault(x, set()).add(y)
...: return a
...:
In [19]: def make_composite(R1, R2):
...: adj1 = make_adjacency_set(R1)
...: adj2 = make_adjacency_set(R2)
...: composite = set()
...: for a, related in adj1.items():
...: for b in related:
...: for c in adj2.get(b, []):
...: composite.add((a, c))
...: return composite
...:
In [20]: R1={(1,1), (1,4), (2,3), (3,1), (3,4)}; R2={(1,0), (2,0), (3,1), (3,2), (4,1)}
In [21]: make_composite(R1, R2)
Out[21]: {(1, 0), (1, 1), (2, 1), (2, 2), (3, 0), (3, 1)}
或者,只坚持使用元组集:
In [25]: composite = set()
...: for a, b in R1:
...: for c, d in R2:
...: if b == c:
...: composite.add((a,d))
...:
...:
In [26]: composite
Out[26]: {(1, 0), (1, 1), (2, 1), (2, 2), (3, 0), (3, 1)}
对于传递测试,只需将Python中的数学定义转换为:
def is_transitive(relation):
for a,b in relation:
for c,d in relation:
if b == c and ((a,d) not in relation):
# print (a,b),(c,d) # uncomment for tests...
return False
return True