Sponsored
Sponsored
This approach uses the Union-Find (or Disjoint Set Union, DSU) data structure. It helps in efficiently finding the number of connected components in the network. Initially, every computer is its own component. We then iterate over each connection and unite the components of the two connected computers. Finally, we count how many separate components remain, since that will determine the number of cables needed to connect them.
Time Complexity: O(n + c), where n is the number of computers and c is the number of connections. Essentially, this is equivalent to O(c) given c > n. Space Complexity: O(n) for storing parent and rank arrays.
1#include <stdio.h>
2#include <stdlib.h>
3
4int find(int* parent, int i) {
5 if (parent[i] == i)
6 return i;
7 return parent[i] = find(parent, parent[i]);
8}
9
10void union_sets(int* parent, int* rank, int x, int y) {
11 int rootX = find(parent, x);
12 int rootY = find(parent, y);
13 if (rootX != rootY) {
14 if (rank[rootX] < rank[rootY])
15 parent[rootX] = rootY;
16 else if (rank[rootX] > rank[rootY])
17 parent[rootY] = rootX;
18 else {
19 parent[rootY] = rootX;
20 rank[rootX]++;
21 }
22 }
23}
24
25int makeConnected(int n, int** connections, int connectionsSize, int* connectionsColSize) {
26 if (connectionsSize < n - 1)
27 return -1;
28
29 int* parent = (int*)malloc(n * sizeof(int));
30 int* rank = (int*)malloc(n * sizeof(int));
31 for (int i = 0; i < n; i++) {
32 parent[i] = i;
33 rank[i] = 0;
34 }
35
36 for (int i = 0; i < connectionsSize; i++) {
37 union_sets(parent, rank, connections[i][0], connections[i][1]);
38 }
39
40 int components = 0;
41 for (int i = 0; i < n; i++) {
42 if (find(parent, i) == i)
43 components++;
44 }
45
46 free(parent);
47 free(rank);
48
49 return components - 1;
50}
This C code employs the Union-Find data structure. We first initialize every computer such that each is its own parent (leader of its own set). We process every connection to unite connected computers. Finally, we calculate the number of connected components by checking which computers are their own parents, and subtract one from this number to find the minimum number of required operations.
In this approach, we consider the computers and connections as a graph and use Depth-First Search (DFS) to determine the number of connected components. If there are at least n - 1 connections, it is possible to make the computers connected; otherwise, it isn't. Once the number of connected components is known, the number of operations required is the number of components minus one.
Time Complexity: O(n + c), where n is the number of computers and c is the connections. Space Complexity: O(n), managing the graph and visited structure.
This C code defines a DFS approach to identify connected components in a graph. The graph is built using an adjacency list representation, and DFS traverses through unvisited nodes to explore connected nodes. The number of traversals gives the number of connected components, and we calculate required operations by subtracting one.