-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelation.py
159 lines (159 loc) · 4.64 KB
/
relation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# An example of a set
set_def = [1,2,3,4,5,6,7,8,9]
# An instance of a subset of 'set_def'
sub_set = [3,4,5,6]
# Instances of a relation with 'set_def'
relation_1 = {
1:[1,2,3,4,5,6,9],
2:[2],
3:[2,3,4,5,6,9],
4:[2,4],
5:[2,4,5],
6:[2,4,6],
7:[1,2,3,4,5,6,7,9],
8:[8],
9:[2,4,9]
}
relation_2 = {
1:[1,2,3,4,5,6,9],
2:[2],
3:[2,3,4,5,6,9],
4:[2,4],
5:[2,4,5],
6:[2,4,5,6,9],
7:[1,2,3,4,5,6,7,9],
8:set_def,
9:[2,4,5,9]
}
relation_3 = {
1:[2,3,4,5,6,9],
2:[],
3:[2,4,5,6,9],
4:[2],
5:[2,4],
6:[],
7:[1,2,3,4,5,6,9],
8:[],
9:[2,4]
}
def subset_gen(S):
A = []
for num in range(2**len(S)):
num_temp,Q = num,[]
for k in range(len(S)):
if num_temp % 2: Q.append(S[k])
num_temp = num_temp / 2
A.append(Q)
return A
def irreflex(A,R):
flag = True
for i in A:
if i in R: flag = flag and not(i in R[i])
if not flag:
print(i,'in reflex: Not an antireflexive relation')
return flag
print('The relation is anti-reflexive')
return flag
def connex(A,R):
flag = True
for i in A:
for j in A:
if i!=j: flag = flag and (j in R[i] or i in R[j])
if not flag:
print(i,j,'intertwined in connex')
return flag
print('The relation is connex.')
return flag
def antisymm(A,R):
for i in A:
if i not in R:
print(i,'not in relation antisymmetry error')
return False
else:
for j in R[i]:
if i in R[j] and j!=i:
print(i,'intertwined with',j,'Antisymmetric failed')
return False
print('The relation is antisymmetric.')
return True
def transitive(A,R):
for i in A:
if i not in R:
print(i,'not in relation transitivity error')
return False
else:
for j in R[i]:
for k in R[j]:
if k not in R[i]:
print(i,j,k,'transitivity error')
return False
print('The relation is transitive.')
return True
def reflexive(A,R):
for i in A:
if i not in R:
print(i,'not in relation reflexivity error')
return False
else:
if i not in R[i]:
print(i,'not in relation reflexivity error')
return False
print('The relation is reflexive.')
return True
def poset(A,R):
flag = [antisymm(A,R),transitive(A,R),reflexive(A,R)]
if flag[0] and flag[1] and flag[2]:
print('The relation is a poset')
else: print('The relation is not a poset')
def total_order(A,R):
flag = [connex(A,R),transitive(A,R),reflexive(A,R)]
if flag[0] and flag[1] and flag[2]:
print('The relation is in total order')
else: print('The relation is not a total order')
def quasi_order(A,R):
flag = [irreflex(A,R),transitive(A,R)]
if flag[0] and flag[1]:
print('The relation is in quasi-order')
else: print('The relation is not in quasi-order')
def least_element(S,R):
least_element = []
for s in S:
flag = True
for b in S:
if b!=s: flag = flag and b in R[s]
if flag: least_element.append(s)
return least_element
# is least element of an poset unique?
def greatest_element(S,R):
greatest_element = []
for g in S:
flag = True
for b in S:
if b!=g: flag = flag and g in R[b]
if flag: greatest_element.append(g)
return greatest_element
# is greatest element of an poset unique?
def well_ordered(A,R):
Q,flag = subset_gen(A),True
for S in Q:
if len(S) != 0:
extract = least_element(S,R)
flag = flag and len(extract)>0
if not flag:
print('The chain is not well ordered for',S,'in',A)
break
if flag: print('The chain is well ordered')
print('Testing poset property for relation 1')
poset(set_def,relation_1)
print('Testing total order property for relation 2')
total_order(set_def,relation_2)
print('Testing poset property for relation 2')
poset(set_def,relation_2)
# Relation 3 is a quasi-order relation from relation 1 just by removing reflexivity
print('Testing quasi-order for relation 3')
quasi_order(set_def,relation_3)
print('The least element of subset',sub_set,'in relation 1 is',least_element(sub_set,relation_1))
print('The greatest element of subset',sub_set,'in relation 1 is',greatest_element(sub_set,relation_1))
# Testing well ordered total set
print('Testing well ordered property in relation 2')
well_ordered(set_def,relation_2)