Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Created July 9, 2019 11:07
Show Gist options
  • Save manojnaidu619/1de58922d0e03438d492ef38fa7a54fa to your computer and use it in GitHub Desktop.
Save manojnaidu619/1de58922d0e03438d492ef38fa7a54fa to your computer and use it in GitHub Desktop.
Depth First Search(DFS) of a Graph in CPP
#include <iostream>
using namespace std;
// DFS technique uses Stack. As, we are implementing it using recursion it automatically uses Stack structure
void DFS(int G[][7],int start,int n){
static int visited[7] = {0};
if(visited[start]!=1){
cout << start << " ";
visited[start] = 1;
for(int i=1;i<n;i++){
if(G[start][i]==1 && visited[i]!=1){
DFS(G,i,n);
}
}
}
}
int main(){
int G[7][7]={{0,0,0,0,0,0,0},
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}};
DFS(G,4,7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment