-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1319-Number_of_Operations_to_Make_Network_Connected-Union_Find.cpp
More file actions
182 lines (166 loc) · 4.46 KB
/
1319-Number_of_Operations_to_Make_Network_Connected-Union_Find.cpp
File metadata and controls
182 lines (166 loc) · 4.46 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
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
/*******************************************************************************
* 1319-Number_of_Operations_to_Make_Network_Connected.cpp
* Billy.Ljm
* 23 Mar 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/number-of-operations-to-make-network-connected
*
* There are n computers numbered from 0 to n - 1 connected by ethernet cables
* connections forming a network where connections[i] = [ai, bi] represents a
* connection between computers ai and bi. Any computer can reach any other
* computer directly or indirectly through the network.
*
* You are given an initial computer network connections. You can extract
* certain cables between two directly connected computers, and place them
* between any pair of disconnected computers to make them directly connected.
*
* Return the minimum number of times you need to do this in order to make all
* the computers connected. If it is not possible, return -1.
*
* ===========
* My Approach
* ===========
* We can check if all the computers can be connected by simply ensuring the
* number of cables/connections is larger than the number of computers.
*
* We then simply have to find the disjoint sets in the network, and connect
* them together using an extra cable, which will always be available to us
* since we already did the aforementioned check. These disjoint sets can be
* found using the disjoint-set/union-find data structure.
*
* This would have a time complexity of O(n + e) and a space complexity of O(n),
* where n is the number of computers/nodes, and e is the number of wires/edges.
******************************************************************************/
#include <iostream>
#include <vector>
#include <numeric>
/**
* Union-find/Disjoint-set data structure
*/
class UnionFind {
private:
std::vector<int> parent, rank;
public:
/**
* Class Constructor
*
* @param size total number of nodes
*/
UnionFind(int size) {
parent = std::vector<int>(size);
std::iota(std::begin(parent), std::end(parent), 0);
rank = std::vector<int>(size, 0);
}
/**
* Find set of node. Uses path compression.
*
* @param i node to find parent of
*
* @return parent of node[i]
*/
int find(int i) {
if (parent[i] != i) {
parent[i] = find(parent[i]);
}
return parent[i];
}
/**
* Union of connected cities. Uses union by rank.
*
* @param x node to union with y
* @param y node to union with x
*/
void unionn(int x, int y) {
int xroot = find(x);
int yroot = find(y);
if (rank[xroot] < rank[yroot]) {
parent[xroot] = yroot;
}
else if (rank[xroot] > rank[yroot]) {
parent[yroot] = xroot;
}
else {
parent[yroot] = xroot;
rank[xroot]++;
}
}
/**
* Count number of disjoint subsets
*
* @return number of disjoint subsets
*/
int count() {
int size = 0;
for (int i = 0; i < parent.size(); i++) {
if (parent[i] == i) {
size++;
}
}
return size;
}
};
/**
* Solution
*/
class Solution {
public:
/**
* Find the number of edge swaps needed to connect all nodes in a network
*
* @param n number of nodes
* @param connections vector of the edges at [node1, node2]
*
* @return number of edge/connection swaps for complete connection
*/
int makeConnected(int n, std::vector<std::vector<int>>& connections) {
// check number of cables is larger than number of computers
if (connections.size() < n - 1) {
return -1;
}
// disjoint-set union find
UnionFind uf(n);
for (const std::vector<int> connection : connections) {
uf.unionn(connection[0], connection[1]);
}
return uf.count() - 1;
}
};
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); i++) {
os << v[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
int n;
std::vector<std::vector<int>> connections;
// test case 1
n = 4;
connections = { {0,1}, {0,2}, {1,2} };
std::cout << "makeConnected(" << n << ", " << connections << ") = "
<< sol.makeConnected(n, connections) << std::endl;
// test case 2
n = 6;
connections = { {0,1}, {0,2}, {0,3}, {1,2}, {1,3} };
std::cout << "makeConnected(" << n << ", " << connections << ") = "
<< sol.makeConnected(n, connections) << std::endl;
// test case 3
n = 6;
connections = { {0,1}, {0,2}, {0,3}, {1,2} };
std::cout << "makeConnected(" << n << ", " << connections << ") = "
<< sol.makeConnected(n, connections) << std::endl;
return 0;
}