Warshall算法求传递闭包

传递闭包

在数学中,在集合 X 上的二元关系 R 的传递闭包是包含 R 的 X 上的最小的传递关系。

例如,如果 X 是(生或死)人的集合而 R 是关系“为父子”,则 R 的传递闭包是关系“x 是 y 的祖先”。再比如,如果 X 是空港的集合而关系 xRy 为“从空港 x 到空港 y 有直航”,则 R 的传递闭包是“可能经一次或多次航行从 x 飞到 y”。

Warshall算法

Warshall在1962年提出了一个求关系的传递闭包的有效算法。其具体过程如下,设在n个元素的有限集上关系R的关系矩阵为M:

(1)置新矩阵A=M;

(2)置k=1;

(3)对所有i如果A[i,k]=1,则对j=1..n执行:

A[i,j]←A[i,j]∨A[k,j];

(4)k增1;

(5)如果k≤n,则转到步骤(3),否则停止。

所得的矩阵A即为关系R的传递闭包t(R)的关系矩阵。

代码实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 typedef unsigned long long ull;
 5 #define INF 0x3f3f3f3f
 6 const ll MAXN = 1e3 + 7;
 7 const ll MAXM = 1e4 + 7;
 8 const ll MOD = 1e9 + 7;
 9 const double pi = acos(-1);
10 int Mat[20][20]; //
11 void Print_Mat(int n)
12 {
13     for (int i = 0; i < n; i++)
14     {
15         for (int j = 0; j < n; j++)
16             cout << Mat[i][j] << " ";
17         cout << endl;
18     }
19     return;
20 }
21 void Warshall(int n)
22 {
23     for (int k = 0; k < n; k++)
24         for (int i = 0; i < n; i++)
25             for (int j = 0; j < n; j++)
26                 if (Mat[i][k] && Mat[k][j])
27                     Mat[i][j] = 1;
28 }
29 int main()
30 {
31     int n;
32     cout << "输入矩阵阶数" << endl;
33     while (cin >> n)
34     {
35         memset(Mat, 0, sizeof(Mat));
36         cout << "输入矩阵M:" << endl;
37         for (int i = 0; i < n; i++)
38             for (int j = 0; j < n; j++)
39                 cin >> Mat[i][j];
40         for (int i = 0; i < n-1; i++)
41             Warshall(n);
42         cout << "矩阵M的传递闭包为:" << endl;
43         Print_Mat(n);
44         cout << "输入矩阵阶数" << endl;
45     }
46     return 0;
47 }

原文地址:https://www.cnblogs.com/graytido/p/10886732.html

时间: 2024-08-22 18:47:40

Warshall算法求传递闭包的相关文章

用warshall算法求关系闭包(离散数学)

#include<stdio.h> main() { int i,n,j,k,a,b,x[100][100]; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&x[i][j]); for(i=1;i<=n;i++) for(j=1;j<=n;j++) for(k=1;k<=n;k++) { if(x[j]

Warshall算法

---用Warshall算法解决传递闭包问题 ---在一个关系R中,如果任意的(a,b)和(b,c)在关系R中,则(a,c)也一定在关系R中,则称R是可传递的.一个关系R的传递闭包是包 含R的最小的传递关系. ---Warshall算法不用来计算最短路径,而是用来判断i和j之间是否单向连接,无论是直接连接还是间接连接 ---初始条件不再是邻接矩阵,而是关系矩阵,如果两个点之间直接单向连接,那么在矩阵中对应的值就为1,反之为0 ---根据已有的直接连接关系得出其它所有的间接连接关系,即判断两点之间

求传递闭包的warshall算法

———————————————————————————— Question:R是定义于集合S上的二元关系,求R的传递闭包. Input:relation R,set A Output:t(R),which is the transitive closure of R  Solution:Warshall algorithm ———————————————————————————— 传递闭包(transitive closure) R* = R1 ∪ R2 ∪ R3 ∪ ...... ∪ Rn  

247 - Calling Circles (Floyd 求传递闭包)

该题恰好用到刚刚讲到的Floyd求传递闭包 , 因为该题的数据量不是很大, 要是大了估计就要用其他方法了 .  但是这毕竟是一个很简单易写的算法 . 求出传递闭包之后就用并查集将在一个电话圈的人合并在一起,最后输出 . 细节参见代码: #include<bits/stdc++.h> using namespace std; int n,m,d[30][30],par[30],kase = 0; string s1,s2; map<string , int > p; map<i

uva 125 Numbering Paths(warshall算法)

uva 125 Numbering Paths Description Download as PDF Background Problems that process input and generate a simple yes'' orno" answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general ef

poj2187 求平面最远点对,garham_scan算法求凸包

poj2187 求平面最远点对,garham_scan算法求凸包 Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 29666   Accepted: 9180 Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'M

EM算法求高斯混合模型参数估计——Python实现

EM算法一般表述: 当有部分数据缺失或者无法观察到时,EM算法提供了一个高效的迭代程序用来计算这些数据的最大似然估计.在每一步迭代分为两个步骤:期望(Expectation)步骤和最大化(Maximization)步骤,因此称为EM算法. 假设全部数据Z是由可观测到的样本X={X1, X2,--, Xn}和不可观测到的样本Z={Z1, Z2,--, Zn}组成的,则Y = X∪Z.EM算法通过搜寻使全部数据的似然函数Log(L(Z; h))的期望值最大来寻找极大似然估计,注意此处的h不是一个变量

算法 - 求两个自然数的最大公约数(C++)

placeholder算法 - 求两个自然数的最大公约数(C++),布布扣,bubuko.com

算法 - 求两个自然数的最小公倍数(C++)

placeholder算法 - 求两个自然数的最小公倍数(C++),布布扣,bubuko.com