-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
114 lines (93 loc) · 1.69 KB
/
Graph.cpp
File metadata and controls
114 lines (93 loc) · 1.69 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Implementing everything of graph .... to be updated with time
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
int scan(){
register int c = getchar_unlocked();
register int n = 0;
register int neg=0;
for( ; (c<48||c>57)&&c!='-' ; c=getchar_unlocked());
if(c=='-'){
neg=1; c=getchar_unlocked();
}
for( ;c>=48&&c<=57; c=getchar_unlocked()){
n = (n<<1) + (n<<3) + c - 48;
}
if(neg) return -n;
else return n;
}
class Graph{
int v;
vi *adj;
public:
Graph(int size){
v=size;
adj = new vi[v];
}
void addedge(int u,int v){
adj[u].push_back(v);
}
void print();
void bfs(int source);
void DFSuntil(int source,bool visited[]);
void dfs(int source);
};
void Graph :: print(){
for(int i=0;i<v;i++){
cout<<"Adjacent verteces to"<<i+1<<endl;
for(auto x:adj[i]){
cout<<x<<" ";
}
cout<<endl;
}
}
void Graph :: bfs(int source){
bool visited[v];
memset(visited,false,sizeof(visited));
queue <int> q;
q.push(source);
visited[source]=true;
while(!q.empty()){
source=q.front();
cout<<source<<" ";
q.pop();
for(auto x:adj[source]){
if(!visited[x]){
q.push(x);
visited[x]=true;
}
}
}
}
void Graph :: DFSuntil(int source,bool visited[]){
cout<<source<<" ";
visited[source]=true;
for(auto x:adj[source]){
if(!visited[x]){
DFSuntil(x,visited);
}
}
}
void Graph :: dfs(int source){
bool visited[v];
memset(visited,false,sizeof(visited));
DFSuntil(source,visited);
}
int main(){
Graph g(4);
g.addedge(0,1);
g.addedge(0,2);
g.addedge(1,2);
g.addedge(2,0);
g.addedge(2,3);
g.addedge(3,3);
g.bfs(2);
g.dfs(2);
return 0;
}