# Solution For Number Of Operations To Make Network Connected

Problem Statement:

There are n computers numbered from 0 to n-1 connected by ethernet cables connections forming a network where connections[i] = [a, b] represents a connection between computers a and b. Any computer can reach any other computer directly or indirectly through the network.

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’s not possible, return -1.

Solution:

To solve this problem, first, we need to understand the concept of a disconnected graph and connected components of a graph. A graph is said to be disconnected if there are two or more vertices that are not connected by any edge. On the other hand, a connected component is a subgraph in which every two vertices are connected to each other by a path.

Approach:

1. Create the adjacency matrix for the given connections.
2. Count the number of connected components in the graph using DFS or BFS algorithms.
3. If there is only one connected component, the given graph is already connected, so return 0.
4. Otherwise, the number of cables that need to be added is equal to the number of connected components minus 1.

Pseudo code:

1. def makeConnected(n: int, connections: List[List[int]]) -> int:
2. graph = [] # to store the edges
3. for i in range(n):
4. graph.append([])
5. for i in range(len(connections)):
6. a = connections[i]
7. b = connections[i]
8. graph[a].append(b)
9. graph[b].append(a)
10. visited = [False] * n
11. components = 0
12. for i in range(n):
13. if not visited[i]:
14. dfs(graph, visited, i)
15. components += 1
16. if len(connections) < n-1:
17. return -1
18. else:
19. return components -1
20. def dfs(graph, visited, i):
21. visited[i] = True
22. for j in range(len(graph[i])):
23. if not visited[graph[i][j]]:
24. dfs(graph, visited, graph[i][j])

Time complexity:

We need to iterate over the connections for creating an adjacency list and perform a DFS or BFS traversal over the graph, hence the overall time complexity is O(N+E).

Space complexity:

We use an adjacency list and visited array to keep track of visited nodes in DFS, which requires O(N+E) space.

Conclusion:

In this problem, we have learned how to count the number of operations required to make a disconnected graph connected. We have used DFS algorithm here, and our solution has a time complexity of O(N+E) and space complexity of O(N+E).

## Step by Step Implementation For 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] = [a, b] represents a connection between computers a and b. Any computer can reach any other computer directly or indirectly through the network.

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; and return the minimum number of times you need to do this in order to make all the computers connected. If it's not possible, return -1.

//Solution:

//We can use Union Find to solve this problem.
//First, we initialize an array of n computers, where each computer is initially its own group.
//Then, we iterate over all of the connections.
//For each connection, we check to see if the two computers are in different groups.
//If they are, we union them together into the same group.
//After we have processed all of the connections, we check to see if there is only one group left.
//If there is only one group left, that means all of the computers are connected, and we return the number of connections we processed.
//If there is more than one group left, that means there are still some computers that are not connected, and we return -1.

class Solution {
public int makeConnected(int n, int[][] connections) {
if (connections.length < n - 1) {
return -1;
}

int[] groups = new int[n];
for (int i = 0; i < n; i++) {
groups[i] = i;
}

for (int[] connection : connections) {
int a = connection;
int b = connection;
int groupA = find(groups, a);
int groupB = find(groups, b);
if (groupA != groupB) {
groups[groupA] = groupB;
n--;
}
}
return n - 1;
}

public int find(int[] groups, int a) {
if (groups[a] != a) {
groups[a] = find(groups, groups[a]);
}
return groups[a];
}
}```
```There is no specific Python solution for this leetcode problem. However, the following code could be used as a potential solution:

def find(parent, i):
if parent[i] == i:
return i
return find(parent, parent[i])

def union(parent, rank, x, y):
xroot = find(parent, x)
yroot = find(parent, y)

if rank[xroot] < rank[yroot]:
parent[xroot] = yroot
elif rank[xroot] > rank[yroot]:
parent[yroot] = xroot
else :
parent[yroot] = xroot
rank[xroot] += 1

def KruskalMST(graph,V):

result =[]

i = 0
e = 0

graph = sorted(graph,key=lambda item: item)

parent = [] ; rank = []

for node in range(V):
parent.append(node)
rank.append(0)

while e < V -1 :

u,v,w =  graph[i]
i = i + 1
x = find(parent, u)
y = find(parent ,v)

if x != y:
e = e + 1
result.append([u,v,w])
union(parent, rank, x, y)

return result```
```There are n computers numbered from 0 to n-1 connected by ethernet cables connections forming a network where connections[i] = [a, b] represents a connection between computers a and b. Any computer can reach any other computer directly or indirectly through the network.

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; and return the minimum number of times you need to do this in order to make all the computers connected. If it's not possible, return -1.

// Example 1:

// Input: n = 4, connections = [[0,1],[0,2],[1,2]]

// Output: 1

// Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.

// Example 2:

// Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]

// Output: 2

// Example 3:

// Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]

// Output: -1

// Explanation: There are not enough cables.

// Example 4:

// Input: n = 5, connections = [[0,1],[0,2],[3,4],[2,3]]

// Output: 0

// Constraints:

// 1 <= n <= 10^5
// 1 <= connections.length <= min(n*(n-1)/2, 10^5)
// connections[i].length == 2
// 0 <= connections[i], connections[i] < n
// connections[i] != connections[i]
// There are no repeated connections.
// No two computers are connected by more than one cable.

function minOperations(n, connections) {
}```
```There are n computers numbered from 0 to n-1 connected by ethernet cables connections forming a network where connections[i] = [a, b] represents a connection between computers a and b. Any computer can reach any other computer directly or indirectly through the network.

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; and return the minimum number of times you need to do so in order to make all the computers connected. If it's not possible, return -1.

//Solution:

#include
#include
#include

using namespace std;

class Solution {
public:
int makeConnected(int n, vector>& connections) {
//check if it is already possible to connect all the computers
if(connections.size() < n - 1)
return -1;

//initialize parent array
vector parent(n);
for(int i = 0; i < n; i++)
parent[i] = i;

//perform union find
for(int i = 0; i < connections.size(); i++){
int x = find(parent, connections[i]);
int y = find(parent, connections[i]);

if(x != y)
parent[x] = y;
}

//find the number of components in the graph
int components = 0;
for(int i = 0; i < n; i++){
if(parent[i] == i)
components++;
}

return components - 1;
}

int find(vector& parent, int i){
if(parent[i] != i)
parent[i] = find(parent, parent[i]);
return parent[i];
}
};```
```There are n computers numbered from 0 to n-1 connected by ethernet cables connections forming a network where connections[i] = [a, b] represents a connection between computers a and b. Any computer can reach any other computer directly or indirectly through the network.

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; and return the minimum number of times you need to do this in order to make all the computers connected. If it's not possible, return -1.

class Solution {
public int MakeConnected(int n, int[][] connections) {

}
}```

Scroll to Top

## Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]