-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdfs.cpp
More file actions
33 lines (27 loc) · 694 Bytes
/
dfs.cpp
File metadata and controls
33 lines (27 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
using namespace std;
int visited[4] = {0, 0, 0, 0};
int G[4][4]={ {0, 1, 0, 0},
{1, 0, 1, 1},
{0, 1, 0, 1},
{0, 1, 1, 0} };
void dfs(int v)
{
//Mark current node as visited and display
cout<<v<<" ";
visited[v] = 1;
//For each node
for(int i = 0; i < 4; ++i)
//Check if it is connected to current node and unvisited
if( (G[v][i] == 1) && (visited[i] != 1) )
{
//Mark as visited and call DFS
visited[i] = 1;
dfs(i);
}
}
int main()
{
dfs(2); //Starting node to perform BFS
return 0;
}