-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.cpp
44 lines (41 loc) · 866 Bytes
/
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
/*
Graph class for connected components
*/
#include "graph.hpp"
// Constructor
// TODO: copy constructor?
Graph::Graph(int _n) : n(_n) {
parents.resize(n);
sizes.resize(n);
clear_edges();
}
void Graph::clear_edges() {
for (int i = 0; i < n; ++i) {
parents[i] = i;
sizes[i] = 1;
}
max_size = 1;
}
// Adds an edge using union by size
// Does nothing if the edge already exists
// TODO: throw an exception if a or b are out of bounds
void Graph::add_edge(int a, int b) {
while (parents[a] != a) {
a = parents[a];
}
while (parents[b] != b) {
b = parents[b];
}
if (a != b) {
if (sizes[a] >= sizes[b]) {
parents[b] = a;
sizes[a] += sizes[b];
max_size = std::max(max_size, sizes[a]);
}
else {
parents[a] = b;
sizes[b] += sizes[a];
max_size = std::max(max_size, sizes[b]);
}
}
}