C program to implement DFS traversal using Adjacency Matrix in a given Graph Last Updated : 13 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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 following path 0-1, 1-3, 1-4, 1-5, 1-6, 6-2. Input: V = 1, E = 0Output: 0Explanation: There is no other vertex than 0 itself. Approach: Follow the approach mentioned below. Initially all vertices are marked unvisited (false).The DFS algorithm starts at a vertex u in the graph. By starting at vertex u it considers the edges from u to other vertices.If the edge leads to an already visited vertex, then backtrack to current vertex u.If an edge leads to an unvisited vertex, then go to that vertex and start processing from that vertex. That means the new vertex becomes the current root for traversal.Follow this process until a vertices are marked visited. Here adjacency matrix is used to store the connection between the vertices. Take the following graph: The adjacency matrix for this graph is: 0110000100111110000010100000010000001000000110000 Below is implementations of simple Depth First Traversal. C // C code to implement above approach #include <stdio.h> #include <stdlib.h> // Globally declared visited array int vis[100]; // Graph structure to store number // of vertices and edges and // Adjacency matrix struct Graph { int V; int E; int** Adj; }; // Function to input data of graph struct Graph* adjMatrix() { struct Graph* G = (struct Graph*) malloc(sizeof(struct Graph)); if (!G) { printf("Memory Error\n"); return NULL; } G->V = 7; G->E = 7; G->Adj = (int**)malloc((G->V) * sizeof(int*)); for (int k = 0; k < G->V; k++) { G->Adj[k] = (int*)malloc((G->V) * sizeof(int)); } for (int u = 0; u < G->V; u++) { for (int v = 0; v < G->V; v++) { G->Adj[u][v] = 0; } } G->Adj[0][1] = G->Adj[1][0] = 1; G->Adj[0][2] = G->Adj[2][0] = 1; G->Adj[1][3] = G->Adj[3][1] = 1; G->Adj[1][4] = G->Adj[4][1] = 1; G->Adj[1][5] = G->Adj[5][1] = 1; G->Adj[1][6] = G->Adj[6][1] = 1; G->Adj[6][2] = G->Adj[2][6] = 1; return G; } // DFS function to print DFS traversal of graph void DFS(struct Graph* G, int u) { vis[u] = 1; printf("%d ", u); for (int v = 0; v < G->V; v++) { if (!vis[v] && G->Adj[u][v]) { DFS(G, v); } } } // Function for DFS traversal void DFStraversal(struct Graph* G) { for (int i = 0; i < 100; i++) { vis[i] = 0; } for (int i = 0; i < G->V; i++) { if (!vis[i]) { DFS(G, i); } } } // Driver code void main() { struct Graph* G; G = adjMatrix(); DFStraversal(G); } Output0 1 3 4 5 6 2 Time Complexity: O(V + E)Auxiliary Space: O(V) Comment More infoAdvertise with us Next Article C program to implement DFS traversal using Adjacency Matrix in a given Graph A abhinandangcs20 Follow Improve Article Tags : Graph C Programs Algo Geek DSA Algo-Geek 2021 DFS +2 More Practice Tags : DFSGraph Similar Reads C program to implement Adjacency Matrix of a given Graph 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[] 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 Implementation of DFS using adjacency matrix Depth First Search (DFS) has been discussed in this article which uses adjacency list for the graph representation. In this article, adjacency matrix will be used to represent the graph.Adjacency matrix representation: In adjacency matrix representation of a graph, the matrix mat[][] of size n*n (wh 8 min read Implementation of BFS using adjacency matrix Breadth First Search (BFS) has been discussed in this article which uses adjacency list for the graph representation. In this article, adjacency matrix will be used to represent the graph.Adjacency matrix representation: In adjacency matrix representation of a graph, the matrix mat[][] of size n*n ( 7 min read Print matrix elements using DFS traversal Given a matrix grid[][] with dimension M Ã N of integers, the task is to print the matrix elements using DFS traversal. Examples: Input: mat[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}Output: 1 2 3 4 8 12 16 15 11 7 6 10 14 13 9 5Explanation: The matrix elements in the orde 15+ min read Convert Adjacency List to Adjacency Matrix representation of a Graph Given an adjacency list representation of a Graph, the task is to convert the given Adjacency List to Adjacency Matrix representation. Examples: Input: adjList[] = {{0 --> 1 --> 3}, {1 --> 2}, {2 --> 3}} Output: 0 1 0 10 0 1 00 0 0 10 0 0 0 Input: adjList[] = {{0 --> 1 --> 4}, {1 - 9 min read Check if the given permutation is a valid DFS of graph Given a graph with N nodes numbered from 1 to N and M edges and an array of numbers from 1 to N. Check if it is possible to obtain any permutation of array by applying DFS (Depth First Traversal) on given graph. Prerequisites: DFS | Map in CPP Examples: Input: N = 3, M = 2 Edges are: 1) 1-2 2) 2-3 P 12 min read Convert Adjacency Matrix to Adjacency List representation of Graph Prerequisite: Graph and its representations Given a adjacency matrix representation of a Graph. The task is to convert the given Adjacency Matrix to Adjacency List representation. Examples: Input: arr[][] = [ [0, 0, 1], [0, 0, 1], [1, 1, 0] ] Output: The adjacency list is: 0 -> 2 1 -> 2 2 - 6 min read Count the number of nodes at a given level in a tree using DFS Given an integer l and a tree represented as an undirected graph rooted at vertex 0. The task is to print the number of nodes present at level l. Examples:Â Input: l = 2Â Â Output: 4Â We have already discussed the BFS approach, in this post we will solve it using DFS. Approach: The idea is to travers 8 min read Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected) We have introduced Graph basics in Graph and its representations. In this post, a different STL-based representation is used that can be helpful to quickly implement graphs using vectors. The implementation is for the adjacency list representation of the graph. Following is an example undirected and 7 min read Like