Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,59 +1,53 @@
package g1301_1400.s1319_number_of_operations_to_make_network_connected;

// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
// #Graph_Theory_I_Day_8_Standard_Traversal #2022_03_19_Time_9_ms_(67.64%)_Space_64.6_MB_(49.60%)

import java.util.Arrays;
// #Graph_Theory_I_Day_8_Standard_Traversal #2026_10_17_Time_4_ms_(88.52%)_Space_65.60_MB_(82.35%)

public class Solution {

private static final int IMPOSSIBLE_TO_CONNECT = -1;
private int disconnectedComputers;
private int[] parent;
private int[] rank;
private int components = 0;
private int extra = 0;

public int makeConnected(int totalNumberOfComputers, int[][] connections) {
if (connections.length < totalNumberOfComputers - 1) {
return IMPOSSIBLE_TO_CONNECT;
private int findPar(int node) {
if (parent[node] == node) {
return node;
}

disconnectedComputers = totalNumberOfComputers;
rank = new int[totalNumberOfComputers];
parent = new int[totalNumberOfComputers];
Arrays.setAll(parent, intFromZero -> intFromZero++);

for (final int[] connection : connections) {
unionFind(connection[0], connection[1]);
}

return disconnectedComputers - 1;
parent[node] = findPar(parent[node]);
return parent[node];
}

private void unionFind(int first, int second) {
int parentFirst = findParent(first);
int parentSecond = findParent(second);

if (parentFirst != parentSecond) {
joinByRank(parentFirst, parentSecond);
disconnectedComputers--;
private void union(int u, int v) {
int up = findPar(u);
int vp = findPar(v);
if (up == vp) {
extra++;
return;
}
}

private int findParent(int index) {
if (parent[index] != index) {
parent[index] = findParent(parent[index]);
components--;
if (rank[up] < rank[vp]) {
parent[up] = vp;
} else if (rank[up] > rank[vp]) {
parent[vp] = up;
} else {
parent[vp] = up;
rank[up]++;
}
return parent[index];
}

private void joinByRank(int first, int second) {
if (rank[first] < rank[second]) {
parent[first] = second;
} else if (rank[second] < rank[first]) {
parent[second] = first;
} else {
parent[first] = second;
rank[second]++;
public int makeConnected(int n, int[][] connections) {
if (connections.length < n - 1) {
return -1;
}
parent = new int[n];
rank = new int[n];
components = n;
for (int i = 0; i < n; i++) {
parent[i] = i;
}
for (int[] edge : connections) {
union(edge[0], edge[1]);
}
return components - 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,86 @@ void makeConnected3() {
new Solution().makeConnected(6, new int[][] {{0, 1}, {0, 2}, {0, 3}, {1, 2}}),
equalTo(-1));
}

@Test
void makeConnected4() {
// Single node, no connections
assertThat(new Solution().makeConnected(1, new int[][] {}), equalTo(0));
}

@Test
void makeConnected5() {
// Two nodes, no connections
assertThat(new Solution().makeConnected(2, new int[][] {}), equalTo(-1));
}

@Test
void makeConnected6() {
// Two nodes connected
assertThat(new Solution().makeConnected(2, new int[][] {{0, 1}}), equalTo(0));
}

@Test
void makeConnected7() {
// Too few cables
assertThat(new Solution().makeConnected(5, new int[][] {{0, 1}, {2, 3}}), equalTo(-1));
}

@Test
void makeConnected8() {
// Already connected chain
assertThat(
new Solution().makeConnected(5, new int[][] {{0, 1}, {1, 2}, {2, 3}, {3, 4}}),
equalTo(0));
}

@Test
void makeConnected9() {
// Already connected star
assertThat(
new Solution().makeConnected(5, new int[][] {{0, 1}, {0, 2}, {0, 3}, {0, 4}}),
equalTo(0));
}

@Test
void makeConnected10() {
// Already connected complete graph
assertThat(
new Solution()
.makeConnected(
4, new int[][] {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}}),
equalTo(0));
}

@Test
void makeConnected11() {
// Connected with edges in non-sequential order
assertThat(
new Solution().makeConnected(5, new int[][] {{0, 1}, {0, 2}, {3, 4}, {2, 3}}),
equalTo(0));
}

@Test
void makeConnected12() {
// Reversed edge direction
assertThat(
new Solution().makeConnected(4, new int[][] {{1, 0}, {2, 1}, {3, 2}}), equalTo(0));
}

@Test
void makeConnected13() {
// {0,1,2} has a cycle (1 spare cable), {3,4} is separate -> 1 move needed
assertThat(
new Solution().makeConnected(5, new int[][] {{0, 1}, {0, 2}, {1, 2}, {3, 4}}),
equalTo(1));
}

@Test
void makeConnected14() {
// Only {0,1} is connected; 4 duplicate cables can connect 4 isolated nodes
assertThat(
new Solution()
.makeConnected(6, new int[][] {{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}}),
equalTo(4));
}
}
Loading