Envato Ads

Sunday, June 25, 2023

DFS (Depth-First Search) Algorithm Code in C/C++

Depth-First Search (DFS) is a graph traversal algorithm that explores vertices or nodes of a graph in depth before visiting the neighboring vertices. It starts at a given vertex and systematically explores as far as possible along each branch before backtracking.

The DFS algorithm follows these steps:

  1. Start by selecting a source vertex to begin the traversal.

  2. Mark the source vertex as visited and process it. This may involve performing a specific action on the vertex, such as printing its value.

  3. Explore an unvisited neighbor of the current vertex. If there are multiple unvisited neighbors, select one of them arbitrarily.

  4. Repeat step 3 recursively for the chosen neighbor. Mark it as visited and process it.

  5. If there are no unvisited neighbors, backtrack to the previous vertex (i.e., return from the recursive call).

  6. Repeat steps 3-5 until all vertices have been visited.

DFS can be implemented using recursion or a stack data structure. The recursive implementation uses the call stack implicitly to keep track of the visited vertices and backtracks when necessary. The iterative implementation, using a stack, simulates the call stack explicitly.

DFS is useful for various graph-related problems, such as finding connected components, detecting cycles, performing topological sorting, and solving maze problems.

It's important to note that DFS doesn't guarantee the shortest path between two vertices. It may traverse long paths before exploring shorter ones. If a complete traversal of all vertices is required, DFS can ensure that every vertex is visited exactly once.

However, in the case of a disconnected graph, it's necessary to apply DFS to each unvisited vertex to cover all components.

Overall, DFS is a fundamental graph traversal algorithm that provides a way to explore and analyze graphs in a systematic manner.

#include <stdio.h> #define MAX_NODES 100 int visited[MAX_NODES]; int adjacencyMatrix[MAX_NODES][MAX_NODES]; int numNodes; void initialize() { int i, j; for (i = 0; i < MAX_NODES; i++) { visited[i] = 0; for (j = 0; j < MAX_NODES; j++) { adjacencyMatrix[i][j] = 0; } } } void addEdge(int startNode, int endNode) { adjacencyMatrix[startNode][endNode] = 1; adjacencyMatrix[endNode][startNode] = 1; } void depthFirstSearch(int currentNode) { int i; printf("%d ", currentNode); visited[currentNode] = 1; for (i = 0; i < numNodes; i++) { if (adjacencyMatrix[currentNode][i] == 1 && visited[i] == 0) { depthFirstSearch(i); } } } int main() { int i, j; int startNode; printf("Enter the number of nodes: "); scanf("%d", &numNodes); initialize(); printf("Enter the adjacency matrix:\n"); for (i = 0; i < numNodes; i++) { for (j = 0; j < numNodes; j++) { scanf("%d", &adjacencyMatrix[i][j]); } } printf("Enter the starting node: "); scanf("%d", &startNode); printf("DFS traversal starting from node %d: ", startNode); depthFirstSearch(startNode); return 0; }



By Md. Rahat (CSE, BU)

Tuesday, August 20, 2019

Codeforces Solution 71A Way Too Long Words || Codeforces Solution 71A in C language

In this section , I am gonna give you the source code of the codeforces solution of the problem 71A  Way Too Long Words.
Problem Name: Way Too Long Words
Online Judge: Codeforces.com
The below given code is written in C language. :

  1. #include<stdio.h>
  2. #include<ctype.h>
  3. int main(){
  4. int n,i,m;
  5. char x[6000];
  6. scanf("%d",&n);
  7. for(i=1;i<=n;i++){
  8. scanf("%s",&x);
  9. int l=strlen(x);
  10.  
  11. if(l>10){
  12.  
  13. printf("%c",x[0]);
  14. printf("%d",l-2);
  15. printf("%c\n",x[l-1]);
  16.  
  17. }
  18. else printf("%s\n",x);}
  19. return 0;
  20. }



In python (basically Python 3) Language , the solution should be :

  1. i=input
  2. for _ in [0]*int(i()):
  3. s=i();l=len(s)-2;print([s,s[0]+str(l)+s[-1]][l>8])





Tuesday, August 7, 2018

Codeforces Problems Solution 226A - Stones on the Table

Hi friends, I am going to post the solve of the problem '226A - Stones on the Table' of Codeforces.
The problem screenshots is given below:

Code:

Codeforces Problems' Solution (339A - Helpful Maths)




The solution of the problem is given below:


#include <stdio.h>
#include <string.h>
int main ()
{
      char s[1000];
      int i, j = 0, k, l, ln, arr[1000], m, n, temp;
      scanf("%s", s);
      ln = strlen(s);
      for(i = 0; i < ln; i++)
      {
          if((i % 2) == 0)
          {
              arr[j] = (int) s[i];
              j++;
          }
      }
      for(m = 1; m < j; m++)
     {
          for(n = 0; n < (j - m); n++)
         {
              if(arr[n] > arr[n + 1])
              {
                  temp = arr[n];
                  arr[n] = arr[n + 1];
                  arr[n + 1] = temp;
              }
          }
      }
      for(k = 0; k < j; k++)
      {
          if(k == (j - 1))
          {
              printf("%c", (char) arr[k]);
          }
          else
          {
              printf("%c%c", (char) arr[k], '+');
          }
      }
      printf("\n");

      return 0;
}


Codeforces Solution of 112A - Petya and Strings in C

Hi friends, I am going to post the solve of the problem '112A - Petya and Strings' of Codeforces.
The problem screenshot is given below:


Code:

Monday, August 6, 2018

Codeforces Solutions- 96A Football

Hi friends, In this post I am gonna give you the solution of the codeforces problem: 96A - Football.
The problem screenshot is given below:


Solutions Of Codeforces Problem- A Bit++ in C language

Problem screenshot is given Below:



Code:

Codeforces Problems' Solution (122A - Lucky Division) in C

Codeforcces Solution of 50A- Domino Pilling in C language

50 A, codeforces 50 A, Codeforces 50A, Domino pilling 50A, domino pilling 50A, 50A domino pilling, 50 A domino pilling
Codeforcces Solution of 50A Domino Pilling in C language

Hi friends, In this post I'm gonna give you the solution of the codeforces problem 50A - Domino Pilling.
The problem screenshot is given below:




Code:


  Happy coding!!!

Codeforces Solution of 158-A Next Round in C Language

Hi friends, In this post I am gonna give you the solution of the codeforces problem: 158A - Next Round.
The problem screenshot is given below:

71A - Way Too Long Words -cfsolv