-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFS.cpp
More file actions
48 lines (45 loc) · 919 Bytes
/
DFS.cpp
File metadata and controls
48 lines (45 loc) · 919 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
#include <iostream>
#include <stack>
#include <list>
using namespace std;
class Graph{
private:
int V;
list<int> *adj;
public:
Graph(int V){this->V=V; adj= new list<int>[V];};
void addEdge(int v, int w);
void DFS(int s);
};
void Graph::DFS(int s){
bool *visited= new bool[V];
for (int i=0;i<V;i++){
visited[i]=false;
}
stack<int> stack;
stack.push(s);
while(!stack.empty()){
s = stack.top();
stack.pop();
if(!visited[s]){
cout<<s<<endl;
visited[s]=true;
}
for(list<int>::iterator i = adj[s].begin();i!=adj[s].end();++i){
if(visited[*i]){
stack.push(*i);
}
}
}
}
void Graph::addEdge(int v, int w){
adj[v].push_back(w);
}
int main(){
Graph g(5);
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(1,0);
g.addEdge(2,0);
g.DFS(0);
}