-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy path01_create_and_print_graph.cpp
59 lines (40 loc) · 1.33 KB
/
01_create_and_print_graph.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
/*
link: https://www.javatpoint.com/graph-theory-graph-representations#:~:text=In%20graph%20theory%2C%20a%20graph,to%20it%20by%20an%20edge).
link: https://onedrive.live.com/?authkey=%21AJrTq%5FU8BPKIWDk&cid=842AECBB531CCEA4&id=842AECBB531CCEA4%211179&parId=842AECBB531CCEA4%211164&o=OneUp
*/
// ----------------------------------------------------------------------------------------------------------------------- //
/*
using adjacency matrix:
TC: O(1) => for searching
SC: O(N^2)
*/
int adjMatrix() {
int rows, cols;
cin >> rows >> cols;
int adj[rows + 1][rows + 1];
for (int i = 0;i < rows;i++) {
int u, v;
cin >> u >> v;
adj[u][v] = 1;
adj[v][u] = 1;
}
}
// ----------------------------------------------------------------------------------------------------------------------- //
/*
using adjacency list
TC: O(Edges of that vertex)
SC: O(N + (2*E))
*/
int adjList() {
int rows, cols;
cin >> rows >> cols;
vector<int> adj(rows + 1);
for (int i = 0;i < m;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u); // comment this line for directed graph
// for weighted graph use: adj[u].push_back({v, wt});
// adj[v].push_back({u, wt});
}
}