Wednesday, February 10, 2010

C-program to return true if any circuit/cycle exit otherwise false, for a for a given adjacency matrix?

data structures (graph theory)C-program to return true if any circuit/cycle exit otherwise false, for a for a given adjacency matrix?
Assumme that you are given a graph of N nodes, with adjacency matrix, adj[N][N].


Adjacency matrix is a boolean matrix, where an entry at a location[i][j] means node i and node j is connected. You have to make sure that all the entries [i][i] are false or zero.





Now you have to multiply this matrix by itself. The formula is simple


for(int i=0;i%26lt;=N-1;i++)


for(iny j= 0 ;j%26lt;=N-1;j++)


{


new_mat[i][j] = False;


for(k = 0;k%26lt;=N-1;k++)


new_mat[i][j] = new_mat[i][j] || (new_mat[i][k] %26amp;%26amp; new_mat[k][j]);


}





You will have to do this multiplication untill you get TRUE at any [i][i] place, or the consecutive two matrices are same.


First case means there is cycle.


Second case means there is no cycle.

No comments:

Post a Comment