python实现 离散数学二元关系判定

构建矩阵
def Creat_Matrix(a,rs):
   array= [[0]*len(a) for i in range(len(a))]
   for i in range((int)(len(rs)/2)):
      array[a.index(rs[i*2])][a.index(rs[i*2+1])]=1
   return array
输出矩阵
def Print_Matrix(array,judge):
   if judge=='r':
      print('-'*10,'关系R矩阵为:','-'*10)
   elif judge=='s':
      print('-'*10,'关系S矩阵为:','-'*10)
   for i in range(len(array)):
      print(array[i])
   return 0

关系的复合运算及结果输出
def Com_Matrix_relation(a,array_r,array_s,judge):
   res= [[0]*len(a) for i in range(len(a))]
   for i in range(len(a)):
      for j in range(len(a)):
         for k in range(len(a)):
            res[i][j] += array_r[k][j] * array_s[i][k]
         if res[i][j]>1:
            res[i][j]=1
   if judge=='r○s':
      print('-'*10,'R○S关系复合运算结果为:','-'*10)
   elif judge=='s○r':
      print('-'*10,'S○R关系复合运算结果为:','-'*10)
   for i in range(len(res)):
      print(res[i])
   return 0
对称性判断:
def Symmetric_Relation(a,array,str_judge):
   for i in range(len(a)):
      for j in range(len(a)):
         if array[i][j]!=array[j][i]:
            print("关系",str_judge,"不是对称的")
            return 0 
      if i==len(a)-1:
         print("关系",str_judge,"是对称的")
         return 0
反对称性判断
def Un_symmetric_Relation(a,array,str_judge):
   for i in range(len(a)):
      for j in range(len(a)):
         if array[i][j]==array[j][i]:
            if i!=j:
               print("关系",str_judge,"不是反对称的")
               return 0 
      if i==len(a)-1:
         print("关系",str_judge,"是反对称的")
         return 0
自反性判断
def Contrary_Relation(a,array,str_judge):
   for i in range(len(a)):
      if array[i][i]==0:
         print("关系",str_judge,"不是自反的")
         return 0
      if i==len(a)-1:
         print("关系",str_judge,"是自反的")
         return 0
反自反性判断
def Un_contrary_Relation(a,array,str_judge):
   for i in range(len(a)):
      if array[i][i]==1:
         print("关系",str_judge,"不是反自反的")
         return 0
      if i==len(a)-1:
         print("关系",str_judge,"是反自反的")
         return 0
传递性判断
def Transmit_Relation(a,array,str_judge):
   for i in range(len(a)):
      for j in range(len(a)):
         for k in range(len(a)):
            if array[i][j]==1 and array[j][k]==1 :
               if array[i][k]!=1:
                  print("关系",str_judge,"不是传递的")
                  return 0
      if i==len(a)-1 :
         print ("关系",str_judge,"是传递的")
         return 0
对称闭包
def Symmetric_Closure(a,array,str_judge):
   judge=0
   print("关系",str_judge,"对称闭包需要添加的关系有")
   for i in range(len(a)):
      for j in range(len(a)):
         if array[i][j]==0 and array[j][i]==1:
            print("<",a[i],",",a[j],">")
            judge=1
   if judge==0:
      print("\t无\n关系",str_judge,"本身就是对称的,s(",str_judge,")=",str_judge)
   return 0
自反闭包
def Reflexive_Closure(a,array,str_judge):
   judge=0
   print("关系",str_judge,"自反闭包需要添加的关系有")
   for i in range(len(a)):
      if array[i][i]!=0:
         print("<",a[i],",",a[i],">")
         judge=1
   if judge==0:
      print("\t无\n关系",str_judge,"本身就是自反的,s(",str_judge,")=",str_judge)
   return 0
传递闭包
def Transitive_Closure(a,array,str_judge):
   judge=0
   print("关系",str_judge,"传递闭包需要添加的关系有")
   for i in range(len(a)):
      for j in range(len(a)):
         for k in range(len(a)):
            if array[i][j]==1 and array[j][k]==1 :
               if array[i][k]!=1:
                  print("<",a[i],",",a[k],">")
                  judge=1
   if judge==0:
      print("\t无\n关系",str_judge,"本身就有传递性,t(",str_judge,")=",str_judge)
   return 0

是这样吗?

就这样吧!

你可能感兴趣的:(自主学习,python)