【离散数学】【改进版】实验二 集合上二元关系性质判定的实现
来源:互联网 发布:网络培训平台 编辑:程序博客网 时间:2024/05/29 06:40
性质判断原理和编码思路
关于自反性、对称性、传递性、反自反性和反对称性的定义不在此赘述。自反性对称性和反自反反对称比较简单,关于传递性的判断,我们使用Warshall算法计算传递闭包,当传递闭包对应的关系矩阵与原关系矩阵一致时,我们认为它是满足传递性的。
关于编码思路,做个提纲:
一共6个函数,前5个函数分别表示对5个性质的判断,第6个是Warshall算法函数,实现封装机制,在第3个判断传递性的函数中直接调用函数6即可。
关于输入输出的说明:第一次输入的是集合元素个数,第二个输入的是关系个数,然后接着输入关系,输出结果判断,我将在下面以例子说明。
实现代码:
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <vector>#include <string.h>#include <cstring>using namespace std;const int LEN = 100;bool Reflexivity(); //自反性bool Symmetry(); //对称性bool Transmission(); //传递性bool Irreflexivity(); //反自反性bool Irsymmetry(); //反对称性void Warshall(); //Warshall算法int num;int relation_num;int relation[LEN][LEN];int A[LEN][LEN];int main(){ while(cin >> num && cin >> relation_num) { int tmp1, tmp2; memset(relation, 0, sizeof(relation)); memset(A, 0, sizeof(A)); for(int i = 1; i <= relation_num; i++) { cin >> tmp1 >> tmp2; relation[tmp1][tmp2] = 1; } if(Reflexivity()) { cout << "Meet the reflexive..." ; } else { cout << "Not meet the reflexive..."; } cout << endl; if(Symmetry()) { cout << "Meet the Symmetry..."; } else { cout << "Not meet the Symmetry..."; } cout << endl; if(Transmission()) { cout << "Meet the Transmission..."; } else { cout << "Not meet the Transmission..."; } cout << endl; if(Irreflexivity()) { cout << "Meet the Irreflexivity..."; } else { cout << "Not meet the Irreflexivity..."; } cout << endl; if(Irsymmetry()) { cout << "Meet the Irsymmetry.."; } else { cout << "Not meet the Irsymmetry..."; } cout << endl; } return 0;}bool Reflexivity() //自反性{ // bool flag = false; for(int i = 1; i <= num; i++) { if(relation[i][i] != 1) { return false; } } return true;}bool Symmetry() //对称性{ for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(relation[i][j] != relation[j][i]) { return false; } } } return true;}bool Transmission() //传递性{ Warshall(); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(A[i][j] != relation[i][j]) { return false; } } } return true;}bool Irreflexivity() //反自反性{ for(int i = 1; i <= num; i++) { if(relation[i][i] == 1) { return false; } } return true;}bool Irsymmetry() //反对称性{ for(int i = 1; i <= num - 1; i++) { for(int j = i + 1; j <= num; j++) { if(relation[i][j] == 1 && relation[j][i] == 1) { if(i != j) { return false; } } } } return true;}void Warshall() //Warshall算法{ for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { A[i][j] = relation[i][j]; } } for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(A[j][i] == 1) { for(int k = 1; k <= num; k++) { A[j][k] = A[j][k] + A[i][k]; if(A[j][k] >= 1) { A[j][k] = 1; } } } } }}
假设我们有一个集合A={1,2,3,4},A上的关系为{<1,1>,<1,3>,<2,2>,<3,3>,<3,1>,<3,4>,<4,3>,<4,4>},接下来我们判断该关系的性质。
因为集合元素个数有4个,所以输入4
因为关系个数共8个,所以接着输入8
接着输入
1 1
1 3
2 2
....
等,一共8组数据。
运行示例如下:
0 0
- 【离散数学】【改进版】实验二 集合上二元关系性质判定的实现
- 【离散数学】实验二 集合上二元关系性质判定的实现
- 南邮离散数学实验2 集合上二元关系性质判定的实现
- 集合上二元关系性质的判定
- 离散数学代码实现 二元关系的可传递性判断
- 【离散数学实验】关系R的性质判断
- 离散数学-二元关系、闭包的概念
- 判定二元关系的可传递性
- 离散数学之集合上关系的判断
- HashSet 集合性质实验
- 【离散数学】实验三 偏序关系中盖住关系的求取及格论中有补格的判定
- 南京邮电大学离散数学实验三偏序关系中盖住关系的求取及格论中有补格的判定
- <<离散数学上机实验>>集合上机实验
- 南邮离散数学实验二
- 判断集合二元关系的符合运算
- 离散数学 集合的运算
- 南邮离散数学实验3 偏序关系中盖住关系的求取及格论中有补格的判定
- 南邮离散数学实验三-偏序关系中盖住关系的求取及格论中有补格的判定
- sql主键和外键的区别
- Flask学习笔记02--Flask基本结构
- 软件功能测试UFT篇
- 如果用Servlet做文件下载的话,当用户中途取消了下载那么Servlet是会继续向用户传送没有传送完的文件还是Servlet会自动关闭当前线程?
- iOS 苹果官方Demo合集
- 【离散数学】【改进版】实验二 集合上二元关系性质判定的实现
- mysql_real_escape_string()--mysql数据库
- CentOS + Tomcat 请配置 JRE_HOME 环境变量
- IOS(Simulator)模拟器切换横竖屏的快捷键
- c++ primer第五版(中文)习题答案 第十章第四节第二小节-iostream迭代器
- Android 开发中的日常积累
- WEB前端学习论坛整理
- 数据结构 单链表实现前插、后插、前删、后删、显示、查找等等
- TCP/IP协议、Http协议、ftp协议