Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 70 additions & 30 deletions labs/01/relation_analyser.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,76 @@
'''
Nancy Silva Alvarez. A00833627
This program determines whether a given set forms a reflexive, symmetric, or transitive relation, and further checks if it qualifies as an equivalence relation.
Additionally, it provides a visual representation of the relation through a directed graph using the graphviz library.
'''
import graphviz # https://graphviz.readthedocs.io/en/stable/index.html

def analyze(val):
"""
Here goes your code to do the analysis
1. Reflexive: aRa for all a in X,
2. Symmetric: aRb implies bRa for all a,b in X
3. Transitive: aRb and bRc imply aRc for all a,b,c in X,
"""
Reflexive = False
Symmetric = False
Transitive = False

return Reflexive,Symmetric,Transitive

def plot():
"""
Here goes your code to do the plot of the set
"""
g = graphviz.Digraph('G', filename='hello.gv')
g.edge('Hello', 'World')
g.view()
def analyze(tupla):
Reflexive = False
Symmetric = False
Transitive = False

elementos = set()
for par in tupla:
elementos.add(par[0])
elementos.add(par[1])

# Reflexive (aRa for all a in )
for elemento in elementos:
if (elemento, elemento) not in tupla:
Reflexive = False
break
else:
Reflexive = True

# Symmetric: aRb implies bRa for all a,b in X
for par in tupla:
if (par[1], par[0]) not in tupla:
Symmetric = False
break
else:
Symmetric = True

# Transitive: aRb and bRc imply aRc for all a,b,c in X
for a in elementos:
for b in elementos:
for c in elementos:
if (a,b) in tupla and (b,c) in tupla and (a,c) not in tupla:
Transitive = False
break
if not Transitive:
break
if not Transitive:
break
else:
Transitive = True

return Reflexive, Symmetric, Transitive


def plot(tupla):
grafica = graphviz.Digraph('example', filename='graph')
for i in tupla:
grafica.edge(str(i[0]), str(i[1]))
grafica.view()


def main():
print("Hello World analyzing input!")
val = input("Enter your set: ")
print(val)
Reflexive,Symmetric,Transitive = analyze(val)
print(f"\
1. Reflexive: {Reflexive} \
2. Symmetric: {Symmetric} \
3. Transitive: {Transitive}")
plot()
print("Hello World analyzing input!")
val = input("Enter your set: ")
print(val)

tupla = eval(val) #De cadena a una estructura de datos

Reflexive,Symmetric,Transitive = analyze(tupla)
plot(tupla)

#Output
print("(a) R is" + ("" if Reflexive else " not") + " reflexive.") #Reflexive
print("(b) R is" + ("" if Symmetric else " not") + " symmetric.") #Symmetric
print("(c) R is" + ("" if Transitive else " not") + " transitive.") #Transitive
print("(d) R does" + ("" if Reflexive and Symmetric and Transitive else " not") + " have equivalence relation.") #Equivalence


if __name__ == "__main__":
main()
main()