Write a C Program for Creation of Adjacency Matrix

By | 11.05.2017

Creation of Adjacency Matrix


Write a C Program for Creation of Adjacency Matrix. Here’s simple Program for adjacency matrix representation of graph in data structure 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.


Check Out : : How to Print Spiral Order Traversal of a Binary Tree

Below is the source code for C Program for Creation of Adjacency Matrix which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program for creation of adjacency matrix */

#include<stdio.h>
#define MAX 100

int adj[MAX][MAX]; /*Adjacency matrix*/
int n;    /*Number of vertices in the graph*/

int main()
{
        int max_edges,i,j,origin,destin;
        int graph_type;

        printf("\nEnter 1 for Undirected graph\nEnter 2 for Directed graph\n");
        printf("\nEnter your choice :: ");
        scanf("%d",&graph_type);

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

        if(graph_type==1)
                max_edges = n*(n-1)/2;
        else
                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 vertex!\n");
                        i--;
                }
                else
                {
                        adj[origin][destin] = 1;
                        if( graph_type == 1) /*if undirected graph*/
                                adj[destin][origin] = 1;
                }
        }/*End of for*/

        printf("\nThe adjacency matrix is :: \n");
        for(i=0; i<=n-1; i++)
        {
                for(j=0; j<=n-1; j++)
                        printf("%4d",adj[i][j]);
                printf("\n");
        }
}/*End of main()*/

OUTPUT : :


/*  C Program for Creation of Adjacency matrix  */

Enter 1 for Undirected graph
Enter 2 for Directed graph

Enter your choice :: 2

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 ) : 2 3

Enter edge [ 6 ] ( -1 -1 to quit ) : 3 4

Enter edge [ 7 ] ( -1 -1 to quit ) : 4 2

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   1   0
   0   0   0   0   1
   0   0   1   0   0

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 : :

4 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments