【C语言】Warshall 算法计算传递闭包

  • 实验目的

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

  • 调试过程

  1. 调试代码

#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;
}
  1. 运行结果

  • 总结

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

  • 23
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值