[关闭]
@Arbalest-Laevatain 2018-05-28T07:54:57.000000Z 字数 5803 阅读 831

离散数学作业07

离散数学作业


第七章练习题

第十五题

(1)图A
哈斯图A.JPG-26.7kB
(2)图B
哈斯图B.JPG-30.5kB

关系计算

求关系的定义域

  1. //求定义域
  2. #include <iostream>
  3. using namespace std;
  4. //定义一种关系
  5. class R
  6. {
  7. public:
  8. int s1,s2;
  9. //int* next;
  10. };
  11. //定义一个同余关系
  12. int mmod(int a,int b)
  13. {
  14. if (a%4==b%4)
  15. return 1;
  16. else
  17. return 0;
  18. }
  19. //定义两个非空集合A、B
  20. int A[10]={1,2,3,4,5,6,7,8,9,10};
  21. int B[5]={1,2,3,4,5};
  22. int main(int argc, char** argv) {
  23. R*g=new R[50];
  24. int n=0;
  25. for (int i=0;i<10;i++)
  26. {
  27. for (int j=0;j<5;j++)
  28. {
  29. if (mmod(A[i],B[j]))
  30. {
  31. g[i].s1=A[i];
  32. g[i].s2=B[j];
  33. n++;
  34. //cout<<(g[i].s1)<<endl;
  35. }
  36. }
  37. }
  38. //cout<<n<<endl;
  39. for (int i=0;i<n;i++)
  40. {
  41. int t=g[i].s1;
  42. for (int j=0;j<n;j++)
  43. {
  44. if (j!=i && g[j].s1==t)
  45. {
  46. g[j].s1=0;
  47. }
  48. }
  49. }
  50. cout<<"该关系的定义域为:"<<endl;
  51. for (int i=0;i<n;i++)
  52. if (g[i].s1)
  53. cout<<g[i].s1<<" ";
  54. cout<<endl;
  55. delete[]g;
  56. return 0;
  57. }

求关系的值域

  1. #include <iostream>
  2. using namespace std;
  3. //定义一种关系
  4. class R
  5. {
  6. public:
  7. int s1,s2;
  8. //int* next;
  9. };
  10. //定义一个同余关系
  11. //模4同余
  12. int mmod(int a,int b)
  13. {
  14. if (a%4==b%4)
  15. return 1;
  16. //满足关系则返回1,否则返回0
  17. else
  18. return 0;
  19. }
  20. //定义两个非空集合A、B
  21. int A[10]={1,2,3,4,5,6,7,8,9,10};
  22. int B[5]={1,2,3,4,5};
  23. int main(int argc, char** argv) {
  24. R*g=new R[50];
  25. int z[20]={0};
  26. int *z0=z;
  27. int n=0,i;
  28. for (i=0;i<10;i++)
  29. {
  30. for (int j=0;j<5;j++)
  31. {
  32. if (mmod(A[i],B[j]))
  33. //判断是否满足关系
  34. {
  35. g[i].s1=A[i];
  36. g[i].s2=B[j];
  37. n++;
  38. //cout<<(g[i].s2)<<'\n';
  39. z[n]=B[j];
  40. z0++;
  41. }
  42. }
  43. }
  44. //cout<<n<<endl;
  45. int m=0;
  46. //消除重复的项
  47. for (i=0;i<n;i++)
  48. {
  49. int t=z[i];
  50. for (int j=0;j<n;j++)
  51. {
  52. if (j!=i && z[j]==t)
  53. {
  54. z[j]=0;
  55. }
  56. }
  57. }
  58. cout<<"该关系的值域为:"<<"\n";
  59. for (i=0;i<n;i++)
  60. if (z[i])
  61. cout<<z[i]<<" ";
  62. cout<<"\n";
  63. delete[]g;
  64. return 0;
  65. }

求关系的逆

  1. #include <iostream>
  2. using namespace std;
  3. //定义一种关系
  4. class R
  5. {
  6. public:
  7. int s1,s2;
  8. //int* next;
  9. };
  10. //定义一个关系
  11. R g0[]={{1,2},{2,3},{3,1}};
  12. int main(int argc, char** argv) {
  13. R g[3];
  14. //获取关系集的长度
  15. int l=(sizeof(g0)/sizeof(R));
  16. for (int i=0;i<l;i++)
  17. {
  18. g[i].s1=g0[i].s2;
  19. g[i].s2=g0[i].s1;
  20. }
  21. cout<<l<<'\n';
  22. cout<<"关系的逆为:";
  23. for (int i=0;i<l;i++)
  24. {
  25. cout<<"<"<<g[i].s1<<","<<g[i].s2<<">"<<",";
  26. } cout<<"\n";
  27. return 0;
  28. }

求关系的复合运算

  1. #include <iostream>
  2. using namespace std;
  3. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  4. //定义一种关系
  5. class R
  6. {
  7. public:
  8. int s1,s2;
  9. //int* next;
  10. };
  11. //定义两个关系
  12. R g1[]={{1,2},{2,3},{3,1}};
  13. R g2[]={{1,3},{2,1},{3,2}};
  14. int main(int argc, char** argv) {
  15. R g[5];
  16. //获取关系集的长度
  17. int l=(sizeof(g1)/sizeof(R));
  18. for (int i=0;i<l;i++)
  19. {
  20. for (int j=0;j<l;j++)
  21. {
  22. g[i].s1=g1[i].s1;
  23. if (g2[j].s1==g1[i].s2)
  24. g[i].s2=g2[j].s2;
  25. }
  26. }
  27. cout<<l<<'\n';
  28. cout<<"关系的复合运算为:";
  29. for (int i=0;i<l;i++)
  30. {
  31. cout<<"<"<<g[i].s1<<","<<g[i].s2<<">"<<",";
  32. } cout<<"\n";
  33. return 0;
  34. }

判定关系是否是自反

  1. #include <iostream>
  2. using namespace std;
  3. //定义一种关系
  4. class R
  5. {
  6. public:
  7. int s1,s2;
  8. //int* next;
  9. };
  10. //定义一个基数为n的非空集合 a
  11. const int n=3;
  12. int a[n]={1,2,3};
  13. //定义一个非空集合a上的关系
  14. R g0[n]={{1,1},{2,2},{3,3}} ;
  15. R g1[n]={{1,2},{2,3},{3,1}};
  16. //判定函数
  17. int reflex(R *g)
  18. {
  19. int l=sizeof(g)/sizeof(R);
  20. int m=0;
  21. for (int i=0;i<l;i++)
  22. {
  23. for (int j=0;j<l;j++)
  24. {
  25. if (g[i].s1==g[i].s2 && g[i].s1==a[j])
  26. m++;
  27. }
  28. }
  29. if (m==l)
  30. return 1;
  31. else
  32. return 0;
  33. }
  34. int main(int argc, char** argv) {
  35. if (reflex(g1))
  36. cout<<"是自反关系"<<'\n';
  37. else
  38. cout<<"不是自反关系"<<'\n';
  39. return 0;
  40. }

判定关系是否反自反

  1. #include <iostream>
  2. using namespace std;
  3. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  4. //定义一种关系
  5. class R
  6. {
  7. public:
  8. int s1,s2;
  9. //int* next;
  10. };
  11. //定义一个非空集合 a
  12. const int n=3;
  13. int a[n]={1,2,3};
  14. //定义一个非空集合a上的关系
  15. R g0[n]={{1,1},{2,2},{3,3}} ;
  16. R g1[n]={{1,2},{2,3},{3,1}};
  17. //判定函数
  18. int antireflex(R *g)
  19. {
  20. int m=0;
  21. for (int i=0;i<n;i++)
  22. {
  23. if (g[i].s1!=g[i].s2)
  24. m++;
  25. }
  26. if (m==l)
  27. return 1;
  28. else
  29. return 0;
  30. }
  31. int main(int argc, char** argv) {
  32. if (antireflex(g0))
  33. cout<<"是反自反关系"<<'\n';
  34. else
  35. cout<<"不是反自反关系"<<'\n';
  36. return 0;
  37. }

判定关系是否为对称

  1. #include <iostream>
  2. using namespace std;
  3. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  4. //定义一种关系
  5. class R
  6. {
  7. public:
  8. int s1,s2;
  9. //int* next;
  10. };
  11. //定义一个非空集合 a
  12. const int n=3;
  13. int a[n]={1,2,3};
  14. //定义一个非空集合a上的关系
  15. R g0[n]={{1,1},{2,2},{3,3}} ;
  16. R g1[n]={{1,1},{2,2},{3,1}};
  17. //判定函数
  18. int symm(R *g)
  19. {
  20. R g0[n];
  21. //int l=sizeof(g)/sizeof(R);
  22. int m=0;
  23. for (int i=0;i<n;i++)
  24. {
  25. g0[i].s2=g[i].s1;
  26. g0[i].s1=g[i].s2;
  27. }
  28. for (int i=0;i<n;i++)
  29. {
  30. for (int j=0;j<n;j++)
  31. {
  32. if (g[i].s1==g0[j].s1 && g[i].s2==g0[j].s2)
  33. m++;
  34. }
  35. }
  36. if (m==n)
  37. return 1;
  38. else
  39. return 0;
  40. }
  41. int main(int argc, char** argv) {
  42. if (symm(g1))
  43. cout<<"是对称关系"<<'\n';
  44. else
  45. cout<<"不是对称关系"<<'\n';
  46. return 0;
  47. }

