C program to implement Adjacency Matrix of a given Graph Last Updated : 20 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given a undirected Graph of N vertices 1 to N and M edges in form of 2D array arr[][] whose every row consists of two numbers X and Y which denotes that there is a edge between X and Y, the task is to write C program to create Adjacency Matrix of the given Graph. Examples: Input: N = 5, M = 4, arr[][] = { { 1, 2 }, { 2, 3 }, { 4, 5 }, { 1, 5 } } Output: 0 1 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 0 Input: N = 3, M = 4, arr[][] = { { 1, 2 }, { 2, 3 }, { 3, 1 }, { 2, 2 } } Output: 0 1 1 1 1 1 1 1 0 Approach: The idea is to use a square Matrix of size NxN to create Adjacency Matrix. Below are the steps: Create a 2D array(say Adj[N+1][N+1]) of size NxN and initialise all value of this matrix to zero.For each edge in arr[][](say X and Y), Update value at Adj[X][Y] and Adj[Y][X] to 1, denotes that there is a edge between X and Y.Display the Adjacency Matrix after the above operation for all the pairs in arr[][]. Below is the implementation of the above approach: C // C program for the above approach #include <stdio.h> // N vertices and M Edges int N, M; // Function to create Adjacency Matrix void createAdjMatrix(int Adj[][N + 1], int arr[][2]) { // Initialise all value to this // Adjacency list to zero for (int i = 0; i < N + 1; i++) { for (int j = 0; j < N + 1; j++) { Adj[i][j] = 0; } } // Traverse the array of Edges for (int i = 0; i < M; i++) { // Find X and Y of Edges int x = arr[i][0]; int y = arr[i][1]; // Update value to 1 Adj[x][y] = 1; Adj[y][x] = 1; } } // Function to print the created // Adjacency Matrix void printAdjMatrix(int Adj[][N + 1]) { // Traverse the Adj[][] for (int i = 1; i < N + 1; i++) { for (int j = 1; j < N + 1; j++) { // Print the value at Adj[i][j] printf("%d ", Adj[i][j]); } printf("\n"); } } // Driver Code int main() { // Number of vertices N = 5; // Given Edges int arr[][2] = { { 1, 2 }, { 2, 3 }, { 4, 5 }, { 1, 5 } }; // Number of Edges M = sizeof(arr) / sizeof(arr[0]); // For Adjacency Matrix int Adj[N + 1][N + 1]; // Function call to create // Adjacency Matrix createAdjMatrix(Adj, arr); // Print Adjacency Matrix printAdjMatrix(Adj); return 0; } Output:0 1 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 0 Time Complexity: O(N^2), where N is the number of vertices in a graph. Space Complexity: O(N^2), where N is the number of vertices. Comment More infoAdvertise with us Next Article C program to implement Adjacency Matrix of a given Graph A alokesh985 Follow Improve Article Tags : Graph Matrix C Programs Data Structures DSA +1 More Practice Tags : Data StructuresGraphMatrix Similar Reads C program to implement DFS traversal using Adjacency Matrix in a given Graph Given a undirected graph with V vertices and E edges. The task is to perform DFS traversal of the graph. Examples: Input: V = 7, E = 7Connections: 0-1, 0-2, 1-3, 1-4, 1-5, 1-6, 6-2See the diagram for connections: Output : 0 1 3 4 5 6 2Explanation: The traversal starts from 0 and follows the followin 3 min read C Program to Implement Adjacency List An adjacency list is a data structure used to represent a graph in the form of an array of linked lists. The index of the array represents a vertex and each element in its linked list represents the other vertices of the graph that form an edge with the vertex at the index. In this article, we will 6 min read Kruskal's Algorithm (Simple Implementation for Adjacency Matrix) Below are the steps for finding MST using Kruskal's algorithm Sort all the edges in non-decreasing order of their weight. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it. Repeat step#2 until there are 9 min read Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation) We have discussed Prim's algorithm and its implementation for adjacency matrix representation of graphs. As discussed in the previous post, in Prim's algorithm, two sets are maintained, one set contains list of vertices already included in MST, other set contains vertices not yet included. In every 9 min read Graph C/C++ Programs Graph algorithms are used to solve various graph-related problems such as shortest path, MSTs, finding cycles, etc. Graph data structures are used to solve various real-world problems and these algorithms provide efficient solutions to different graph operations and functionalities. In this article, 2 min read Like