Transitive Đóng nó thành ma trận khả năng truy xuất để đạt được từ đỉnh u đến đỉnh v của đồ thị. Một đồ thị được đưa ra, chúng ta phải tìm một đỉnh v có thể tới được từ một đỉnh u khác, cho tất cả các cặp đỉnh (u, v).
Ma trận cuối cùng là kiểu Boolean. Khi có giá trị 1 cho đỉnh u đến đỉnh v, điều đó có nghĩa là có ít nhất một đường đi từ u đến v.
Đầu vào và Đầu ra
Input: 1 1 0 1 0 1 1 0 0 0 1 1 0 0 0 1 Output: The matrix of transitive closure 1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1
Thuật toán
transColsure(graph)
Đầu vào: Đồ thị đã cho.
Đầu ra: Ma trận đóng cửa bắc cầu.
Begin copy the adjacency matrix into another matrix named transMat for any vertex k in the graph, do for each vertex i in the graph, do for each vertex j in the graph, do transMat[i, j] := transMat[i, j] OR (transMat[i, k]) AND transMat[k, j]) done done done Display the transMat End
<2> Ví dụ
#include<iostream>
#include<vector>
#define NODE 4
using namespace std;
/* int graph[NODE][NODE] = {
{0, 1, 1, 0},
{0, 0, 1, 0},
{1, 0, 0, 1},
{0, 0, 0, 0}
}; */
int graph[NODE][NODE] = {
{1, 1, 0, 1},
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1}
};
int result[NODE][NODE];
void transClosure() {
for(int i = 0; i<NODE; i++)
for(int j = 0; j<NODE; j++)
result[i][j] = graph[i][j]; //initially copy the graph to the result matrix
for(int k = 0; k<NODE; k++)
for(int i = 0; i<NODE; i++)
for(int j = 0; j<NODE; j++)
result[i][j] = result[i][j] || (result[i][k] && result[k][j]);
for(int i = 0; i<NODE; i++) { //print the result matrix
for(int j = 0; j<NODE; j++)
cout << result[i][j] << " ";
cout << endl;
}
}
int main() {
transClosure();
} Đầu ra
1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1