C Program to Insert Delete Edges in a Directed graph using Adjacency Matrix

By | May 13, 2017

Directed Graph using Adjacency Matrix


Write a C Program to Insert Delete Edges in a Directed graph using Adjacency Matrix. Here’s simple Program to Insert Delete Edges in a Directed graph using 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 Check : : C Program for Creation of Adjacency Matrix

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 Insert Delete Edges in a directed graph using Adjacency Matrix */

#include<stdio.h>
#include<stdlib.h>

#define MAX 100

int adj[MAX][MAX];
int n;

void create_graph();
void display();
void insert_edge(int origin,int destin);
void del_edge(int origin, int destin);

int main()
{
	int choice,origin,destin;

	create_graph();

	while(1)
	{
		printf("\n1.Insert an edge\n");
		printf("2.Delete an edge\n");
		printf("3.Display\n");
		printf("4.Exit\n");
		printf("\nEnter your choice : ");
		scanf("%d",&choice);

		switch(choice)
		{
		 case 1:
			printf("\nEnter an edge to be inserted : ");
			scanf("%d %d",&origin,&destin);
			insert_edge(origin,destin);
			break;
		 case 2:
			printf("\nEnter an edge to be deleted : ");
			scanf("%d %d",&origin,&destin);
			del_edge(origin,destin);
			break;
		 case 3:
			display();
			break;
		 case 4:
			exit(1);
		 default:
			printf("\nWrong choice\n");
			break;
		 }/*End of switch*/
	}/*End of while*/

	return 0;
}/*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); /*directed graph*/

	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()*/

void insert_edge(int origin,int destin)
{
	if(origin<0 || origin>=n)
	{
		printf("\nOrigin vertex does not exist\n");
		return;
	}
	if(destin<0 || destin>=n)
	{
		printf("\nDestination vertex does not exist\n");
		return;
	}
	adj[origin][destin] = 1;
}/*End of insert_edge()*/

void del_edge(int origin, int destin)
{
     if(origin<0 || origin>=n || destin<0 || destin>=n || adj[origin][destin]==0)
     {
		printf("\nThis edge does not exist\n");
		return;
     }
     adj[origin][destin] = 0;
}/*End of del_edge()*/

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

OUTPUT : :


/*  C Program to Insert Delete Edges in a directed graph using 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 : 2 3

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

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

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

1.Insert an edge
2.Delete an edge
3.Display
4.Exit

Enter your choice : 3
   0   1   1   1   0
   0   0   0   1   0
   0   0   0   1   0
   0   0   0   0   1
   0   1   0   0   0

1.Insert an edge
2.Delete an edge
3.Display
4.Exit

Enter your choice : 1

Enter an edge to be inserted : 3 1

1.Insert an edge
2.Delete an edge
3.Display
4.Exit

Enter your choice : 3
   0   1   1   1   0
   0   0   0   1   0
   0   0   0   1   0
   0   1   0   0   1
   0   1   0   0   0

1.Insert an edge
2.Delete an edge
3.Display
4.Exit

Enter your choice : 2

Enter an edge to be deleted : 4 1

1.Insert an edge
2.Delete an edge
3.Display
4.Exit

Enter your choice : 3
   0   1   1   1   0
   0   0   0   1   0
   0   0   0   1   0
   0   1   0   0   1
   0   0   0   0   0

1.Insert an edge
2.Delete an edge
3.Display
4.Exit

Enter your choice : 4

Process returned 1

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

Leave a Reply