A graph G,consists of two sets V and E. V is a finite non-empty set of vertices.E is a set of pairs of vertices,these pairs are called as edges V(G) and E(G) will represent the sets of vertices and edges of graph G.
Undirected graph – It is a graph with V vertices and E edges where E edges are undirected. In undirected graph, each edge which is present between the vertices Vi and Vj,is represented by using a pair of round vertices (Vi,Vj).
Directed graph – It is a graph with V vertices and E edges where E edges are directed.In directed graph,if Vi and Vj nodes having an edge.than it is represented by a pair of triangular brackets Vi,Vj.
Here is the source code of the C program to create a graph using adjacency matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
//... A Program to represent a Graph by using an Adjacency Matrix method
#include <stdio.h>
#include <stdlib.h>
void main()
{
int option;
do
{
printf("\n A Program to represent a Graph by using an ");
printf("Adjacency Matrix method \n ");
printf("\n 1. Directed Graph ");
printf("\n 2. Un-Directed Graph ");
printf("\n 3. Exit ");
printf("\n\n Select a proper option : ");
scanf("%d", &option);
switch(option)
{
case 1 : dir_graph();
break;
case 2 : undir_graph();
break;
case 3 : exit(0);
} // switch
}while(1);
}
int dir_graph()
{
int adj_mat[50][50];
int n;
int in_deg, out_deg, i, j;
printf("\n How Many Vertices ? : ");
scanf("%d", &n);
read_graph(adj_mat, n);
printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ");
for (i = 1; i <= n ; i++ )
{
in_deg = out_deg = 0;
for ( j = 1 ; j <= n ; j++ )
{
if ( adj_mat[j][i] == 1 )
in_deg++;
}
for ( j = 1 ; j <= n ; j++ )
if (adj_mat[i][j] == 1 )
out_deg++;
printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n",i,in_deg,out_deg,in_deg+out_deg);
}
return;
}
int undir_graph()
{
int adj_mat[50][50];
int deg, i, j, n;
printf("\n How Many Vertices ? : ");
scanf("%d", &n);
read_graph(adj_mat, n);
printf("\n Vertex \t Degree ");
for ( i = 1 ; i <= n ; i++ )
{
deg = 0;
for ( j = 1 ; j <= n ; j++ )
if ( adj_mat[i][j] == 1)
deg++;
printf("\n\n %5d \t\t %d\n\n", i, deg);
}
return;
}
int read_graph ( int adj_mat[50][50], int n )
{
int i, j;
char reply;
for ( i = 1 ; i <= n ; i++ )
{
for ( j = 1 ; j <= n ; j++ )
{
if ( i == j )
{
adj_mat[i][j] = 0;
continue;
}
printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j);
scanf("%c", &reply);
if ( reply == 'y' || reply == 'Y' )
adj_mat[i][j] = 1;
else
adj_mat[i][j] = 0;
}
}
return;
}
$ gcc graph.c -o graph $ ./graph A Program to represent a Graph by using an Adjacency Matrix method 1. Directed Graph 2. Un-Directed Graph 3. Exit Select a proper option : How Many Vertices ? : Vertices 1 & 2 are Adjacent ? (Y/N) : N Vertices 1 & 3 are Adjacent ? (Y/N) : Y Vertices 1 & 4 are Adjacent ? (Y/N) : Y Vertices 2 & 1 are Adjacent ? (Y/N) : Y Vertices 2 & 3 are Adjacent ? (Y/N) : Y Vertices 2 & 4 are Adjacent ? (Y/N) : N Vertices 3 & 1 are Adjacent ? (Y/N) : Y Vertices 3 & 2 are Adjacent ? (Y/N) : Y Vertices 3 & 4 are Adjacent ? (Y/N) : Y Vertices 4 & 1 are Adjacent ? (Y/N) : Y Vertices 4 & 2 are Adjacent ? (Y/N) : N Vertices 4 & 3 are Adjacent ? (Y/N) : Y Vertex In_Degree Out_Degree Total_Degree 1 2 0 2 2 1 2 3 3 0 1 1 4 1 1 2 A Program to represent a Graph by using an Adjacency Matrix method 1. Directed Graph 2. Un-Directed Graph 3. Exit
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Apply for Data Structure Internship
- Buy Programming Books
- Practice Programming MCQs
- Apply for Computer Science Internship