C Program to find Path Matrix by powers of Adjacency matrix

By | 14.05.2017

Path Matrix by powers of Adjacency Matrix


Write a C Program to find Path Matrix by powers of Adjacency Matrix. Here’s simple Program to find Path Matrix by powers of Adjacency Matrix in C Programming Language.


Adjacency Matrix: 


Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.

Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w.

Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time. Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).

Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges), it consumes the same space. Adding a vertex is O(V^2) time.


Also Read : : Insertion Deletion of Vertices and Edges in Graph using Adjacency list

Below is the source code for C Program to Insert Delete Edges in a Directed graph using Adjacency Matrix which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to find Path Matrix by powers of Adjacency matrix  */

#include<stdio.h>
#define MAX 100

void display(int matrix[MAX][MAX]);
void pow_matrix(int p,int adjp[MAX][MAX] );
void multiply(int mat1[MAX][MAX],int mat2[MAX][MAX],int mat3[MAX][MAX]);

void create_graph( );
int adj[MAX][MAX];
int n;

int main()
{

        int adjp[MAX][MAX];
        int x[MAX][MAX],path[MAX][MAX],i,j,p;

        create_graph();

        printf("\nThe adjacency matrix is :\n");
        display(adj);

        /*Initialize all elements of matrix x to zero*/
        for(i=0; i<n; i++)
                for(j=0; j<n; j++)
                   x[i][j] = 0;

        /*All the powers of adj will be added to matrix x */
        for(p=1; p<=n; p++)
        {
                pow_matrix(p,adjp);
                printf("\nAdjacency matrix raised to power [ %d ] is - \n", p);
                display(adjp);
                for(i=0; i<n; i++)
                  for(j=0; j<n; j++)
                         x[i][j] = x[i][j]+adjp[i][j];
        }

        printf("\nThe matrix x is :\n");
        display(x);

        for(i=0; i<n; i++)
                for(j=0; j<n; j++)
                        if (x[i][j] == 0 )
                                path[i][j] = 0;
                        else
                                path[i][j] = 1;

        printf("\nThe path matrix is :\n");
        display(path);
}/*End of main() */


void create_graph()
{
        int i,max_edges,origin,destin;

        printf("\nEnter number of vertices : ");
        scanf("%d",&n);

        max_edges = n*(n-1);

        for( i=1; i<=max_edges; i++ )
        {
                printf("\nEnter edge %d( -1 -1 ) to quit : ",i);
                scanf("%d %d",&origin,&destin);
                if( (origin == -1) && (destin == -1) )
                        break;
                if( origin >= n || destin >= n || origin<0 || destin<0)
                {
                        printf("\nInvalid edge!\n");
                        i--;
                }
                else
                        adj[origin][destin] = 1;
        }/*End of for*/
}/*End of create_graph()*/


/*This function computes the pth power of matrix adj and stores result in adjp */
void pow_matrix(int p,int adjp[MAX][MAX])
{

        int i,j,k,tmp[MAX][MAX];

        /*Initially adjp is equal to adj*/
        for(i=0; i<n; i++)
          for(j=0; j<n; j++)
           adjp[i][j] = adj[i][j];

        for(k=1; k<p; k++)
        {
                /*Multiply adjp with adj and store result in tmp */
                multiply(adjp,adj,tmp);
                for(i=0; i<n; i++)
                  for(j=0; j<n; j++)
                     adjp[i][j] = tmp[i][j]; /* New adjp is equal to tmp */
        }
}/*End of pow_matrix()*/

/*This function multiplies mat1 and mat2 and stores the result in mat3 */
void multiply(int mat1[MAX][MAX],int mat2[MAX][MAX],int mat3[MAX][MAX])
{
        int i,j,k;

        for(i=0; i<n; i++)
                for(j=0; j<n; j++)
                {
                        mat3[i][j] = 0;
                        for(k=0; k<n; k++)
                                mat3[i][j] = mat3[i][j]+ mat1[i][k] * mat2[k][j];
           }
}/*End of multiply()*/

void display(int matrix[MAX][MAX])
{
        int i,j;
        for(i=0; i<n; i++)
        {
                for(j=0; j<n; j++)
                        printf("%4d",matrix[i][j]);
                printf("\n");
        }
        printf("\n");
}/*End of display()*/

OUTPUT : :


/*  C Program to find Path Matrix by powers of Adjacency matrix  */

Enter number of vertices : 5

Enter edge 1( -1 -1 ) to quit : 0 1

Enter edge 2( -1 -1 ) to quit : 0 2

Enter edge 3( -1 -1 ) to quit : 0 3

Enter edge 4( -1 -1 ) to quit : 1 3

Enter edge 5( -1 -1 ) to quit : 3 2

Enter edge 6( -1 -1 ) to quit : 2 4

Enter edge 7( -1 -1 ) to quit : 4 3

Enter edge 8( -1 -1 ) to quit : -1 -1

The adjacency matrix is :
   0   1   1   1   0
   0   0   0   1   0
   0   0   0   0   1
   0   0   1   0   0
   0   0   0   1   0


Adjacency matrix raised to power [ 1 ] is -
   0   1   1   1   0
   0   0   0   1   0
   0   0   0   0   1
   0   0   1   0   0
   0   0   0   1   0


Adjacency matrix raised to power [ 2 ] is -
   0   0   1   1   1
   0   0   1   0   0
   0   0   0   1   0
   0   0   0   0   1
   0   0   1   0   0


Adjacency matrix raised to power [ 3 ] is -
   0   0   1   1   1
   0   0   0   0   1
   0   0   1   0   0
   0   0   0   1   0
   0   0   0   0   1


Adjacency matrix raised to power [ 4 ] is -
   0   0   1   1   1
   0   0   0   1   0
   0   0   0   0   1
   0   0   1   0   0
   0   0   0   1   0


Adjacency matrix raised to power [ 5 ] is -
   0   0   1   1   1
   0   0   1   0   0
   0   0   0   1   0
   0   0   0   0   1
   0   0   1   0   0


The matrix x is :
   0   1   5   5   4
   0   0   2   2   1
   0   0   1   2   2
   0   0   2   1   2
   0   0   2   2   1


The path matrix is :
   0   1   1   1   1
   0   0   1   1   1
   0   0   1   1   1
   0   0   1   1   1
   0   0   1   1   1


Process returned 0

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post…


Recommended Posts : :

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments