Home » Data Structure

Creation of adjacency matrix

In this article, we will learn about what adjacency matrix is and how it is created?
Submitted by Manu Jemini, on January 06, 2018

A graph is a set of nodes or known number of vertices. When these vertices are paired together, we call it edges. An Edge is a line from one node to other. Every edge can have its cost or weight.

Graphs are two types Directed and Undirected. Directed graphs are the graphs in which the vertices are ordered and in undirected graphs the vertices are unordered.

An Adjacency matrix is a finite set of values used to create an easy way to look for an edge. If the value at 0th row and 1st column are zero, it means the edge does not exist.

Adjacent Matrix

Image source: https://www.geeksforgeeks.org/wpcontent/uploads/adjacency_matrix_representation.png

In the example below, the program is made to create an adjacency matrix for either of Directed or Undirected type of graph.

The Program will ask for the number of nodes then the directed or undirected graph. After that it will ask for the values of the node. In the end, it will print the matrix.

The Adjacency matrix is the 2-D array of integers.

C program to create adjacency matrix

#define max 20

int adj[max][max]; /*Adjacency matrix */
int n;    /* Denotes number of nodes in the graph */
	int max_edges,i,j,origin,destin;
	char graph_type;
	printf("Enter number of nodes : ");
	printf("Enter type of graph, directed or undirected (d/u) : ");


		printf("Enter edge %d( 0 0 to quit ) : ",i);
		scanf("%d %d",&origin,&destin);
		if( (origin==0) && (destin==0) )
		if( origin > n || destin > n || origin<=0 || destin<=0)
			printf("Invalid edge!\n");
			if( graph_type=='u')
	}/*End of for*/

	printf("The adjacency matrix is :\n");
}/*End of main()*/


Adjacency matrix creation in C language program

Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com (2015-2018), Some rights reserved.