-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphs-DFS-List.cpp
58 lines (49 loc) · 1.11 KB
/
Graphs-DFS-List.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
#include <iostream>
using namespace std;
#include <vector>
#include <cmath>
#include <time.h>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bits/stdc++.h>
// Note: 'v' num of vertices => nodes val lies betn 1 to 'v'
class Graph{
private:
int vert;
int edge;
vector<vector<int>> graph;
vector<bool> isVisited;
public:
Graph(int v, int e){
vert= v, edge= e;
graph.resize(vert+1);
isVisited.resize(vert+1, false);
}
void adjacencyList(){
cout<< "Enter Connected Nodes: "<< endl;
for(int i=0;i<edge;i++){
int v1, v2;
cin>> v1>> v2;
graph[v1].push_back(v2);
graph[v2].push_back(v1);
}
}
void dfs(int vertex){ // O(vert+edge)
if(isVisited[vertex]) return;
// process curr vertex
isVisited[vertex]= true;
cout<< vertex<< " ";
for(int child: graph[vertex]) dfs(child);
}
};
int main(){
return 0;
}