C Simple Algorithm to find Transitive Closure


Introduction

In mathematics, the transitive closure of a binary relation R on a set X is the smallest relation on X that contains R and is transitive.

Matrix Representation of a Binary Relation 

If R is a binary relation between the finite indexed sets X and Y (so R ⊆ X×Y), then R can be represented by the logical matrix M whose row and column indices index the elements of X and Y, respectively, such that the entries of M are defined by:
In order to designate the row and column numbers of the matrix, the sets X and Y are indexed with positive integers: i ranges from 1 to the cardinality (size) of X and j ranges from 1 to the cardinality of Y. See the entry on indexed sets for more detail.

Example

The binary relation R on the set {1, 2, 3, 4} is defined so that aRb holds if and only if a divides b evenly, with no remainder. For example, 2R4 holds because 2 divides 4 without leaving a remainder, but 3R4 does not hold because when 3 divides 4 there is a remainder of 1. The following set is the set of pairs for which the relation R holds.
{(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 4), (3, 3), (4, 4)}.
The corresponding representation as a logical matrix is:
which includes a diagonal of ones since each number divides itself.  
I hope you're clear with Matrix Representation of Binary Set. To be fair, I must say; I was not clear with standard conventions while developing  algorithm and made code in its transpose form. But if you can understand the code it will be pretty simple to develop your own version with standard conventions.

 Good, let's go ahead with algorithm.

Sample set :

{(a,b) ,(b,c)}  

Solution :

{(a,b) ,(b,c) , (a,c)}  

Our matrix will be:
     
       0  0  0
       1  0  0
       0  1  0

Solution Matrix:

       0  0  0
       1  0  0
       1  1  0

We will start with a for loop to scan each and every element of matrix.Now if element if 1, then:

 (Remember we are checking relation from column to row.)

Suppose, our ith row and jth column is 1, now we will check for all elements in ith column and all rows.So we will need a third int for that let that be k.

If k row and i column is 1 we will make k row and j column = 1.This can be easily be understood using figure below.


The above diagram represents only i=0 part, this is the part where actual stuff happens. For rest of the part you can always trace the code given below.

Finished Code:

int main()
{
    int n=4;
    printf("Given Matrix\n");
   
    int a[4][4]={
        {0,1,0,0},
        {0,0,1,0},
        {0,0,0,1},
        {1,0,0,0}
    };
   
    int i=0,j=0,k=0;
                printf("\n");
     for(i=0;i<n;i++)
           { for(j=0;j<n;j++)
                printf("%d ",a[i][j]);
                printf("\n");}
   
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(a[i][j]==1){
                for(k=0;k<n;k++)
                {
                   if(a[k][i]==1)
                   {
                       a[k][j]=1;
                   }
                }
            }
        }
    }
   
   
     printf("\n");
     for(i=0;i<n;i++)
           { for(j=0;j<n;j++)
                printf("%d ",a[i][j]);
                printf("\n");}
    return 0;
}

That's it folks, go ahead and test the code!
  
Please let me know if you find any case for which this algorithm doesn't work.

Comments