Created
July 9, 2019 11:07
-
-
Save manojnaidu619/1de58922d0e03438d492ef38fa7a54fa to your computer and use it in GitHub Desktop.
Depth First Search(DFS) of a Graph in CPP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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