判定关系是否为反对称

  1. #include <iostream>
  2. using namespace std;
  3. //定义一种关系
  4. class R
  5. {
  6. public:
  7. int s1,s2;
  8. //int* next;
  9. };
  10. //定义一个非空集合 a
  11. const int n=3;
  12. int a[n]={1,2,3};
  13. //定义一个非空集合a上的关系
  14. R g0[n]={{1,2},{2,1},{3,3}} ;
  15. R g1[n]={{1,1},{2,3},{3,1}};
  16. //判定函数
  17. int antisymm(R *g)
  18. {
  19. R g0[n];
  20. //int l=sizeof(g)/sizeof(R);
  21. int m1=0,i;
  22. int nn=n;
  23. for (i=0;i<n;i++)
  24. {
  25. g0[i].s2=g[i].s1;
  26. g0[i].s1=g[i].s2;
  27. }
  28. for (i=0;i<n;i++)
  29. {
  30. if (g[i].s1==g[i].s2)
  31. nn--;
  32. int m2=0;
  33. for (int j=0;j<n;j++)
  34. {
  35. if (g[i].s1!=g[i].s2)
  36. {
  37. if ((g[i].s1==g0[j].s1 && g[i].s2==g0[j].s2)==0)
  38. m2++;
  39. }
  40. }
  41. if (m2==n)
  42. m1++;
  43. }
  44. if (m1==nn)
  45. return 1;
  46. else
  47. return 0;
  48. }
  49. int main(int argc, char** argv) {
  50. if (antisymm(g0))
  51. cout<<"是反对称关系"<<'\n';
  52. else
  53. cout<<"不是反对称关系"<<'\n';
  54. return 0;
  55. }

判定关系是否为传递

  1. //判断函数
  2. **
  3. * 判断一个关系是否具有传递性。
  4. * pA 原始集合
  5. * pBinaryRelationR 卡氏积集合,该集合是一个pA上的二元关系
  6. * 如果pBinaryRelationR具有传递性,则返回true,否则返回false
  7. */
  8. boolean IsTransitive(pOriginalSet pA, pCartersianSet pBinaryRelationR)
  9. {
  10. pOriginalSetElem First1,Second1;
  11. pOriginalSetElem First2,Second2;
  12. pCartersianSet pB=copyCartersianSet(pBinaryRelationR);
  13. pCartersianSet pC=copyCartersianSet(pBinaryRelationR);
  14. if(!isNullCartersianSet(pBinaryRelationR))
  15. {
  16. for(resetCartersianSet(pC);!isEndOfCartersianSet(pC);nextCartersianSetPos(pC))
  17. {
  18. First1=getFirstElemOfOrderedCouple(getCurrentCartersianSetElem(pC));
  19. Second1=getSecondElemOfOrderedCouple(getCurrentCartersianSetElem(pC));
  20. for(resetCartersianSet(pB);!isEndOfCartersianSet(pB);nextCartersianSetPos(pB))
  21. {
  22. First2=getFirstElemOfOrderedCouple(getCurrentCartersianSetElem(pB));
  23. Second2=getSecondElemOfOrderedCouple(getCurrentCartersianSetElem(pB));
  24. if(isEqualOriginalSetElem(First2,Second1))
  25. {
  26. if(!isInCartersianSet(pBinaryRelationR,createOrderedCouple(First1,Second2)))
  27. return false;}
  28. }
  29. }
  30. }
  31. else return true;
  32. return true;
  33. }

第二十题

解:
画出P的子集T的哈斯图,易得上界为3,4,5的公倍数
下界为1,最大上界为1,最小上界为60.

第二十一题

证明:
自反性:
根据定义有
既有,满足自反性
反对称性:
,若有时,仅当时,,满足反对称性
传递性:
,即,即,满足传递性
综上,是偏序集

六例关系

等价关系两例

  1. 集合为一条水准路线中的测站点,关系为精度相同的杆件测站
  2. 是实数域上的矩阵组成的集合,关系为矩阵,的行数列数相同且同秩

偏序关系两例

3、设集合中的每一个元素都是一位公司职员,关系为上司给下级分派任务
4、设为我们国家的大地控制网中的各级导线,关系为从已知点设计测量路线对未知地域的测量

5、 设为数据结点集,关系为链表关系。从头结点开始,一个指向一个
6、设为决策分类树集合,关系为分类结果相同的决策分类树

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注