-
实验目的
Understand how to calculate transitive closures by writing program of Warshall’s Algorithm.
-
实验内容
Input a relation matrix R and calculate and out the transitive closure of matrix R.
-
使用环境
Win 11 & visual studio code 2022
-
算法介绍
Algorithm: Warshall’s Algorithm
Procedure transitive closure (MR:a n×n boolean matrix)
A→MR
B→A
for i→2 to n
A→A ⨀ MR
B→B∨A
Return B
-
调试过程
-
调试代码
#include <stdio.h>
int main()
{
int a[4][4];
printf("Please input your matrix \n");
int i, j, k;
for (i = 0; i < 4; i++) //输入矩阵
{
for (j = 0; j < 4; j++)
scanf("%d", &a[i][j]);
}
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
if (a[i][j] == 1)
{
for (k = 0; k < 4; k++)
a[i][k] = a[j][k] + a[i][k];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
if (a[i][j] != 0)
a[i][j] = 1;
}
printf("output:\n");
for (i = 0; i < 4; i++) //输出矩阵
{
for (j = 0; j < 4; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
-
运行结果
-
总结
The transitive closure is the transitive closure of the relation R above the set Ain mathematics, which is the smallest transitive relationship including R on A. Through writing programs, Warshall’s Algorithm and the properties of transitive closure is more understood and clarified. Through the violent loop algorithm can get a correct result, but with greater complexity.
-
参考文献
[1]谭浩强,C 程序设计[M] (第四版).北京:清华大学出版社,2010年6月(中国高等院校计算机基础教育课程体系规划教材)
[2]谭浩强, C 程序设计( 第四版 )学习辅导 ,北京:清华大学出版社,2010年7月(中国高等院校计算机基础教育课程体系规划教材)
[3]C Primer Plus (第6版)中文版,Stephen Prata 著;姜佑译 ——北京 :人名邮电出版社,2019.11