离散数学之集合关系(赋代码实现版)
前言
- 最近在学习离散数学,在这里给大家总结一下离散数学的集合关系吧
- 自反性
- 对称性
- 传递性
- 并且我用代码给大家实现了一下,但是代码测试的并不多,不晓得有没有bug,大家若留了这方面的作业,可拿去一测😀
一、自反性
- 自反性其实很简单,我先给大家画个图。
- 再给大家举个简单的例子
集合A={1,2,3,4},
自反的条件:必须满足集合中元素都有,<1 1> <2 2> < 3 3> <4 4>,其他不用考虑,只有有这四个恒等对,就使自反
反自反的条件:若没用任何一个恒等对,它就使反自反的!
既不自反也不反自反的条件:有恒等对,但是不全,它就是既不自反也不是反自反。
测试用例:
- <1 1> <2 2> < 3 3> <4 4>自反
- <1 2> <2 3> <2 4>反自反
- <1 1> <2 4> < 3 4>既不是自反,也不是反自反
二、对称性
- 对称性其实可以分成四种情况讨论,还是先看个图吧。
- 还是这个集合A=
- 首先要明确:恒等对并不影响对称性和反对称性。
- 对称:不需要考虑恒等对,若其它都是对称的则就对称。eg:<1 1> <2 1> <1 2>对称
- 反对称:不需要考虑恒等对,若有任何一对不是对称的,则就是反对称。
- 既对称也是反对称:只有恒等对的情况下成立<1 1> <2 2>...
- 既不是对称,也不是反对称:存在对称的情况,但同时也存在不对称的情况,则就是既不是对称也不是反对称<1 1> <2 1> <1 2> <2 3>
测试用例:
- <1 1> <2 2> <1 2> <2 1> <2 3> < 3 2>对称
- <2 1> <1 2> <2 4> <4 2>对称
- <1 2> <1 3> <2 4>反对称
- <1 1> < 3 3> <1 2>反对称
- <1 1> <2 2> < 3 3> <4 4>既对称也是反对称
- <1 1> <2 1> <1 2> <1 3> < 3 3>既不是对称,也不是反对称
三、传递性
- 传递性分成两种情况:废话少说上图!👆
- 若<.x y>∈R且<.y z>∈R,则<.x z>∈R
- 还是这个集合A=
- 传递:满足条件2,或则不满足条件2的“若”情况。这里只要记住如果前面是假的情况,则后面不需要考虑,我们的结果一定为真
- 非传递:不满足条件2的整体情况,具体给大家举几个例子就明白了
测试用例:
- <1 3> < 3 2> < 1 2>传递(满足条件2)
- <1 3> < 1 4> 传递(因为条件2的若情况为假,有
没用 ,所以结果就为真,这里需要大家仔细体会) - 举个例子就是:假如太阳从西边升起,我就写作业。 首先太阳从西边升起肯定是假的,但是我写不写作业都无所谓。我写作业为真,不写作业就为假。所以不取决于前者。
- <1 1> <1 2> <2 1>非传递(因为没有<2 2>,大家需要细心判断)
- <1 1> < 2 3> < 3 4> < <2 4>传递
四、代码实现
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class MathTest { public static void main(String[] args) throws IOException { Math.judgeOpposite(); //判断自反性 Math.judgeSymmetry(); //判断对称性 Math.judgePass(); //判断传递性 } }
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.*; public class Math { public Math() { } public static void judgeOpposite() throws IOException{ //判断自反性 String filePath = "D:\\lspd.txt"; int read = 0; int count = 0; int key = 0; ArrayList<Object> list = new ArrayList<>(); FileInputStream file = null; try { file = new FileInputStream(filePath); while((read = file.read()) != '<'){ if(read >= 65 && read <= 122){ key++; } } while(read != -1) { if(read >= 65 && read <= 122) { list.add(read); } System.out.print((char)read); read = file.read(); } System.out.println(); for (int i = 0; i < list.size() -1; i += 2) { if(list.get(i) == list.get(i+1)) { count++; } } if(count == key) { System.out.println("它是自反"); } else if(count == 0) { System.out.println("它是反自反"); } else { System.out.println("既不是自反,也不是反自反"); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { file.close(); } } public static void judgeSymmetry() throws IOException { //判断对称性 String filePath = "D:\\lspd.txt"; int read = 0; int count = 0; int key = 0; int k = 0; int sum = 0; boolean flag = true; boolean obj = true; ArrayList<Object> list = new ArrayList<>(); FileInputStream file = null; try { file = new FileInputStream(filePath); while((read = file.read()) != -1){ if(read == '<') sum++; } file.close(); read = 0; file = new FileInputStream(filePath); while((read = file.read()) != -1){ if(read == '<') sum++; } file.close(); read = 0; file = new FileInputStream(filePath); while((read = file.read()) != '<'){ if(read >= 65 && read <= 122){ key++; } } while(read != -1) { if(read >= 65 && read <= 122) { int a = read; read = file.read(); read = file.read(); int b = read; if(a==b){ count++; obj = true; }else{ count--; obj = false; list.add(a); list.add(b); } } read = file.read(); } for (int i = 0; i < list.size() - 2; i += 2) { for (int j = 0; j < list.size()-2; j += 2) { if(list.get(i) == list.get(j+3) && list.get(i+1) == list.get(j+2)){ flag = true; k++; }else{ flag = false; } } } if(obj) { System.out.println("它既是对称,也是反对称"); } else if(flag){ System.out.println("它是对称的"); }else{ if(k>0){ System.out.println("它不是对称,也不是反对称"); } else{ System.out.println("它是反对称"); } } } catch (IOException e) { e.printStackTrace(); } finally { file.close(); } } public static void judgePass() throws IOException { //判断传递性 HashMap<Integer,Integer> map = new HashMap<>(); String filePath = "D:\\lspd.txt"; int read = 0; int key = 0; boolean flag = true; FileInputStream file = null; try { file = new FileInputStream(filePath); while((read = file.read()) != -1){ if(read == '<') key++; } file.close(); read = 0; file = new FileInputStream(filePath); while((read = file.read()) != '<'); int[][] list = new int[key][2]; int i = 0; while(read !=-1){ for (; i < key &&read >= 65 && read <= 122; i++,read = file.read()) { for (int j = 0; j < 2 &&read >= 65 && read <= 122; j++,read = file.read()) { list[i][j] = read; read = file.read(); } } read = file.read(); } int index1 = 0; int index2 = 0; for (int j = 0; j < key; j++) { for (int k = 0; k < key; k++) { if(list[j][1] == list[k][0]){ int a = list[j][0]; int b = list[k][1]; for (int l = 0; l < key; l++) { index1 = 0; index2 = -1; if(a == list[l][0]){ index1 = l; } for (int m = 0; m < key; m++) { if(b == list[m][1]) { index2 = m; } if(index1 == index2) break; } } } } } int q1 = 0; int q2 = 0; for (int j = 0; j < key; j++) { for (int k = j+1; k < key; k++) { if(list[j][0] == list[k][0]){ q1++; } if(list[j][0] == list[k-1][1]){ q2++; } } } if(index1 == index2){ System.out.println("它是传递的"); }else if(q1+q2 == key){ System.out.println("它是传递的"); }else{ System.out.println("它是非传递的"); } } catch (IOException e) { e.printStackTrace(); } finally { file.close(); } } }
五、结尾
- 对于几何关系内容就总结这么多,希望大家可以多多练习。如果有不足之处,希望大家多多包涵,多多支持。如果有不懂的地方可以直接私信问我,欢迎来访!
- 这部分内容也可以使用集合表达式进行判断,这里就不在详细讲解
- 我将会继续更新关于计算机的学习知识,喜欢Java的,感兴趣的小伙伴可以关注一下。
- 文章写得比较走心,用了很长时间,绝对不是copy过来的!
- 尊重每一位学习知识的人,同时也尊重每一位分享知识的人。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步