离散数学——小白编写判断关系是否是传递的,C语言
1.判断方法
具有传递性的二元关系矩阵性质:如果矩阵第i行第j列和第j行第i列为1,则第i行第i列也应为1
(也就是二元关系传递性定义:R具有传递性指的是若元组(a, b)与(b, c)都在R中,那么(a, c)一定在R中)
为什么用关系矩阵做?
因为直接根据定义:若元组(a, b)与(b, c)都在R中,那么(a, c)一定在R中,用类似
r1.a==r2.b&&r2.a==r3.a&&r1.b==r3.b
(r1,r2,r3为存储序偶<a,b>的结构体)去判断时,
- 1.需要对传入函数的三个序偶进行排列组合,把所有排列可能性传进去才行(传进去时可能r1,r2,r3这样排列不满足,但当排列为r3,r1,r2时满足)
- 2.还要考虑关系具有传递性,不代表着关系里所有序偶之间都要满足“若元组(a, b)与(b, c)都在R中,那么(a, c)一定在R中”传递性这个定义,例如:R={<1,2>,<2,3>,<1,3>,<4,4>,<5,5>}满足传递性,因此需要格外编写这些情况下的关系是否传递的程序,个人觉得有些繁杂
- 3.而在关系矩阵里根据数值判断,貌似(没有进行十分全面的测试)可以省去以上两点考虑,较为轻便地得出结果
2.代码缺陷(小白以目前水平自查,欢迎指正建议)
- 1.没能将具体功能划分为函数使用,困难:关系矩阵的行数列数不确定,不好作为形参传入
- 2.其中有较多类似的代码进行重复,实现过程比较绕,不知道C语言有没有自带的生成矩阵的,矩阵乘法的函数
- 3.变量起名不规范,有点奇怪
3.代码流程
- 1.创建序偶结构体<a,b>
typedef struct
{
int a;
int b;
} Pair;
-
2.创建序偶类型数组,输入关系序偶
int size = 0; printf("Enter the size of relation R:"); scanf("%d", &size); Pair R[size]; for (int i = 0; i < size; i++) { R[i].a = 0; R[i].b = 0; } for (int i = 0; i < size; i++) { printf("Enter the <a,b> of relation R[%d]:", i); scanf("%d %d", &R[i].a, &R[i].b); }
-
3.创建并赋值关系矩阵
// 数行数列数 int row = 0; int col = 0; // count2,count3计数R[j].a,R[j].b与前面<a,b>不同的次数,与前面全都不同就行数,列数+1 int count2 = 0; int count3 = 0; for (int i = 0; i < size; i++) { if (i == 0) { row++; col++; } else { for (int j = 0; j < i; j++) { if (R[j].a == R[i].a) { break; } else { if (R[j].a != R[i].a) { count2++; } } } for (int j = 0; j < i; j++) { if (R[j].b == R[i].b) { break; } else { if (R[j].b != R[i].b) { count3++; } } } if (count2 == i) { row++; } if (count3 == i) { col++; } count2 = 0; count3 = 0; } } int relation_matrix[row][col]; // 赋值 for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { relation_matrix[i][j] = 0; } } // count6,count7通过计数R[i].a和R[i].b与之前所有的序偶有多少个不同,就可得出其所在行,列 int count6 = 0; int count7 = 0; for (int i = 0; i < size; i++) { for (int j = i; j > -1; j--) { if (R[i].a != R[j].a) { count6++; } if (R[i].b != R[j].b) { count7++; } } relation_matrix[count6][count7] = 1; count6 = 0; count7 = 0; }
-
4.判断传递性,输出结果
// 通过判断count4与count5的数值有无差值,可知全部<a,c>,<c,b>是否都有<a,b> int count4 = 0; int count5 = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (i != j) { if (relation_matrix[i][j] == 1 && relation_matrix[j][i] == 1) { count4++; if (relation_matrix[i][j] == 1) { count5++; } } } } } if (count4 == count5) { printf("The relationship R has transitivity."); } else { printf("The relationship R does not have transitivity."); }
4.整体代码
#include <stdio.h>
// 创建序偶结构体<a,b>
typedef struct
{
int a;
int b;
} Pair;
int main()
{
int size = 0;
printf("Enter the size of relation R:");
scanf("%d", &size);
Pair R[size];
for (int i = 0; i < size; i++)
{
R[i].a = 0;
R[i].b = 0;
}
for (int i = 0; i < size; i++)
{
printf("Enter the <a,b> of relation R[%d]:", i);
scanf("%d %d", &R[i].a, &R[i].b);
}
// 创建关系矩阵
// 数行数列数
int row = 0;
int col = 0;
// count2,count3计数R[j].a,R[j].b与前面<a,b>不同的次数,与前面全都不同就行数,列数+1
int count2 = 0;
int count3 = 0;
for (int i = 0; i < size; i++)
{
if (i == 0)
{
row++;
col++;
}
else
{
for (int j = 0; j < i; j++)
{
if (R[j].a == R[i].a)
{
break;
}
else
{
if (R[j].a != R[i].a)
{
count2++;
}
}
}
for (int j = 0; j < i; j++)
{
if (R[j].b == R[i].b)
{
break;
}
else
{
if (R[j].b != R[i].b)
{
count3++;
}
}
}
if (count2 == i)
{
row++;
}
if (count3 == i)
{
col++;
}
count2 = 0;
count3 = 0;
}
}
int relation_matrix[row][col];
// 赋值
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
relation_matrix[i][j] = 0;
}
}
// count6,count7通过计数R[i].a和R[i].b与之前所有的序偶有多少个不同,就可得出其所在行,列
int count6 = 0;
int count7 = 0;
for (int i = 0; i < size; i++)
{
for (int j = i; j > -1; j--)
{
if (R[i].a != R[j].a)
{
count6++;
}
if (R[i].b != R[j].b)
{
count7++;
}
}
relation_matrix[count6][count7] = 1;
count6 = 0;
count7 = 0;
}
// 判断传递性
// 通过判断count4与count5的数值有无差值,可知全部<a,c>,<c,b>是否都有<a,b>
int count4 = 0;
int count5 = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (i != j)
{
if (relation_matrix[i][j] == 1 && relation_matrix[j][i] == 1)
{
count4++;
if (relation_matrix[i][i] == 1)
{
count5++;
}
}
}
}
}
if (count4 == count5)
{
printf("The relationship R has transitivity.");
}
else
{
printf("The relationship R does not have transitivity.");
}
return 0;
}