Similar Problems
Similar Problems not available
Graph Connectivity With Threshold - Leetcode Solution
Companies:
LeetCode: Graph Connectivity With Threshold Leetcode Solution
Difficulty: Hard
Topics: math union-find array
Problem Statement:
You are given an undirected graph consisting of n vertices numbered from 0 to n-1. You are also given an integer threshold.
Given the i-th edge (u, v) of the graph and an integer weight[i], the weight of the edge (u, v) is weight[i].
Return the smallest possible value of the largest connected component in the graph after removing at most k edges, or -1 if it's impossible to do so.
Solution:
To solve this problem, we can use binary search to find the smallest possible value of the largest connected component in the graph. We can set the left boundary as 1 and the right boundary as the sum of all edge weights. In each iteration, we set the mid point as the possible threshold value and check if we can remove k edges to make the largest connected component’s weight <= mid point.
To check if we can remove k edges, we need to use union-find algorithm. We first initialize the parent array to be [0, 1, 2, ..., n-1]. Then for each edge (u, v), if its weight is greater than mid point, we do not want to include it in the connected component and we do nothing. Otherwise, we union the two vertices u and v into one connected component by setting their parent to be the same.
After all edges have been processed, we iterate through all connected component sizes using a hash map, and count the number of connected components whose size is greater than mid point. If the number of connected components whose size is greater than mid point is less than or equal to k, it means we can remove at most k edges to make the largest connected component’s weight <= mid point. Therefore, we update the right boundary to be mid point. Otherwise, we cannot remove at most k edges, and we need to update the left boundary to be mid point + 1.
We repeat the above steps until the left boundary is greater than the right boundary. At the end, if we have found a valid threshold value, we return it. Otherwise, we return -1.
Time Complexity:
The time complexity of this solution is O(mlog(sum(weights))), where m is the number of edges in the graph and sum(weights) is the sum of all edge weights. The reason is that we do binary search for sum(weights) times, and in each iteration, we use union-find algorithm to check if we can remove k edges, which takes O(m) time. In addition, we need to count the number of connected components whose size is greater than mid point, which takes O(n) time.
Space Complexity:
The space complexity of this solution is O(n), where n is the number of vertices in the graph. We use an array of size n to store the parent of each vertex in the union-find algorithm. We also use a hash map to count the number of connected components whose size is greater than mid point, which has at most n entries.
Graph Connectivity With Threshold Solution Code
1