-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS_recursive.cpp
More file actions
70 lines (50 loc) · 998 Bytes
/
Copy pathDFS_recursive.cpp
File metadata and controls
70 lines (50 loc) · 998 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
#define max 10
class graph
{
public:
int g[max][max];
int visited[max], n;
void readg();
void dfs(int);
};
void graph::readg()
{
cout << "How many nodes: ";
cin >> n;
cout << "Enter adjacency matrix (0 or 1 for each pair of nodes):\n";
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> g[i][j];
}
}
for (int i = 1; i <= n; i++)
{
visited[i] = 0;
}
int v;
cout << "Enter starting vertex (1 to " << n << "): ";
cin >> v;
dfs(v);
}
void graph::dfs(int i)
{
cout << i << " ";
visited[i] = 1;
for (int j = 1; j <= n; j++)
{
if (visited[j] != 1 && g[i][j] == 1)
{
dfs(j);
}
}
}
int main()
{
graph g;
g.readg();
return 0;
}