-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.cpp
100 lines (85 loc) · 2.3 KB
/
DFS.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<bits/stdc++.h>
using namespace std;
void DFSRec(vector<int> adj[], int s, bool visited[])
{
visited[s]=true;
cout<<s<<" ";
for(int u:adj[s])
if(visited[u]==false)
DFSRec(adj, u, visited);
}
void DFS(vector<int> adj[], int v, int s)
{
bool visited[v];
for(int i=0; i<v;i++)
visited[i]=false;
DFSRec(adj,s,visited);
}
void printGraph(vector<int> adj[], int v) {
for (int i = 0; i < v; i++) {
for (int x : adj[i])
cout << x << " ";
cout << endl;
}
}
void addedge(vector<int> adj[], int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
int main(){
int v = 4;
vector<int> adj[v];
addedge(adj, 0, 2);
addedge(adj, 0, 1);
addedge(adj, 1, 2);
addedge(adj, 1, 3);
printGraph(adj, v);
return 0;
}
// // A simple representation of graph using STL,
// // for the purpose of competitive programming
// #include <bits/stdc++.h>
// using namespace std;
// // A utility function to add an edge in an
// // undirected graph.
// void addEdge(vector<int> adj[], int u, int v) {
// adj[u].push_back(v);
// adj[v].push_back(u);
// }
// // A utility function to do DFS of graph
// // recursively from a given vertex u.
// void DFSUtil(int u, vector<int> adj[], vector<bool>& visited) {
// visited[u] = true;
// cout << u << " ";
// for (int i = 0; i < adj[u].size(); i++)
// if (visited[adj[u][i]] == false)
// DFSUtil(adj[u][i], adj, visited);
// }
// // This function does DFSUtil() for all
// // unvisited vertices.
// void DFS(vector<int> adj[], int V) {
// vector<bool> visited(V, false);
// for (int u = 0; u < V; u++)
// if (visited[u] == false)
// DFSUtil(u, adj, visited);
// }
// // Driver code
// int main() {
// int V = 5;
// // The below line may not work on all
// // compilers. If it does not work on
// // your compiler, please replace it with
// // following
// // vector<int> *adj = new vector<int>[V];
// vector<int> adj[V];
// // Vertex numbers should be from 0 to 4.
// addEdge(adj, 0, 1);
// addEdge(adj, 0, 4);
// addEdge(adj, 1, 2);
// addEdge(adj, 1, 3);
// addEdge(adj, 1, 4);
// addEdge(adj, 2, 3);
// addEdge(adj, 3, 4);
// DFS(adj, V);
// return 0;
// }