-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph
306 lines (171 loc) · 9.13 KB
/
Graph
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// Graph
/*
ek graph hamra dikhne me asa hota hai...simple visualizzation
O--------O
| \ -- edges
| O O nodes
| /
O--------O
// (pic.A)
// so, WHat is Graph ??
// Basically it is a type of data structure which is a combination of nodes and edges.
// ab agar mai types of Graph ki baat kru toh uske lie..mujhe ek graph bnana pdega...lets see..
O------->O
| / ^ --> directed edges
| / | O nodes
v / |
O------->O
// (pic.B)
// so , Graph are of two types :
// 1. Undirected graph (pic.A) -- disme direction na ho wo Undirected
// 2. directed graph (pic.B) --> or jisme direction ho wo directed graph,simple
NOTE: // jse pic.A ki baat kru toh..manlo first node ko maine a bol dia or second node ko b toh..yha a or b k bich m ek edge hai
// or or or b or a k bich me v ek edge hai ...
// but,,,
// as in pic.B manlo ek node ko maine x bol dia or dusre node ko maine y bol dia toh...yha x or y k bich me ek edge hai...
// lekin y or x k bich m ek v edge nahi h qki...ye directed h ...x-->y eseli..lekin upper Undirected m asa kuch nahi tha
// eslie wha possible hoopya a--b or b--a. (mtb mai a se b ki trf v jaskta hu or b se a ki trf v ..both are possibel)
ab mainne pocha bhyia...NODE kya hota hai ???
// to Node ek trike ka ..entity hota h jo ki..Basically data store krta hai...kisi v type ka...(int,char)
// edges: connections hai ...for nodes
U--------V
| | \ -- edges
| | Z O nodes
| | /
X--------Y
NOw,..lets understand some more terms:
Degree: // eska mtlb hai ki..ek node se kitni edges connected hai ....
// for ex: lets say ...(V) ab V se (U) bhi connected hai V se (Y) v connected hai or V se (Z) v connected hai
// to (V) ki Degree--> 3 aagyiii....
// same : for (U) --> 2 (X) -->2 (Y) --> 3 (Z) --> 2
A------->B
| / ^ --> directed edges
| / | O nodes
v / |
C------->D
In Directed Graph:
(i) Indegree : // eska mtlb..kitni edges meri trff aari hai...
// for ex: (B) lelo ,ab B ki trg total teen edes ari hai...toh eski indegree --> 3 hogyi..
// same : (C) --> 1 (D) --> 1 (A) --> 0
(ii) Outdegree : // eska mtlb..kitni edges mere se door jari hai...
// for ex: (B) lelo ,ab B se ek v edge door ni jari hai...toh eski Outdegree --> 0 hogya..
// same : (C) --> 1 (D) --> 1 (A) --> 2
ab baat aati hai >>>> Weighted Graph:
// eska mtlb hai ki...kisi v edge p kuch numbers dale hue hai.whi unka Weight hogya....
5
U--------V
| | \6 -- edges
2 | | Z O nodes
| | /8
X--------Y
0
// yha savi edges par kuch Weight dale hue h mtlb ki number...whi Weighted graph hote hai...
// jse U ---> V k bich m jo edges hai uska Weight kitna hia..uska Weight 5 hai ...
// same: U--->X = 2 X--->Y =0 Y---Z = 8 V--->Z = 6
NOTE: agar kisi case me apke pas Weight nahi derakha..lkn apko asi algorithm lgaani h jha par Weights ki need h toh us case me hum assume
karlete hai..ki savi pe Weight --> 1 hai.
// Weighted Directed graph : jispe Weight v ho..or direction v ho edges ka
// Weighted Undirected graph : jispe Weighted toh ho ...lkn direction nahi ho edges ka
Path : // sequence of node, jisme ki apka node exactly ek bar hi ata hai ...
U--------V
| | \ -- edges
| | Z O nodes
| | /
X--------Y
// example : U--V--Z V--Z--Y X--Y--V--U toh ye sab ek path hai ..or ensab me har node ek hi bar aara hai..this is what it is.
Cyclic Graph : // agar aap ek asi node pe aachuke hai..jo ki already ek exist kr chuki hai mtlb ki aap Cyclic graph me hai ....
U<-------V
| ^ -- edges
| | O nodes
v |
X------->Y
// U -->X-->Y-->V-->U so this is Cyclic graph
Acylic Graph : // jisme ki cycle form na hori ho....
U------->V
| ^ -- edges
| | O nodes
v |
X------->Y
// dhyn se dekho tooh esme..cycle nahi ban ri ....U-->X-->Y-->V U-->V
ab,baat aati hai ...Graph ko represent kaise kre??? mtlb bhyia...mtllb ki mre pas kya kya representation hai..us graph ko implement krne ki...
// toh esme do chize aati hai ...hum endono mese koi sa v use krk problem solve krskte hia ....
1. Adjacency Matrix :
2. Adjacency List :
// input format me apko teen chize milenge:
1. number of nodes (N)
2. number of edges (M)
3. list of edges(mtlb ki kya kya data h edges ki,kon kis se connected hai .ye sab)
//suppose: N=3 M=3 or edges : 0-->1 1-->2 2-->0 (given)
0
/ ^
/ \
v \
1------>2
// Adjacency Matrix ke case me apko ek 2D Matrix banani hoti hai...jisme kii...indexes nodes ko darsati hai
/* 0 1 2
0 0 1 0 to ess prakar..apne 2D Matrix ka use krk..grpah ko darsha dia...jisme ki har ek edes exits krti
1 0 0 1 hai ki nahi uske base pr bta dia..agar krti hia toh (1) nahi toh (0)
2 1 0 0
*/
/*
space complexity : O(N^2)
// Adjacency list ke case m kya hota h ki...esme aap sari nodes list krlete ho..mtlb ki kon kon si node hia..phir uske baad har ek node k lie
// uska conncection check krlete ho..ki ye particular node kis kis se connected h wo ek sath likh do..lets see....
suppose, N=3 M=3 edges : 0--1--2--3--4--0 (Undirected graph) gieven:
0--------1
| | \ -- edges
| | 2 O nodes
| | /
4--------3
0 --> 1,4
1 --> 0,2,3
2 --> 1,3 // esko hi mai bolta hu...Adjacency list or eske dwara mai apne graph ko darshata hu
3 --> 1,2,4
4 --> 0,3
implementation: map<(datatype),list<(datatype)>>ans;
// ex: map<int,list<int>> ans;
*/
// CODE
class graph{
public:
unordered_map<int,list<int>>adj;
void addEdge(int u,int v,bool direction){
// direction = 0 --> Undirected
// direction = 1 --> directed
// crete an edges from u to v
adj[u].push_back(v);
if(direction==0){
adj[v].push_back(u);
}
}
void printAdjList(){
for(auto i:adj){
cout<<i.first<<"--> ";
for(auto j:i.second) {
cout<<j<<", ";
}
cout<<endl;
}
}
};
int main(){
int n;
cout<<"Enter the number of nodes: ";
cin>>n;
int m;
cout<<"Enter the number of edges: ";
cin>>m;
graph g;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
// creating an Undirected grpah
g.addEdge(u,v,0);
}
// printing graph
g.printAdjList();
return 0;
}