-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_tutorial.cpp
83 lines (66 loc) · 1.65 KB
/
graph_tutorial.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
using namespace std;
void printGraph(vector<int> graph[], int n) {
for (int i = 1; i <= n; i++) {
cout << "Node " << i << ": ";
for (int j = 0; j < graph[i].size(); j++) {
cout << graph[i][j] << " ";
}
cout << endl;
}
}
int main() {
// Example graph
vector<int> graph[6];
graph[1].push_back(2);
graph[1].push_back(3);
graph[2].push_back(1);
graph[2].push_back(4);
graph[2].push_back(5);
graph[3].push_back(1);
graph[3].push_back(5);
graph[4].push_back(2);
graph[5].push_back(2);
graph[5].push_back(3);
// Print out the graph
int n = 5; // number of nodes in the graph
printGraph(graph, n);
return 0;
/* output
1 -> 2, 3
2 -> 1, 4, 5
3 -> 1, 5
4 -> 2
5 -> 2, 3
*/
}
/*
To represent this graph using vector<int> graph[],
we would create an array of vectors with size 6 (since we have 5 vertices numbered from 1 to 5):
vector<int> graph[6];
Then, we would add the edges to the array of vectors:
graph[1].push_back(2);
graph[1].push_back(3);
graph[2].push_back(1);
graph[2].push_back(4);
graph[2].push_back(5);
graph[3].push_back(1);
graph[3].push_back(5);
graph[4].push_back(2);
graph[5].push_back(2);
graph[5].push_back(3);
The resulting graph data structure would look like this:
graph[1]: 2 3
graph[2]: 1 4 5
graph[3]: 1 5
graph[4]: 2
graph[5]: 2 3
Each element graph[i] in the array is a vector that represents the neighbors of node i.
In this example, graph[1] is a vector containing the neighbors of node 1, which are nodes 2 and 3.
1 -> 2, 3
2 -> 1, 4, 5
3 -> 1, 5
4 -> 2
5 -> 2, 3
*/