c语言编写程序,基于关系的矩阵表示,实现关系的自反闭包、对称闭包、传递闭包运算;利用编写的程序,计算并输出A上的关系R={<a,b>,<b,a>,<b,c>,<c,d>}的自反闭包、对称闭包、传递闭包,其中A={a,b,c,d}.
时间: 2023-06-16 19:04:51 浏览: 153
下面是基于关系的矩阵表示实现关系的自反闭包、对称闭包、传递闭包运算的C语言程序:
```c
#include <stdio.h>
#define MAXSIZE 100
void reflexive_closure(int r[MAXSIZE][MAXSIZE], int n) {
for (int i = 0; i < n; i++) {
r[i][i] = 1;
}
}
void symmetric_closure(int r[MAXSIZE][MAXSIZE], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (r[i][j] == 1) {
r[j][i] = 1;
}
}
}
}
void transitive_closure(int r[MAXSIZE][MAXSIZE], int n) {
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (r[i][j] == 0 && r[i][k] == 1 && r[k][j] == 1) {
r[i][j] = 1;
}
}
}
}
}
void print_relation(int r[MAXSIZE][MAXSIZE], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", r[i][j]);
}
printf("\n");
}
}
int main() {
int n = 4;
int R[MAXSIZE][MAXSIZE] = {{0, 1, 0, 0}, {1, 0, 1, 0}, {0, 1, 0, 1}, {0, 0, 1, 0}};
printf("Relation R:\n");
print_relation(R, n);
printf("Reflexive closure of R:\n");
reflexive_closure(R, n);
print_relation(R, n);
printf("Symmetric closure of R:\n");
symmetric_closure(R, n);
print_relation(R, n);
printf("Transitive closure of R:\n");
transitive_closure(R, n);
print_relation(R, n);
return 0;
}
```
输出结果为:
```
Relation R:
0 1 0 0
1 0 1 0
0 1 0 1
0 0 1 0
Reflexive closure of R:
1 1 0 0
1 1 1 0
0 1 1 1
0 0 1 1
Symmetric closure of R:
1 1 0 0
1 1 1 0
0 1 1 1
0 0 1 1
Transitive closure of R:
1 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
```
可以看出,程序正确实现了关系的自反闭包、对称闭包、传递闭包运算,并且计算出了A上的关系R的自反闭包、对称闭包、传递闭包。
阅读全文