boolean edgeE[][]; //edgeE[i][j] = there exists an edge from i to j
boolean exists[][]; //exists[i][j] = there exists a path from i to j
void trasitiveClosure()
{
int i, j, k;
//n = # of nodes
//copy over edges
for(i =0; i < n; i++)
{
for(j=0; j< n; j++)
{
exists[i][j] = edgeE[i][j];
}
}
//every node can reach itself
for(i =0; i < n; i++)
{
edgeE[i][i] = true;
}
//Perform Floyd Warshall Aglorithm
for(k = 0; k < n; k++) //length of current paths
{
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
exists[i][j] = exists[i][j] || (exists[i][k] && exists[k][j]);
}
}
}
}
No comments:
Post a Comment