Python与元组集的关系

时间:2017-04-01 05:51:11

标签: python set tuples relation

我正在尝试确定元组集是否具有某种类型的关系。我试图找出传递关系和复合关系。

对于传递关系:

 # 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

2 个答案:

答案 0 :(得分:2)

我相信这是一种有效的蛮力方法。首先,我们将使用"邻接集"表示,然后只使用深度嵌套的for循环显式测试:

Could not find a version that satisfies the requirement keys (from versions: ) No matching distribution found for keys

然而,对于你的第二个例子:

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

使用这种数据结构应该会使时间复杂度的POV变得不那么可怕。

至于构建复合材料:

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

或者,只是坚持使用元组:

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)}

答案 1 :(得分:1)

对于传递测试,只需在Python中转换数学定义:

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)}